diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java index 21aef2231..286b07471 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityManager.java @@ -82,6 +82,7 @@ import mineplex.core.preferences.Preference; import mineplex.core.preferences.PreferencesManager; import mineplex.core.recharge.Recharge; import mineplex.core.serverConfig.ServerConfiguration; +import mineplex.core.treasure.animation.animations.reward.CommonRewardAnimation; import mineplex.serverdata.Region; import mineplex.serverdata.commands.ServerCommandManager; import mineplex.serverdata.data.PlayerStatus; @@ -165,7 +166,7 @@ public class CommunityManager extends MiniDbClientPlugin return _prefManager; } - private CustomDataManager getCustomDataManager() + public CustomDataManager getCustomDataManager() { if (_customDataManager == null) { @@ -219,7 +220,7 @@ public class CommunityManager extends MiniDbClientPlugin if (!data.Invites.isEmpty()) { - loadBrowserCommunities(data.Invites, null); + runAsync(() -> loadBrowserCommunities(data.Invites, null)); } } @@ -400,7 +401,22 @@ public class CommunityManager extends MiniDbClientPlugin return; } - runAsync(() -> consumer.accept(_repo.loadCommunity(new HashMap<>(1), id))); + runAsync(() -> + { + try + { + final Map store = new HashMap<>(); + _repo.loadCommunity(store, id); + + if (!store.isEmpty()) + { + runSync(() -> consumer.accept(store.get(id))); + } + } catch (Exception ex) + { + ex.printStackTrace(); + } + }); } public ICommunity getBrowserCommunity(int id) @@ -423,20 +439,30 @@ public class CommunityManager extends MiniDbClientPlugin { return _loadedCommunities.get(id); } - + + public ICommunity getCommunity(String name) + { + return getByName(_loadedCommunities, name, getByName(_browserCommunities, name, null)); + } + public Community getLoadedCommunity(String name) { - for (Entry entry : _loadedCommunities.entrySet()) + return (Community) getByName(_loadedCommunities, name, null); + } + + private ICommunity getByName(Map map, String name, ICommunity def) + { + for (Entry entry : map.entrySet()) { if (entry.getValue().getName().equalsIgnoreCase(name)) { return entry.getValue(); } } - - return null; + + return def; } - + public void handleCommunitySettingUpdate(Integer id, String sender, CommunitySetting setting, String newValue) { Community community = _loadedCommunities.get(id); @@ -955,8 +981,12 @@ public class CommunityManager extends MiniDbClientPlugin if (!load.isEmpty()) { _browserCommunities.keySet().removeAll(load); - _repo.handlePlayerJoin(_loadedCommunities, load, accountId); - System.out.println("Loaded communities: " + load + "; Total: " + _loadedCommunities.size()); + + runAsync(() -> + { + _repo.handlePlayerJoin(_loadedCommunities, load, accountId); + System.out.println("Loaded communities: " + load + "; Total: " + _loadedCommunities.size()); + }); } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityRepository.java index 40019631c..b69ad0272 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/CommunityRepository.java @@ -12,8 +12,6 @@ import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.bukkit.plugin.java.JavaPlugin; - import mineplex.core.common.timing.TimingManager; import mineplex.core.common.util.Callback; import mineplex.core.communities.data.BrowserCommunity; @@ -38,7 +36,7 @@ public class CommunityRepository extends RepositoryBase { private static final String GET_COMMUNITIES_BY_ID = "SELECT * FROM communities"; private static final String GET_COMMUNITY_MEMBERS = "SELECT cm.communityId, cm.accountId, cm.communityRole, ac.name, ac.uuid, ac.lastLogin, cm.readingChat FROM communityMembers cm INNER JOIN accounts ac ON ac.id=cm.accountId"; - private static final String GET_COMMUNITY_SIZE = "SELECT COUNT(accountId) AS total, communityId FROM communityMembers"; + private static final String GET_COMMUNITY_SIZE = "SELECT communityId FROM communityMembers"; private static final String GET_COMMUNITY_JOIN_REQUESTS = "SELECT cjr.communityId, cjr.accountId, ac.name, ac.uuid FROM communityJoinRequests cjr INNER JOIN accounts ac ON ac.id=cjr.accountId WHERE cjr.accountId=?;"; private static final String GET_COMMUNITY_SETTINGS = "SELECT communityId, settingId, settingValue FROM communitySettings"; private static final String GET_PUBLIC_COMMUNITIES = "SELECT communityId FROM communitySettings WHERE settingId=8 AND settingValue='true';"; @@ -134,19 +132,19 @@ public class CommunityRepository extends RepositoryBase { while (resultSet.next()) { - int members = resultSet.getInt("total"); - int id = resultSet.getInt("communityId"); - - BrowserCommunity com = store.get(id); + int communityId = resultSet.getInt("communityId"); + BrowserCommunity com = store.get(communityId); if (com != null) { - com.setMembers(members); + com.addMember(); } } }, idColumns); idColumns = genIdColumns("communityId", load); - executeQuery(connection, GET_COMMUNITY_SETTINGS + inClause.replace("%col", "communityId"), settingSet -> + executeQuery(connection, GET_COMMUNITY_SETTINGS + + inClause.replace("%col", "communityId").replace(";", "") + + " AND (settingId=5 OR settingId=6 OR settingId=7);", settingSet -> { while (settingSet.next()) { @@ -177,9 +175,9 @@ public class CommunityRepository extends RepositoryBase /** * Loads and stores a single community. */ - public Community loadCommunity(final Map store, final int id) + public void loadCommunity(final Map store, final int id) { - return loadInternal(store, Collections.singletonList(id), -1).get(0); + loadInternal(store, Collections.singletonList(id), -1); } /** diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCommand.java index 93ed59351..efb367fb0 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityCommand.java @@ -49,6 +49,7 @@ public class CommunityCommand extends MultiCommandBase UtilPlayer.message(caller, F.help("/com disband ", "Disbands a community you own", ChatColor.DARK_AQUA)); return; } + Community community = Plugin.getLoadedCommunity(args[0]); if (community == null) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityJoinCommand.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityJoinCommand.java index 53728d919..a7bae022f 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityJoinCommand.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/commands/CommunityJoinCommand.java @@ -9,6 +9,7 @@ import mineplex.core.common.util.UtilPlayer; import mineplex.core.communities.data.Community; import mineplex.core.communities.data.Community.PrivacySetting; import mineplex.core.communities.CommunityManager; +import mineplex.core.communities.data.ICommunity; public class CommunityJoinCommand extends CommandBase { @@ -25,22 +26,27 @@ public class CommunityJoinCommand extends CommandBase UtilPlayer.message(caller, F.help("/com join ", "Joins a community that is open or you have been invited to", ChatColor.DARK_AQUA)); return; } - Community c = Plugin.getLoadedCommunity(args[0]); + + ICommunity c = Plugin.getCommunity(args[0]); if (c == null) { UtilPlayer.message(caller, F.main(Plugin.getName(), "That community was not found!")); return; } - if (c.getMembers().containsKey(caller.getUniqueId())) - { - UtilPlayer.message(caller, F.main(Plugin.getName(), "You are already in " + F.name(c.getName()) + "!")); - return; - } + + // edge case someone can try if they really want: open communities with less than 5 members are forgotten here if (c.getPrivacySetting() != PrivacySetting.OPEN && !Plugin.Get(caller).Invites.contains(c.getId())) { UtilPlayer.message(caller, F.main(Plugin.getName(), "You are have not been invited to " + F.name(c.getName()) + "!")); return; } + + if (c instanceof Community && ((Community) c).getMembers().containsKey(caller.getUniqueId())) + { + UtilPlayer.message(caller, F.main(Plugin.getName(), "You are already in " + F.name(c.getName()) + "!")); + return; + } + Plugin.handleJoin(caller, c, Plugin.Get(caller).Invites.contains(c.getId())); } } \ No newline at end of file diff --git a/Plugins/Mineplex.Core/src/mineplex/core/communities/data/BrowserCommunity.java b/Plugins/Mineplex.Core/src/mineplex/core/communities/data/BrowserCommunity.java index c071c4b60..19ad38e0e 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/communities/data/BrowserCommunity.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/communities/data/BrowserCommunity.java @@ -64,6 +64,11 @@ public class BrowserCommunity implements ICommunity return this; } + public void addMember() + { + _members++; + } + public BrowserCommunity setDescription(String description) { _description = Preconditions.checkNotNull(description);