diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java index 3b33cad86..0136473fc 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/Community.java @@ -23,6 +23,7 @@ public class Community private PrivacySetting _privacy; private transient boolean unloaded = false; + private transient boolean persist = false; public Community(int id, String name) { @@ -134,6 +135,16 @@ public class Community { return unloaded; } + + public void persist() + { + this.persist = persist; + } + + public boolean isPersistent() + { + return persist; + } 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 5b16459f1..f9aca1fa2 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -8,6 +8,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -65,10 +66,12 @@ import mineplex.core.communities.storage.CommunityRepository; import mineplex.core.preferences.Preference; import mineplex.core.preferences.PreferencesManager; import mineplex.core.recharge.Recharge; +import mineplex.core.serverConfig.ServerConfiguration; import mineplex.serverdata.Region; import mineplex.serverdata.commands.ServerCommandManager; import mineplex.serverdata.data.DataRepository; import mineplex.serverdata.data.PlayerStatus; +import mineplex.serverdata.data.ServerGroup; import mineplex.serverdata.redis.RedisDataRepository; import mineplex.serverdata.servers.ServerManager; @@ -193,7 +196,7 @@ public class CommunityManager extends MiniDbClientPlugin Bukkit.getScheduler().scheduleSyncRepeatingTask(_plugin, this::cycleBrowser, 0L, 20 * 30); - // _repo.loadCommunities(_loadedCommunities); + // _repo.handlePlayerJoin(_loadedCommunities); addCommand(new CommunityCommand(this)); @@ -209,13 +212,24 @@ 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)); - + + // Load and keep community for MCS + ServerGroup group = require(ServerConfiguration.class).getServerGroup(); + if (group.getName().startsWith("COM-")) + { + int comId = Integer.parseInt(group.getName().split("-")[1]); + Community community = getLoadedCommunity(comId); + if (community == null) + community = _repo.loadCommunity(_loadedCommunities, comId); + + community.persist(); + } + generatePermissions(); } - + private void generatePermissions() { - PermissionGroup.ETERNAL.setPermission(Perm.OWN_COMMUNITY, true, true); PermissionGroup.PLAYER.setPermission(Perm.COMMUNITY_CHAT_COMMAND, true, true); PermissionGroup.PLAYER.setPermission(Perm.COMMUNITY_COMMAND, true, true); @@ -849,7 +863,7 @@ public class CommunityManager extends MiniDbClientPlugin runAsync(() -> { - _repo.loadCommunities(_loadedCommunities, load, accountId); + _repo.handlePlayerJoin(_loadedCommunities, load, accountId); System.out.println("Loaded communities: " + load + "; Total: " + _loadedCommunities.size()); }); } @@ -860,14 +874,11 @@ public class CommunityManager extends MiniDbClientPlugin // Remove their communities from memory if they're the last player Player player = event.getPlayer(); List communities = Get(player).getCommunities(); - com: for (Community community : communities) + for (Community community : communities) { - for (UUID uuid : community.getMembers().keySet()) - { - // See if there's anyone else online other than our quitting player - if (!player.getUniqueId().equals(uuid) && Bukkit.getPlayer(uuid) != null) - continue com; - } + if (community.isPersistent() + || community.getMembers().keySet().stream().anyMatch(uuid -> !player.getUniqueId().equals(uuid) && Bukkit.getPlayer(uuid) != null)) + continue; System.out.println("Unloading community: " + 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 93a06a44c..0274014e4 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/storage/CommunityRepository.java @@ -2,11 +2,10 @@ package mineplex.core.communities.storage; import java.sql.Connection; import java.sql.SQLException; -import java.util.HashMap; +import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -70,7 +69,18 @@ public class CommunityRepository extends RepositoryBase return nums.stream().map(i -> new ColumnInt(colName, i)).toArray(ColumnInt[]::new); } - public void loadCommunities(final Map store, final List load, final int accountId) + public Community loadCommunity(final Map store, final int id) + { + loadInternal(store, Collections.singletonList(id), -1); + return store.get(id); + } + + public void handlePlayerJoin(final Map store, final List load, final int accountId) + { + loadInternal(store, load, accountId); + } + + private void loadInternal(final Map store, final List load, final int accountId) { try (Connection connection = getConnection()) { @@ -126,22 +136,25 @@ public class CommunityRepository extends RepositoryBase } }, idColumns); - executeQuery(connection, GET_COMMUNITY_JOIN_REQUESTS, requestSet -> + if (accountId != -1) { - while (requestSet.next()) + executeQuery(connection, GET_COMMUNITY_JOIN_REQUESTS, requestSet -> { - 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 = store.get(communityId); - if (community != null) + while (requestSet.next()) { - community.getJoinRequests().put(uuid, new CommunityJoinRequestInfo(name, uuid, accountId)); + 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 = store.get(communityId); + if (community != null) + { + community.getJoinRequests().put(uuid, new CommunityJoinRequestInfo(name, uuid, accountId)); + } } - } - }, new ColumnInt("cjr.accountId", accountId)); + }, new ColumnInt("cjr.accountId", accountId)); + } idColumns = genIdColumns("communityId", load); executeQuery(connection, GET_COMMUNITY_SETTINGS + inClause.replace("%col", "communityId"), settingSet ->