Hub Music, HubMusic Preference

This commit is contained in:
Shaun Bennett 2015-12-07 00:58:44 -05:00
parent 5486d8acf6
commit 7b5db2d4fb
9 changed files with 189 additions and 23 deletions

View File

@ -0,0 +1,10 @@
package mineplex.core.noteblock;
import org.bukkit.entity.Player;
public interface INoteVerifier
{
public boolean shouldPlay(Player player);
}

View File

@ -1,26 +1,41 @@
package mineplex.core.noteblock; package mineplex.core.noteblock;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.common.util.UtilServer; import mineplex.core.common.util.UtilServer;
import mineplex.core.noteblock.event.SongFinishEvent;
import mineplex.core.noteblock.event.SongStartEvent;
public class NotePlayer public class NotePlayer
{ {
private final JavaPlugin _plugin;
private final NoteSong _song; private final NoteSong _song;
private final INoteVerifier _verifier;
private final long _sleepMs; private final long _sleepMs;
private volatile float _volumeMult;
private volatile boolean _loop; private volatile boolean _loop;
private volatile int _tick; private volatile int _tick;
private volatile boolean _finished; private volatile boolean _finished;
public NotePlayer(NoteSong song, boolean loop) public NotePlayer(JavaPlugin plugin, NoteSong song, INoteVerifier verifier, float volumeMult, boolean loop)
{ {
_plugin = plugin;
_song = song; _song = song;
_verifier = verifier;
_sleepMs = (long) (1000 / (song.getTempo() / 100D)); _sleepMs = (long) (1000 / (song.getTempo() / 100D));
_loop = loop; _loop = loop;
_tick = 0; _tick = 0;
_volumeMult = volumeMult;
_finished = false; _finished = false;
startThread(); startThread();
SongStartEvent event = new SongStartEvent(_song);
_plugin.getServer().getPluginManager().callEvent(event);
} }
private void startThread() private void startThread()
@ -43,6 +58,17 @@ public class NotePlayer
else else
{ {
_finished = true; _finished = true;
_plugin.getServer().getScheduler().runTask(_plugin, new Runnable()
{
@Override
public void run()
{
SongFinishEvent event = new SongFinishEvent(_song);
_plugin.getServer().getPluginManager().callEvent(event);
}
});
return; return;
} }
} }
@ -66,13 +92,21 @@ public class NotePlayer
private void playTick(int tick) private void playTick(int tick)
{ {
Player[] playerArray = UtilServer.getPlayers();
List<Player> players = new ArrayList<>(playerArray.length);
for (Player player : playerArray)
{
if (_verifier.shouldPlay(player))
players.add(player);
}
for (NoteLayer layer : _song.getLayers()) for (NoteLayer layer : _song.getLayers())
{ {
Note note = layer.getNote(tick); Note note = layer.getNote(tick);
if (note != null) if (note != null)
{ {
float volume = layer.getVolume() / 100F; float volume = _volumeMult * (layer.getVolume() / 100F);
for (Player player : UtilServer.getPlayers()) for (Player player : players)
{ {
player.playSound(player.getEyeLocation(), UtilNote.getInstrumentSound(note.getInstrument()), volume, (float) UtilNote.getPitch(note.getNote() - 33)); player.playSound(player.getEyeLocation(), UtilNote.getInstrumentSound(note.getInstrument()), volume, (float) UtilNote.getPitch(note.getNote() - 33));
} }

View File

@ -0,0 +1,25 @@
package mineplex.core.noteblock.event;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import mineplex.core.noteblock.NoteSong;
public class SongFinishEvent extends Event
{
private static final HandlerList handlers = new HandlerList();
public static HandlerList getHandlerList() { return handlers; }
public HandlerList getHandlers() { return handlers; }
private NoteSong _song;
public SongFinishEvent(NoteSong song)
{
_song = song;
}
public NoteSong getSong()
{
return _song;
}
}

View File

@ -0,0 +1,25 @@
package mineplex.core.noteblock.event;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import mineplex.core.noteblock.NoteSong;
public class SongStartEvent extends Event
{
private static final HandlerList handlers = new HandlerList();
public static HandlerList getHandlerList() { return handlers; }
public HandlerList getHandlers() { return handlers; }
private NoteSong _song;
public SongStartEvent(NoteSong song)
{
_song = song;
}
public NoteSong getSong()
{
return _song;
}
}

View File

@ -136,6 +136,6 @@ public class PreferencesManager extends MiniDbClientPlugin<UserPreferences>
@Override @Override
public String getQuery(int accountId, String uuid, String name) public String getQuery(int accountId, String uuid, String name)
{ {
return "SELECT games, visibility, showChat, friendChat, privateMessaging, partyRequests, invisibility, forcefield, showMacReports, ignoreVelocity, pendingFriendRequests, friendDisplayInventoryUI, clanTips FROM accountPreferences WHERE accountPreferences.uuid = '" + uuid + "' LIMIT 1;"; return "SELECT games, visibility, showChat, friendChat, privateMessaging, partyRequests, invisibility, forcefield, showMacReports, ignoreVelocity, pendingFriendRequests, friendDisplayInventoryUI, clanTips, hubMusic FROM accountPreferences WHERE accountPreferences.uuid = '" + uuid + "' LIMIT 1;";
} }
} }

View File

@ -24,7 +24,7 @@ public class PreferencesRepository extends RepositoryBase
// DEFAULT 0, showMacReports BOOL NOT NULL DEFAULT 0, ignoreVelocity BOOL // DEFAULT 0, showMacReports BOOL NOT NULL DEFAULT 0, ignoreVelocity BOOL
// NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE INDEX uuid_index (uuid));"; // NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE INDEX uuid_index (uuid));";
private static String INSERT_ACCOUNT = "INSERT INTO accountPreferences (uuid) VALUES (?) ON DUPLICATE KEY UPDATE uuid=uuid;"; private static String INSERT_ACCOUNT = "INSERT INTO accountPreferences (uuid) VALUES (?) ON DUPLICATE KEY UPDATE uuid=uuid;";
private static String UPDATE_ACCOUNT_PREFERENCES = "UPDATE accountPreferences SET games = ?, visibility = ?, showChat = ?, friendChat = ?, privateMessaging = ?, partyRequests = ?, invisibility = ?, forcefield = ?, showMacReports = ?, ignoreVelocity = ?, pendingFriendRequests = ?, friendDisplayInventoryUI = ?, clanTips = ? WHERE uuid=?;"; private static String UPDATE_ACCOUNT_PREFERENCES = "UPDATE accountPreferences SET games = ?, visibility = ?, showChat = ?, friendChat = ?, privateMessaging = ?, partyRequests = ?, invisibility = ?, forcefield = ?, showMacReports = ?, ignoreVelocity = ?, pendingFriendRequests = ?, friendDisplayInventoryUI = ?, clanTips = ?, hubMusic = ? WHERE uuid=?;";
public PreferencesRepository(JavaPlugin plugin) public PreferencesRepository(JavaPlugin plugin)
{ {
@ -61,8 +61,9 @@ public class PreferencesRepository extends RepositoryBase
preparedStatement.setBoolean(11, entry.getValue().PendingFriendRequests); preparedStatement.setBoolean(11, entry.getValue().PendingFriendRequests);
preparedStatement.setBoolean(12, entry.getValue().friendDisplayInventoryUI); preparedStatement.setBoolean(12, entry.getValue().friendDisplayInventoryUI);
preparedStatement.setBoolean(13, entry.getValue().ClanTips); preparedStatement.setBoolean(13, entry.getValue().ClanTips);
preparedStatement.setBoolean(14, entry.getValue().HubMusic);
System.out.println(">> " + entry.getValue().ClanTips); System.out.println(">> " + entry.getValue().ClanTips);
preparedStatement.setString(14, entry.getKey()); preparedStatement.setString(15, entry.getKey());
preparedStatement.addBatch(); preparedStatement.addBatch();
} }
@ -89,8 +90,9 @@ public class PreferencesRepository extends RepositoryBase
preparedStatement.setBoolean(11, entry.getValue().PendingFriendRequests); preparedStatement.setBoolean(11, entry.getValue().PendingFriendRequests);
preparedStatement.setBoolean(12, entry.getValue().friendDisplayInventoryUI); preparedStatement.setBoolean(12, entry.getValue().friendDisplayInventoryUI);
preparedStatement.setBoolean(13, entry.getValue().ClanTips); preparedStatement.setBoolean(13, entry.getValue().ClanTips);
preparedStatement.setBoolean(14, entry.getValue().HubMusic);
System.out.println(">> " + entry.getValue().ClanTips); System.out.println(">> " + entry.getValue().ClanTips);
preparedStatement.setString(14, entry.getKey()); preparedStatement.setString(15, entry.getKey());
preparedStatement.execute(); preparedStatement.execute();
} }
@ -122,6 +124,7 @@ public class PreferencesRepository extends RepositoryBase
preferences.PendingFriendRequests = resultSet.getBoolean(11); preferences.PendingFriendRequests = resultSet.getBoolean(11);
preferences.friendDisplayInventoryUI = resultSet.getBoolean(12); preferences.friendDisplayInventoryUI = resultSet.getBoolean(12);
preferences.ClanTips = resultSet.getBoolean(13); preferences.ClanTips = resultSet.getBoolean(13);
preferences.HubMusic = resultSet.getBoolean(14);
System.out.println("<< " + resultSet.getBoolean(13)); System.out.println("<< " + resultSet.getBoolean(13));
} }

