From 953ad66aca8df7a3b4dd8f693d8388293cbfe719 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Tue, 11 Jul 2017 05:42:21 -0400 Subject: [PATCH] Update SalesAnnouncement system to allow clans-only announcements and make cross-server updating work properly --- .../src/mineplex/clanshub/ClansHub.java | 2 +- .../src/mineplex/clanshub/HubManager.java | 3 +- .../SalesAnnouncementData.java | 6 +-- .../SalesAnnouncementDeleteCommand.java | 12 ++++-- .../SalesAnnouncementDeleteHandler.java | 11 +++-- .../SalesAnnouncementManager.java | 43 ++++++++++--------- .../SalesAnnouncementPage.java | 5 ++- .../SalesAnnouncementRepository.java | 33 +++++++------- .../SalesAnnouncementUpdateCommand.java | 13 ++++-- .../SalesAnnouncementUpdateHandler.java | 11 +++-- .../SalesAnnouncementData.java | 6 +-- .../SalesAnnouncementDeleteCommand.java | 12 ++++-- .../SalesAnnouncementDeleteHandler.java | 11 +++-- .../SalesAnnouncementManager.java | 43 ++++++++++--------- .../SalesAnnouncementPage.java | 5 ++- .../SalesAnnouncementRepository.java | 33 +++++++------- .../SalesAnnouncementUpdateCommand.java | 13 ++++-- .../SalesAnnouncementUpdateHandler.java | 11 +++-- .../serverdata/commands/CommandCallback.java | 4 +- .../serverdata/commands/CommandType.java | 10 ++--- .../commands/ServerCommandManager.java | 9 ++-- 21 files changed, 163 insertions(+), 133 deletions(-) diff --git a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/ClansHub.java b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/ClansHub.java index 42a6f3a74..df7dd09c4 100644 --- a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/ClansHub.java +++ b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/ClansHub.java @@ -154,7 +154,7 @@ public class ClansHub extends JavaPlugin ConditionManager condition = new ConditionManager(this); ThankManager thankManager = new ThankManager(this, clientManager, donationManager); BoosterManager boosterManager = new BoosterManager(this, "", clientManager, donationManager, inventoryManager, thankManager); - HubManager hubManager = new HubManager(this, blockRestore, clientManager, incognito, donationManager, inventoryManager, condition, disguiseManager, new TaskManager(this, clientManager), portal, partyManager, preferenceManager, petManager, pollManager, statsManager, achievementManager, new HologramManager(this, packetHandler), npcManager, packetHandler, punish, serverStatusManager, customDataManager, thankManager, boosterManager); + HubManager hubManager = new HubManager(this, blockRestore, clientManager, incognito, donationManager, inventoryManager, condition, disguiseManager, new TaskManager(this, clientManager), portal, partyManager, preferenceManager, petManager, pollManager, statsManager, achievementManager, hologramManager, npcManager, packetHandler, punish, serverStatusManager, customDataManager, thankManager, boosterManager, castleManager); ClansTransferManager serverManager = new ClansTransferManager(this, clientManager, donationManager, partyManager, portal, hubManager); diff --git a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/HubManager.java b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/HubManager.java index b02d21718..19d55cb24 100644 --- a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/HubManager.java +++ b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/HubManager.java @@ -172,7 +172,7 @@ public class HubManager extends MiniPlugin implements IChatMessageFormatter private HashMap> _creativeAdmin = new HashMap>(); - public HubManager(JavaPlugin plugin, BlockRestore blockRestore, CoreClientManager clientManager, IncognitoManager incognito, 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, PacketHandler packetHandler, Punish punish, ServerStatusManager serverStatusManager, CustomDataManager customDataManager, ThankManager thankManager, BoosterManager boosterManager) + public HubManager(JavaPlugin plugin, BlockRestore blockRestore, CoreClientManager clientManager, IncognitoManager incognito, 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, PacketHandler packetHandler, Punish punish, ServerStatusManager serverStatusManager, CustomDataManager customDataManager, ThankManager thankManager, BoosterManager boosterManager, CastleManager castleManager) { super("Hub Manager", plugin); @@ -195,7 +195,6 @@ public class HubManager extends MiniPlugin implements IChatMessageFormatter _inventoryManager = inventoryManager; new BenefitManager(plugin, clientManager, _inventoryManager); - CastleManager castleManager = new CastleManager(_plugin, _clientManager, hologramManager, false); _gadgetManager = new GadgetManager(_plugin, clientManager, donationManager, _inventoryManager, _mountManager, petManager, preferences, disguiseManager, blockRestore, new ProjectileManager(plugin), achievementManager, packetHandler, hologramManager, incognito, castleManager); FacebookManager facebookManager = new FacebookManager(plugin, clientManager, donationManager, inventoryManager); diff --git a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementData.java b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementData.java index f12564495..23c2f5e7a 100644 --- a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementData.java +++ b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementData.java @@ -12,12 +12,12 @@ import mineplex.core.itemstack.ItemBuilder; public class SalesAnnouncementData { - private final int _id; + private final Integer _id; private final Rank[] _displayTo; private final String _message; private boolean _enabled; - public SalesAnnouncementData(int id, Rank[] displayTo, String message, boolean enabled) + public SalesAnnouncementData(Integer id, Rank[] displayTo, String message, boolean enabled) { _id = id; _displayTo = displayTo; @@ -25,7 +25,7 @@ public class SalesAnnouncementData _enabled = enabled; } - public int getId() + public Integer getId() { return _id; } diff --git a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementDeleteCommand.java b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementDeleteCommand.java index 90c08ad87..705c41f59 100644 --- a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementDeleteCommand.java +++ b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementDeleteCommand.java @@ -4,16 +4,17 @@ import mineplex.serverdata.commands.ServerCommand; public class SalesAnnouncementDeleteCommand extends ServerCommand { - private String _id; + private Integer _id; private String _from; + private boolean _clans; - public SalesAnnouncementDeleteCommand(String id, String from) + public SalesAnnouncementDeleteCommand(Integer id, String from, boolean clans) { _id = id; _from = from; } - public String getId() + public Integer getId() { return _id; } @@ -22,4 +23,9 @@ public class SalesAnnouncementDeleteCommand extends ServerCommand { return _from; } + + public boolean isClans() + { + return _clans; + } } \ No newline at end of file diff --git a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementDeleteHandler.java b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementDeleteHandler.java index 215789987..b8ab47fdd 100644 --- a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementDeleteHandler.java +++ b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementDeleteHandler.java @@ -1,9 +1,8 @@ package mineplex.clanshub.salesannouncements; import mineplex.serverdata.commands.CommandCallback; -import mineplex.serverdata.commands.ServerCommand; -public class SalesAnnouncementDeleteHandler implements CommandCallback +public class SalesAnnouncementDeleteHandler implements CommandCallback { private final SalesAnnouncementManager _manager; @@ -13,16 +12,16 @@ public class SalesAnnouncementDeleteHandler implements CommandCallback } @Override - public void run(ServerCommand command) + public void run(SalesAnnouncementDeleteCommand command) { - if (!(command instanceof SalesAnnouncementDeleteCommand)) + if (_manager.getServer().equalsIgnoreCase(command.getFrom())) { return; } - if (_manager.getServer().equalsIgnoreCase(((SalesAnnouncementDeleteCommand) command).getFrom())) + if (_manager.CLANS != command.isClans()) { return; } - _manager.handleRemoteDeletion(Integer.parseInt(((SalesAnnouncementDeleteCommand)command).getId())); + _manager.handleRemoteDeletion(command.getId()); } } \ No newline at end of file diff --git a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementManager.java b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementManager.java index 82398a078..bc5610cf3 100644 --- a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementManager.java +++ b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementManager.java @@ -1,14 +1,13 @@ package mineplex.clanshub.salesannouncements; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.plugin.java.JavaPlugin; -import com.google.common.collect.Lists; - import mineplex.core.Managers; import mineplex.core.MiniPlugin; import mineplex.core.account.CoreClientManager; @@ -22,22 +21,23 @@ import mineplex.serverdata.commands.ServerCommandManager; public class SalesAnnouncementManager extends MiniPlugin { private static final String LINE = C.cDGreenB + C.Strike + "============================================="; - private final List _data = Lists.newArrayList(); + private final Map _data = new HashMap<>(); private final SalesAnnouncementRepository _repo; + public final boolean CLANS = true; public SalesAnnouncementManager(JavaPlugin plugin) { super("Sales", plugin); - _repo = new SalesAnnouncementRepository(plugin); + _repo = new SalesAnnouncementRepository(plugin, CLANS); _repo.loadAnnouncements(_data); addCommand(new SalesAnnouncementCommand(this)); - ServerCommandManager.getInstance().registerCommandType("SalesAnnouncementUpdate", SalesAnnouncementUpdateCommand.class, new SalesAnnouncementUpdateHandler(this)); - ServerCommandManager.getInstance().registerCommandType("SalesAnnouncementDelete", SalesAnnouncementDeleteCommand.class, new SalesAnnouncementDeleteHandler(this)); + ServerCommandManager.getInstance().registerCommandType(SalesAnnouncementUpdateCommand.class, new SalesAnnouncementUpdateHandler(this)); + ServerCommandManager.getInstance().registerCommandType(SalesAnnouncementDeleteCommand.class, new SalesAnnouncementDeleteHandler(this)); } - public List getLoadedAnnouncements() + public Map getLoadedAnnouncements() { return _data; } @@ -57,8 +57,8 @@ public class SalesAnnouncementManager extends MiniPlugin _repo.createAnnouncement(displayTo, message, data -> { UtilPlayer.message(creator, F.main(getName(), "Announcement successfully created!")); - _data.add(data); - new SalesAnnouncementUpdateCommand(data.getId() + "", getServer()).publish(); + _data.put(data.getId(), data); + new SalesAnnouncementUpdateCommand(data.getId(), getServer(), CLANS).publish(); }); } @@ -73,9 +73,9 @@ public class SalesAnnouncementManager extends MiniPlugin UtilPlayer.message(deletor, F.main(getName(), "Successfully deleted announcement!")); if (!forceRemoveFromList) { - _data.remove(data); + _data.remove(data.getId()); } - new SalesAnnouncementDeleteCommand(data.getId() + "", getServer()).publish(); + new SalesAnnouncementDeleteCommand(data.getId(), getServer(), CLANS).publish(); }); } @@ -85,23 +85,26 @@ public class SalesAnnouncementManager extends MiniPlugin _repo.updateAnnouncementStatus(data, () -> { UtilPlayer.message(toggler, F.main(getName(), "Successfully toggled announcement!")); - new SalesAnnouncementUpdateCommand(data.getId() + "", getServer()).publish(); + new SalesAnnouncementUpdateCommand(data.getId(), getServer(), CLANS).publish(); }); } public void handleRemoteDeletion(int id) { - _data.removeIf(data -> data.getId() == id); - UtilServer.CallEvent(new SalesAnnouncementRemoteListUpdateEvent()); + runSync(() -> + { + _data.remove(Integer.valueOf(id)); + UtilServer.CallEvent(new SalesAnnouncementRemoteListUpdateEvent()); + }); } public void handleRemoteUpdate(int id) { - if (_data.stream().filter(data -> data.getId() == id).toArray().length > 0) + if (_data.containsKey(Integer.valueOf(id))) { _repo.loadAnnouncement(id, data -> { - _data.stream().filter(existing -> existing.getId() == data.getId()).forEach(existing -> existing.setEnabled(data.isEnabled())); + _data.get(data.getId()).setEnabled(data.isEnabled()); UtilServer.CallEvent(new SalesAnnouncementRemoteListUpdateEvent()); }); } @@ -109,7 +112,7 @@ public class SalesAnnouncementManager extends MiniPlugin { _repo.loadAnnouncement(id, data -> { - _data.add(data); + _data.put(data.getId(), data); UtilServer.CallEvent(new SalesAnnouncementRemoteListUpdateEvent()); }); } @@ -118,7 +121,7 @@ public class SalesAnnouncementManager extends MiniPlugin @EventHandler public void onJoin(PlayerJoinEvent event) { - if (_data.isEmpty() || _data.stream().filter(data -> data.isEnabled()).toArray().length == 0) + if (_data.isEmpty() || !_data.values().stream().filter(data -> data.isEnabled()).findAny().isPresent()) { return; } @@ -127,7 +130,7 @@ public class SalesAnnouncementManager extends MiniPlugin runSyncLater(() -> { - _data.stream().filter(data -> data.isEnabled() && data.shouldDisplayTo(rank)).forEach(data -> + _data.values().stream().filter(data -> data.isEnabled() && data.shouldDisplayTo(rank)).forEach(data -> { player.sendMessage(" "); player.sendMessage(LINE); diff --git a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementPage.java b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementPage.java index c81eb6b76..966409666 100644 --- a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementPage.java +++ b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementPage.java @@ -36,10 +36,11 @@ public class SalesAnnouncementPage implements Listener private void setup() { _buttons.clear(); - for (int i = 0; i < _manager.getLoadedAnnouncements().size(); i++) + int i = 0; + for (SalesAnnouncementData data : _manager.getLoadedAnnouncements().values()) { - SalesAnnouncementData data = _manager.getLoadedAnnouncements().get(i); _buttons.put(i, new SalesAnnouncementButton(data, this)); + i++; } updateButtons(false); } diff --git a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementRepository.java b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementRepository.java index 6876ba6f2..78762f61c 100644 --- a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementRepository.java +++ b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementRepository.java @@ -1,6 +1,8 @@ package mineplex.clanshub.salesannouncements; +import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -9,7 +11,6 @@ import com.google.common.collect.Lists; import mineplex.core.common.Rank; import mineplex.core.common.util.Callback; -import mineplex.core.database.MinecraftRepository; import mineplex.serverdata.database.DBPool; import mineplex.serverdata.database.RepositoryBase; import mineplex.serverdata.database.column.ColumnBoolean; @@ -18,20 +19,22 @@ import mineplex.serverdata.database.column.ColumnVarChar; public class SalesAnnouncementRepository extends RepositoryBase { - private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS salesAnnouncements (id INT NOT NULL AUTO_INCREMENT, ranks VARCHAR(250), message VARCHAR(256), enabled BOOL, PRIMARY KEY (id));"; + private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS salesAnnouncements (id INT NOT NULL AUTO_INCREMENT, ranks VARCHAR(250), message VARCHAR(256), enabled BOOL, clans BOOL, PRIMARY KEY (id), INDEX typeIndex (clans));"; - private static final String GET_ANNOUNCEMENTS = "SELECT * FROM salesAnnouncements;"; + private static final String GET_ANNOUNCEMENTS = "SELECT * FROM salesAnnouncements WHERE clans=?;"; private static final String GET_ANNOUNCEMENT = "SELECT * FROM salesAnnouncements WHERE id=?;"; private static final String UPDATE_ANNOUNCEMENT_STATUS = "UPDATE salesAnnouncements SET enabled=? WHERE id=?;"; - private static final String INSERT_ANNOUNCEMENT = "INSERT INTO salesAnnouncements (ranks, message, enabled) VALUES(?, ?, ?);"; + private static final String INSERT_ANNOUNCEMENT = "INSERT INTO salesAnnouncements (ranks, message, enabled, clans) VALUES(?, ?, ?, ?);"; private static final String DELETE_ANNOUNCEMENT = "DELETE FROM salesAnnouncements WHERE id=?;"; private final JavaPlugin _plugin; + private final boolean _clans; - public SalesAnnouncementRepository(JavaPlugin plugin) + public SalesAnnouncementRepository(JavaPlugin plugin, boolean clans) { super(DBPool.getAccount()); _plugin = plugin; + _clans = clans; } private void runAsync(Runnable runnable) @@ -44,13 +47,13 @@ public class SalesAnnouncementRepository extends RepositoryBase Bukkit.getScheduler().runTask(_plugin, runnable); } - public void loadAnnouncements(final List announcementList) + public void loadAnnouncements(final Map map) { runAsync(() -> { executeQuery(GET_ANNOUNCEMENTS, resultSet -> { - final List data = Lists.newArrayList(); + final List data = new ArrayList<>(); while (resultSet.next()) { int id = resultSet.getInt("id"); @@ -71,15 +74,15 @@ public class SalesAnnouncementRepository extends RepositoryBase String message = resultSet.getString("message"); boolean enabled = resultSet.getBoolean("enabled"); - data.add(new SalesAnnouncementData(id, displayTo, message, enabled)); + data.add(new SalesAnnouncementData(Integer.valueOf(id), displayTo, message, enabled)); } runSync(() -> { - announcementList.clear(); - data.forEach(sData -> announcementList.add(sData)); + map.clear(); + data.forEach(sData -> map.put(sData.getId(), sData)); }); - }); + }, new ColumnBoolean("clans", _clans)); }); } @@ -109,7 +112,7 @@ public class SalesAnnouncementRepository extends RepositoryBase String message = resultSet.getString("message"); boolean enabled = resultSet.getBoolean("enabled"); - final SalesAnnouncementData data = new SalesAnnouncementData(aId, displayTo, message, enabled); + final SalesAnnouncementData data = new SalesAnnouncementData(Integer.valueOf(aId), displayTo, message, enabled); runSync(() -> { callback.run(data); @@ -139,7 +142,7 @@ public class SalesAnnouncementRepository extends RepositoryBase runSync(() -> callback.run(data)); } } - }, new ColumnVarChar("ranks", 250, rankStr), new ColumnVarChar("message", 256, message), new ColumnBoolean("enabled", true)); + }, new ColumnVarChar("ranks", 250, rankStr), new ColumnVarChar("message", 256, message), new ColumnBoolean("enabled", true), new ColumnBoolean("clans", _clans)); }); } @@ -147,7 +150,7 @@ public class SalesAnnouncementRepository extends RepositoryBase { runAsync(() -> { - executeUpdate(UPDATE_ANNOUNCEMENT_STATUS, new ColumnBoolean("enabled", data.isEnabled()), new ColumnInt("id", data.getId())); + executeUpdate(UPDATE_ANNOUNCEMENT_STATUS, new ColumnBoolean("enabled", data.isEnabled()), new ColumnInt("id", data.getId().intValue())); if (after != null) { runSync(after); @@ -159,7 +162,7 @@ public class SalesAnnouncementRepository extends RepositoryBase { runAsync(() -> { - executeUpdate(DELETE_ANNOUNCEMENT, new ColumnInt("id", data.getId())); + executeUpdate(DELETE_ANNOUNCEMENT, new ColumnInt("id", data.getId().intValue())); if (after != null) { runSync(after); diff --git a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementUpdateCommand.java b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementUpdateCommand.java index 8c1c52054..16edeabc9 100644 --- a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementUpdateCommand.java +++ b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementUpdateCommand.java @@ -4,16 +4,18 @@ import mineplex.serverdata.commands.ServerCommand; public class SalesAnnouncementUpdateCommand extends ServerCommand { - private String _id; + private Integer _id; private String _from; + private boolean _clans; - public SalesAnnouncementUpdateCommand(String id, String from) + public SalesAnnouncementUpdateCommand(Integer id, String from, boolean clans) { _id = id; _from = from; + _clans = clans; } - public String getId() + public Integer getId() { return _id; } @@ -22,4 +24,9 @@ public class SalesAnnouncementUpdateCommand extends ServerCommand { return _from; } + + public boolean isClans() + { + return _clans; + } } \ No newline at end of file diff --git a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementUpdateHandler.java b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementUpdateHandler.java index 1c56c5ca6..d2771b953 100644 --- a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementUpdateHandler.java +++ b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/salesannouncements/SalesAnnouncementUpdateHandler.java @@ -1,9 +1,8 @@ package mineplex.clanshub.salesannouncements; import mineplex.serverdata.commands.CommandCallback; -import mineplex.serverdata.commands.ServerCommand; -public class SalesAnnouncementUpdateHandler implements CommandCallback +public class SalesAnnouncementUpdateHandler implements CommandCallback { private final SalesAnnouncementManager _manager; @@ -13,16 +12,16 @@ public class SalesAnnouncementUpdateHandler implements CommandCallback } @Override - public void run(ServerCommand command) + public void run(SalesAnnouncementUpdateCommand command) { - if (!(command instanceof SalesAnnouncementUpdateCommand)) + if (_manager.getServer().equalsIgnoreCase(command.getFrom())) { return; } - if (_manager.getServer().equalsIgnoreCase(((SalesAnnouncementUpdateCommand) command).getFrom())) + if (_manager.CLANS != command.isClans()) { return; } - _manager.handleRemoteUpdate(Integer.parseInt(((SalesAnnouncementUpdateCommand)command).getId())); + _manager.handleRemoteUpdate(command.getId()); } } \ No newline at end of file diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementData.java b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementData.java index a7663e430..964962bf7 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementData.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementData.java @@ -12,12 +12,12 @@ import mineplex.core.itemstack.ItemBuilder; public class SalesAnnouncementData { - private final int _id; + private final Integer _id; private final Rank[] _displayTo; private final String _message; private boolean _enabled; - public SalesAnnouncementData(int id, Rank[] displayTo, String message, boolean enabled) + public SalesAnnouncementData(Integer id, Rank[] displayTo, String message, boolean enabled) { _id = id; _displayTo = displayTo; @@ -25,7 +25,7 @@ public class SalesAnnouncementData _enabled = enabled; } - public int getId() + public Integer getId() { return _id; } diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementDeleteCommand.java b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementDeleteCommand.java index f8244ec41..e7e1f0c7e 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementDeleteCommand.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementDeleteCommand.java @@ -4,16 +4,17 @@ import mineplex.serverdata.commands.ServerCommand; public class SalesAnnouncementDeleteCommand extends ServerCommand { - private String _id; + private Integer _id; private String _from; + private boolean _clans; - public SalesAnnouncementDeleteCommand(String id, String from) + public SalesAnnouncementDeleteCommand(Integer id, String from, boolean clans) { _id = id; _from = from; } - public String getId() + public Integer getId() { return _id; } @@ -22,4 +23,9 @@ public class SalesAnnouncementDeleteCommand extends ServerCommand { return _from; } + + public boolean isClans() + { + return _clans; + } } \ No newline at end of file diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementDeleteHandler.java b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementDeleteHandler.java index ec8a8934d..88071df75 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementDeleteHandler.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementDeleteHandler.java @@ -1,9 +1,8 @@ package mineplex.hub.modules.salesannouncements; import mineplex.serverdata.commands.CommandCallback; -import mineplex.serverdata.commands.ServerCommand; -public class SalesAnnouncementDeleteHandler implements CommandCallback +public class SalesAnnouncementDeleteHandler implements CommandCallback { private final SalesAnnouncementManager _manager; @@ -13,16 +12,16 @@ public class SalesAnnouncementDeleteHandler implements CommandCallback } @Override - public void run(ServerCommand command) + public void run(SalesAnnouncementDeleteCommand command) { - if (!(command instanceof SalesAnnouncementDeleteCommand)) + if (_manager.getServer().equalsIgnoreCase(command.getFrom())) { return; } - if (_manager.getServer().equalsIgnoreCase(((SalesAnnouncementDeleteCommand) command).getFrom())) + if (_manager.CLANS != command.isClans()) { return; } - _manager.handleRemoteDeletion(Integer.parseInt(((SalesAnnouncementDeleteCommand)command).getId())); + _manager.handleRemoteDeletion(command.getId()); } } \ No newline at end of file diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementManager.java b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementManager.java index f91f399f8..b45cf8401 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementManager.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementManager.java @@ -1,14 +1,13 @@ package mineplex.hub.modules.salesannouncements; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.plugin.java.JavaPlugin; -import com.google.common.collect.Lists; - import mineplex.core.Managers; import mineplex.core.MiniPlugin; import mineplex.core.account.CoreClientManager; @@ -22,22 +21,23 @@ import mineplex.serverdata.commands.ServerCommandManager; public class SalesAnnouncementManager extends MiniPlugin { private static final String LINE = C.cDGreenB + C.Strike + "============================================="; - private final List _data = Lists.newArrayList(); + private final Map _data = new HashMap<>(); private final SalesAnnouncementRepository _repo; + public final boolean CLANS = false; public SalesAnnouncementManager(JavaPlugin plugin) { super("Sales", plugin); - _repo = new SalesAnnouncementRepository(plugin); + _repo = new SalesAnnouncementRepository(plugin, CLANS); _repo.loadAnnouncements(_data); addCommand(new SalesAnnouncementCommand(this)); - ServerCommandManager.getInstance().registerCommandType("SalesAnnouncementUpdate", SalesAnnouncementUpdateCommand.class, new SalesAnnouncementUpdateHandler(this)); - ServerCommandManager.getInstance().registerCommandType("SalesAnnouncementDelete", SalesAnnouncementDeleteCommand.class, new SalesAnnouncementDeleteHandler(this)); + ServerCommandManager.getInstance().registerCommandType(SalesAnnouncementUpdateCommand.class, new SalesAnnouncementUpdateHandler(this)); + ServerCommandManager.getInstance().registerCommandType(SalesAnnouncementDeleteCommand.class, new SalesAnnouncementDeleteHandler(this)); } - public List getLoadedAnnouncements() + public Map getLoadedAnnouncements() { return _data; } @@ -57,8 +57,8 @@ public class SalesAnnouncementManager extends MiniPlugin _repo.createAnnouncement(displayTo, message, data -> { UtilPlayer.message(creator, F.main(getName(), "Announcement successfully created!")); - _data.add(data); - new SalesAnnouncementUpdateCommand(data.getId() + "", getServer()).publish(); + _data.put(data.getId(), data); + new SalesAnnouncementUpdateCommand(data.getId(), getServer(), CLANS).publish(); }); } @@ -73,9 +73,9 @@ public class SalesAnnouncementManager extends MiniPlugin UtilPlayer.message(deletor, F.main(getName(), "Successfully deleted announcement!")); if (!forceRemoveFromList) { - _data.remove(data); + _data.remove(data.getId()); } - new SalesAnnouncementDeleteCommand(data.getId() + "", getServer()).publish(); + new SalesAnnouncementDeleteCommand(data.getId(), getServer(), CLANS).publish(); }); } @@ -85,23 +85,26 @@ public class SalesAnnouncementManager extends MiniPlugin _repo.updateAnnouncementStatus(data, () -> { UtilPlayer.message(toggler, F.main(getName(), "Successfully toggled announcement!")); - new SalesAnnouncementUpdateCommand(data.getId() + "", getServer()).publish(); + new SalesAnnouncementUpdateCommand(data.getId(), getServer(), CLANS).publish(); }); } public void handleRemoteDeletion(int id) { - _data.removeIf(data -> data.getId() == id); - UtilServer.CallEvent(new SalesAnnouncementRemoteListUpdateEvent()); + runSync(() -> + { + _data.remove(Integer.valueOf(id)); + UtilServer.CallEvent(new SalesAnnouncementRemoteListUpdateEvent()); + }); } public void handleRemoteUpdate(int id) { - if (_data.stream().filter(data -> data.getId() == id).toArray().length > 0) + if (_data.containsKey(Integer.valueOf(id))) { _repo.loadAnnouncement(id, data -> { - _data.stream().filter(existing -> existing.getId() == data.getId()).forEach(existing -> existing.setEnabled(data.isEnabled())); + _data.get(data.getId()).setEnabled(data.isEnabled()); UtilServer.CallEvent(new SalesAnnouncementRemoteListUpdateEvent()); }); } @@ -109,7 +112,7 @@ public class SalesAnnouncementManager extends MiniPlugin { _repo.loadAnnouncement(id, data -> { - _data.add(data); + _data.put(data.getId(), data); UtilServer.CallEvent(new SalesAnnouncementRemoteListUpdateEvent()); }); } @@ -118,7 +121,7 @@ public class SalesAnnouncementManager extends MiniPlugin @EventHandler public void onJoin(PlayerJoinEvent event) { - if (_data.isEmpty() || _data.stream().filter(data -> data.isEnabled()).toArray().length == 0) + if (_data.isEmpty() || !_data.values().stream().filter(data -> data.isEnabled()).findAny().isPresent()) { return; } @@ -127,7 +130,7 @@ public class SalesAnnouncementManager extends MiniPlugin runSyncLater(() -> { - _data.stream().filter(data -> data.isEnabled() && data.shouldDisplayTo(rank)).forEach(data -> + _data.values().stream().filter(data -> data.isEnabled() && data.shouldDisplayTo(rank)).forEach(data -> { player.sendMessage(" "); player.sendMessage(LINE); diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementPage.java b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementPage.java index c2be6a7b8..53990c319 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementPage.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementPage.java @@ -36,10 +36,11 @@ public class SalesAnnouncementPage implements Listener private void setup() { _buttons.clear(); - for (int i = 0; i < _manager.getLoadedAnnouncements().size(); i++) + int i = 0; + for (SalesAnnouncementData data : _manager.getLoadedAnnouncements().values()) { - SalesAnnouncementData data = _manager.getLoadedAnnouncements().get(i); _buttons.put(i, new SalesAnnouncementButton(data, this)); + i++; } updateButtons(false); } diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementRepository.java b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementRepository.java index 844a3b5ee..43584cd7e 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementRepository.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementRepository.java @@ -1,6 +1,8 @@ package mineplex.hub.modules.salesannouncements; +import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -9,7 +11,6 @@ import com.google.common.collect.Lists; import mineplex.core.common.Rank; import mineplex.core.common.util.Callback; -import mineplex.core.database.MinecraftRepository; import mineplex.serverdata.database.DBPool; import mineplex.serverdata.database.RepositoryBase; import mineplex.serverdata.database.column.ColumnBoolean; @@ -18,20 +19,22 @@ import mineplex.serverdata.database.column.ColumnVarChar; public class SalesAnnouncementRepository extends RepositoryBase { - private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS salesAnnouncements (id INT NOT NULL AUTO_INCREMENT, ranks VARCHAR(250), message VARCHAR(256), enabled BOOL, PRIMARY KEY (id));"; + private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS salesAnnouncements (id INT NOT NULL AUTO_INCREMENT, ranks VARCHAR(250), message VARCHAR(256), enabled BOOL, clans BOOL, PRIMARY KEY (id), INDEX typeIndex (clans));"; - private static final String GET_ANNOUNCEMENTS = "SELECT * FROM salesAnnouncements;"; + private static final String GET_ANNOUNCEMENTS = "SELECT * FROM salesAnnouncements WHERE clans=?;"; private static final String GET_ANNOUNCEMENT = "SELECT * FROM salesAnnouncements WHERE id=?;"; private static final String UPDATE_ANNOUNCEMENT_STATUS = "UPDATE salesAnnouncements SET enabled=? WHERE id=?;"; - private static final String INSERT_ANNOUNCEMENT = "INSERT INTO salesAnnouncements (ranks, message, enabled) VALUES(?, ?, ?);"; + private static final String INSERT_ANNOUNCEMENT = "INSERT INTO salesAnnouncements (ranks, message, enabled, clans) VALUES(?, ?, ?, ?);"; private static final String DELETE_ANNOUNCEMENT = "DELETE FROM salesAnnouncements WHERE id=?;"; private final JavaPlugin _plugin; + private final boolean _clans; - public SalesAnnouncementRepository(JavaPlugin plugin) + public SalesAnnouncementRepository(JavaPlugin plugin, boolean clans) { super(DBPool.getAccount()); _plugin = plugin; + _clans = clans; } private void runAsync(Runnable runnable) @@ -44,13 +47,13 @@ public class SalesAnnouncementRepository extends RepositoryBase Bukkit.getScheduler().runTask(_plugin, runnable); } - public void loadAnnouncements(final List announcementList) + public void loadAnnouncements(final Map map) { runAsync(() -> { executeQuery(GET_ANNOUNCEMENTS, resultSet -> { - final List data = Lists.newArrayList(); + final List data = new ArrayList<>(); while (resultSet.next()) { int id = resultSet.getInt("id"); @@ -71,15 +74,15 @@ public class SalesAnnouncementRepository extends RepositoryBase String message = resultSet.getString("message"); boolean enabled = resultSet.getBoolean("enabled"); - data.add(new SalesAnnouncementData(id, displayTo, message, enabled)); + data.add(new SalesAnnouncementData(Integer.valueOf(id), displayTo, message, enabled)); } runSync(() -> { - announcementList.clear(); - data.forEach(sData -> announcementList.add(sData)); + map.clear(); + data.forEach(sData -> map.put(sData.getId(), sData)); }); - }); + }, new ColumnBoolean("clans", _clans)); }); } @@ -109,7 +112,7 @@ public class SalesAnnouncementRepository extends RepositoryBase String message = resultSet.getString("message"); boolean enabled = resultSet.getBoolean("enabled"); - final SalesAnnouncementData data = new SalesAnnouncementData(aId, displayTo, message, enabled); + final SalesAnnouncementData data = new SalesAnnouncementData(Integer.valueOf(aId), displayTo, message, enabled); runSync(() -> { callback.run(data); @@ -139,7 +142,7 @@ public class SalesAnnouncementRepository extends RepositoryBase runSync(() -> callback.run(data)); } } - }, new ColumnVarChar("ranks", 250, rankStr), new ColumnVarChar("message", 256, message), new ColumnBoolean("enabled", true)); + }, new ColumnVarChar("ranks", 250, rankStr), new ColumnVarChar("message", 256, message), new ColumnBoolean("enabled", true), new ColumnBoolean("clans", _clans)); }); } @@ -147,7 +150,7 @@ public class SalesAnnouncementRepository extends RepositoryBase { runAsync(() -> { - executeUpdate(UPDATE_ANNOUNCEMENT_STATUS, new ColumnBoolean("enabled", data.isEnabled()), new ColumnInt("id", data.getId())); + executeUpdate(UPDATE_ANNOUNCEMENT_STATUS, new ColumnBoolean("enabled", data.isEnabled()), new ColumnInt("id", data.getId().intValue())); if (after != null) { runSync(after); @@ -159,7 +162,7 @@ public class SalesAnnouncementRepository extends RepositoryBase { runAsync(() -> { - executeUpdate(DELETE_ANNOUNCEMENT, new ColumnInt("id", data.getId())); + executeUpdate(DELETE_ANNOUNCEMENT, new ColumnInt("id", data.getId().intValue())); if (after != null) { runSync(after); diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementUpdateCommand.java b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementUpdateCommand.java index 698154e7c..c1735a5ba 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementUpdateCommand.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementUpdateCommand.java @@ -4,16 +4,18 @@ import mineplex.serverdata.commands.ServerCommand; public class SalesAnnouncementUpdateCommand extends ServerCommand { - private String _id; + private Integer _id; private String _from; + private boolean _clans; - public SalesAnnouncementUpdateCommand(String id, String from) + public SalesAnnouncementUpdateCommand(Integer id, String from, boolean clans) { _id = id; _from = from; + _clans = clans; } - public String getId() + public Integer getId() { return _id; } @@ -22,4 +24,9 @@ public class SalesAnnouncementUpdateCommand extends ServerCommand { return _from; } + + public boolean isClans() + { + return _clans; + } } \ No newline at end of file diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementUpdateHandler.java b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementUpdateHandler.java index a9143764f..1939a18d4 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementUpdateHandler.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/modules/salesannouncements/SalesAnnouncementUpdateHandler.java @@ -1,9 +1,8 @@ package mineplex.hub.modules.salesannouncements; import mineplex.serverdata.commands.CommandCallback; -import mineplex.serverdata.commands.ServerCommand; -public class SalesAnnouncementUpdateHandler implements CommandCallback +public class SalesAnnouncementUpdateHandler implements CommandCallback { private final SalesAnnouncementManager _manager; @@ -13,16 +12,16 @@ public class SalesAnnouncementUpdateHandler implements CommandCallback } @Override - public void run(ServerCommand command) + public void run(SalesAnnouncementUpdateCommand command) { - if (!(command instanceof SalesAnnouncementUpdateCommand)) + if (_manager.getServer().equalsIgnoreCase(command.getFrom())) { return; } - if (_manager.getServer().equalsIgnoreCase(((SalesAnnouncementUpdateCommand) command).getFrom())) + if (_manager.CLANS != command.isClans()) { return; } - _manager.handleRemoteUpdate(Integer.parseInt(((SalesAnnouncementUpdateCommand)command).getId())); + _manager.handleRemoteUpdate(command.getId()); } } \ No newline at end of file diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/CommandCallback.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/CommandCallback.java index 2153d4758..fcf12abca 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/CommandCallback.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/CommandCallback.java @@ -1,8 +1,6 @@ package mineplex.serverdata.commands; - public interface CommandCallback { - void run(T command); -} +} \ No newline at end of file diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/CommandType.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/CommandType.java index 9f541b8b8..4fe680f44 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/CommandType.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/CommandType.java @@ -1,18 +1,16 @@ package mineplex.serverdata.commands; - public class CommandType { - private Class _commandClazz; public Class getCommandType() { return _commandClazz; } - private CommandCallback _commandCallback; - public CommandCallback getCallback() { return _commandCallback; } + private CommandCallback _commandCallback; + public CommandCallback getCallback() { return _commandCallback; } - public CommandType(Class commandClazz, CommandCallback commandCallback) + public CommandType(Class commandClazz, CommandCallback commandCallback) { _commandClazz = commandClazz; _commandCallback = commandCallback; } -} +} \ No newline at end of file diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/ServerCommandManager.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/ServerCommandManager.java index 259a5ef5f..72f86461a 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/ServerCommandManager.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/ServerCommandManager.java @@ -9,8 +9,6 @@ import mineplex.serverdata.Utility; import mineplex.serverdata.servers.ServerManager; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPoolConfig; -import redis.clients.jedis.exceptions.JedisConnectionException; public class ServerCommandManager { @@ -35,7 +33,7 @@ public class ServerCommandManager public boolean isServerInitialized() { return _localServerName != null; } public String getServerName() { - return this._localServerName; + return _localServerName; } /** @@ -46,7 +44,7 @@ public class ServerCommandManager _writePool = Utility.generatePool(ServerManager.getMasterConnection()); // Publish to master instance _readPool = Utility.generatePool(ServerManager.getSlaveConnection()); // Read from slave instance - _commandTypes = new HashMap(); + _commandTypes = new HashMap<>(); initialize(); } @@ -98,6 +96,7 @@ public class ServerCommandManager * @param commandType - the type of command being received * @param serializedCommand - the serialized {@link ServerCommand} data. */ + @SuppressWarnings({ "unchecked", "rawtypes" }) public void handleCommand(final String commandType, String serializedCommand) { if (!isServerInitialized()) @@ -163,4 +162,4 @@ public class ServerCommandManager return _instance; } -} +} \ No newline at end of file