From 74a6071d4ec377d446c795ff1d5858f10424f024 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Fri, 23 Dec 2016 18:01:52 -0500 Subject: [PATCH] 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