View File

@ -16,4 +16,5 @@ public class UserPreferences
public boolean PendingFriendRequests = true; public boolean PendingFriendRequests = true;
public boolean friendDisplayInventoryUI = true; public boolean friendDisplayInventoryUI = true;
public boolean ClanTips = true; public boolean ClanTips = true;
public volatile boolean HubMusic = true;
} }

View File

@ -25,6 +25,7 @@ public class PreferencesPage extends ShopPageBase<PreferencesManager, Preference
private IButton _toggleHubPartyRequests; private IButton _toggleHubPartyRequests;
private IButton _togglePendingFriendRequests; private IButton _togglePendingFriendRequests;
private IButton _toggleClanTips; private IButton _toggleClanTips;
private IButton _toggleHubMusic;
private boolean _hubGamesToggled; private boolean _hubGamesToggled;
private boolean _hubPlayersToggled; private boolean _hubPlayersToggled;
@ -33,6 +34,7 @@ public class PreferencesPage extends ShopPageBase<PreferencesManager, Preference
private boolean _hubPartyRequestsToggled; private boolean _hubPartyRequestsToggled;
private boolean _pendingFriendRequestsToggled; private boolean _pendingFriendRequestsToggled;
private boolean _clanTipsToggled; private boolean _clanTipsToggled;
private boolean _hubMusicToggled;
private ExclusivePreferencesShop _exclusiveShop; private ExclusivePreferencesShop _exclusiveShop;
@ -109,6 +111,22 @@ public class PreferencesPage extends ShopPageBase<PreferencesManager, Preference
togglePendingFriendRequests(player); togglePendingFriendRequests(player);
} }
}; };
_toggleHubMusic = new IButton()
{
@Override
public void onClick(Player player, ClickType clickType)
{
toggleHubMusic(player);
}
};
}
private void toggleHubMusic(Player player)
{
getPlugin().Get(player).HubMusic = !getPlugin().Get(player).HubMusic;
_hubMusicToggled = !_hubMusicToggled;
buildPage();
} }
private void toggleHubPartyRequests(Player player) private void toggleHubPartyRequests(Player player)
@ -180,7 +198,7 @@ public class PreferencesPage extends ShopPageBase<PreferencesManager, Preference
UserPreferences userPreferences = getPlugin().Get(getPlayer()); UserPreferences userPreferences = getPlugin().Get(getPlayer());
Rank rank = getClientManager().Get(getPlayer()).GetRank(); Rank rank = getClientManager().Get(getPlayer()).GetRank();
int[] indices = UtilUI.getIndicesFor(7, 0, 2); int[] indices = UtilUI.getIndicesFor(8, 0, 2);
buildPreference(indices[0], Material.FIREBALL, "Hub Player Stacker", userPreferences.HubGames, _toggleHubGames); buildPreference(indices[0], Material.FIREBALL, "Hub Player Stacker", userPreferences.HubGames, _toggleHubGames);
buildPreference(indices[1], Material.EYE_OF_ENDER, "Hub Player Visibility", userPreferences.ShowPlayers, _toggleHubPlayers); buildPreference(indices[1], Material.EYE_OF_ENDER, "Hub Player Visibility", userPreferences.ShowPlayers, _toggleHubPlayers);
@ -189,6 +207,7 @@ public class PreferencesPage extends ShopPageBase<PreferencesManager, Preference
buildPreference(indices[4], Material.SKULL_ITEM, (byte) 3, "Hub Party Requests", userPreferences.PartyRequests, _toggleHubPartyRequests); buildPreference(indices[4], Material.SKULL_ITEM, (byte) 3, "Hub Party Requests", userPreferences.PartyRequests, _toggleHubPartyRequests);
buildPreference(indices[5], Material.RED_ROSE, "Show Pending Friend Requests", userPreferences.PendingFriendRequests, _togglePendingFriendRequests); buildPreference(indices[5], Material.RED_ROSE, "Show Pending Friend Requests", userPreferences.PendingFriendRequests, _togglePendingFriendRequests);
buildPreference(indices[6], Material.IRON_SWORD, "Show Clan Tips", userPreferences.ClanTips, _toggleClanTips); buildPreference(indices[6], Material.IRON_SWORD, "Show Clan Tips", userPreferences.ClanTips, _toggleClanTips);
buildPreference(indices[7], Material.NOTE_BLOCK, "Hub Music", userPreferences.HubMusic, _toggleHubMusic);
if (rank.has(Rank.MODERATOR) || rank == Rank.YOUTUBE || rank == Rank.TWITCH) if (rank.has(Rank.MODERATOR) || rank == Rank.YOUTUBE || rank == Rank.TWITCH)
{ {
@ -219,6 +238,6 @@ public class PreferencesPage extends ShopPageBase<PreferencesManager, Preference
public boolean preferencesChanged() public boolean preferencesChanged()
{ {
return _hubGamesToggled || _hubPlayersToggled || _hubChatToggled || _hubPrivateChatToggled || _hubPartyRequestsToggled || _pendingFriendRequestsToggled || _clanTipsToggled; return _hubGamesToggled || _hubPlayersToggled || _hubChatToggled || _hubPrivateChatToggled || _hubPartyRequestsToggled || _pendingFriendRequestsToggled || _clanTipsToggled || _hubMusicToggled;
} }
} }

