diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java index 86406e7ba..3b33cad86 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java @@ -21,6 +21,8 @@ public class Community private long _chatDelay; private GameDisplay _favoriteGame; private PrivacySetting _privacy; + + private transient boolean unloaded = false; public Community(int id, String name) { @@ -122,6 +124,16 @@ public class Community { getMembers().values().stream().filter(member -> member.Role.ordinal() <= minimumRole.ordinal()).forEach(member -> UtilPlayer.message(Bukkit.getPlayer(member.UUID), message)); } + + public void markUnloaded() + { + this.unloaded = true; + } + + public boolean isUnloaded() + { + return unloaded; + } public static enum PrivacySetting { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index 68bd4bf44..b1342afad 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -112,6 +112,7 @@ public class CommunityManager extends MiniDbClientPlugin private boolean _us; private final Set dirty = Collections.newSetFromMap(new ConcurrentHashMap<>()); // Communities with redis updates + private int _updateCycleCount; // The number of update cycles since we've updated all communities private volatile boolean _cycling = false; @@ -174,6 +175,9 @@ public class CommunityManager extends MiniDbClientPlugin { // It's been five minutes since a full update; update all communities _updateCycleCount = 0; + + // Make sure to include communities that should be unloaded after their update + dirty.stream().filter(Community::isUnloaded).forEach(communities::add); dirty.clear(); communities.addAll(_loadedCommunities.values()); @@ -843,10 +847,14 @@ public class CommunityManager extends MiniDbClientPlugin final int accountId = _clientManager.getAccountId(player); - runAsync(() -> _repo.loadCommunities(_loadedCommunities, load, accountId)); + + runAsync(() -> { + _repo.loadCommunities(_loadedCommunities, load, accountId); + System.out.println("Loaded communities: " + load + "; Total: " + _loadedCommunities.size()); + }); } - @EventHandler(priority = EventPriority.MONITOR) + @EventHandler(priority = EventPriority.LOW) public void onPlayerQuit(PlayerQuitEvent event) { // Remove their communities from memory if they're the last player @@ -856,14 +864,15 @@ public class CommunityManager extends MiniDbClientPlugin { for (UUID uuid : community.getMembers().keySet()) { - // See if there's anyone else online besides our quitting player + // See if there's anyone else online other than our quitting player if (!player.getUniqueId().equals(uuid) && Bukkit.getPlayer(uuid) != null) - { - break com; - } + continue com; } + System.out.println("Unloading community: " + community.getId()); + // Unload this community from memory + community.markUnloaded(); _loadedCommunities.remove(community.getId()); } } 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 4a01d0c7f..93a06a44c 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -65,14 +65,18 @@ public class CommunityRepository extends RepositoryBase _us = us; } - // TODO this could probably be further optimized, either way it needs to be tested + private ColumnInt[] genIdColumns(String colName, List nums) + { + return nums.stream().map(i -> new ColumnInt(colName, i)).toArray(ColumnInt[]::new); + } + public void loadCommunities(final Map store, final List load, final int accountId) { try (Connection connection = getConnection()) { // Only load the info for communities with the given IDs StringBuilder builder = new StringBuilder(); - builder.append(" WHERE id IN ("); + builder.append(" WHERE %col IN ("); for (int index = 0; index < load.size(); index++) { @@ -84,18 +88,22 @@ public class CommunityRepository extends RepositoryBase builder.append(");"); String inClause = builder.toString(); - ColumnInt[] idColumns = load.stream().map(i -> new ColumnInt("id", i)).toArray(x -> new ColumnInt[0]); + ColumnInt[] idColumns = genIdColumns("id", load); - executeQuery(connection, GET_COMMUNITIES_BY_ID + inClause, resultSet -> + executeQuery(connection, GET_COMMUNITIES_BY_ID + inClause.replace("%col", "id"), resultSet -> { - final int id = resultSet.getInt("id"); - final String cName = resultSet.getString("name"); - final Community community = new Community(id, cName); + while (resultSet.next()) + { + final int id = resultSet.getInt("id"); + final String cName = resultSet.getString("name"); + final Community community = new Community(id, cName); - store.put(id, community); + store.put(id, community); + } }, idColumns); - executeQuery(connection, GET_COMMUNITY_MEMBERS + inClause, memberSet -> + idColumns = genIdColumns("cm.communityId", load); + executeQuery(connection, GET_COMMUNITY_MEMBERS + inClause.replace("%col", "cm.communityId"), memberSet -> { while (memberSet.next()) { @@ -135,7 +143,8 @@ public class CommunityRepository extends RepositoryBase } }, new ColumnInt("cjr.accountId", accountId)); - executeQuery(connection, GET_COMMUNITY_SETTINGS + inClause, settingSet -> + idColumns = genIdColumns("communityId", load); + executeQuery(connection, GET_COMMUNITY_SETTINGS + inClause.replace("%col", "communityId"), settingSet -> { while (settingSet.next()) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/monitor/LagMeter.java b/Plugins/Mineplex.Core/src/mineplex/core/monitor/LagMeter.java index 41d9ceb15..061b902de 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/monitor/LagMeter.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/monitor/LagMeter.java @@ -150,43 +150,36 @@ public class LagMeter extends MiniPlugin private void sendUpdate(Player player) { - double[] tps = MinecraftServer.getServer().recentTps; - String mcString = ""; - for (int i = 0; i < tps.length; i++) - { - mcString += getPrefix(tps[i]) + (double)Math.round(tps[i] * 100.0D) / 100.0D; - if (i < tps.length - 1) mcString += C.cWhite + ", "; - } - - player.sendMessage(" "); - player.sendMessage(" "); - player.sendMessage(" "); - player.sendMessage(" "); - player.sendMessage(" "); - player.sendMessage(F.main(getName(), ChatColor.GRAY + "Live-------" + ChatColor.YELLOW + String.format("%.00f", _ticksPerSecond))); - player.sendMessage(F.main(getName(), ChatColor.GRAY + "Avg--------" + ChatColor.YELLOW + String.format("%.00f", _ticksPerSecondAverage * 20))); - player.sendMessage(F.main(getName(), ChatColor.YELLOW + "MC Timings (5,10,15 min avg)")); - player.sendMessage(F.main(getName(), ChatColor.GRAY + mcString)); - player.sendMessage(F.main(getName(), ChatColor.YELLOW + "MEM")); - player.sendMessage(F.main(getName(), ChatColor.GRAY + "Free-------" + ChatColor.YELLOW + (Runtime.getRuntime().freeMemory() / 1048576) + "MB")); - player.sendMessage(F.main(getName(), ChatColor.GRAY + "Max--------" + ChatColor.YELLOW + (Runtime.getRuntime().maxMemory() / 1048576)) + "MB"); - - // Statistics for Dan; should be temporary - if (_clientManager.Get(player).getPrimaryGroup().inheritsFrom(PermissionGroup.DEV)) + double[] tps = MinecraftServer.getServer().recentTps; + StringBuilder mcString = new StringBuilder(); + for (int i = 0; i < tps.length; i++) { - player.sendMessage(" "); - player.sendMessage(F.main(getName(), ChatColor.GRAY + "Dev Stats -----")); - - player.sendMessage(F.main(getName(), - ChatColor.YELLOW + String.valueOf(player.getWorld().getLoadedChunks().length) + ChatColor.GRAY + - " chunks loaded")); - - if (_communities == null) - _communities = Managers.get(CommunityManager.class); - - player.sendMessage(F.main(getName(), - ChatColor.YELLOW + String.valueOf(_communities.getCount()) + ChatColor.GRAY + - " communities loaded")); + mcString.append(getPrefix(tps[i])).append((double) Math.round(tps[i] * 100.0D) / 100.0D); + if (i < tps.length - 1) mcString.append(C.cWhite).append(", "); } + + player.sendMessage(" "); + player.sendMessage(" "); + player.sendMessage(" "); + player.sendMessage(" "); + player.sendMessage(" "); + player.sendMessage(F.main(getName(), ChatColor.GRAY + "Live-------" + ChatColor.YELLOW + String.format("%.00f", _ticksPerSecond))); + player.sendMessage(F.main(getName(), ChatColor.GRAY + "Avg--------" + ChatColor.YELLOW + String.format("%.00f", _ticksPerSecondAverage * 20))); + player.sendMessage(F.main(getName(), ChatColor.YELLOW + "MC Timings (5,10,15 min avg)")); + player.sendMessage(F.main(getName(), ChatColor.GRAY + mcString.toString())); + player.sendMessage(F.main(getName(), ChatColor.YELLOW + "MEM")); + player.sendMessage(F.main(getName(), ChatColor.GRAY + "Free-------" + ChatColor.YELLOW + (Runtime.getRuntime().freeMemory() / 1048576) + "MB")); + player.sendMessage(F.main(getName(), ChatColor.GRAY + "Max--------" + ChatColor.YELLOW + (Runtime.getRuntime().maxMemory() / 1048576)) + "MB"); + + // Statistics for Dan, ideally this'll be temporary + player.sendMessage(" "); + player.sendMessage(F.main(getName(), ChatColor.GRAY + "Temp Stats -----")); + + player.sendMessage(F.main(getName(), ChatColor.YELLOW + String.valueOf(player.getWorld().getLoadedChunks().length) + ChatColor.GRAY + " chunks loaded")); + + if (_communities == null) + _communities = Managers.get(CommunityManager.class); + + player.sendMessage(F.main(getName(), ChatColor.YELLOW + String.valueOf(_communities.getCount()) + ChatColor.GRAY + " communities loaded")); } } \ No newline at end of file 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 5021741d3..757831758 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 @@ -456,7 +456,7 @@ public class ClansManager extends MiniClientPlugin implements IRelat _bannerManager.loadBanners(this); new PersonalServerManager(plugin, _clientManager).setUseInterfaceItem(false); - new CommunityManager(plugin, _clientManager); + new CommunityManager(); Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "Replay|Restrict"); diff --git a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/HubManager.java b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/HubManager.java index 315422a8e..df34cc2e8 100644 --- a/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/HubManager.java +++ b/Plugins/Mineplex.Hub.Clans/src/mineplex/clanshub/HubManager.java @@ -239,7 +239,7 @@ public class HubManager extends MiniPlugin implements IChatMessageFormatter new SalesAnnouncementManager(plugin); new PersonalServerManager(plugin, _clientManager).setUseInterfaceItem(false); - new CommunityManager(plugin, _clientManager); + new CommunityManager(); require(TabListSorter.class); ScoreboardManager scoreboardManager = new ScoreboardManager(plugin) { diff --git a/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java b/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java index 089f551db..01b96d08f 100644 --- a/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java +++ b/Plugins/Mineplex.Hub/src/mineplex/hub/HubManager.java @@ -214,7 +214,7 @@ public class HubManager extends MiniClientPlugin implements IChatMess new SalesAnnouncementManager(_plugin); - new CommunityManager(_plugin, _clientManager); + new CommunityManager(); _hologramManager = hologramManager; 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 8f3735bce..aa5949c86 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/ArcadeManager.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/ArcadeManager.java @@ -408,7 +408,7 @@ public class ArcadeManager extends MiniPlugin implements IRelation addCommand(new TauntCommand(this)); new PersonalServerManager(plugin, _clientManager).setUseInterfaceItem(false); - new CommunityManager(plugin, _clientManager); + new CommunityManager(); _scoreboardManager = new ScoreboardManager(_plugin) { diff --git a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/GemHunters.java b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/GemHunters.java index 4a75d23b4..5e483022d 100644 --- a/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/GemHunters.java +++ b/Plugins/mineplex-game-gemhunters/src/mineplex/gemhunters/GemHunters.java @@ -217,7 +217,7 @@ public class GemHunters extends JavaPlugin new PartyManager(); // Communities - new CommunityManager(this, clientManager); + new CommunityManager(); // Fixes new MemoryFix(this);