Ensure the community is always loaded for MCS

This commit is contained in:
Dan Mulloy 2018-01-11 16:46:16 -05:00 committed by Alexander Meech
parent 48d705290b
commit c5f29533b7
3 changed files with 62 additions and 27 deletions

View File

@ -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)
{
@ -135,6 +136,16 @@ public class Community
return unloaded;
}
public void persist()
{
this.persist = persist;
}
public boolean isPersistent()
{
return persist;
}
public static enum PrivacySetting
{
OPEN("Open to Join"),

View File

@ -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<CommunityMemberData>
Bukkit.getScheduler().scheduleSyncRepeatingTask(_plugin, this::cycleBrowser, 0L, 20 * 30);
// _repo.loadCommunities(_loadedCommunities);
// _repo.handlePlayerJoin(_loadedCommunities);
addCommand(new CommunityCommand(this));
@ -210,12 +213,23 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
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<CommunityMemberData>
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<CommunityMemberData>
// Remove their communities from memory if they're the last player
Player player = event.getPlayer();
List<Community> 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());

View File

@ -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<Integer, Community> store, final List<Integer> load, final int accountId)
public Community loadCommunity(final Map<Integer, Community> store, final int id)
{
loadInternal(store, Collections.singletonList(id), -1);
return store.get(id);
}
public void handlePlayerJoin(final Map<Integer, Community> store, final List<Integer> load, final int accountId)
{
loadInternal(store, load, accountId);
}
private void loadInternal(final Map<Integer, Community> store, final List<Integer> 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 ->