View File

@ -3,6 +3,7 @@ package mineplex.hub;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Random;
import mineplex.core.MiniClientPlugin; import mineplex.core.MiniClientPlugin;
import mineplex.core.account.CoreClient; import mineplex.core.account.CoreClient;
@ -29,15 +30,19 @@ import mineplex.core.disguise.DisguiseManager;
import mineplex.core.disguise.disguises.DisguiseSlime; import mineplex.core.disguise.disguises.DisguiseSlime;
import mineplex.core.donation.DonationManager; import mineplex.core.donation.DonationManager;
import mineplex.core.gadget.GadgetManager; import mineplex.core.gadget.GadgetManager;
import mineplex.core.gadget.event.GadgetActivateEvent;
import mineplex.core.gadget.event.GadgetCollideEntityEvent; import mineplex.core.gadget.event.GadgetCollideEntityEvent;
import mineplex.core.gadget.types.GadgetType;
import mineplex.core.giveaway.GiveawayManager; import mineplex.core.giveaway.GiveawayManager;
import mineplex.core.hologram.HologramManager; import mineplex.core.hologram.HologramManager;
import mineplex.core.inventory.InventoryManager; import mineplex.core.inventory.InventoryManager;
import mineplex.core.message.PrivateMessageEvent; import mineplex.core.message.PrivateMessageEvent;
import mineplex.core.mount.MountManager; import mineplex.core.mount.MountManager;
import mineplex.core.noteblock.INoteVerifier;
import mineplex.core.noteblock.NBSReader; import mineplex.core.noteblock.NBSReader;
import mineplex.core.noteblock.NotePlayer; import mineplex.core.noteblock.NotePlayer;
import mineplex.core.noteblock.NoteSong; import mineplex.core.noteblock.NoteSong;
import mineplex.core.noteblock.event.SongFinishEvent;
import mineplex.core.notifier.NotificationManager; import mineplex.core.notifier.NotificationManager;
import mineplex.core.npc.NpcManager; import mineplex.core.npc.NpcManager;
import mineplex.core.packethandler.PacketHandler; import mineplex.core.packethandler.PacketHandler;
@ -163,6 +168,12 @@ public class HubManager extends MiniClientPlugin<HubClient>
private HashMap<String, ArrayList<String>> _creativeAdmin = new HashMap<String, ArrayList<String>>(); private HashMap<String, ArrayList<String>> _creativeAdmin = new HashMap<String, ArrayList<String>>();
// Christmas Songs
private Random _random = new Random();
private int _lastSong = 0;
private final String[] _songNames = {"JingleBells.nbs", "TheFirstNoel.nbs", "Hark.nbs", "DeckTheHalls.nbs", "Joy.nbs", "MerryChristmas.nbs"};
private final NoteSong[] _songs;
public HubManager(JavaPlugin plugin, BlockRestore blockRestore, CoreClientManager clientManager, DonationManager donationManager, InventoryManager inventoryManager, ConditionManager conditionManager, DisguiseManager disguiseManager, TaskManager taskManager, Portal portal, PartyManager partyManager, PreferencesManager preferences, PetManager petManager, PollManager pollManager, StatsManager statsManager, AchievementManager achievementManager, HologramManager hologramManager, NpcManager npcManager, PersonalServerManager personalServerManager, PacketHandler packetHandler, Punish punish, ServerStatusManager serverStatusManager, GiveawayManager giveawayManager) public HubManager(JavaPlugin plugin, BlockRestore blockRestore, CoreClientManager clientManager, DonationManager donationManager, InventoryManager inventoryManager, ConditionManager conditionManager, DisguiseManager disguiseManager, TaskManager taskManager, Portal portal, PartyManager partyManager, PreferencesManager preferences, PetManager petManager, PollManager pollManager, StatsManager statsManager, AchievementManager achievementManager, HologramManager hologramManager, NpcManager npcManager, PersonalServerManager personalServerManager, PacketHandler packetHandler, Punish punish, ServerStatusManager serverStatusManager, GiveawayManager giveawayManager)
{ {
super("Hub Manager", plugin); super("Hub Manager", plugin);
@ -230,22 +241,44 @@ public class HubManager extends MiniClientPlugin<HubClient>
// NotificationManager notificationManager = new NotificationManager(plugin, clientManager, donationManager); // NotificationManager notificationManager = new NotificationManager(plugin, clientManager, donationManager);
// new MailManager(_plugin, notificationManager); // new MailManager(_plugin, notificationManager);
_songs = new NoteSong[_songNames.length];
try
{
for (int i = 0; i < _songNames.length; i++)
{
String name = "songs/" + _songNames[i];
NoteSong song = NBSReader.loadSong(name);
_songs[i] = song;
System.out.println("Loaded Song: " + name);
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
System.out.println("FAILED TO LOAD SONG!!");
}
playNextSong();
_serverName = getPlugin().getConfig().getString("serverstatus.name"); _serverName = getPlugin().getConfig().getString("serverstatus.name");
_serverName = _serverName.substring(0, Math.min(16, _serverName.length())); _serverName = _serverName.substring(0, Math.min(16, _serverName.length()));
}
// try private void playNextSong()
// { {
// NoteSong song = NBSReader.loadSong("songs/LetItGo.nbs"); int index = (_lastSong + _random.nextInt(_songs.length - 1)) % _songs.length;
// if (song != null) NoteSong song = _songs[index];
// { if (song != null)
// NotePlayer player = new NotePlayer(song, true); {
// } new NotePlayer(_plugin, song, new INoteVerifier()
// } {
// catch (FileNotFoundException e) @Override
// { public boolean shouldPlay(Player player)
// e.printStackTrace(); {
// System.out.println("FAILED TO LOAD SONG!!"); return _preferences.Get(player).HubMusic;
// } }
}, 0.5F, false);
}
} }
@Override @Override
@ -944,4 +977,20 @@ public class HubManager extends MiniClientPlugin<HubClient>
new GUIProfile(getPlugin(), event.getPlayer(), _preferences, _achievementManager, _personalServerManager).openInventory();; new GUIProfile(getPlugin(), event.getPlayer(), _preferences, _achievementManager, _personalServerManager).openInventory();;
} }
@EventHandler
public void disableMusicChristmas(GadgetActivateEvent event)
{
if (event.getGadget() != null && event.getGadget().getGadgetType() == GadgetType.MusicDisc)
{
UtilPlayer.message(event.getPlayer(), F.main("Hub", "Music Discs are disabled right now"));
event.setCancelled(true);
}
}
@EventHandler
public void onSongFinish(SongFinishEvent event)
{
playNextSong();
}
} }