From 2fe9423196a0e0a2c55041170d9c053d8ec63c86 Mon Sep 17 00:00:00 2001 From: cnr Date: Thu, 22 Dec 2016 16:44:17 -0700 Subject: [PATCH 01/22] Remove inner query in CommunityRepository --- .../core/communities/CommunityManager.java | 9 ++++++++- .../gui/community/CommunityMemberButton.java | 2 +- .../communities/storage/CommunityRepository.java | 14 +++----------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index a8bb438a7..4ffbcf304 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -74,7 +74,7 @@ public class CommunityManager extends MiniDbClientPlugin public final String[] BLOCKED_NAMES = new String[] {"help", "chat", "create", "description", "disband", "invite", "join", "mcs", "rename", "uninvite", "trainee", "mod", "moderator", "srmod", "seniormod", "seniormoderator", "builder", "maplead", "twitch", "youtube", "support", "admin", "administrator", "leader", "dev", "developer", "owner", "party", "mineplex", "mineplexOfficial", "staff", "mineplexstaff", "qualityassurance", "traineemanagement", "modcoordination", "forumninja", "communitymanagement", "event", "socialmedia"}; private final CommunityRepository _repo; private final Map _loadedCommunities; - + public final List BrowserIds = new LinkedList<>(); public final DataRepository StatusRepository; @@ -135,6 +135,13 @@ public class CommunityManager extends MiniDbClientPlugin ServerCommandManager.getInstance().registerCommandType(CommunityUpdateName.class, new CommunityUpdateNameHandler(this)); ServerCommandManager.getInstance().registerCommandType(CommunityUpdateSetting.class, new CommunityUpdateSettingHandler(this)); } + + public boolean ownsCommunity(UUID uuid) + { + return _loadedCommunities.values().stream() + .flatMap(community -> community.getMembers().entrySet().stream()) + .anyMatch(entry -> entry.getKey().equals(uuid) && entry.getValue().Role == CommunityRole.LEADER); + } private void cycleBrowser() { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMemberButton.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMemberButton.java index 79f2fd375..4fd034064 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMemberButton.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMemberButton.java @@ -54,7 +54,7 @@ public class CommunityMemberButton extends CommunitiesGUIButton } if (_info.Role == CommunityRole.COLEADER && type == ClickType.SHIFT_LEFT) { - if (_info.OwnsCommunity) + if (getCommunityManager().ownsCommunity(_info.UUID)) { UtilPlayer.message(_viewer, F.main(getCommunityManager().getName(), F.name(_info.Name) + " can only own one community at a time!")); return; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index 3147d104c..9e1dd6ae5 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -31,7 +31,7 @@ public class CommunityRepository extends MinecraftRepository private static final String GET_ALL_COMMUNITIES = "SELECT * FROM communities WHERE region=?;"; private static final String GET_COMMUNITY_BY_ID = "SELECT * FROM communities WHERE id=?;"; private static final String GET_COMMUNITY_BY_NAME = "SELECT * FROM communities WHERE name=? AND region=?;"; - private static final String GET_COMMUNITY_MEMBERS = "SELECT cm.accountId, cm.communityRole, ac.name, ac.uuid, ac.lastLogin, now(), cm.readingChat, (SELECT COUNT(id) FROM communityMembers WHERE accountId=cm.accountId AND communityRole='LEADER') FROM communityMembers cm INNER JOIN accounts ac ON ac.id=cm.accountId WHERE communityId=?;"; + private static final String GET_COMMUNITY_MEMBERS = "SELECT cm.accountId, cm.communityRole, ac.name, ac.uuid, ac.lastLogin, now(), cm.readingChat FROM communityMembers cm INNER JOIN accounts ac ON ac.id=cm.accountId WHERE communityId=?;"; private static final String GET_COMMUNITY_JOIN_REQUESTS = "SELECT cjr.accountId, ac.name, ac.uuid FROM communityJoinRequests cjr INNER JOIN accounts ac ON ac.id=cjr.accountId WHERE communityId=?;"; private static final String GET_COMMUNITY_SETTINGS = "SELECT settingId, settingValue FROM communitySettings WHERE communityId=?;"; @@ -81,7 +81,6 @@ public class CommunityRepository extends MinecraftRepository final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); final long timeSinceOnline = memberSet.getTimestamp(6).getTime() - memberSet.getTimestamp(5).getTime(); boolean readingChat = memberSet.getBoolean("readingChat"); - final int owns = memberSet.getInt(8); CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, timeSinceOnline); PlayerStatus status = _repo.getElement(name); @@ -90,7 +89,6 @@ public class CommunityRepository extends MinecraftRepository info.update(name, role, timeSinceOnline, true, status.getServer()); } info.ReadingChat = readingChat; - info.OwnsCommunity = owns > 0; community.getMembers().put(info.UUID, info); } }, new ColumnInt("communityId", community.getId())); @@ -154,7 +152,6 @@ public class CommunityRepository extends MinecraftRepository final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); final long timeSinceOnline = memberSet.getTimestamp(6).getTime() - memberSet.getTimestamp(5).getTime(); boolean readingChat = memberSet.getBoolean("readingChat"); - final int owns = memberSet.getInt(8); CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, timeSinceOnline); PlayerStatus status = _repo.getElement(name); @@ -163,7 +160,6 @@ public class CommunityRepository extends MinecraftRepository info.update(name, role, timeSinceOnline, true, status.getServer()); } info.ReadingChat = readingChat; - info.OwnsCommunity = owns > 0; community.getMembers().put(info.UUID, info); } }, new ColumnInt("communityId", community.getId())); @@ -239,8 +235,7 @@ public class CommunityRepository extends MinecraftRepository final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); final long timeSinceOnline = memberSet.getTimestamp(6).getTime() - memberSet.getTimestamp(5).getTime(); //boolean readingChat = memberSet.getBoolean("readingChat"); - final int owns = memberSet.getInt(8); - + if (c.getMembers().containsKey(uuid)) { PlayerStatus status = _repo.getElement(name); @@ -252,7 +247,6 @@ public class CommunityRepository extends MinecraftRepository server = status.getServer(); } //c.getMembers().get(uuid).ReadingChat = readingChat; - c.getMembers().get(uuid).OwnsCommunity = owns > 0; c.getMembers().get(uuid).update(name, role, timeSinceOnline, online, server); } } @@ -291,8 +285,7 @@ public class CommunityRepository extends MinecraftRepository final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); final long timeSinceOnline = memberSet.getTimestamp(5).getTime() - memberSet.getTimestamp(4).getTime(); //boolean readingChat = memberSet.getBoolean("readingChat"); - final int owns = memberSet.getInt(8); - + if (members.containsKey(uuid)) { PlayerStatus status = _repo.getElement(name); @@ -304,7 +297,6 @@ public class CommunityRepository extends MinecraftRepository server = status.getServer(); } //members.get(uuid).ReadingChat = readingChat; - members.get(uuid).OwnsCommunity = owns > 0; members.get(uuid).update(name, role, timeSinceOnline, online, server); } } From ca19840b19cf73b6f015a33bf71eb1de072715b5 Mon Sep 17 00:00:00 2001 From: cnr Date: Thu, 22 Dec 2016 16:50:35 -0700 Subject: [PATCH 02/22] Revert "Disable communities temporarily." This reverts commit 6d5d2b551b88d37a566a2897990ebecf0cace6ff. --- .../core/communities/commands/CommunityChatCommand.java | 6 ------ .../core/communities/commands/CommunityCommand.java | 6 ------ .../core/communities/commands/CommunityCreateCommand.java | 6 ------ .../communities/commands/CommunityDescriptionCommand.java | 6 ------ .../core/communities/commands/CommunityDisbandCommand.java | 6 ------ .../core/communities/commands/CommunityInviteCommand.java | 6 ------ .../core/communities/commands/CommunityJoinCommand.java | 6 ------ .../core/communities/commands/CommunityMCSCommand.java | 6 ------ .../core/communities/commands/CommunityRenameCommand.java | 6 ------ .../core/communities/commands/CommunityUnInviteCommand.java | 6 ------ .../src/mineplex/game/clans/clans/ClansManager.java | 2 +- Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java | 2 +- .../src/nautilus/game/arcade/ArcadeManager.java | 2 +- 13 files changed, 3 insertions(+), 63 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityChatCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityChatCommand.java index f9c60a0cf..a1edba960 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityChatCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityChatCommand.java @@ -20,12 +20,6 @@ public class CommunityChatCommand extends CommandBase @Override public void Execute(Player caller, String[] args) { - if (true) - { - caller.sendMessage(F.main("Communities", "Coming soon!")); - return; - } - if (args.length < 1) { UtilPlayer.message(caller, F.help("/com chat ", "Selects which community you chat to", Rank.ALL, ChatColor.AQUA)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCommand.java index b0180dfd4..90af36ac7 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCommand.java @@ -33,12 +33,6 @@ public class CommunityCommand extends MultiCommandBase @Override protected void Help(Player caller, String[] args) { - if (true) - { - caller.sendMessage(F.main("Communities", "Coming soon!")); - return; - } - if (args.length > 0) { if (args[0].equalsIgnoreCase("help")) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java index 892169a3b..5e01f39cb 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java @@ -25,12 +25,6 @@ public class CommunityCreateCommand extends CommandBase @Override public void Execute(Player caller, String[] args) { - if (true) - { - caller.sendMessage(F.main("Communities", "Coming soon!")); - return; - } - if (args.length < 1) { UtilPlayer.message(caller, F.help("/com create ", "Creates a new community", Rank.ETERNAL, ChatColor.AQUA)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityDescriptionCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityDescriptionCommand.java index e1ccd0656..efaae74b2 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityDescriptionCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityDescriptionCommand.java @@ -26,12 +26,6 @@ public class CommunityDescriptionCommand extends CommandBase @Override public void Execute(Player caller, String[] args) { - if (true) - { - caller.sendMessage(F.main("Communities", "Coming soon!")); - return; - } - if (args.length < 2) { UtilPlayer.message(caller, F.help("/com description ", "Sets the description of a community you manage", Rank.ALL, ChatColor.AQUA)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityDisbandCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityDisbandCommand.java index 2cef1b4ad..a70b77542 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityDisbandCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityDisbandCommand.java @@ -24,12 +24,6 @@ public class CommunityDisbandCommand extends CommandBase @Override public void Execute(Player caller, String[] args) { - if (true) - { - caller.sendMessage(F.main("Communities", "Coming soon!")); - return; - } - if (args.length < 1) { UtilPlayer.message(caller, F.help("/com disband ", "Disbands a community you own", Rank.ETERNAL, ChatColor.AQUA)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityInviteCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityInviteCommand.java index 004cd7dc7..f3b6e6b65 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityInviteCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityInviteCommand.java @@ -24,12 +24,6 @@ public class CommunityInviteCommand extends CommandBase @Override public void Execute(Player caller, String[] args) { - if (true) - { - caller.sendMessage(F.main("Communities", "Coming soon!")); - return; - } - if (args.length < 2) { UtilPlayer.message(caller, F.help("/com invite ", "Invites a player to a community you manage", Rank.ALL, ChatColor.AQUA)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityJoinCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityJoinCommand.java index 4802dfa6d..202d73fc3 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityJoinCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityJoinCommand.java @@ -21,12 +21,6 @@ public class CommunityJoinCommand extends CommandBase @Override public void Execute(Player caller, String[] args) { - if (true) - { - caller.sendMessage(F.main("Communities", "Coming soon!")); - return; - } - if (args.length < 1) { UtilPlayer.message(caller, F.help("/com join ", "Joins a community that is open or you have been invited to", Rank.ALL, ChatColor.AQUA)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityMCSCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityMCSCommand.java index 41d498a1b..1e499c57f 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityMCSCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityMCSCommand.java @@ -25,12 +25,6 @@ public class CommunityMCSCommand extends CommandBase @Override public void Execute(Player caller, String[] args) { - if (true) - { - caller.sendMessage(F.main("Communities", "Coming soon!")); - return; - } - if (args.length < 1) { UtilPlayer.message(caller, F.help("/com mcs ", "Opens the Mineplex Community Server of a community you manage", Rank.ALL, ChatColor.AQUA)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityRenameCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityRenameCommand.java index b932b0a4e..bfa1863bf 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityRenameCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityRenameCommand.java @@ -27,12 +27,6 @@ public class CommunityRenameCommand extends CommandBase @Override public void Execute(Player caller, String[] args) { - if (true) - { - caller.sendMessage(F.main("Communities", "Coming soon!")); - return; - } - if (args.length < 2) { UtilPlayer.message(caller, F.help("/com rename ", "Changes the name of a community you own", Rank.ETERNAL, ChatColor.AQUA)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityUnInviteCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityUnInviteCommand.java index fc2035653..99b5e9636 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityUnInviteCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityUnInviteCommand.java @@ -24,12 +24,6 @@ public class CommunityUnInviteCommand extends CommandBase @Override public void Execute(Player caller, String[] args) { - if (true) - { - caller.sendMessage(F.main("Communities", "Coming soon!")); - return; - } - if (args.length < 2) { UtilPlayer.message(caller, F.help("/com uninvite ", "Revokes a player's invitation to a community you manage", Rank.ALL, ChatColor.AQUA)); diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java index c728fe4fa..3fb75c743 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansManager.java @@ -409,7 +409,7 @@ public class ClansManager extends MiniClientPluginimplements IRelati _bannerManager.loadBanners(this); new PersonalServerManager(plugin, _clientManager).setUseInterfaceItem(false); -// new CommunityManager(plugin, _clientManager); + new CommunityManager(plugin, _clientManager); Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "Replay|Restrict"); diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java b/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java index fabd131ec..5b65fd27a 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java @@ -275,7 +275,7 @@ public class HubManager extends MiniClientPlugin implements IChatMess new SalesAnnouncementManager(plugin); -// new CommunityManager(plugin, _clientManager); + new CommunityManager(plugin, _clientManager); ScoreboardManager scoreboardManager = new ScoreboardManager(plugin) { diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/ArcadeManager.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/ArcadeManager.java index 1f70552d3..cbc0cd184 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/ArcadeManager.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/ArcadeManager.java @@ -407,7 +407,7 @@ public class ArcadeManager extends MiniPlugin implements IRelation addCommand(new TauntCommand(this)); new PersonalServerManager(plugin, _clientManager).setUseInterfaceItem(false); -// new CommunityManager(plugin, _clientManager); + new CommunityManager(plugin, _clientManager); _scoreboardManager = new ScoreboardManager(_plugin) { From df5c53766c9b5f140759a1f6d34dc6ac7069f021 Mon Sep 17 00:00:00 2001 From: cnr Date: Thu, 22 Dec 2016 17:54:50 -0700 Subject: [PATCH 03/22] Disable VL logging --- .../mineplex/core/antihack/logging/AnticheatDatabase.java | 4 ++-- .../src/mineplex/core/antihack/logging/AntihackLogger.java | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AnticheatDatabase.java b/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AnticheatDatabase.java index 8d2f78421..d17991f69 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AnticheatDatabase.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AnticheatDatabase.java @@ -52,7 +52,7 @@ public class AnticheatDatabase extends MinecraftRepository */ public void saveViolationLevels(Map uploadQueue) { - try (Connection connection = getConnection()) + /*try (Connection connection = getConnection()) { PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_VIOLATIONS); @@ -96,7 +96,7 @@ public class AnticheatDatabase extends MinecraftRepository catch (SQLException ex) { ex.printStackTrace(); - } + }*/ } /** diff --git a/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AntihackLogger.java b/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AntihackLogger.java index 2a9a628c8..23036a2de 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AntihackLogger.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AntihackLogger.java @@ -11,7 +11,6 @@ import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; -import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.tukaani.xz.LZMA2Options; import org.tukaani.xz.XZ; @@ -118,7 +117,7 @@ public class AntihackLogger extends MiniPlugin playerVls.incrementAlerts(check); } - @EventHandler + /*@EventHandler public void onLoad(PlayerLoginEvent event) { runAsync(() -> @@ -131,7 +130,7 @@ public class AntihackLogger extends MiniPlugin _violationLevels.put(accountId, vls); }); }); - } + }*/ @EventHandler public void onQuit(PlayerQuitEvent event) @@ -209,6 +208,6 @@ public class AntihackLogger extends MiniPlugin public void resetViolations(Player player, Runnable after) { - _db.clearLastBan(_clientManager.getAccountId(player), after); + //_db.clearLastBan(_clientManager.getAccountId(player), after); } } From 1bcbddee873d9ae1cb71b664cce6e297a8a828d9 Mon Sep 17 00:00:00 2001 From: samczsun Date: Thu, 22 Dec 2016 22:25:18 -0500 Subject: [PATCH 04/22] Remove VL logging for good --- .../src/mineplex/core/antihack/AntiHack.java | 13 +- .../antihack/logging/AnticheatDatabase.java | 154 +----------------- .../core/antihack/logging/AntihackLogger.java | 63 +------ 3 files changed, 7 insertions(+), 223 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHack.java b/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHack.java index 0023bb1e6..ea977558e 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHack.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHack.java @@ -203,16 +203,13 @@ public class AntiHack extends MiniPlugin String finalMessage = "[GWEN] " + id; _logger.saveMetadata(player, id, () -> { - _logger.resetViolations(player, () -> + runAsync(() -> { - runAsync(() -> - { - GwenBanNotification notification = new GwenBanNotification(_thisServer, player.getName(), player.getUniqueId().toString(), CheckManager.getCheckSimpleName(cause), id); - ServerCommandManager.getInstance().publishCommand(notification); - }); - - _punish.AddPunishment(coreClient.getName(), Category.Hacking, finalMessage, AntiHack.NAME, 3, true, -1, true, after); + GwenBanNotification notification = new GwenBanNotification(_thisServer, player.getName(), player.getUniqueId().toString(), CheckManager.getCheckSimpleName(cause), id); + ServerCommandManager.getInstance().publishCommand(notification); }); + + _punish.AddPunishment(coreClient.getName(), Category.Hacking, finalMessage, AntiHack.NAME, 3, true, -1, true, after); }, custom); }; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AnticheatDatabase.java b/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AnticheatDatabase.java index d17991f69..d366a548a 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AnticheatDatabase.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AnticheatDatabase.java @@ -2,164 +2,24 @@ package mineplex.core.antihack.logging; import java.sql.Connection; import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Map; -import java.util.Optional; -import org.bukkit.plugin.java.JavaPlugin; - -import com.mineplex.anticheat.checks.Check; -import com.mineplex.anticheat.checks.CheckManager; - -import mineplex.core.antihack.ViolationLevels; import mineplex.core.database.MinecraftRepository; import mineplex.serverdata.database.DBPool; -import gnu.trove.map.TIntObjectMap; - public class AnticheatDatabase extends MinecraftRepository { /* - CREATE TABLE IF NOT EXISTS anticheat_vl_logs (accountId INT, checkId INT, maxViolations INT, totalAlerts INT, sinceLastBan INT, PRIMARY KEY(accountId, checkId)); CREATE TABLE IF NOT EXISTS anticheat_ban_metadata (id INT NOT NULL AUTO_INCREMENT, accountId INT, banId CHAR(10) NOT NULL, data MEDIUMTEXT NOT NULL, PRIMARY KEY(id)); */ private static final String INSERT_INTO_METADATA = "INSERT INTO anticheat_ban_metadata (accountId, banId, data) VALUES (?, ?, ?);"; - private static final String UPDATE_VIOLATIONS = "INSERT INTO anticheat_vl_logs (accountId, checkId, " - + "maxViolations, sinceLastBan, totalAlerts) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY" - + " UPDATE maxViolations = VALUES(maxViolations), totalAlerts = VALUES(totalAlerts), sinceLastBan = VALUES(sinceLastBan);"; - - private static final String CLEAR_LAST_BAN_VIOLATIONS = "UPDATE anticheat_vl_logs SET sinceLastBan = 0 WHERE accountId = ?;"; - - private static final String GET_VLS = "SELECT checkId, maxViolations, sinceLastBan, totalAlerts FROM anticheat_vl_logs"; - - private static final String GET_VLS_BY_ACCOUNT_ID = GET_VLS + " WHERE accountId = ?"; - - private static final String GET_VLS_FOR_CHECK = GET_VLS + " WHERE checkId = ? AND accountId = ?"; - - - public AnticheatDatabase(JavaPlugin plugin) + public AnticheatDatabase() { super(DBPool.getAccount()); } - /** - * Submit a set of user violation changes batch style. - * - * @param uploadQueue the {@link TIntObjectMap} describing the changes. - */ - public void saveViolationLevels(Map uploadQueue) - { - /*try (Connection connection = getConnection()) - { - PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_VIOLATIONS); - - uploadQueue.forEach((accountId, vls) -> - { - CheckManager.AVAILABLE_CHECKS.values().forEach(check -> - { - int checkId = CheckManager.getCheckId(check), - maxVls = vls.getMaxViolationsForCheck(check), - maxVlsSinceLastBan = vls.getLastBanViolationsForCheck(check), - totalAlerts = vls.getTotalAlertsForCheck(check); - - // if neither value has been set don't store anything - if (maxVls < 0 && totalAlerts < 0 && maxVlsSinceLastBan < 0) - { - return; - } - - maxVls = Math.max(maxVls, 0); - maxVlsSinceLastBan = Math.max(maxVlsSinceLastBan, 0); - totalAlerts = Math.max(totalAlerts, 0); - - try - { - preparedStatement.setInt(1, accountId); - preparedStatement.setInt(2, checkId); - preparedStatement.setInt(3, maxVls); - preparedStatement.setInt(4, maxVlsSinceLastBan); - preparedStatement.setInt(5, totalAlerts); - preparedStatement.addBatch(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - }); - }); - - preparedStatement.executeBatch(); - } - catch (SQLException ex) - { - ex.printStackTrace(); - }*/ - } - - /** - * Attempts to retrieve violation levels for the given account id. - * - * @param accountId The account id; - * @return an {@link Optional} describing the user's violation levels, or an empty one if none - * are found. - * @throws SQLException On failing to connect to the database. - */ - public Optional getViolationLevels(int accountId) - { - ViolationLevels levels = new ViolationLevels(); - - try (Connection connection = getConnection()) - { - PreparedStatement statement = connection.prepareStatement(GET_VLS_BY_ACCOUNT_ID); - statement.setInt(1, accountId); - - ResultSet result = statement.executeQuery(); - - while (result.next()) - { - int checkId = result.getInt("checkId"); - Class checkType = CheckManager.getCheckById(checkId); - if (checkType == null) - { - System.err.println("Whoops. Unintended refactor?"); - continue; - } - levels.updateMaxViolations(checkType, result.getInt("maxViolations")); - levels.updateMaxViolationsSinceLastBan(checkType, result.getInt("sinceLastBan")); - levels.setTotalAlerts(checkType, result.getInt("totalAlerts")); - } - } - catch (SQLException ex) - { - ex.printStackTrace(); - } - - return Optional.of(levels); - } - - public void clearLastBan(int accountId, Runnable after) - { - try (Connection connection = getConnection()) - { - PreparedStatement statement = connection.prepareStatement(CLEAR_LAST_BAN_VIOLATIONS); - statement.setInt(1, accountId); - - statement.executeUpdate(); - } - catch (SQLException e) - { - e.printStackTrace(); - } - finally - { - if (after != null) - after.run(); - } - } - public void saveMetadata(int accountId, String id, String base64, Runnable after) { try (Connection connection = getConnection()) @@ -181,16 +41,4 @@ public class AnticheatDatabase extends MinecraftRepository after.run(); } } - - @Override - protected void initialize() - { - - } - - @Override - protected void update() - { - - } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AntihackLogger.java b/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AntihackLogger.java index 23036a2de..b5dafd513 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AntihackLogger.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/antihack/logging/AntihackLogger.java @@ -42,12 +42,8 @@ public class AntihackLogger extends MiniPlugin { public static final Gson GSON = new Gson(); - private static final int PUSH_QUEUE_TIME_IN_SECONDS = 60; - private final CoreClientManager _clientManager = require(CoreClientManager.class); - private final Map _violationLevels = new ConcurrentHashMap<>(); - private final Map _metadata = new HashMap<>(); private final AnticheatDatabase _db; @@ -56,9 +52,7 @@ public class AntihackLogger extends MiniPlugin { super("AnticheatPlugin"); - _db = new AnticheatDatabase(getPlugin()); - - runSyncTimer(this::pushQueuedViolationChanges, 20, 20 * PUSH_QUEUE_TIME_IN_SECONDS); + _db = new AnticheatDatabase(); registerMetadata(new ServerInfoMetadata()); registerMetadata(new ViolationInfoMetadata()); @@ -66,12 +60,6 @@ public class AntihackLogger extends MiniPlugin registerMetadata(new PlayerInfoMetadata()); } - @Override - public void disable() - { - pushQueuedViolationChanges(); - } - @EventHandler public void addCommands() { @@ -101,53 +89,9 @@ public class AntihackLogger extends MiniPlugin } } - private void pushQueuedViolationChanges() - { - Map clone = new HashMap<>(_violationLevels); - runAsync(() -> _db.saveViolationLevels(clone)); - } - - @EventHandler - public void onCheckFail(PlayerViolationEvent event) - { - ViolationLevels playerVls = _violationLevels.get(_clientManager.getAccountId(event.getPlayer())); - Class check = event.getCheckClass(); - playerVls.updateMaxViolations(check, event.getViolations()); - playerVls.updateMaxViolationsSinceLastBan(check, event.getViolations()); - playerVls.incrementAlerts(check); - } - - /*@EventHandler - public void onLoad(PlayerLoginEvent event) - { - runAsync(() -> - { - int accountId = _clientManager.getAccountId(event.getPlayer()); - - _db.getViolationLevels(accountId) - .ifPresent(vls -> - { - _violationLevels.put(accountId, vls); - }); - }); - }*/ - @EventHandler public void onQuit(PlayerQuitEvent event) { - Player player = event.getPlayer(); - int accountId =_clientManager.getAccountId(event.getPlayer()); - - ViolationLevels levels = _violationLevels.get(accountId); - - if (levels != null) - { - Map clone = new HashMap<>(); - clone.put(accountId, levels); - - runAsync(() -> _db.saveViolationLevels(clone)); - } - _metadata.values().forEach(metadata -> metadata.remove(event.getPlayer().getUniqueId())); } @@ -205,9 +149,4 @@ public class AntihackLogger extends MiniPlugin throw new IllegalArgumentException("Attempting to register: " + metadata.getId()); } } - - public void resetViolations(Player player, Runnable after) - { - //_db.clearLastBan(_clientManager.getAccountId(player), after); - } } From a0716ae77456b37bdae1b044c061eb5b1ef1d41a Mon Sep 17 00:00:00 2001 From: samczsun Date: Thu, 22 Dec 2016 22:42:07 -0500 Subject: [PATCH 05/22] Try to prevent duplicate bans even more --- .../src/mineplex/core/antihack/AntiHack.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHack.java b/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHack.java index ea977558e..e08f6f4f8 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHack.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/antihack/AntiHack.java @@ -1,9 +1,11 @@ package mineplex.core.antihack; import javax.xml.bind.DatatypeConverter; +import java.util.Collections; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; @@ -27,6 +29,7 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerToggleFlightEvent; import org.bukkit.plugin.ServicePriority; @@ -138,7 +141,9 @@ public class AntiHack extends MiniPlugin private final Set _detailedMessages = new HashSet<>(); - private Set _pendingBan = new HashSet<>(); + private Set _pendingBan = Collections.synchronizedSet(new HashSet<>()); + + private Set _banned = Collections.synchronizedSet(new HashSet<>()); // These are the GWEN checks to ignore when handling PlayerViolationEvent private HashSet> _ignoredChecks = new HashSet<>(); @@ -186,11 +191,18 @@ public class AntiHack extends MiniPlugin new BanwaveAnimationSpin().run(player, after); } + @EventHandler + public void onQuit(PlayerQuitEvent event) + { + UUID uuid = event.getPlayer().getUniqueId(); + runSync(() -> _banned.remove(uuid)); + } + public void doBan(Player player, Class cause) { runSync(() -> { - if (_pendingBan.add(player)) + if (_pendingBan.add(player) && !_banned.contains(player.getUniqueId())) { CoreClient coreClient = _clientManager.Get(player); @@ -215,7 +227,11 @@ public class AntiHack extends MiniPlugin if (coreClient.GetRank().has(Rank.TWITCH)) { - doPunish.accept(result -> _pendingBan.remove(player)); + doPunish.accept(result -> + { + _pendingBan.remove(player); + _banned.add(player.getUniqueId()); + }); } else { @@ -225,6 +241,7 @@ public class AntiHack extends MiniPlugin if (result == PunishmentResponse.Punished) { announceBan(player); + _banned.add(player.getUniqueId()); } _pendingBan.remove(player); }) From 56ecab19855fa2f37556899c266d183f458a7429 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 22 Dec 2016 20:27:33 -0500 Subject: [PATCH 06/22] Make community membership updating entirely redis-based --- .../bungee/playerTracker/PlayerTracker.java | 2 +- .../mineplex/core/communities/Community.java | 1 - .../core/communities/CommunityManager.java | 23 +++- .../core/communities/CommunityMemberInfo.java | 16 ++- .../communities/redis/PlayerJoinHandler.java | 28 ++++ .../storage/CommunityRepository.java | 120 +++--------------- .../commands/PlayerJoinCommand.java | 11 +- 7 files changed, 89 insertions(+), 112 deletions(-) create mode 100644 Plugins/Mineplex.Core/src/mineplex/core/communities/redis/PlayerJoinHandler.java diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java index 09c11dfff..cb2f05f7f 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java @@ -82,7 +82,7 @@ public class PlayerTracker implements Listener public void playerConnect(final PostLoginEvent event) { _ignoreKick.add(event.getPlayer().getUniqueId()); - PlayerJoinCommand command = new PlayerJoinCommand(event.getPlayer().getUniqueId()); + PlayerJoinCommand command = new PlayerJoinCommand(event.getPlayer().getUniqueId(), event.getPlayer().getName()); command.publish(); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java index 41c4391d3..931e32e6c 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java @@ -7,7 +7,6 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.ChatColor; -import mineplex.core.common.util.C; import mineplex.core.common.util.UtilPlayer; import mineplex.core.game.GameDisplay; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index 4ffbcf304..69fdecd9e 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -2,6 +2,7 @@ package mineplex.core.communities; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -56,11 +57,13 @@ import mineplex.core.communities.redis.CommunityUpdateName; import mineplex.core.communities.redis.CommunityUpdateNameHandler; import mineplex.core.communities.redis.CommunityUpdateSetting; import mineplex.core.communities.redis.CommunityUpdateSettingHandler; +import mineplex.core.communities.redis.PlayerJoinHandler; import mineplex.core.communities.storage.CommunityRepository; import mineplex.core.preferences.Preference; import mineplex.core.preferences.PreferencesManager; import mineplex.core.recharge.Recharge; import mineplex.serverdata.Region; +import mineplex.serverdata.commands.PlayerJoinCommand; import mineplex.serverdata.commands.ServerCommandManager; import mineplex.serverdata.data.DataRepository; import mineplex.serverdata.data.PlayerStatus; @@ -76,6 +79,7 @@ public class CommunityManager extends MiniDbClientPlugin private final Map _loadedCommunities; public final List BrowserIds = new LinkedList<>(); + private final List _creating = new ArrayList<>(); public final DataRepository StatusRepository; @@ -110,7 +114,7 @@ public class CommunityManager extends MiniDbClientPlugin LinkedList communities = new LinkedList<>(); _loadedCommunities.values().forEach(community -> communities.add(community)); - _repo.updateMembersAndJoinRequests(communities); + _repo.updateMembers(communities); }, 0L, 20 * 5); Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> @@ -134,6 +138,7 @@ public class CommunityManager extends MiniDbClientPlugin ServerCommandManager.getInstance().registerCommandType(CommunityUpdateMembership.class, new CommunityUpdateMembershipHandler(this)); ServerCommandManager.getInstance().registerCommandType(CommunityUpdateName.class, new CommunityUpdateNameHandler(this)); ServerCommandManager.getInstance().registerCommandType(CommunityUpdateSetting.class, new CommunityUpdateSettingHandler(this)); + ServerCommandManager.getInstance().registerCommandType(PlayerJoinCommand.class, new PlayerJoinHandler(this)); } public boolean ownsCommunity(UUID uuid) @@ -143,6 +148,15 @@ public class CommunityManager extends MiniDbClientPlugin .anyMatch(entry -> entry.getKey().equals(uuid) && entry.getValue().Role == CommunityRole.LEADER); } + public void updateAllMemberData(UUID uuid, String name) + { + _loadedCommunities.values().stream().filter(community -> community.getMembers().containsKey(uuid) && !community.getMembers().get(uuid).Name.equalsIgnoreCase(name)).forEach(community -> + { + community.getMembers().get(uuid).updateName(name); + UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community)); + }); + } + private void cycleBrowser() { if (_cycling) @@ -595,6 +609,12 @@ public class CommunityManager extends MiniDbClientPlugin public void handleCreate(Player sender, int accountId, String name) { + if (_creating.contains(sender.getUniqueId())) + { + sender.sendMessage(F.main(getName(), "You are already creating a Community!")); + return; + } + _creating.add(sender.getUniqueId()); runAsync(() -> { _repo.createCommunity(name, accountId, id -> @@ -606,6 +626,7 @@ public class CommunityManager extends MiniDbClientPlugin else { new CommunityCreate(sender.getUniqueId().toString(), id).publish(); + runSync(() -> _creating.remove(sender.getUniqueId())); } }); }); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java index fe62d8376..08691dcec 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java @@ -15,7 +15,6 @@ public class CommunityMemberInfo public final UUID UUID; public final int AccountId; public CommunityRole Role; - public boolean OwnsCommunity = false; public boolean ReadingChat = true; private ItemStack _memberIcon, _outsiderIcon; private long _sinceLastLogin; @@ -54,10 +53,21 @@ public class CommunityMemberInfo _memberIcon = builder.build(); } - public void update(String name, CommunityRole role, long timeSinceLastLogin, boolean online, String currentServer) + public void setOffline() + { + _online = false; + _currentServer = ""; + buildIcons(); + } + + public void updateName(String name) { Name = name; - Role = role; + buildIcons(); + } + + public void update(long timeSinceLastLogin, boolean online, String currentServer) + { _sinceLastLogin = timeSinceLastLogin; _online = online; _currentServer = currentServer; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/PlayerJoinHandler.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/PlayerJoinHandler.java new file mode 100644 index 000000000..47fc8210a --- /dev/null +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/PlayerJoinHandler.java @@ -0,0 +1,28 @@ +package mineplex.core.communities.redis; + +import java.util.UUID; + +import mineplex.core.communities.CommunityManager; +import mineplex.serverdata.commands.CommandCallback; +import mineplex.serverdata.commands.PlayerJoinCommand; +import mineplex.serverdata.commands.ServerCommand; + +public class PlayerJoinHandler implements CommandCallback +{ + private CommunityManager _communityManager; + + public PlayerJoinHandler(CommunityManager communityManager) + { + _communityManager = communityManager; + } + + @Override + public void run(ServerCommand command) + { + if (command instanceof PlayerJoinCommand) + { + PlayerJoinCommand joinCommand = (PlayerJoinCommand)command; + _communityManager.updateAllMemberData(UUID.fromString(joinCommand.getUuid()), joinCommand.getName()); + } + } +} \ No newline at end of file diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index 9e1dd6ae5..87a678289 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -1,9 +1,7 @@ package mineplex.core.communities.storage; import java.sql.Connection; -import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -86,7 +84,7 @@ public class CommunityRepository extends MinecraftRepository PlayerStatus status = _repo.getElement(name); if (status != null) { - info.update(name, role, timeSinceOnline, true, status.getServer()); + info.update(timeSinceOnline, true, status.getServer()); } info.ReadingChat = readingChat; community.getMembers().put(info.UUID, info); @@ -157,7 +155,7 @@ public class CommunityRepository extends MinecraftRepository PlayerStatus status = _repo.getElement(name); if (status != null) { - info.update(name, role, timeSinceOnline, true, status.getServer()); + info.update(timeSinceOnline, true, status.getServer()); } info.ReadingChat = readingChat; community.getMembers().put(info.UUID, info); @@ -204,120 +202,34 @@ public class CommunityRepository extends MinecraftRepository } } - public void updateMembersAndJoinRequests(LinkedList communities) + public void updateMembers(LinkedList communities) { if (communities.isEmpty()) { return; } - String query = ""; for (Community c : communities) { - query = query + GET_COMMUNITY_MEMBERS.replace("?", c.getId().toString()) + GET_COMMUNITY_JOIN_REQUESTS.replace("?", c.getId().toString()); - } - - if (query.isEmpty()) - { - return; - } - - try (Connection connection = getConnection(); Statement statement = connection.createStatement()) - { - statement.executeQuery(query); - - for (Community c : communities) + for (CommunityMemberInfo info : c.getMembers().values()) { - ResultSet memberSet = statement.getResultSet(); - while (memberSet.next()) + PlayerStatus status = _repo.getElement(info.Name); + boolean online = false; + String server = ""; + if (status != null) { - final UUID uuid = UUID.fromString(memberSet.getString("uuid")); - final String name = memberSet.getString("name"); - final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); - final long timeSinceOnline = memberSet.getTimestamp(6).getTime() - memberSet.getTimestamp(5).getTime(); - //boolean readingChat = memberSet.getBoolean("readingChat"); - - if (c.getMembers().containsKey(uuid)) + online = true; + server = status.getServer(); + info.update(System.currentTimeMillis(), online, server); + } + else + { + if (info.isOnline()) { - PlayerStatus status = _repo.getElement(name); - boolean online = false; - String server = ""; - if (status != null) - { - online = true; - server = status.getServer(); - } - //c.getMembers().get(uuid).ReadingChat = readingChat; - c.getMembers().get(uuid).update(name, role, timeSinceOnline, online, server); + info.setOffline(); } } - - statement.getMoreResults(); - - ResultSet requestSet = statement.getResultSet(); - while (requestSet.next()) - { - final UUID uuid = UUID.fromString(requestSet.getString("uuid")); - final String name = requestSet.getString("name"); - - if (c.getJoinRequests().containsKey(uuid)) - { - c.getJoinRequests().get(uuid).update(name); - } - } - - statement.getMoreResults(); } } - catch (SQLException e) - { - e.printStackTrace(); - } - } - - public void updateMembers(Map members, int communityId) - { - executeQuery(GET_COMMUNITY_MEMBERS, memberSet -> - { - while (memberSet.next()) - { - final UUID uuid = UUID.fromString(memberSet.getString("uuid")); - final String name = memberSet.getString("name"); - final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); - final long timeSinceOnline = memberSet.getTimestamp(5).getTime() - memberSet.getTimestamp(4).getTime(); - //boolean readingChat = memberSet.getBoolean("readingChat"); - - if (members.containsKey(uuid)) - { - PlayerStatus status = _repo.getElement(name); - boolean online = false; - String server = ""; - if (status != null) - { - online = true; - server = status.getServer(); - } - //members.get(uuid).ReadingChat = readingChat; - members.get(uuid).update(name, role, timeSinceOnline, online, server); - } - } - }, new ColumnInt("communityId", communityId)); - } - - public void updateJoinRequests(Map requests, int communityId) - { - executeQuery(GET_COMMUNITY_JOIN_REQUESTS, requestSet -> - { - while (requestSet.next()) - { - final UUID uuid = UUID.fromString(requestSet.getString("uuid")); - final String name = requestSet.getString("name"); - - if (requests.containsKey(uuid)) - { - requests.get(uuid).update(name); - } - } - }, new ColumnInt("communityId", communityId)); } public void loadInvites(int accountId, List invites) diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/PlayerJoinCommand.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/PlayerJoinCommand.java index b41ba4555..424ae0c33 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/PlayerJoinCommand.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/commands/PlayerJoinCommand.java @@ -5,10 +5,12 @@ import java.util.UUID; public class PlayerJoinCommand extends ServerCommand { private String _uuid; + private String _name; - public PlayerJoinCommand(UUID uuid) + public PlayerJoinCommand(UUID uuid, String name) { _uuid = uuid.toString(); + _name = name; } @Override @@ -21,4 +23,9 @@ public class PlayerJoinCommand extends ServerCommand { return _uuid; } -} + + public String getName() + { + return _name; + } +} \ No newline at end of file From e5aaa555b700d04768a00271d80bb58eef010352 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 22 Dec 2016 20:29:44 -0500 Subject: [PATCH 07/22] Make join request name updating redis-based --- .../src/mineplex/core/communities/CommunityManager.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index 69fdecd9e..d52f31671 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -157,6 +157,15 @@ public class CommunityManager extends MiniDbClientPlugin }); } + public void updateAllJoinRequests(UUID uuid, String name) + { + _loadedCommunities.values().stream().filter(community -> community.getJoinRequests().containsKey(uuid) && !community.getJoinRequests().get(uuid).Name.equalsIgnoreCase(name)).forEach(community -> + { + community.getJoinRequests().get(uuid).update(name); + UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community)); + }); + } + private void cycleBrowser() { if (_cycling) From 36f671d944565131020cecdf8da7db32d2c0354f Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 22 Dec 2016 20:32:14 -0500 Subject: [PATCH 08/22] Use UtilPlayer.message instead of sender.sendMessage to fit standards --- .../src/mineplex/core/communities/CommunityManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index d52f31671..ecb8dfeea 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -620,7 +620,7 @@ public class CommunityManager extends MiniDbClientPlugin { if (_creating.contains(sender.getUniqueId())) { - sender.sendMessage(F.main(getName(), "You are already creating a Community!")); + UtilPlayer.message(sender, F.main(getName(), "You are already creating a Community!")); return; } _creating.add(sender.getUniqueId()); From 66c6f6676836df81ef557b042baee0433f3d21d4 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 22 Dec 2016 21:00:28 -0500 Subject: [PATCH 09/22] Fix threading of community create command and make invite loader run on a LoginProcessor --- .../core/communities/CommunityManager.java | 46 ++++++++++++++----- .../commands/CommunityCreateCommand.java | 2 +- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index ecb8dfeea..d12d3c0ed 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -25,6 +25,7 @@ import com.google.common.collect.Lists; import mineplex.core.Managers; import mineplex.core.MiniDbClientPlugin; import mineplex.core.account.CoreClientManager; +import mineplex.core.account.ILoginProcessor; import mineplex.core.common.jsonchat.ClickEvent; import mineplex.core.common.jsonchat.JsonMessage; import mineplex.core.common.util.C; @@ -105,6 +106,34 @@ public class CommunityManager extends MiniDbClientPlugin _loadedCommunities = new ConcurrentHashMap<>(); + clientManager.addStoredProcedureLoginProcessor(new ILoginProcessor() + { + @Override + public String getName() + { + return "community-invite-loader"; + } + + @Override + public void processLoginResultSet(String playerName, UUID uuid, int accountId, ResultSet resultSet) throws SQLException + { + while (resultSet.next()) + { + String region = resultSet.getString("region"); + if ((_us && region.equalsIgnoreCase("US")) || (!_us && region.equalsIgnoreCase("EU"))) + { + CommunityManager.this.Get(uuid).Invites.add(resultSet.getInt("communityId")); + } + } + } + + @Override + public String getQuery(int accountId, String uuid, String name) + { + return "SELECT ci.communityId, c.region FROM communityInvites AS ci INNER JOIN communities AS c ON c.id=ci.communityId WHERE accountId=" + accountId + ";"; + } + }); + Bukkit.getScheduler().scheduleAsyncRepeatingTask(plugin, () -> { if (_cycling) @@ -630,7 +659,8 @@ public class CommunityManager extends MiniDbClientPlugin { if (id == -1) { - sender.sendMessage(F.main(getName(), "Failed to create community " + F.elem(name))); + UtilPlayer.message(sender, F.main(getName(), "Failed to create community " + F.elem(name))); + runSync(() -> _creating.remove(sender.getUniqueId())); } else { @@ -688,19 +718,11 @@ public class CommunityManager extends MiniDbClientPlugin @EventHandler public void loadInvites(PlayerJoinEvent event) { - final int accountId = Managers.get(CoreClientManager.class).getAccountId(event.getPlayer()); final CommunityMemberData data = Get(event.getPlayer()); - runAsync(() -> + if (data.Invites.size() > 0 && Managers.get(PreferencesManager.class).get(event.getPlayer()).isActive(Preference.COMMUNITY_INVITES)) { - _repo.loadInvites(accountId, data.Invites); - runSync(() -> - { - if (data.Invites.size() > 0 && Managers.get(PreferencesManager.class).get(event.getPlayer()).isActive(Preference.COMMUNITY_INVITES)) - { - UtilPlayer.message(event.getPlayer(), F.main(getName(), "You have been invited to join " + F.elem(data.Invites.size()) + " communities!")); - } - }); - }); + UtilPlayer.message(event.getPlayer(), F.main(getName(), "You have been invited to join " + F.elem(data.Invites.size()) + " communities!")); + } } @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java index 5e01f39cb..f8e1c98f5 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java @@ -60,7 +60,7 @@ public class CommunityCreateCommand extends CommandBase } else { - Plugin.handleCreate(caller, accountId, args[0]); + Plugin.runSync(() -> Plugin.handleCreate(caller, accountId, args[0])); } }); } From 286a9fe246eaa4b2e2907b4dc16c2abccd77beb5 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 22 Dec 2016 21:35:46 -0500 Subject: [PATCH 10/22] Fix various synchronization issues and make community creation load the new community through redis instead of a database call --- .../mineplex/core/communities/Community.java | 6 +- .../core/communities/CommunityManager.java | 66 ++++++++++--------- .../core/communities/CommunityMemberData.java | 4 +- .../commands/CommunityCreateCommand.java | 5 +- .../communities/redis/CommunityCreate.java | 23 ++++++- .../redis/CommunityCreateHandler.java | 5 +- 6 files changed, 69 insertions(+), 40 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java index 931e32e6c..86406e7ba 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java @@ -1,8 +1,8 @@ package mineplex.core.communities; -import java.util.HashMap; import java.util.Map; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -15,8 +15,8 @@ public class Community private final int _id; private String _name; private String _description; - private Map _members = new HashMap<>(); - private Map _joinRequests = new HashMap<>(); + private Map _members = new ConcurrentHashMap<>(); + private Map _joinRequests = new ConcurrentHashMap<>(); private ChatColor[] _chatFormat; private long _chatDelay; private GameDisplay _favoriteGame; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index d12d3c0ed..51792999e 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -3,6 +3,7 @@ package mineplex.core.communities; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -20,8 +21,6 @@ import org.bukkit.event.player.AsyncPlayerChatEvent; 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.MiniDbClientPlugin; import mineplex.core.account.CoreClientManager; @@ -87,7 +86,7 @@ public class CommunityManager extends MiniDbClientPlugin private ServerRepository _serverRepo; private boolean _us; - private boolean _cycling = false; + private volatile boolean _cycling = false; @SuppressWarnings("deprecation") public CommunityManager(JavaPlugin plugin, CoreClientManager clientManager) @@ -204,15 +203,10 @@ public class CommunityManager extends MiniDbClientPlugin _cycling = true; runAsync(() -> { - final List resultant = Lists.newArrayList(); final List ids = new LinkedList<>(); - _loadedCommunities.values().stream().filter(c -> c.getMembers().size() >= 5 && c.getPrivacySetting() != PrivacySetting.PRIVATE).forEach(c -> resultant.add(c.getId())); + _loadedCommunities.values().stream().filter(c -> c.getMembers().size() >= 5 && c.getPrivacySetting() != PrivacySetting.PRIVATE).forEach(c -> ids.add(c.getId())); - Random random = new Random(); - while (!resultant.isEmpty()) - { - ids.add(resultant.remove(random.nextInt(resultant.size()))); - } + Collections.shuffle(ids); runSync(() -> { @@ -321,7 +315,7 @@ public class CommunityManager extends MiniDbClientPlugin if (Bukkit.getPlayer(playerUUID) != null) { Get(Bukkit.getPlayer(playerUUID)).joinCommunity(community); - Get(Bukkit.getPlayer(playerUUID)).Invites.remove(community.getId()); + runSync(() -> Get(Bukkit.getPlayer(playerUUID)).Invites.remove(community.getId())); } community.message(F.main(getName(), F.name(playerName) + " has joined " + F.name(community.getName()) + "!")); @@ -341,19 +335,22 @@ public class CommunityManager extends MiniDbClientPlugin { return; } - if (Bukkit.getPlayer(targetUUID) != null) + runSync(() -> { - if (!Get(Bukkit.getPlayer(targetUUID)).Invites.contains(community.getId())) + if (Bukkit.getPlayer(targetUUID) != null) { - Get(Bukkit.getPlayer(targetUUID)).Invites.add(community.getId()); - if (Managers.get(PreferencesManager.class).get(Bukkit.getPlayer(targetUUID)).isActive(Preference.COMMUNITY_INVITES)) + if (!Get(Bukkit.getPlayer(targetUUID)).Invites.contains(community.getId())) { - new JsonMessage(F.main(getName(), "You have been invited to join " + F.elem(community.getName()) + " by " + F.name(sender) + "! Click to join!")).click(ClickEvent.RUN_COMMAND, "/community join " + community.getName()).sendToPlayer(Bukkit.getPlayer(targetUUID)); + Get(Bukkit.getPlayer(targetUUID)).Invites.add(community.getId()); + if (Managers.get(PreferencesManager.class).get(Bukkit.getPlayer(targetUUID)).isActive(Preference.COMMUNITY_INVITES)) + { + new JsonMessage(F.main(getName(), "You have been invited to join " + F.elem(community.getName()) + " by " + F.name(sender) + "! Click to join!")).click(ClickEvent.RUN_COMMAND, "/community join " + community.getName()).sendToPlayer(Bukkit.getPlayer(targetUUID)); + } + + UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(targetUUID))); } - - UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(targetUUID))); } - } + }); community.message(F.main(getName(), F.name(sender) + " has invited " + F.name(targetName) + " to " + F.name(community.getName()) + "!"), CommunityRole.COLEADER); } @@ -364,15 +361,18 @@ public class CommunityManager extends MiniDbClientPlugin { return; } - if (Bukkit.getPlayer(targetUUID) != null) + runSync(() -> { - Get(Bukkit.getPlayer(targetUUID)).Invites.remove(community.getId()); - if (Managers.get(PreferencesManager.class).get(Bukkit.getPlayer(targetUUID)).isActive(Preference.COMMUNITY_INVITES) && announce) + if (Bukkit.getPlayer(targetUUID) != null) { - UtilPlayer.message(Bukkit.getPlayer(targetUUID), F.main(getName(), "Your invitation to join " + F.elem(community.getName()) + " has been revoked by " + F.name(sender) + "!")); + Get(Bukkit.getPlayer(targetUUID)).Invites.remove(community.getId()); + if (Managers.get(PreferencesManager.class).get(Bukkit.getPlayer(targetUUID)).isActive(Preference.COMMUNITY_INVITES) && announce) + { + UtilPlayer.message(Bukkit.getPlayer(targetUUID), F.main(getName(), "Your invitation to join " + F.elem(community.getName()) + " has been revoked by " + F.name(sender) + "!")); + } + UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(targetUUID))); } - UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(targetUUID))); - } + }); if (announce) { community.message(F.main(getName(), F.name(targetName) + "'s invitation to join " + F.name(community.getName()) + " has been revoked by " + F.name(sender) + "!"), CommunityRole.COLEADER); @@ -416,11 +416,12 @@ public class CommunityManager extends MiniDbClientPlugin UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community)); } - public void handleCommunityCreation(Integer id, UUID leaderUUID) + public void handleCommunityCreation(Integer id, String name, Integer leaderId, UUID leaderUUID, String leaderName) { runAsync(() -> { - _repo.loadCommunity(id, _loadedCommunities); + _loadedCommunities.put(id, new Community(id, name)); + _loadedCommunities.get(id).getMembers().put(leaderUUID, new CommunityMemberInfo(leaderName, leaderUUID, leaderId, CommunityRole.LEADER, System.currentTimeMillis())); runSync(() -> { Community community = _loadedCommunities.get(id); @@ -443,7 +444,10 @@ public class CommunityManager extends MiniDbClientPlugin } community.message(F.main(getName(), F.name(senderName) + " has disbanded community " + F.name(community.getName()) + "!")); UtilServer.CallEvent(new CommunityDisbandEvent(community)); - UtilServer.GetPlayers().stream().filter(player -> Get(player).Invites.contains(community.getId())).forEach(player -> Get(player).Invites.remove(community.getId())); + runSync(() -> + { + UtilServer.GetPlayers().stream().filter(player -> Get(player).Invites.contains(community.getId())).forEach(player -> Get(player).Invites.remove(community.getId())); + }); community.getMembers().keySet().stream().filter(uuid -> Bukkit.getPlayer(uuid) != null).forEach(uuid -> Get(Bukkit.getPlayer(uuid)).leaveCommunity(community)); _loadedCommunities.remove(community.getId()); runSync(() -> @@ -514,7 +518,7 @@ public class CommunityManager extends MiniDbClientPlugin } else { - UtilPlayer.message(sender, F.main(getName(), "Either " + F.name(target) + " does not exist or you have already invited them to " + F.name(community.getName()) + "!")); + UtilPlayer.message(sender, F.main(getName(), F.name(target) + " does not exist!")); } }); } @@ -645,7 +649,7 @@ public class CommunityManager extends MiniDbClientPlugin new CommunityUpdateMemberRole(community.getId(), sender.getName(), info.UUID.toString(), role.toString()).publish(); } - public void handleCreate(Player sender, int accountId, String name) + public void handleCreate(Player sender, String senderName, int accountId, String name) { if (_creating.contains(sender.getUniqueId())) { @@ -664,7 +668,7 @@ public class CommunityManager extends MiniDbClientPlugin } else { - new CommunityCreate(sender.getUniqueId().toString(), id).publish(); + new CommunityCreate(sender.getUniqueId().toString(), senderName, accountId, id, name).publish(); runSync(() -> _creating.remove(sender.getUniqueId())); } }); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberData.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberData.java index d7138721a..72c44fe00 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberData.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberData.java @@ -1,15 +1,15 @@ package mineplex.core.communities; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import mineplex.core.Managers; public class CommunityMemberData { - private final Map _communities = new HashMap<>(); + private final Map _communities = new ConcurrentHashMap<>(); public final List Invites = new ArrayList<>(); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java index f8e1c98f5..af4d189d4 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCreateCommand.java @@ -51,16 +51,17 @@ public class CommunityCreateCommand extends CommandBase UtilPlayer.message(caller, F.main(Plugin.getName(), "That name is not allowed!")); return; } + final int accountId = Managers.get(CoreClientManager.class).getAccountId(caller); + final String senderName = Managers.get(CoreClientManager.class).Get(caller).getName(); Plugin.runAsync(() -> { - int accountId = Managers.get(CoreClientManager.class).getAccountId(caller); if (Managers.get(Chat.class).getFilteredMessage(caller, args[0]).contains("*")) { UtilPlayer.message(caller, F.main(Plugin.getName(), "That name is not allowed!")); } else { - Plugin.runSync(() -> Plugin.handleCreate(caller, accountId, args[0])); + Plugin.runSync(() -> Plugin.handleCreate(caller, senderName, accountId, args[0])); } }); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/CommunityCreate.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/CommunityCreate.java index 0637057e5..4e3a364e1 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/CommunityCreate.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/CommunityCreate.java @@ -5,12 +5,18 @@ import mineplex.serverdata.commands.ServerCommand; public class CommunityCreate extends ServerCommand { private String _leaderUUID; + private String _leaderName; + private Integer _leaderId; private Integer _communityId; + private String _communityName; - public CommunityCreate(String leaderUUID, Integer communityId) + public CommunityCreate(String leaderUUID, String leaderName, Integer leaderId, Integer communityId, String communityName) { _leaderUUID = leaderUUID; + _leaderName = leaderName; + _leaderId = leaderId; _communityId = communityId; + _communityName = communityName; } public String getLeaderUUID() @@ -18,8 +24,23 @@ public class CommunityCreate extends ServerCommand return _leaderUUID; } + public String getLeaderName() + { + return _leaderName; + } + + public Integer getLeaderId() + { + return _leaderId; + } + public Integer getCommunityId() { return _communityId; } + + public String getCommunityName() + { + return _communityName; + } } \ No newline at end of file diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/CommunityCreateHandler.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/CommunityCreateHandler.java index 87803feeb..741ace018 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/CommunityCreateHandler.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/CommunityCreateHandler.java @@ -23,8 +23,11 @@ public class CommunityCreateHandler implements CommandCallback CommunityCreate update = ((CommunityCreate) command); UUID leaderUUID = UUID.fromString(update.getLeaderUUID()); Integer communityId = update.getCommunityId(); + String communityName = update.getCommunityName(); + Integer leaderId = update.getLeaderId(); + String leaderName = update.getLeaderName(); - _manager.handleCommunityCreation(communityId, leaderUUID); + _manager.handleCommunityCreation(communityId, communityName, leaderId, leaderUUID, leaderName); } } } \ No newline at end of file From 9acd6b7832681ebfd0db5c39572f1323808b1d37 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 22 Dec 2016 21:42:12 -0500 Subject: [PATCH 11/22] Ensure comparison of ints with == instead of Integers --- .../core/communities/gui/community/CommunitySettingsPage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunitySettingsPage.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunitySettingsPage.java index 4ed663137..835dfe6a4 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunitySettingsPage.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunitySettingsPage.java @@ -121,7 +121,7 @@ public class CommunitySettingsPage extends CommunitiesGUIPage @EventHandler public void onSettingsUpdate(CommunitySettingUpdateEvent event) { - if (event.getCommunity().getId() != _community.getId()) + if (event.getCommunity().getId().intValue() != _community.getId().intValue()) { return; } From 8e9336ad238abc63699b32a723317f792416cae3 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 22 Dec 2016 22:22:51 -0500 Subject: [PATCH 12/22] Make community button refresh with join requests --- .../core/communities/CommunityManager.java | 11 ++++++++++- .../core/communities/CommunityMemberInfo.java | 18 ++++++++++++------ .../gui/community/CommunityMembersPage.java | 15 +++++++++++++++ .../gui/community/CommunitySettingsPage.java | 15 +++++++++++++++ .../storage/CommunityRepository.java | 12 ++++++------ 5 files changed, 58 insertions(+), 13 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index 51792999e..3ded2db87 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -178,15 +178,24 @@ public class CommunityManager extends MiniDbClientPlugin public void updateAllMemberData(UUID uuid, String name) { + if (uuid == null) + { + return; + } _loadedCommunities.values().stream().filter(community -> community.getMembers().containsKey(uuid) && !community.getMembers().get(uuid).Name.equalsIgnoreCase(name)).forEach(community -> { community.getMembers().get(uuid).updateName(name); + community.getMembers().get(uuid).update(System.currentTimeMillis(), false, ""); UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community)); }); } public void updateAllJoinRequests(UUID uuid, String name) { + if (uuid == null) + { + return; + } _loadedCommunities.values().stream().filter(community -> community.getJoinRequests().containsKey(uuid) && !community.getJoinRequests().get(uuid).Name.equalsIgnoreCase(name)).forEach(community -> { community.getJoinRequests().get(uuid).update(name); @@ -269,7 +278,7 @@ public class CommunityManager extends MiniDbClientPlugin return; } CommunityMemberInfo member = community.getMembers().get(uuid); - member.Role = role; + member.updateRole(role); if (Bukkit.getPlayer(uuid) != null) { if (role.ordinal() > CommunityRole.COLEADER.ordinal()) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java index 08691dcec..46e2a5d76 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java @@ -17,17 +17,17 @@ public class CommunityMemberInfo public CommunityRole Role; public boolean ReadingChat = true; private ItemStack _memberIcon, _outsiderIcon; - private long _sinceLastLogin; + private long _lastLogin; private boolean _online = false; private String _currentServer = ""; - public CommunityMemberInfo(String name, UUID uuid, int accountId, CommunityRole role, long timeSinceLastLogin) + public CommunityMemberInfo(String name, UUID uuid, int accountId, CommunityRole role, long lastLogin) { Name = name; UUID = uuid; AccountId = accountId; Role = role; - _sinceLastLogin = timeSinceLastLogin; + _lastLogin = lastLogin; buildIcons(); } @@ -43,7 +43,7 @@ public class CommunityMemberInfo } else { - builder.addLore(C.cYellow + "Last Seen " + C.cWhite + UtilTime.MakeStr(_sinceLastLogin) + " Ago"); + builder.addLore(C.cYellow + "Last Seen " + C.cWhite + UtilTime.MakeStr(System.currentTimeMillis() - _lastLogin) + " Ago"); } if (_online) { @@ -66,9 +66,15 @@ public class CommunityMemberInfo buildIcons(); } - public void update(long timeSinceLastLogin, boolean online, String currentServer) + public void updateRole(CommunityRole role) { - _sinceLastLogin = timeSinceLastLogin; + Role = role; + buildIcons(); + } + + public void update(long lastLogin, boolean online, String currentServer) + { + _lastLogin = lastLogin; _online = online; _currentServer = currentServer; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMembersPage.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMembersPage.java index 347312750..ed6f79d41 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMembersPage.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMembersPage.java @@ -10,11 +10,13 @@ import org.bukkit.event.EventHandler; import mineplex.core.common.util.C; import mineplex.core.communities.Community; import mineplex.core.communities.CommunityDisbandEvent; +import mineplex.core.communities.CommunityJoinRequestsUpdateEvent; import mineplex.core.communities.CommunityMemberDataUpdateEvent; import mineplex.core.communities.CommunityMemberInfo; import mineplex.core.communities.CommunityMembershipUpdateEvent; import mineplex.core.communities.CommunityRole; import mineplex.core.communities.gui.ActionButton; +import mineplex.core.communities.gui.CommunitiesGUIButton; import mineplex.core.communities.gui.CommunitiesGUIPage; import mineplex.core.communities.gui.overview.CommunityOverviewPage; import mineplex.core.itemstack.ItemBuilder; @@ -142,6 +144,19 @@ public class CommunityMembersPage extends CommunitiesGUIPage Viewer.updateInventory(); } + @EventHandler + public void onRequestsUpdate(CommunityJoinRequestsUpdateEvent event) + { + if (event.getCommunity().getId().intValue() != _community.getId().intValue()) + { + return; + } + CommunitiesGUIButton button = Buttons.get(4); + button.update(); + Inv.setItem(4, button.Button); + Viewer.updateInventory(); + } + @EventHandler public void onMembershipUpdate(CommunityMembershipUpdateEvent event) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunitySettingsPage.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunitySettingsPage.java index 835dfe6a4..56e1d0ba4 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunitySettingsPage.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunitySettingsPage.java @@ -7,12 +7,14 @@ import org.bukkit.event.EventHandler; import mineplex.core.common.util.C; import mineplex.core.communities.Community; import mineplex.core.communities.CommunityDisbandEvent; +import mineplex.core.communities.CommunityJoinRequestsUpdateEvent; import mineplex.core.communities.CommunityMemberDataUpdateEvent; import mineplex.core.communities.CommunityMembershipUpdateEvent; import mineplex.core.communities.CommunityRole; import mineplex.core.communities.CommunitySetting; import mineplex.core.communities.CommunitySettingUpdateEvent; import mineplex.core.communities.gui.ActionButton; +import mineplex.core.communities.gui.CommunitiesGUIButton; import mineplex.core.communities.gui.CommunitiesGUIPage; import mineplex.core.communities.gui.overview.CommunityOverviewPage; import mineplex.core.itemstack.ItemBuilder; @@ -104,6 +106,19 @@ public class CommunitySettingsPage extends CommunitiesGUIPage Viewer.updateInventory(); } + @EventHandler + public void onRequestsUpdate(CommunityJoinRequestsUpdateEvent event) + { + if (event.getCommunity().getId().intValue() != _community.getId().intValue()) + { + return; + } + CommunitiesGUIButton button = Buttons.get(4); + button.update(); + Inv.setItem(4, button.Button); + Viewer.updateInventory(); + } + @EventHandler public void onMembershipUpdate(CommunityMembershipUpdateEvent event) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index 87a678289..9b32d09cf 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -77,14 +77,14 @@ public class CommunityRepository extends MinecraftRepository final String name = memberSet.getString("name"); final UUID uuid = UUID.fromString(memberSet.getString("uuid")); final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); - final long timeSinceOnline = memberSet.getTimestamp(6).getTime() - memberSet.getTimestamp(5).getTime(); + final long lastLogin = memberSet.getTimestamp("lastLogin").getTime(); boolean readingChat = memberSet.getBoolean("readingChat"); - CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, timeSinceOnline); + CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); PlayerStatus status = _repo.getElement(name); if (status != null) { - info.update(timeSinceOnline, true, status.getServer()); + info.update(lastLogin, true, status.getServer()); } info.ReadingChat = readingChat; community.getMembers().put(info.UUID, info); @@ -148,14 +148,14 @@ public class CommunityRepository extends MinecraftRepository final String name = memberSet.getString("name"); final UUID uuid = UUID.fromString(memberSet.getString("uuid")); final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); - final long timeSinceOnline = memberSet.getTimestamp(6).getTime() - memberSet.getTimestamp(5).getTime(); + final long lastLogin = memberSet.getTimestamp("lastLogin").getTime(); boolean readingChat = memberSet.getBoolean("readingChat"); - CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, timeSinceOnline); + CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); PlayerStatus status = _repo.getElement(name); if (status != null) { - info.update(timeSinceOnline, true, status.getServer()); + info.update(lastLogin, true, status.getServer()); } info.ReadingChat = readingChat; community.getMembers().put(info.UUID, info); From 09e711e20219967d2eef4e300e205679384b4604 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 22 Dec 2016 22:32:46 -0500 Subject: [PATCH 13/22] Properly get online player key from redis --- .../core/communities/storage/CommunityRepository.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index 9b32d09cf..09ecdc1fc 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -29,7 +29,7 @@ public class CommunityRepository extends MinecraftRepository private static final String GET_ALL_COMMUNITIES = "SELECT * FROM communities WHERE region=?;"; private static final String GET_COMMUNITY_BY_ID = "SELECT * FROM communities WHERE id=?;"; private static final String GET_COMMUNITY_BY_NAME = "SELECT * FROM communities WHERE name=? AND region=?;"; - private static final String GET_COMMUNITY_MEMBERS = "SELECT cm.accountId, cm.communityRole, ac.name, ac.uuid, ac.lastLogin, now(), cm.readingChat FROM communityMembers cm INNER JOIN accounts ac ON ac.id=cm.accountId WHERE communityId=?;"; + private static final String GET_COMMUNITY_MEMBERS = "SELECT cm.accountId, cm.communityRole, ac.name, ac.uuid, ac.lastLogin, cm.readingChat FROM communityMembers cm INNER JOIN accounts ac ON ac.id=cm.accountId WHERE communityId=?;"; private static final String GET_COMMUNITY_JOIN_REQUESTS = "SELECT cjr.accountId, ac.name, ac.uuid FROM communityJoinRequests cjr INNER JOIN accounts ac ON ac.id=cjr.accountId WHERE communityId=?;"; private static final String GET_COMMUNITY_SETTINGS = "SELECT settingId, settingValue FROM communitySettings WHERE communityId=?;"; @@ -81,7 +81,7 @@ public class CommunityRepository extends MinecraftRepository boolean readingChat = memberSet.getBoolean("readingChat"); CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); - PlayerStatus status = _repo.getElement(name); + PlayerStatus status = _repo.getElement(name.toLowerCase()); if (status != null) { info.update(lastLogin, true, status.getServer()); @@ -152,7 +152,7 @@ public class CommunityRepository extends MinecraftRepository boolean readingChat = memberSet.getBoolean("readingChat"); CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); - PlayerStatus status = _repo.getElement(name); + PlayerStatus status = _repo.getElement(name.toLowerCase()); if (status != null) { info.update(lastLogin, true, status.getServer()); @@ -212,7 +212,7 @@ public class CommunityRepository extends MinecraftRepository { for (CommunityMemberInfo info : c.getMembers().values()) { - PlayerStatus status = _repo.getElement(info.Name); + PlayerStatus status = _repo.getElement(info.Name.toLowerCase()); boolean online = false; String server = ""; if (status != null) From 0a69562579e0df69018863d09758249648a1c379 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 22 Dec 2016 22:57:16 -0500 Subject: [PATCH 14/22] Improve communities member page sorter to base on role as well as name and online status --- .../gui/community/CommunityMembersPage.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMembersPage.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMembersPage.java index ed6f79d41..720d5a41c 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMembersPage.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/gui/community/CommunityMembersPage.java @@ -110,7 +110,18 @@ public class CommunityMembersPage extends CommunitiesGUIPage { if (info1.isOnline() == info2.isOnline()) { - return info1.Name.compareToIgnoreCase(info2.Name); + if (info1.Role == info2.Role) + { + return info1.Name.compareToIgnoreCase(info2.Name); + } + else if (info1.Role.ordinal() < info2.Role.ordinal()) + { + return -1; + } + else + { + return 1; + } } if (info1.isOnline()) From ac8d8aa69c28acfbcd13b7069f2ef94ceae83577 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 22 Dec 2016 23:15:26 -0500 Subject: [PATCH 15/22] Prevent Name value in info classes from being null --- .../core/communities/CommunityJoinRequestInfo.java | 4 ++++ .../src/mineplex/core/communities/CommunityManager.java | 8 ++++++++ .../mineplex/core/communities/CommunityMemberInfo.java | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityJoinRequestInfo.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityJoinRequestInfo.java index 1eb973551..3e42d6bcd 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityJoinRequestInfo.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityJoinRequestInfo.java @@ -36,6 +36,10 @@ public class CommunityJoinRequestInfo public void update(String name) { + if (name == null) + { + return; + } Name = name; buildIcon(); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index 3ded2db87..1751eddf2 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -182,6 +182,10 @@ public class CommunityManager extends MiniDbClientPlugin { return; } + if (name == null) + { + return; + } _loadedCommunities.values().stream().filter(community -> community.getMembers().containsKey(uuid) && !community.getMembers().get(uuid).Name.equalsIgnoreCase(name)).forEach(community -> { community.getMembers().get(uuid).updateName(name); @@ -196,6 +200,10 @@ public class CommunityManager extends MiniDbClientPlugin { return; } + if (name == null) + { + return; + } _loadedCommunities.values().stream().filter(community -> community.getJoinRequests().containsKey(uuid) && !community.getJoinRequests().get(uuid).Name.equalsIgnoreCase(name)).forEach(community -> { community.getJoinRequests().get(uuid).update(name); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java index 46e2a5d76..03e98963f 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityMemberInfo.java @@ -62,6 +62,10 @@ public class CommunityMemberInfo public void updateName(String name) { + if (name == null) + { + return; + } Name = name; buildIcons(); } From 7a482c33866567a8f43d8375d528476d7b5fa0b7 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Fri, 23 Dec 2016 00:05:00 -0500 Subject: [PATCH 16/22] Implement system to run code when an SQL error occurs, and ensure that duplicate key errors on community creation inform the community creator --- .../core/communities/CommunityManager.java | 23 ++--- .../storage/CommunityRepository.java | 15 ++-- .../serverdata/database/RepositoryBase.java | 89 +++++++++++++++++-- 3 files changed, 105 insertions(+), 22 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index 1751eddf2..2bde45a7d 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -262,7 +262,7 @@ public class CommunityManager extends MiniDbClientPlugin } setting.parseValueInto(newValue, community); //community.message(F.main(getName(), F.name(sender) + " has changed settings in " + F.name(community.getName()) + "!")); - UtilServer.CallEvent(new CommunitySettingUpdateEvent(community)); + runSync(() -> UtilServer.CallEvent(new CommunitySettingUpdateEvent(community))); } public void handleCommunityNameUpdate(Integer id, String sender, String name) @@ -275,7 +275,7 @@ public class CommunityManager extends MiniDbClientPlugin String oldName = community.getName(); community.setName(name); community.message(F.main(getName(), F.name(sender) + " has changed the name of " + F.name(oldName) + " to " + F.name(community.getName()) + "!")); - UtilServer.CallEvent(new CommunityNameUpdateEvent(community)); + runSync(() -> UtilServer.CallEvent(new CommunityNameUpdateEvent(community))); } public void handleCommunityMembershipRoleUpdate(Integer id, String sender, UUID uuid, CommunityRole role) @@ -297,7 +297,7 @@ public class CommunityManager extends MiniDbClientPlugin } String name = member.Name; community.message(F.main(getName(), F.name(sender) + " has changed " + F.name(name + "'s") + " role to " + F.elem(role.getDisplay()) + " in " + F.name(community.getName()) + "!"), CommunityRole.COLEADER); - UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community)); + runSync(() -> UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community))); } public void handleCommunityMembershipUpdate(Integer id, String sender, String playerName, UUID playerUUID, Integer accountId, boolean kick, boolean leave) @@ -337,12 +337,15 @@ public class CommunityManager extends MiniDbClientPlugin community.message(F.main(getName(), F.name(playerName) + " has joined " + F.name(community.getName()) + "!")); } - - UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community)); - if (Bukkit.getPlayer(playerUUID) != null) + + runSync(() -> { - UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(playerUUID))); - } + UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community)); + if (Bukkit.getPlayer(playerUUID) != null) + { + UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(playerUUID))); + } + }); } public void handleCommunityInvite(Integer id, String sender, String targetName, UUID targetUUID) @@ -410,7 +413,7 @@ public class CommunityManager extends MiniDbClientPlugin community.getJoinRequests().put(playerUUID, new CommunityJoinRequestInfo(playerName, playerUUID, accountId)); community.message(F.main(getName(), F.name(playerName) + " has requested to join " + F.name(community.getName()) + "!"), CommunityRole.COLEADER); - UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community)); + runSync(() -> UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community))); } public void handleCommunityCloseJoinRequest(Integer id, String sender, String playerName, UUID playerUUID, Integer accountId, boolean announce) @@ -430,7 +433,7 @@ public class CommunityManager extends MiniDbClientPlugin community.message(F.main(getName(), F.name(playerName) + "'s request to join " + F.name(community.getName()) + " has been denied by " + F.name(sender) + "!"), CommunityRole.COLEADER); } - UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community)); + runSync(() -> UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community))); } public void handleCommunityCreation(Integer id, String name, Integer leaderId, UUID leaderUUID, String leaderName) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index 09ecdc1fc..331a3cf2f 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -301,18 +301,19 @@ public class CommunityRepository extends MinecraftRepository if (resultSet.next()) { int id = resultSet.getInt(1); - executeUpdate(connection, ADD_TO_COMMUNITY, new ColumnInt("accountId", leaderAccount), new ColumnInt("communityId", id), new ColumnVarChar("communityRole", 20, CommunityRole.LEADER.toString())); + executeUpdate(connection, ADD_TO_COMMUNITY, null, new ColumnInt("accountId", leaderAccount), new ColumnInt("communityId", id), new ColumnVarChar("communityRole", 20, CommunityRole.LEADER.toString())); idCallback.run(id); } else { idCallback.run(-1); } - }, new ColumnVarChar("name", 15, name), new ColumnVarChar("region", 5, _us ? "US" : "EU")); + }, () -> idCallback.run(-1), new ColumnVarChar("name", 15, name), new ColumnVarChar("region", 5, _us ? "US" : "EU")); } catch (SQLException e) { e.printStackTrace(); + idCallback.run(-1); } } @@ -320,11 +321,11 @@ public class CommunityRepository extends MinecraftRepository { try (Connection connection = getConnection()) { - executeUpdate(connection, "DELETE FROM communities WHERE id=?;", new ColumnInt("id", communityId)); - executeUpdate(connection, "DELETE FROM communitySettings WHERE communityId=?;", new ColumnInt("communityId", communityId)); - executeUpdate(connection, "DELETE FROM communityMembers WHERE communityId=?;", new ColumnInt("communityId", communityId)); - executeUpdate(connection, "DELETE FROM communityInvites WHERE communityId=?;", new ColumnInt("communityId", communityId)); - executeUpdate(connection, "DELETE FROM communityJoinRequests WHERE communityId=?", new ColumnInt("communityId", communityId)); + executeUpdate(connection, "DELETE FROM communities WHERE id=?;", null, new ColumnInt("id", communityId)); + executeUpdate(connection, "DELETE FROM communitySettings WHERE communityId=?;", null, new ColumnInt("communityId", communityId)); + executeUpdate(connection, "DELETE FROM communityMembers WHERE communityId=?;", null, new ColumnInt("communityId", communityId)); + executeUpdate(connection, "DELETE FROM communityInvites WHERE communityId=?;", null, new ColumnInt("communityId", communityId)); + executeUpdate(connection, "DELETE FROM communityJoinRequests WHERE communityId=?", null, new ColumnInt("communityId", communityId)); } catch (SQLException e) { diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/database/RepositoryBase.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/database/RepositoryBase.java index 987904b2c..8a175212d 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/database/RepositoryBase.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/database/RepositoryBase.java @@ -76,9 +76,14 @@ public abstract class RepositoryBase } } - protected int executeUpdate(Connection connection, String query, Column...columns) + protected int executeUpdate(Connection connection, String query, Runnable onSQLError, Column...columns) { - return executeInsert(connection, query, null, columns); + return executeInsert(connection, query, null, onSQLError, columns); + } + + protected int executeUpdate(String query, Runnable onSQLError, Column...columns) + { + return executeInsert(query, null, columns); } /** @@ -92,7 +97,7 @@ public abstract class RepositoryBase return executeInsert(query, null, columns); } - protected int executeInsert(Connection connection, String query, ResultSetCallable callable, Column...columns) + protected int executeInsert(Connection connection, String query, ResultSetCallable callable, Runnable onSQLError, Column...columns) { int affectedRows = 0; @@ -114,6 +119,33 @@ public abstract class RepositoryBase } } catch (SQLException exception) + { + exception.printStackTrace(); + if (onSQLError != null) + { + onSQLError.run(); + } + } + catch (Exception exception) + { + exception.printStackTrace(); + } + + return affectedRows; + } + + protected int executeInsert(String query, ResultSetCallable callable, Runnable onSQLError, Column...columns) + { + int affectedRows = 0; + + // Automatic resource management for handling/closing objects. + try ( + Connection connection = getConnection(); + ) + { + affectedRows = executeInsert(connection, query, callable, onSQLError, columns); + } + catch (SQLException exception) { exception.printStackTrace(); } @@ -134,7 +166,7 @@ public abstract class RepositoryBase Connection connection = getConnection(); ) { - affectedRows = executeInsert(connection, query, callable, columns); + affectedRows = executeInsert(connection, query, callable, null, columns); } catch (SQLException exception) { @@ -148,7 +180,7 @@ public abstract class RepositoryBase return affectedRows; } - protected void executeQuery(PreparedStatement statement, ResultSetCallable callable, Column...columns) + protected void executeQuery(PreparedStatement statement, ResultSetCallable callable, Runnable onSQLError, Column...columns) { try { @@ -166,6 +198,34 @@ public abstract class RepositoryBase } } catch (SQLException exception) + { + exception.printStackTrace(); + if (onSQLError != null) + { + onSQLError.run(); + } + } + catch (Exception exception) + { + exception.printStackTrace(); + } + } + + protected void executeQuery(PreparedStatement statement, ResultSetCallable callable, Column...columns) + { + executeQuery(statement, callable, null, columns); + } + + protected void executeQuery(Connection connection, String query, ResultSetCallable callable, Runnable onSQLError, Column...columns) + { + // Automatic resource management for handling/closing objects. + try ( + PreparedStatement preparedStatement = connection.prepareStatement(query) + ) + { + executeQuery(preparedStatement, callable, onSQLError, columns); + } + catch (SQLException exception) { exception.printStackTrace(); } @@ -194,6 +254,25 @@ public abstract class RepositoryBase } } + protected void executeQuery(String query, ResultSetCallable callable, Runnable onSQLError, Column...columns) + { + // Automatic resource management for handling/closing objects. + try ( + Connection connection = getConnection(); + ) + { + executeQuery(connection, query, callable, onSQLError, columns); + } + catch (SQLException exception) + { + exception.printStackTrace(); + } + catch (Exception exception) + { + exception.printStackTrace(); + } + } + protected void executeQuery(String query, ResultSetCallable callable, Column...columns) { // Automatic resource management for handling/closing objects. From 1e0ba62557da33d080aa28c61fffaaa50986ab5e Mon Sep 17 00:00:00 2001 From: William Burns Date: Fri, 23 Dec 2016 18:13:44 +0000 Subject: [PATCH 17/22] To many typos. --- .../src/mineplex/core/gadget/gadgets/taunts/EternalTaunt.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/gadget/gadgets/taunts/EternalTaunt.java b/Plugins/Mineplex.Core/src/mineplex/core/gadget/gadgets/taunts/EternalTaunt.java index f34726000..68a926d15 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/gadget/gadgets/taunts/EternalTaunt.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/gadget/gadgets/taunts/EternalTaunt.java @@ -44,7 +44,7 @@ public class EternalTaunt extends TauntGadget public EternalTaunt(GadgetManager manager) { - super(manager, "Eternal Taunt", UtilText.splitLinesToArray(new String[]{C.cGray + "Although the Eternal has been around forever, he waited to long for a worthy opponent and he turned to bones.", + super(manager, "Eternal Taunt", UtilText.splitLinesToArray(new String[]{C.cGray + "Although the Eternal has been around forever, he waited too long for a worthy opponent and he turned to bones.", "", C.cWhite + "Use /taunt in game to show how long you've been waiting.", C.cRed + "Cannot be used while in PvP!"}, LineFormat.LORE), From 4ae21e66c9d19826f476eca57893b444101bc38d Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Fri, 23 Dec 2016 15:07:52 -0500 Subject: [PATCH 18/22] Delete new communities if the leader has been transferred a new community during creation --- .../src/mineplex/core/communities/CommunityManager.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index 2bde45a7d..7e4aa3801 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -688,6 +688,13 @@ public class CommunityManager extends MiniDbClientPlugin } else { + if (ownsCommunity(sender.getUniqueId())) + { + UtilPlayer.message(sender, F.main(getName(), "You already own a community!")); + _repo.deleteCommunity(id); + runSync(() -> _creating.remove(sender.getUniqueId())); + return; + } new CommunityCreate(sender.getUniqueId().toString(), senderName, accountId, id, name).publish(); runSync(() -> _creating.remove(sender.getUniqueId())); } From 74a6071d4ec377d446c795ff1d5858f10424f024 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Fri, 23 Dec 2016 18:01:52 -0500 Subject: [PATCH 19/22] Refactor Redis PlayerStatus to use UUID as a key instead of Name, and make communities not force every server to subscribe to join publishes from proxies --- .../bungee/playerTracker/PlayerTracker.java | 4 +- .../core/communities/CommunityManager.java | 41 +------------------ .../communities/redis/PlayerJoinHandler.java | 4 +- .../storage/CommunityRepository.java | 6 ++- .../playerdisguise/PlayerDisguiseManager.java | 8 ++-- .../mineplex/core/friend/FriendManager.java | 10 ++--- .../core/friend/data/FriendRepository.java | 26 ++++++------ .../core/friend/data/FriendStatus.java | 1 + .../serverdata/data/PlayerStatus.java | 17 ++++---- 9 files changed, 43 insertions(+), 74 deletions(-) diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java index cb2f05f7f..aa387b0a8 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java @@ -60,7 +60,7 @@ public class PlayerTracker implements Listener { public void run() { - PlayerStatus snapshot = new PlayerStatus(event.getPlayer().getName(), event.getServer().getInfo().getName()); + PlayerStatus snapshot = new PlayerStatus(event.getPlayer().getUniqueId().toString(), event.getPlayer().getName(), event.getServer().getInfo().getName()); _repository.addElement(snapshot, DEFAULT_STATUS_TIMEOUT); } }); @@ -73,7 +73,7 @@ public class PlayerTracker implements Listener { public void run() { - _repository.removeElement(event.getPlayer().getName().toLowerCase()); + _repository.removeElement(event.getPlayer().getUniqueId().toString()); } }); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index 7e4aa3801..236b94fbd 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -8,7 +8,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Random; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; @@ -57,13 +56,11 @@ import mineplex.core.communities.redis.CommunityUpdateName; import mineplex.core.communities.redis.CommunityUpdateNameHandler; import mineplex.core.communities.redis.CommunityUpdateSetting; import mineplex.core.communities.redis.CommunityUpdateSettingHandler; -import mineplex.core.communities.redis.PlayerJoinHandler; import mineplex.core.communities.storage.CommunityRepository; import mineplex.core.preferences.Preference; import mineplex.core.preferences.PreferencesManager; import mineplex.core.recharge.Recharge; import mineplex.serverdata.Region; -import mineplex.serverdata.commands.PlayerJoinCommand; import mineplex.serverdata.commands.ServerCommandManager; import mineplex.serverdata.data.DataRepository; import mineplex.serverdata.data.PlayerStatus; @@ -143,7 +140,7 @@ public class CommunityManager extends MiniDbClientPlugin LinkedList communities = new LinkedList<>(); _loadedCommunities.values().forEach(community -> communities.add(community)); _repo.updateMembers(communities); - }, 0L, 20 * 5); + }, 0L, 20 * 7); Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> { @@ -166,7 +163,6 @@ public class CommunityManager extends MiniDbClientPlugin ServerCommandManager.getInstance().registerCommandType(CommunityUpdateMembership.class, new CommunityUpdateMembershipHandler(this)); ServerCommandManager.getInstance().registerCommandType(CommunityUpdateName.class, new CommunityUpdateNameHandler(this)); ServerCommandManager.getInstance().registerCommandType(CommunityUpdateSetting.class, new CommunityUpdateSettingHandler(this)); - ServerCommandManager.getInstance().registerCommandType(PlayerJoinCommand.class, new PlayerJoinHandler(this)); } public boolean ownsCommunity(UUID uuid) @@ -176,41 +172,6 @@ public class CommunityManager extends MiniDbClientPlugin .anyMatch(entry -> entry.getKey().equals(uuid) && entry.getValue().Role == CommunityRole.LEADER); } - public void updateAllMemberData(UUID uuid, String name) - { - if (uuid == null) - { - return; - } - if (name == null) - { - return; - } - _loadedCommunities.values().stream().filter(community -> community.getMembers().containsKey(uuid) && !community.getMembers().get(uuid).Name.equalsIgnoreCase(name)).forEach(community -> - { - community.getMembers().get(uuid).updateName(name); - community.getMembers().get(uuid).update(System.currentTimeMillis(), false, ""); - UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community)); - }); - } - - public void updateAllJoinRequests(UUID uuid, String name) - { - if (uuid == null) - { - return; - } - if (name == null) - { - return; - } - _loadedCommunities.values().stream().filter(community -> community.getJoinRequests().containsKey(uuid) && !community.getJoinRequests().get(uuid).Name.equalsIgnoreCase(name)).forEach(community -> - { - community.getJoinRequests().get(uuid).update(name); - UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community)); - }); - } - private void cycleBrowser() { if (_cycling) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/PlayerJoinHandler.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/PlayerJoinHandler.java index 47fc8210a..a691f1f9b 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/PlayerJoinHandler.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/redis/PlayerJoinHandler.java @@ -1,7 +1,5 @@ package mineplex.core.communities.redis; -import java.util.UUID; - import mineplex.core.communities.CommunityManager; import mineplex.serverdata.commands.CommandCallback; import mineplex.serverdata.commands.PlayerJoinCommand; @@ -22,7 +20,7 @@ public class PlayerJoinHandler implements CommandCallback if (command instanceof PlayerJoinCommand) { PlayerJoinCommand joinCommand = (PlayerJoinCommand)command; - _communityManager.updateAllMemberData(UUID.fromString(joinCommand.getUuid()), joinCommand.getName()); + //_communityManager.updateAllMemberData(UUID.fromString(joinCommand.getUuid()), joinCommand.getName()); } } } \ No newline at end of file diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index 331a3cf2f..4801bebb0 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -212,7 +212,7 @@ public class CommunityRepository extends MinecraftRepository { for (CommunityMemberInfo info : c.getMembers().values()) { - PlayerStatus status = _repo.getElement(info.Name.toLowerCase()); + PlayerStatus status = _repo.getElement(info.UUID.toString()); boolean online = false; String server = ""; if (status != null) @@ -220,6 +220,10 @@ public class CommunityRepository extends MinecraftRepository online = true; server = status.getServer(); info.update(System.currentTimeMillis(), online, server); + if (!info.Name.equalsIgnoreCase(status.getName())) + { + info.updateName(status.getName()); + } } else { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/disguise/playerdisguise/PlayerDisguiseManager.java b/Plugins/Mineplex.Core/src/mineplex/core/disguise/playerdisguise/PlayerDisguiseManager.java index 4c7e16502..20beec367 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/disguise/playerdisguise/PlayerDisguiseManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/disguise/playerdisguise/PlayerDisguiseManager.java @@ -392,8 +392,8 @@ public class PlayerDisguiseManager extends MiniPlugin implements IPacketHandler CoreClient client = getClientManager().Get(caller); client.undisguise(); - require(FriendManager.class).updatePlayerStatus(disguisedProfile.getName(), null); - require(FriendManager.class).updatePlayerStatus(originalProfile.getName(), new PlayerStatus(originalProfile.getName(), _serverName)); + require(FriendManager.class).updatePlayerStatus(disguisedProfile.getId().toString(), null); + require(FriendManager.class).updatePlayerStatus(originalProfile.getId().toString(), new PlayerStatus(originalProfile.getId().toString(), originalProfile.getName(), _serverName)); getPreferencesManager().handlePlayerJoin(caller, true); require(ScoreboardManager.class).handlePlayerJoin(disguise.getOriginalProfile().getName()); @@ -539,8 +539,8 @@ public class PlayerDisguiseManager extends MiniPlugin implements IPacketHandler callerProfile.getProperties().removeAll(ORIGINAL_UUID_KEY); callerProfile.getProperties().put(ORIGINAL_UUID_KEY, new Property(ORIGINAL_UUID_KEY, caller.getUniqueId().toString())); - require(FriendManager.class).updatePlayerStatus(disguisePlayer.getOriginalProfile().getName(), null); - require(FriendManager.class).updatePlayerStatus(requestedUsername, new PlayerStatus(requestedUsername, _serverName)); + require(FriendManager.class).updatePlayerStatus(disguisePlayer.getOriginalProfile().getId().toString(), null); + require(FriendManager.class).updatePlayerStatus(disguisePlayer.getProfile().getId().toString(), new PlayerStatus(disguisePlayer.getProfile().getId().toString(), requestedUsername, _serverName)); getPreferencesManager().handlePlayerJoin(caller, true); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java index 99a168f9f..d48fcea85 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java @@ -402,14 +402,14 @@ public class FriendManager extends MiniDbClientPlugin } - public void updatePlayerStatus(String playerName, PlayerStatus status) + public void updatePlayerStatus(String playerUUID, PlayerStatus status) { - _repository.updatePlayerStatus(playerName, status); + _repository.updatePlayerStatus(playerUUID, status); } - public PlayerStatus getStatus(String playerName) + public PlayerStatus getStatus(String playerUUID) { - return _repository.getStatus(playerName); + return _repository.getStatus(playerUUID); } @Override @@ -421,7 +421,7 @@ public class FriendManager extends MiniDbClientPlugin @Override public String getQuery(int accountId, String uuid, String name) { - return "SELECT tA.Name, status, tA.lastLogin, now() FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget WHERE uuidSource = '" + return "SELECT tA.Name, status, tA.lastLogin, now(), uuidTarget FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget WHERE uuidSource = '" + uuid + "';"; } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java index 3a5787d9f..91f165215 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java @@ -26,7 +26,7 @@ import mineplex.serverdata.servers.ServerManager; public class FriendRepository extends MinecraftRepository { private static String CREATE_FRIEND_TABLE = "CREATE TABLE IF NOT EXISTS accountFriend (id INT NOT NULL AUTO_INCREMENT, uuidSource VARCHAR(100), uuidTarget VARCHAR(100), status VARCHAR(100), PRIMARY KEY (id), UNIQUE INDEX uuidIndex (uuidSource, uuidTarget));"; - private static String RETRIEVE_MULTIPLE_FRIEND_RECORDS = "SELECT uuidSource, tA.Name, status, tA.lastLogin, now() FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget WHERE uuidSource IN "; + private static String RETRIEVE_MULTIPLE_FRIEND_RECORDS = "SELECT uuidSource, tA.Name, status, tA.lastLogin, now(), uuidTarget FROM accountFriend INNER Join accounts AS fA ON fA.uuid = uuidSource INNER JOIN accounts AS tA ON tA.uuid = uuidTarget WHERE uuidSource IN "; private static String ADD_FRIEND_RECORD = "INSERT INTO accountFriend (uuidSource, uuidTarget, status, created) SELECT fA.uuid AS uuidSource, tA.uuid AS uuidTarget, ?, now() FROM accounts as fA LEFT JOIN accounts AS tA ON tA.name = ? WHERE fA.name = ?;"; private static String UPDATE_MUTUAL_RECORD = "UPDATE accountFriend AS aF INNER JOIN accounts as fA ON aF.uuidSource = fA.uuid INNER JOIN accounts AS tA ON aF.uuidTarget = tA.uuid SET aF.status = ? WHERE tA.name = ? AND fA.name = ?;"; private static String DELETE_FRIEND_RECORD = "DELETE aF FROM accountFriend AS aF INNER JOIN accounts as fA ON aF.uuidSource = fA.uuid INNER JOIN accounts AS tA ON aF.uuidTarget = tA.uuid WHERE fA.name = ? AND tA.name = ?;"; @@ -95,6 +95,7 @@ public class FriendRepository extends MinecraftRepository friend.Name = resultSet.getString(2); friend.Status = Enum.valueOf(FriendStatusType.class, resultSet.getString(3)); friend.LastSeenOnline = resultSet.getTimestamp(5).getTime() - resultSet.getTimestamp(4).getTime(); + friend.UUID = resultSet.getString(6); if (!friends.containsKey(uuidSource)) friends.put(uuidSource, new FriendData()); @@ -126,6 +127,7 @@ public class FriendRepository extends MinecraftRepository friend.Name = resultSet.getString(1); friend.Status = Enum.valueOf(FriendStatusType.class, resultSet.getString(2)); friend.LastSeenOnline = resultSet.getTimestamp(4).getTime() - resultSet.getTimestamp(3).getTime(); + friend.UUID = resultSet.getString(5); friend.ServerName = null; friend.Online = (friend.ServerName != null); friendData.getFriends().add(friend); @@ -144,26 +146,26 @@ public class FriendRepository extends MinecraftRepository public void loadFriendStatuses(FriendData friendData) { // Generate a set of all friend names - Set friendNames = new HashSet(); + Set friendUUIDS = new HashSet(); for(FriendStatus status : friendData.getFriends()) { - friendNames.add(status.Name.toLowerCase()); + friendUUIDS.add(status.UUID); } // Load PlayerStatus' for friends - Collection statuses = _repository.getElements(friendNames); + Collection statuses = _repository.getElements(friendUUIDS); // Load player statuses into a mapping Map playerStatuses = new HashMap(); for(PlayerStatus status : statuses) { - playerStatuses.put(status.getName(), status); + playerStatuses.put(status.getUUID(), status); } // Load status information into friend data. for (FriendStatus friend : friendData.getFriends()) { - PlayerStatus status = playerStatuses.get(friend.Name); + PlayerStatus status = playerStatuses.get(friend.UUID); friend.Online = (status != null); friend.ServerName = (friend.Online) ? status.getServer() : null; } @@ -174,14 +176,14 @@ public class FriendRepository extends MinecraftRepository * @return the name that the player matching {@code playerName} * is currently online on, if they are online, null otherwise. */ - public String fetchPlayerServer(String playerName) + public String fetchPlayerServer(String playerUUID) { - PlayerStatus status = _repository.getElement(playerName.toLowerCase()); + PlayerStatus status = _repository.getElement(playerUUID); return (status == null) ? null : status.getServer(); } - public void updatePlayerStatus(String playerName, PlayerStatus status) + public void updatePlayerStatus(String playerUUID, PlayerStatus status) { if (status != null) { @@ -189,12 +191,12 @@ public class FriendRepository extends MinecraftRepository } else { - _repository.removeElement(playerName.toLowerCase()); + _repository.removeElement(playerUUID); } } - public PlayerStatus getStatus(String playerName) + public PlayerStatus getStatus(String playerUUID) { - return _repository.getElement(playerName.toLowerCase()); + return _repository.getElement(playerUUID); } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendStatus.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendStatus.java index 9c60df03f..cabc1c7f7 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendStatus.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendStatus.java @@ -5,6 +5,7 @@ import mineplex.core.friend.FriendStatusType; public class FriendStatus { public String Name; + public String UUID; public String ServerName; public boolean Online; /** diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/PlayerStatus.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/PlayerStatus.java index 5cb4d9072..44039f23b 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/PlayerStatus.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/PlayerStatus.java @@ -4,11 +4,15 @@ import mineplex.serverdata.data.Data; public class PlayerStatus implements Data { - // The name of this server. + // The uuid of this player. + private String _uuid; + public String getUUID() { return _uuid; } + + // The name of this player. private String _name; public String getName() { return _name; } - // The current message of the day (MOTD) of the server. + // The current server occupied by this player. private String _server; public String getServer() { return _server; } @@ -17,19 +21,18 @@ public class PlayerStatus implements Data * @param name * @param server */ - public PlayerStatus(String name, String server) + public PlayerStatus(String uuid, String name, String server) { + _uuid = uuid; _name = name; _server = server; } /** * Unique identifying String ID associated with this {@link PlayerStatus}. - * - * Use the lowercase name so we can have case-insensitive lookup */ public String getDataId() { - return _name.toLowerCase(); + return _uuid; } -} +} \ No newline at end of file From 35a9eabf7408887395cceba0f1c5e4abad5d5036 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Fri, 23 Dec 2016 18:34:52 -0500 Subject: [PATCH 20/22] Use UUID object instead of String for PlayerStatus and FriendStatus, as well as update CommunityJoinRequestInfo on a task --- .../bungee/playerTracker/PlayerTracker.java | 2 +- .../core/communities/CommunityManager.java | 7 +++++ .../storage/CommunityRepository.java | 24 +++++++++++++- .../playerdisguise/PlayerDisguiseManager.java | 8 ++--- .../mineplex/core/friend/FriendManager.java | 31 ++++++++++--------- .../core/friend/data/FriendRepository.java | 23 +++++++------- .../core/friend/data/FriendStatus.java | 4 ++- .../serverdata/data/PlayerStatus.java | 10 +++--- 8 files changed, 71 insertions(+), 38 deletions(-) diff --git a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java index aa387b0a8..923f627a0 100644 --- a/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java +++ b/Plugins/Mineplex.Bungee.Mineplexer/src/mineplex/bungee/playerTracker/PlayerTracker.java @@ -60,7 +60,7 @@ public class PlayerTracker implements Listener { public void run() { - PlayerStatus snapshot = new PlayerStatus(event.getPlayer().getUniqueId().toString(), event.getPlayer().getName(), event.getServer().getInfo().getName()); + PlayerStatus snapshot = new PlayerStatus(event.getPlayer().getUniqueId(), event.getPlayer().getName(), event.getServer().getInfo().getName()); _repository.addElement(snapshot, DEFAULT_STATUS_TIMEOUT); } }); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index 236b94fbd..c465a7ce8 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -85,6 +85,8 @@ public class CommunityManager extends MiniDbClientPlugin private volatile boolean _cycling = false; + private volatile boolean _updateJoinRequests = false; + @SuppressWarnings("deprecation") public CommunityManager(JavaPlugin plugin, CoreClientManager clientManager) { @@ -140,6 +142,11 @@ public class CommunityManager extends MiniDbClientPlugin LinkedList communities = new LinkedList<>(); _loadedCommunities.values().forEach(community -> communities.add(community)); _repo.updateMembers(communities); + if (_updateJoinRequests) + { + _repo.updateJoinRequests(communities); + } + _updateJoinRequests = !_updateJoinRequests; }, 0L, 20 * 7); Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index 4801bebb0..6b814c0c9 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -220,7 +220,7 @@ public class CommunityRepository extends MinecraftRepository online = true; server = status.getServer(); info.update(System.currentTimeMillis(), online, server); - if (!info.Name.equalsIgnoreCase(status.getName())) + if (!info.Name.equals(status.getName())) { info.updateName(status.getName()); } @@ -236,6 +236,28 @@ public class CommunityRepository extends MinecraftRepository } } + public void updateJoinRequests(LinkedList communities) + { + if (communities.isEmpty()) + { + return; + } + for (Community c : communities) + { + for (CommunityJoinRequestInfo info : c.getJoinRequests().values()) + { + PlayerStatus status = _repo.getElement(info.UUID.toString()); + if (status != null) + { + if (!info.Name.equals(status.getName())) + { + info.update(status.getName()); + } + } + } + } + } + public void loadInvites(int accountId, List invites) { executeQuery("SELECT ci.communityId, c.region FROM communityInvites AS ci INNER JOIN communities AS c ON c.id=ci.communityId WHERE accountId=?;", resultSet -> diff --git a/Plugins/Mineplex.Core/src/mineplex/core/disguise/playerdisguise/PlayerDisguiseManager.java b/Plugins/Mineplex.Core/src/mineplex/core/disguise/playerdisguise/PlayerDisguiseManager.java index 20beec367..fd1b44c80 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/disguise/playerdisguise/PlayerDisguiseManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/disguise/playerdisguise/PlayerDisguiseManager.java @@ -392,8 +392,8 @@ public class PlayerDisguiseManager extends MiniPlugin implements IPacketHandler CoreClient client = getClientManager().Get(caller); client.undisguise(); - require(FriendManager.class).updatePlayerStatus(disguisedProfile.getId().toString(), null); - require(FriendManager.class).updatePlayerStatus(originalProfile.getId().toString(), new PlayerStatus(originalProfile.getId().toString(), originalProfile.getName(), _serverName)); + require(FriendManager.class).updatePlayerStatus(disguisedProfile.getId(), null); + require(FriendManager.class).updatePlayerStatus(originalProfile.getId(), new PlayerStatus(originalProfile.getId(), originalProfile.getName(), _serverName)); getPreferencesManager().handlePlayerJoin(caller, true); require(ScoreboardManager.class).handlePlayerJoin(disguise.getOriginalProfile().getName()); @@ -539,8 +539,8 @@ public class PlayerDisguiseManager extends MiniPlugin implements IPacketHandler callerProfile.getProperties().removeAll(ORIGINAL_UUID_KEY); callerProfile.getProperties().put(ORIGINAL_UUID_KEY, new Property(ORIGINAL_UUID_KEY, caller.getUniqueId().toString())); - require(FriendManager.class).updatePlayerStatus(disguisePlayer.getOriginalProfile().getId().toString(), null); - require(FriendManager.class).updatePlayerStatus(disguisePlayer.getProfile().getId().toString(), new PlayerStatus(disguisePlayer.getProfile().getId().toString(), requestedUsername, _serverName)); + require(FriendManager.class).updatePlayerStatus(disguisePlayer.getOriginalProfile().getId(), null); + require(FriendManager.class).updatePlayerStatus(disguisePlayer.getProfile().getId(), new PlayerStatus(disguisePlayer.getProfile().getId(), requestedUsername, _serverName)); getPreferencesManager().handlePlayerJoin(caller, true); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java index d48fcea85..32effd24c 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/FriendManager.java @@ -1,5 +1,19 @@ package mineplex.core.friend; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.plugin.java.JavaPlugin; + import mineplex.core.MiniDbClientPlugin; import mineplex.core.account.CoreClientManager; import mineplex.core.common.Rank; @@ -22,19 +36,6 @@ import mineplex.core.preferences.PreferencesManager; import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import mineplex.serverdata.data.PlayerStatus; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.plugin.java.JavaPlugin; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.UUID; public class FriendManager extends MiniDbClientPlugin { @@ -402,12 +403,12 @@ public class FriendManager extends MiniDbClientPlugin } - public void updatePlayerStatus(String playerUUID, PlayerStatus status) + public void updatePlayerStatus(UUID playerUUID, PlayerStatus status) { _repository.updatePlayerStatus(playerUUID, status); } - public PlayerStatus getStatus(String playerUUID) + public PlayerStatus getStatus(UUID playerUUID) { return _repository.getStatus(playerUUID); } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java index 91f165215..12432b50e 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendRepository.java @@ -7,6 +7,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.UUID; import mineplex.core.database.MinecraftRepository; import org.bukkit.entity.Player; @@ -95,7 +96,7 @@ public class FriendRepository extends MinecraftRepository friend.Name = resultSet.getString(2); friend.Status = Enum.valueOf(FriendStatusType.class, resultSet.getString(3)); friend.LastSeenOnline = resultSet.getTimestamp(5).getTime() - resultSet.getTimestamp(4).getTime(); - friend.UUID = resultSet.getString(6); + friend.UUID = UUID.fromString(resultSet.getString(6)); if (!friends.containsKey(uuidSource)) friends.put(uuidSource, new FriendData()); @@ -127,7 +128,7 @@ public class FriendRepository extends MinecraftRepository friend.Name = resultSet.getString(1); friend.Status = Enum.valueOf(FriendStatusType.class, resultSet.getString(2)); friend.LastSeenOnline = resultSet.getTimestamp(4).getTime() - resultSet.getTimestamp(3).getTime(); - friend.UUID = resultSet.getString(5); + friend.UUID = UUID.fromString(resultSet.getString(5)); friend.ServerName = null; friend.Online = (friend.ServerName != null); friendData.getFriends().add(friend); @@ -146,17 +147,17 @@ public class FriendRepository extends MinecraftRepository public void loadFriendStatuses(FriendData friendData) { // Generate a set of all friend names - Set friendUUIDS = new HashSet(); + Set friendUUIDS = new HashSet<>(); for(FriendStatus status : friendData.getFriends()) { - friendUUIDS.add(status.UUID); + friendUUIDS.add(status.UUID.toString()); } // Load PlayerStatus' for friends Collection statuses = _repository.getElements(friendUUIDS); // Load player statuses into a mapping - Map playerStatuses = new HashMap(); + Map playerStatuses = new HashMap<>(); for(PlayerStatus status : statuses) { playerStatuses.put(status.getUUID(), status); @@ -176,14 +177,14 @@ public class FriendRepository extends MinecraftRepository * @return the name that the player matching {@code playerName} * is currently online on, if they are online, null otherwise. */ - public String fetchPlayerServer(String playerUUID) + public String fetchPlayerServer(UUID playerUUID) { - PlayerStatus status = _repository.getElement(playerUUID); + PlayerStatus status = _repository.getElement(playerUUID.toString()); return (status == null) ? null : status.getServer(); } - public void updatePlayerStatus(String playerUUID, PlayerStatus status) + public void updatePlayerStatus(UUID playerUUID, PlayerStatus status) { if (status != null) { @@ -191,12 +192,12 @@ public class FriendRepository extends MinecraftRepository } else { - _repository.removeElement(playerUUID); + _repository.removeElement(playerUUID.toString()); } } - public PlayerStatus getStatus(String playerUUID) + public PlayerStatus getStatus(UUID playerUUID) { - return _repository.getElement(playerUUID); + return _repository.getElement(playerUUID.toString()); } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendStatus.java b/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendStatus.java index cabc1c7f7..fa5f10870 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendStatus.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/friend/data/FriendStatus.java @@ -1,11 +1,13 @@ package mineplex.core.friend.data; +import java.util.UUID; + import mineplex.core.friend.FriendStatusType; public class FriendStatus { public String Name; - public String UUID; + public UUID UUID; public String ServerName; public boolean Online; /** diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/PlayerStatus.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/PlayerStatus.java index 44039f23b..ee9eb3eca 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/PlayerStatus.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/PlayerStatus.java @@ -1,12 +1,12 @@ package mineplex.serverdata.data; -import mineplex.serverdata.data.Data; +import java.util.UUID; public class PlayerStatus implements Data { // The uuid of this player. - private String _uuid; - public String getUUID() { return _uuid; } + private UUID _uuid; + public UUID getUUID() { return _uuid; } // The name of this player. private String _name; @@ -21,7 +21,7 @@ public class PlayerStatus implements Data * @param name * @param server */ - public PlayerStatus(String uuid, String name, String server) + public PlayerStatus(UUID uuid, String name, String server) { _uuid = uuid; _name = name; @@ -33,6 +33,6 @@ public class PlayerStatus implements Data */ public String getDataId() { - return _uuid; + return _uuid.toString(); } } \ No newline at end of file From 08d65f36622ccf8626286b7cfeb745dcc6f2c298 Mon Sep 17 00:00:00 2001 From: cnr Date: Fri, 23 Dec 2016 20:27:54 -0700 Subject: [PATCH 21/22] Load communities with 4 queries instead of 3n+1 --- .../storage/CommunityRepository.java | 186 ++++++------------ 1 file changed, 65 insertions(+), 121 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index 6b814c0c9..c8087bf32 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -29,9 +29,9 @@ public class CommunityRepository extends MinecraftRepository private static final String GET_ALL_COMMUNITIES = "SELECT * FROM communities WHERE region=?;"; private static final String GET_COMMUNITY_BY_ID = "SELECT * FROM communities WHERE id=?;"; private static final String GET_COMMUNITY_BY_NAME = "SELECT * FROM communities WHERE name=? AND region=?;"; - private static final String GET_COMMUNITY_MEMBERS = "SELECT cm.accountId, cm.communityRole, ac.name, ac.uuid, ac.lastLogin, cm.readingChat FROM communityMembers cm INNER JOIN accounts ac ON ac.id=cm.accountId WHERE communityId=?;"; - private static final String GET_COMMUNITY_JOIN_REQUESTS = "SELECT cjr.accountId, ac.name, ac.uuid FROM communityJoinRequests cjr INNER JOIN accounts ac ON ac.id=cjr.accountId WHERE communityId=?;"; - private static final String GET_COMMUNITY_SETTINGS = "SELECT settingId, settingValue FROM communitySettings WHERE communityId=?;"; + private static final String GET_COMMUNITY_MEMBERS = "SELECT cm.communityId, cm.accountId, cm.communityRole, ac.name, ac.uuid, ac.lastLogin, cm.readingChat FROM communityMembers cm INNER JOIN accounts ac ON ac.id=cm.accountId;"; + private static final String GET_COMMUNITY_JOIN_REQUESTS = "SELECT cjr.communityId, cjr.accountId, ac.name, ac.uuid FROM communityJoinRequests cjr INNER JOIN accounts ac ON ac.id=cjr.accountId;"; + private static final String GET_COMMUNITY_SETTINGS = "SELECT communityId, settingId, settingValue FROM communitySettings;"; private static final String REMOVE_FROM_COMMUNITY = "DELETE FROM communityMembers WHERE accountId=? AND communityId=?;"; private static final String UPDATE_COMMUNITY_ROLE = "UPDATE communityMembers SET communityRole=? WHERE accountId=? AND communityId=?;"; @@ -57,77 +57,7 @@ public class CommunityRepository extends MinecraftRepository _repo = statusRepo; _us = us; } - - public void loadCommunity(int communityId, final Map communityMap) - { - try (Connection connection = getConnection()) - { - executeQuery(connection, GET_COMMUNITY_BY_ID, resultSet -> - { - if (resultSet.next()) - { - final int id = resultSet.getInt("id"); - final String cName = resultSet.getString("name"); - final Community community = new Community(id, cName); - executeQuery(connection, GET_COMMUNITY_MEMBERS, memberSet -> - { - while (memberSet.next()) - { - final int accountId = memberSet.getInt("accountId"); - final String name = memberSet.getString("name"); - final UUID uuid = UUID.fromString(memberSet.getString("uuid")); - final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); - final long lastLogin = memberSet.getTimestamp("lastLogin").getTime(); - boolean readingChat = memberSet.getBoolean("readingChat"); - CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); - PlayerStatus status = _repo.getElement(name.toLowerCase()); - if (status != null) - { - info.update(lastLogin, true, status.getServer()); - } - info.ReadingChat = readingChat; - community.getMembers().put(info.UUID, info); - } - }, new ColumnInt("communityId", community.getId())); - - executeQuery(connection, GET_COMMUNITY_JOIN_REQUESTS, requestSet -> - { - while (requestSet.next()) - { - final int accountId = requestSet.getInt("accountId"); - final UUID uuid = UUID.fromString(requestSet.getString("uuid")); - final String name = requestSet.getString("name"); - - community.getJoinRequests().put(uuid, new CommunityJoinRequestInfo(name, uuid, accountId)); - } - }, new ColumnInt("communityId", community.getId())); - - executeQuery(connection, GET_COMMUNITY_SETTINGS, settingSet -> - { - while (settingSet.next()) - { - final int settingId = settingSet.getInt("settingId"); - final String value = settingSet.getString("settingValue"); - - CommunitySetting setting = CommunitySetting.getSetting(settingId); - if (setting != null) - { - setting.parseValueInto(value, community); - } - } - }, new ColumnInt("communityId", community.getId())); - - communityMap.put(community.getId(), community); - } - }, new ColumnInt("id", communityId)); - } - catch (SQLException e) - { - e.printStackTrace(); - } - } - public void loadCommunities(final Map communityMap) { try (Connection connection = getConnection()) @@ -140,54 +70,6 @@ public class CommunityRepository extends MinecraftRepository final int id = resultSet.getInt("id"); final String cName = resultSet.getString("name"); final Community community = new Community(id, cName); - executeQuery(connection, GET_COMMUNITY_MEMBERS, memberSet -> - { - while (memberSet.next()) - { - final int accountId = memberSet.getInt("accountId"); - final String name = memberSet.getString("name"); - final UUID uuid = UUID.fromString(memberSet.getString("uuid")); - final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); - final long lastLogin = memberSet.getTimestamp("lastLogin").getTime(); - boolean readingChat = memberSet.getBoolean("readingChat"); - - CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); - PlayerStatus status = _repo.getElement(name.toLowerCase()); - if (status != null) - { - info.update(lastLogin, true, status.getServer()); - } - info.ReadingChat = readingChat; - community.getMembers().put(info.UUID, info); - } - }, new ColumnInt("communityId", community.getId())); - - executeQuery(connection, GET_COMMUNITY_JOIN_REQUESTS, requestSet -> - { - while (requestSet.next()) - { - final int accountId = requestSet.getInt("accountId"); - final UUID uuid = UUID.fromString(requestSet.getString("uuid")); - final String name = requestSet.getString("name"); - - community.getJoinRequests().put(uuid, new CommunityJoinRequestInfo(name, uuid, accountId)); - } - }, new ColumnInt("communityId", community.getId())); - - executeQuery(connection, GET_COMMUNITY_SETTINGS, settingSet -> - { - while (settingSet.next()) - { - final int settingId = settingSet.getInt("settingId"); - final String value = settingSet.getString("settingValue"); - - CommunitySetting setting = CommunitySetting.getSetting(settingId); - if (setting != null) - { - setting.parseValueInto(value, community); - } - } - }, new ColumnInt("communityId", community.getId())); resultant.put(community.getId(), community); } @@ -195,6 +77,68 @@ public class CommunityRepository extends MinecraftRepository communityMap.clear(); communityMap.putAll(resultant); }, new ColumnVarChar("region", 5, _us ? "US" : "EU")); + + executeQuery(connection, GET_COMMUNITY_MEMBERS, memberSet -> + { + while (memberSet.next()) + { + final int communityId = memberSet.getInt("communityId"); + final int accountId = memberSet.getInt("accountId"); + final String name = memberSet.getString("name"); + final UUID uuid = UUID.fromString(memberSet.getString("uuid")); + final CommunityRole role = CommunityRole.parseRole(memberSet.getString("communityRole")); + final long lastLogin = memberSet.getTimestamp("lastLogin").getTime(); + boolean readingChat = memberSet.getBoolean("readingChat"); + + CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); + PlayerStatus status = _repo.getElement(name.toLowerCase()); + if (status != null) + { + info.update(lastLogin, true, status.getServer()); + } + info.ReadingChat = readingChat; + + Community community = communityMap.get(communityId); + if (community != null) + { + community.getMembers().put(info.UUID, info); + } + } + }); + + executeQuery(connection, GET_COMMUNITY_JOIN_REQUESTS, requestSet -> + { + while (requestSet.next()) + { + final int communityId = requestSet.getInt("communityId"); + final int accountId = requestSet.getInt("accountId"); + final UUID uuid = UUID.fromString(requestSet.getString("uuid")); + final String name = requestSet.getString("name"); + + Community community = communityMap.get(communityId); + if (community != null) + { + community.getJoinRequests().put(uuid, new CommunityJoinRequestInfo(name, uuid, accountId)); + } + } + }); + + executeQuery(connection, GET_COMMUNITY_SETTINGS, settingSet -> + { + while (settingSet.next()) + { + final int communityId = settingSet.getInt("communityId"); + final int settingId = settingSet.getInt("settingId"); + final String value = settingSet.getString("settingValue"); + + Community community = communityMap.get(communityId); + CommunitySetting setting = CommunitySetting.getSetting(settingId); + if (community != null && setting != null) + { + setting.parseValueInto(value, community); + } + } + }); } catch (SQLException e) { From 73cefb69c94d755399dc6ce89198ba432346c236 Mon Sep 17 00:00:00 2001 From: cnr Date: Fri, 23 Dec 2016 22:28:27 -0700 Subject: [PATCH 22/22] Use redis pipeline for community player statuses --- .../storage/CommunityRepository.java | 28 ++++++++++------ .../serverdata/data/DataRepository.java | 3 ++ .../serverdata/redis/RedisDataRepository.java | 32 ++++++++++++++++++- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java index c8087bf32..8c86d5b86 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -2,14 +2,17 @@ package mineplex.core.communities.storage; import java.sql.Connection; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.stream.Collectors; import org.bukkit.plugin.java.JavaPlugin; +import mineplex.core.common.timing.TimingManager; import mineplex.core.common.util.Callback; import mineplex.core.communities.Community; import mineplex.core.communities.CommunityJoinRequestInfo; @@ -91,11 +94,6 @@ public class CommunityRepository extends MinecraftRepository boolean readingChat = memberSet.getBoolean("readingChat"); CommunityMemberInfo info = new CommunityMemberInfo(name, uuid, accountId, role, lastLogin); - PlayerStatus status = _repo.getElement(name.toLowerCase()); - if (status != null) - { - info.update(lastLogin, true, status.getServer()); - } info.ReadingChat = readingChat; Community community = communityMap.get(communityId); @@ -152,11 +150,16 @@ public class CommunityRepository extends MinecraftRepository { return; } + TimingManager.start("member elements"); for (Community c : communities) { - for (CommunityMemberInfo info : c.getMembers().values()) + List members = new ArrayList<>(c.getMembers().values()); + List statuses = _repo.getElementsSequential(members.stream().map(info -> info.UUID.toString()).collect(Collectors.toList())); + for (int i = 0; i < members.size(); i++) { - PlayerStatus status = _repo.getElement(info.UUID.toString()); + CommunityMemberInfo info = members.get(i); + PlayerStatus status = statuses.get(i); + boolean online = false; String server = ""; if (status != null) @@ -178,6 +181,7 @@ public class CommunityRepository extends MinecraftRepository } } } + TimingManager.stop("member elements"); } public void updateJoinRequests(LinkedList communities) @@ -186,11 +190,16 @@ public class CommunityRepository extends MinecraftRepository { return; } + TimingManager.start("request elements"); for (Community c : communities) { - for (CommunityJoinRequestInfo info : c.getJoinRequests().values()) + List requests = new ArrayList<>(c.getJoinRequests().values()); + List statuses = _repo.getElementsSequential(requests.stream().map(info -> info.UUID.toString()).collect(Collectors.toList())); + for (int i = 0; i < requests.size(); i++) { - PlayerStatus status = _repo.getElement(info.UUID.toString()); + CommunityJoinRequestInfo info = requests.get(i); + PlayerStatus status = statuses.get(i); + if (status != null) { if (!info.Name.equals(status.getName())) @@ -200,6 +209,7 @@ public class CommunityRepository extends MinecraftRepository } } } + TimingManager.stop("request elements"); } public void loadInvites(int accountId, List invites) diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/DataRepository.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/DataRepository.java index fb9f9e7c4..cdac8d117 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/DataRepository.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/data/DataRepository.java @@ -1,6 +1,7 @@ package mineplex.serverdata.data; import java.util.Collection; +import java.util.List; /** * DataRepository is used to store {@link Data} objects in a central database @@ -18,6 +19,8 @@ public interface DataRepository public Collection getElements(Collection dataIds); + public List getElementsSequential(List dataIds); + public void addElement(T element, int timeout); public void addElement(T element); diff --git a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java index 5ccfd291a..46e74fc6b 100644 --- a/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java +++ b/Plugins/Mineplex.ServerData/src/mineplex/serverdata/redis/RedisDataRepository.java @@ -102,7 +102,37 @@ public class RedisDataRepository extends RedisRepository impleme return elements; } - + + @Override + public List getElementsSequential(List dataIds) + { + List elements = new ArrayList<>(); + + try(Jedis jedis = getResource(false)) + { + Pipeline pipeline = jedis.pipelined(); + + List> responses = new ArrayList<>(); + for (String dataId : dataIds) + { + responses.add(pipeline.get(generateKey(dataId))); + } + + // Block until all requests have received pipelined responses + pipeline.sync(); + + for (Response response : responses) + { + String serializedData = response.get(); + T element = deserialize(serializedData); + + elements.add(element); + } + } + + return elements; + } + @Override public T getElement(String dataId) {