From 7b5db2d4fbe21295f37c8a5c7868b383ad70ec7d Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Mon, 7 Dec 2015 00:58:44 -0500 Subject: [PATCH] Hub Music, HubMusic Preference --- .../core/noteblock/INoteVerifier.java | 10 +++ .../mineplex/core/noteblock/NotePlayer.java | 40 +++++++++- .../core/noteblock/event/SongFinishEvent.java | 25 +++++++ .../core/noteblock/event/SongStartEvent.java | 25 +++++++ .../core/preferences/PreferencesManager.java | 2 +- .../preferences/PreferencesRepository.java | 9 ++- .../core/preferences/UserPreferences.java | 1 + .../core/preferences/ui/PreferencesPage.java | 25 ++++++- .../src/mineplex/hub/HubManager.java | 75 +++++++++++++++---- 9 files changed, 189 insertions(+), 23 deletions(-) create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/noteblock/INoteVerifier.java create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/noteblock/event/SongFinishEvent.java create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/noteblock/event/SongStartEvent.java diff --git a/Plugins/Mineplex.Core/src/mineplex/core/noteblock/INoteVerifier.java b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/INoteVerifier.java new file mode 100644 index 000000000..f423c6394 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/INoteVerifier.java @@ -0,0 +1,10 @@ +package mineplex.core.noteblock; + +import org.bukkit.entity.Player; + +public interface INoteVerifier +{ + + public boolean shouldPlay(Player player); + +} diff --git a/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NotePlayer.java b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NotePlayer.java index 19e48f16d..941bc1dd9 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NotePlayer.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/NotePlayer.java @@ -1,26 +1,41 @@ package mineplex.core.noteblock; +import java.util.ArrayList; +import java.util.List; + import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; import mineplex.core.common.util.UtilServer; +import mineplex.core.noteblock.event.SongFinishEvent; +import mineplex.core.noteblock.event.SongStartEvent; public class NotePlayer { + private final JavaPlugin _plugin; private final NoteSong _song; + private final INoteVerifier _verifier; private final long _sleepMs; + private volatile float _volumeMult; private volatile boolean _loop; private volatile int _tick; 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; + _verifier = verifier; _sleepMs = (long) (1000 / (song.getTempo() / 100D)); _loop = loop; _tick = 0; + _volumeMult = volumeMult; _finished = false; startThread(); + + SongStartEvent event = new SongStartEvent(_song); + _plugin.getServer().getPluginManager().callEvent(event); } private void startThread() @@ -43,6 +58,17 @@ public class NotePlayer else { _finished = true; + + _plugin.getServer().getScheduler().runTask(_plugin, new Runnable() + { + @Override + public void run() + { + SongFinishEvent event = new SongFinishEvent(_song); + _plugin.getServer().getPluginManager().callEvent(event); + } + }); + return; } } @@ -66,13 +92,21 @@ public class NotePlayer private void playTick(int tick) { + Player[] playerArray = UtilServer.getPlayers(); + List players = new ArrayList<>(playerArray.length); + for (Player player : playerArray) + { + if (_verifier.shouldPlay(player)) + players.add(player); + } + for (NoteLayer layer : _song.getLayers()) { Note note = layer.getNote(tick); if (note != null) { - float volume = layer.getVolume() / 100F; - for (Player player : UtilServer.getPlayers()) + float volume = _volumeMult * (layer.getVolume() / 100F); + for (Player player : players) { player.playSound(player.getEyeLocation(), UtilNote.getInstrumentSound(note.getInstrument()), volume, (float) UtilNote.getPitch(note.getNote() - 33)); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/noteblock/event/SongFinishEvent.java b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/event/SongFinishEvent.java new file mode 100644 index 000000000..863537903 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/event/SongFinishEvent.java @@ -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; + } +} \ No newline at end of file diff --git a/Plugins/Mineplex.Core/src/mineplex/core/noteblock/event/SongStartEvent.java b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/event/SongStartEvent.java new file mode 100644 index 000000000..00993f7c8 --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/noteblock/event/SongStartEvent.java @@ -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; + } +} \ No newline at end of file diff --git a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java index 82eec02d1..23d067d8f 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesManager.java @@ -136,6 +136,6 @@ public class PreferencesManager extends MiniDbClientPlugin @Override 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;"; } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java index 7449c83c9..895555da6 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/preferences/PreferencesRepository.java @@ -24,7 +24,7 @@ public class PreferencesRepository extends RepositoryBase // DEFAULT 0, showMacReports BOOL NOT NULL DEFAULT 0, ignoreVelocity BOOL // 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 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) { @@ -61,8 +61,9 @@ public class PreferencesRepository extends RepositoryBase preparedStatement.setBoolean(11, entry.getValue().PendingFriendRequests); preparedStatement.setBoolean(12, entry.getValue().friendDisplayInventoryUI); preparedStatement.setBoolean(13, entry.getValue().ClanTips); + preparedStatement.setBoolean(14, entry.getValue().HubMusic); System.out.println(">> " + entry.getValue().ClanTips); - preparedStatement.setString(14, entry.getKey()); + preparedStatement.setString(15, entry.getKey()); preparedStatement.addBatch(); } @@ -89,8 +90,9 @@ public class PreferencesRepository extends RepositoryBase preparedStatement.setBoolean(11, entry.getValue().PendingFriendRequests); preparedStatement.setBoolean(12, entry.getValue().friendDisplayInventoryUI); preparedStatement.setBoolean(13, entry.getValue().ClanTips); + preparedStatement.setBoolean(14, entry.getValue().HubMusic); System.out.println(">> " + entry.getValue().ClanTips); - preparedStatement.setString(14, entry.getKey()); + preparedStatement.setString(15, entry.getKey()); preparedStatement.execute(); } @@ -122,6 +124,7 @@ public class PreferencesRepository extends RepositoryBase preferences.PendingFriendRequests = resultSet.getBoolean(11); preferences.friendDisplayInventoryUI = resultSet.getBoolean(12); preferences.ClanTips = resultSet.getBoolean(13); + preferences.HubMusic = resultSet.getBoolean(14); System.out.println("<< " + resultSet.getBoolean(13)); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/preferences/UserPreferences.java b/Plugins/Mineplex.Core/src/mineplex/core/preferences/UserPreferences.java index 728f549e2..4036b494f 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/preferences/UserPreferences.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/preferences/UserPreferences.java @@ -16,4 +16,5 @@ public class UserPreferences public boolean PendingFriendRequests = true; public boolean friendDisplayInventoryUI = true; public boolean ClanTips = true; + public volatile boolean HubMusic = true; } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/preferences/ui/PreferencesPage.java b/Plugins/Mineplex.Core/src/mineplex/core/preferences/ui/PreferencesPage.java index 7cd111100..27e20e3d6 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/preferences/ui/PreferencesPage.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/preferences/ui/PreferencesPage.java @@ -25,6 +25,7 @@ public class PreferencesPage extends ShopPageBase private HashMap> _creativeAdmin = new HashMap>(); + // 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) { super("Hub Manager", plugin); @@ -230,22 +241,44 @@ public class HubManager extends MiniClientPlugin // NotificationManager notificationManager = new NotificationManager(plugin, clientManager, donationManager); // 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 = _serverName.substring(0, Math.min(16, _serverName.length())); + } -// try -// { -// NoteSong song = NBSReader.loadSong("songs/LetItGo.nbs"); -// if (song != null) -// { -// NotePlayer player = new NotePlayer(song, true); -// } -// } -// catch (FileNotFoundException e) -// { -// e.printStackTrace(); -// System.out.println("FAILED TO LOAD SONG!!"); -// } + private void playNextSong() + { + int index = (_lastSong + _random.nextInt(_songs.length - 1)) % _songs.length; + NoteSong song = _songs[index]; + if (song != null) + { + new NotePlayer(_plugin, song, new INoteVerifier() + { + @Override + public boolean shouldPlay(Player player) + { + return _preferences.Get(player).HubMusic; + } + }, 0.5F, false); + } } @Override @@ -944,4 +977,20 @@ public class HubManager extends MiniClientPlugin 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(); + } }