Fix a few issues with the community changes

This commit is contained in:
Dan Mulloy 2017-12-11 17:55:51 -05:00 committed by Alexander Meech
parent 482e603af6
commit 8eb8ebf504
9 changed files with 80 additions and 57 deletions

View File

@ -21,6 +21,8 @@ public class Community
private long _chatDelay; private long _chatDelay;
private GameDisplay _favoriteGame; private GameDisplay _favoriteGame;
private PrivacySetting _privacy; private PrivacySetting _privacy;
private transient boolean unloaded = false;
public Community(int id, String name) 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)); 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 public static enum PrivacySetting
{ {

View File

@ -112,6 +112,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
private boolean _us; private boolean _us;
private final Set<Community> dirty = Collections.newSetFromMap(new ConcurrentHashMap<>()); // Communities with redis updates private final Set<Community> dirty = Collections.newSetFromMap(new ConcurrentHashMap<>()); // Communities with redis updates
private int _updateCycleCount; // The number of update cycles since we've updated all communities private int _updateCycleCount; // The number of update cycles since we've updated all communities
private volatile boolean _cycling = false; private volatile boolean _cycling = false;
@ -174,6 +175,9 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
{ {
// It's been five minutes since a full update; update all communities // It's been five minutes since a full update; update all communities
_updateCycleCount = 0; _updateCycleCount = 0;
// Make sure to include communities that should be unloaded after their update
dirty.stream().filter(Community::isUnloaded).forEach(communities::add);
dirty.clear(); dirty.clear();
communities.addAll(_loadedCommunities.values()); communities.addAll(_loadedCommunities.values());
@ -843,10 +847,14 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
final int accountId = _clientManager.getAccountId(player); 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) public void onPlayerQuit(PlayerQuitEvent event)
{ {
// Remove their communities from memory if they're the last player // Remove their communities from memory if they're the last player
@ -856,14 +864,15 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
{ {
for (UUID uuid : community.getMembers().keySet()) 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) if (!player.getUniqueId().equals(uuid) && Bukkit.getPlayer(uuid) != null)
{ continue com;
break com;
}
} }
System.out.println("Unloading community: " + community.getId());
// Unload this community from memory // Unload this community from memory
community.markUnloaded();
_loadedCommunities.remove(community.getId()); _loadedCommunities.remove(community.getId());
} }
} }

View File

@ -65,14 +65,18 @@ public class CommunityRepository extends RepositoryBase
_us = us; _us = us;
} }
// TODO this could probably be further optimized, either way it needs to be tested private ColumnInt[] genIdColumns(String colName, List<Integer> nums)
{
return nums.stream().map(i -> new ColumnInt(colName, i)).toArray(ColumnInt[]::new);
}
public void loadCommunities(final Map<Integer, Community> store, final List<Integer> load, final int accountId) public void loadCommunities(final Map<Integer, Community> store, final List<Integer> load, final int accountId)
{ {
try (Connection connection = getConnection()) try (Connection connection = getConnection())
{ {
// Only load the info for communities with the given IDs // Only load the info for communities with the given IDs
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append(" WHERE id IN ("); builder.append(" WHERE %col IN (");
for (int index = 0; index < load.size(); index++) for (int index = 0; index < load.size(); index++)
{ {
@ -84,18 +88,22 @@ public class CommunityRepository extends RepositoryBase
builder.append(");"); builder.append(");");
String inClause = builder.toString(); 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"); while (resultSet.next())
final String cName = resultSet.getString("name"); {
final Community community = new Community(id, cName); 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); }, 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()) while (memberSet.next())
{ {
@ -135,7 +143,8 @@ public class CommunityRepository extends RepositoryBase
} }
}, new ColumnInt("cjr.accountId", accountId)); }, 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()) while (settingSet.next())
{ {

View File

@ -150,43 +150,36 @@ public class LagMeter extends MiniPlugin
private void sendUpdate(Player player) private void sendUpdate(Player player)
{ {
double[] tps = MinecraftServer.getServer().recentTps; double[] tps = MinecraftServer.getServer().recentTps;
String mcString = ""; StringBuilder mcString = new StringBuilder();
for (int i = 0; i < tps.length; i++) 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))
{ {
player.sendMessage(" "); mcString.append(getPrefix(tps[i])).append((double) Math.round(tps[i] * 100.0D) / 100.0D);
player.sendMessage(F.main(getName(), ChatColor.GRAY + "Dev Stats -----")); if (i < tps.length - 1) mcString.append(C.cWhite).append(", ");
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"));
} }
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"));
} }
} }

View File

@ -456,7 +456,7 @@ public class ClansManager extends MiniClientPlugin<ClientClan> implements IRelat
_bannerManager.loadBanners(this); _bannerManager.loadBanners(this);
new PersonalServerManager(plugin, _clientManager).setUseInterfaceItem(false); new PersonalServerManager(plugin, _clientManager).setUseInterfaceItem(false);
new CommunityManager(plugin, _clientManager); new CommunityManager();
Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "Replay|Restrict"); Bukkit.getMessenger().registerOutgoingPluginChannel(plugin, "Replay|Restrict");

View File

@ -239,7 +239,7 @@ public class HubManager extends MiniPlugin implements IChatMessageFormatter
new SalesAnnouncementManager(plugin); new SalesAnnouncementManager(plugin);
new PersonalServerManager(plugin, _clientManager).setUseInterfaceItem(false); new PersonalServerManager(plugin, _clientManager).setUseInterfaceItem(false);
new CommunityManager(plugin, _clientManager); new CommunityManager();
require(TabListSorter.class); require(TabListSorter.class);
ScoreboardManager scoreboardManager = new ScoreboardManager(plugin) ScoreboardManager scoreboardManager = new ScoreboardManager(plugin)
{ {

View File

@ -214,7 +214,7 @@ public class HubManager extends MiniClientPlugin<HubClient> implements IChatMess
new SalesAnnouncementManager(_plugin); new SalesAnnouncementManager(_plugin);
new CommunityManager(_plugin, _clientManager); new CommunityManager();
_hologramManager = hologramManager; _hologramManager = hologramManager;

View File

@ -408,7 +408,7 @@ public class ArcadeManager extends MiniPlugin implements IRelation
addCommand(new TauntCommand(this)); addCommand(new TauntCommand(this));
new PersonalServerManager(plugin, _clientManager).setUseInterfaceItem(false); new PersonalServerManager(plugin, _clientManager).setUseInterfaceItem(false);
new CommunityManager(plugin, _clientManager); new CommunityManager();
_scoreboardManager = new ScoreboardManager(_plugin) _scoreboardManager = new ScoreboardManager(_plugin)
{ {

View File

@ -217,7 +217,7 @@ public class GemHunters extends JavaPlugin
new PartyManager(); new PartyManager();
// Communities // Communities
new CommunityManager(this, clientManager); new CommunityManager();
// Fixes // Fixes
new MemoryFix(this); new MemoryFix(this);