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