Fix various synchronization issues and make community creation load the new community through redis instead of a database call

This commit is contained in:
AlexTheCoder 2016-12-22 21:35:46 -05:00 committed by cnr
parent 66c6f66768
commit 286a9fe246
6 changed files with 69 additions and 40 deletions

View File

@ -1,8 +1,8 @@
package mineplex.core.communities; package mineplex.core.communities;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -15,8 +15,8 @@ public class Community
private final int _id; private final int _id;
private String _name; private String _name;
private String _description; private String _description;
private Map<UUID, CommunityMemberInfo> _members = new HashMap<>(); private Map<UUID, CommunityMemberInfo> _members = new ConcurrentHashMap<>();
private Map<UUID, CommunityJoinRequestInfo> _joinRequests = new HashMap<>(); private Map<UUID, CommunityJoinRequestInfo> _joinRequests = new ConcurrentHashMap<>();
private ChatColor[] _chatFormat; private ChatColor[] _chatFormat;
private long _chatDelay; private long _chatDelay;
private GameDisplay _favoriteGame; private GameDisplay _favoriteGame;

View File

@ -3,6 +3,7 @@ package mineplex.core.communities;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -20,8 +21,6 @@ import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.google.common.collect.Lists;
import mineplex.core.Managers; import mineplex.core.Managers;
import mineplex.core.MiniDbClientPlugin; import mineplex.core.MiniDbClientPlugin;
import mineplex.core.account.CoreClientManager; import mineplex.core.account.CoreClientManager;
@ -87,7 +86,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
private ServerRepository _serverRepo; private ServerRepository _serverRepo;
private boolean _us; private boolean _us;
private boolean _cycling = false; private volatile boolean _cycling = false;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public CommunityManager(JavaPlugin plugin, CoreClientManager clientManager) public CommunityManager(JavaPlugin plugin, CoreClientManager clientManager)
@ -204,15 +203,10 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
_cycling = true; _cycling = true;
runAsync(() -> runAsync(() ->
{ {
final List<Integer> resultant = Lists.newArrayList();
final List<Integer> ids = new LinkedList<>(); final List<Integer> ids = new LinkedList<>();
_loadedCommunities.values().stream().filter(c -> c.getMembers().size() >= 5 && c.getPrivacySetting() != PrivacySetting.PRIVATE).forEach(c -> resultant.add(c.getId())); _loadedCommunities.values().stream().filter(c -> c.getMembers().size() >= 5 && c.getPrivacySetting() != PrivacySetting.PRIVATE).forEach(c -> ids.add(c.getId()));
Random random = new Random(); Collections.shuffle(ids);
while (!resultant.isEmpty())
{
ids.add(resultant.remove(random.nextInt(resultant.size())));
}
runSync(() -> runSync(() ->
{ {
@ -321,7 +315,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
if (Bukkit.getPlayer(playerUUID) != null) if (Bukkit.getPlayer(playerUUID) != null)
{ {
Get(Bukkit.getPlayer(playerUUID)).joinCommunity(community); Get(Bukkit.getPlayer(playerUUID)).joinCommunity(community);
Get(Bukkit.getPlayer(playerUUID)).Invites.remove(community.getId()); runSync(() -> Get(Bukkit.getPlayer(playerUUID)).Invites.remove(community.getId()));
} }
community.message(F.main(getName(), F.name(playerName) + " has joined " + F.name(community.getName()) + "!")); community.message(F.main(getName(), F.name(playerName) + " has joined " + F.name(community.getName()) + "!"));
@ -341,19 +335,22 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
{ {
return; return;
} }
if (Bukkit.getPlayer(targetUUID) != null) runSync(() ->
{ {
if (!Get(Bukkit.getPlayer(targetUUID)).Invites.contains(community.getId())) if (Bukkit.getPlayer(targetUUID) != null)
{ {
Get(Bukkit.getPlayer(targetUUID)).Invites.add(community.getId()); if (!Get(Bukkit.getPlayer(targetUUID)).Invites.contains(community.getId()))
if (Managers.get(PreferencesManager.class).get(Bukkit.getPlayer(targetUUID)).isActive(Preference.COMMUNITY_INVITES))
{ {
new JsonMessage(F.main(getName(), "You have been invited to join " + F.elem(community.getName()) + " by " + F.name(sender) + "! Click to join!")).click(ClickEvent.RUN_COMMAND, "/community join " + community.getName()).sendToPlayer(Bukkit.getPlayer(targetUUID)); Get(Bukkit.getPlayer(targetUUID)).Invites.add(community.getId());
if (Managers.get(PreferencesManager.class).get(Bukkit.getPlayer(targetUUID)).isActive(Preference.COMMUNITY_INVITES))
{
new JsonMessage(F.main(getName(), "You have been invited to join " + F.elem(community.getName()) + " by " + F.name(sender) + "! Click to join!")).click(ClickEvent.RUN_COMMAND, "/community join " + community.getName()).sendToPlayer(Bukkit.getPlayer(targetUUID));
}
UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(targetUUID)));
} }
UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(targetUUID)));
} }
} });
community.message(F.main(getName(), F.name(sender) + " has invited " + F.name(targetName) + " to " + F.name(community.getName()) + "!"), CommunityRole.COLEADER); community.message(F.main(getName(), F.name(sender) + " has invited " + F.name(targetName) + " to " + F.name(community.getName()) + "!"), CommunityRole.COLEADER);
} }
@ -364,15 +361,18 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
{ {
return; return;
} }
if (Bukkit.getPlayer(targetUUID) != null) runSync(() ->
{ {
Get(Bukkit.getPlayer(targetUUID)).Invites.remove(community.getId()); if (Bukkit.getPlayer(targetUUID) != null)
if (Managers.get(PreferencesManager.class).get(Bukkit.getPlayer(targetUUID)).isActive(Preference.COMMUNITY_INVITES) && announce)
{ {
UtilPlayer.message(Bukkit.getPlayer(targetUUID), F.main(getName(), "Your invitation to join " + F.elem(community.getName()) + " has been revoked by " + F.name(sender) + "!")); Get(Bukkit.getPlayer(targetUUID)).Invites.remove(community.getId());
if (Managers.get(PreferencesManager.class).get(Bukkit.getPlayer(targetUUID)).isActive(Preference.COMMUNITY_INVITES) && announce)
{
UtilPlayer.message(Bukkit.getPlayer(targetUUID), F.main(getName(), "Your invitation to join " + F.elem(community.getName()) + " has been revoked by " + F.name(sender) + "!"));
}
UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(targetUUID)));
} }
UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(targetUUID))); });
}
if (announce) if (announce)
{ {
community.message(F.main(getName(), F.name(targetName) + "'s invitation to join " + F.name(community.getName()) + " has been revoked by " + F.name(sender) + "!"), CommunityRole.COLEADER); community.message(F.main(getName(), F.name(targetName) + "'s invitation to join " + F.name(community.getName()) + " has been revoked by " + F.name(sender) + "!"), CommunityRole.COLEADER);
@ -416,11 +416,12 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community)); UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community));
} }
public void handleCommunityCreation(Integer id, UUID leaderUUID) public void handleCommunityCreation(Integer id, String name, Integer leaderId, UUID leaderUUID, String leaderName)
{ {
runAsync(() -> runAsync(() ->
{ {
_repo.loadCommunity(id, _loadedCommunities); _loadedCommunities.put(id, new Community(id, name));
_loadedCommunities.get(id).getMembers().put(leaderUUID, new CommunityMemberInfo(leaderName, leaderUUID, leaderId, CommunityRole.LEADER, System.currentTimeMillis()));
runSync(() -> runSync(() ->
{ {
Community community = _loadedCommunities.get(id); Community community = _loadedCommunities.get(id);
@ -443,7 +444,10 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
} }
community.message(F.main(getName(), F.name(senderName) + " has disbanded community " + F.name(community.getName()) + "!")); community.message(F.main(getName(), F.name(senderName) + " has disbanded community " + F.name(community.getName()) + "!"));
UtilServer.CallEvent(new CommunityDisbandEvent(community)); UtilServer.CallEvent(new CommunityDisbandEvent(community));
UtilServer.GetPlayers().stream().filter(player -> Get(player).Invites.contains(community.getId())).forEach(player -> Get(player).Invites.remove(community.getId())); runSync(() ->
{
UtilServer.GetPlayers().stream().filter(player -> Get(player).Invites.contains(community.getId())).forEach(player -> Get(player).Invites.remove(community.getId()));
});
community.getMembers().keySet().stream().filter(uuid -> Bukkit.getPlayer(uuid) != null).forEach(uuid -> Get(Bukkit.getPlayer(uuid)).leaveCommunity(community)); community.getMembers().keySet().stream().filter(uuid -> Bukkit.getPlayer(uuid) != null).forEach(uuid -> Get(Bukkit.getPlayer(uuid)).leaveCommunity(community));
_loadedCommunities.remove(community.getId()); _loadedCommunities.remove(community.getId());
runSync(() -> runSync(() ->
@ -514,7 +518,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
} }
else else
{ {
UtilPlayer.message(sender, F.main(getName(), "Either " + F.name(target) + " does not exist or you have already invited them to " + F.name(community.getName()) + "!")); UtilPlayer.message(sender, F.main(getName(), F.name(target) + " does not exist!"));
} }
}); });
} }
@ -645,7 +649,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
new CommunityUpdateMemberRole(community.getId(), sender.getName(), info.UUID.toString(), role.toString()).publish(); new CommunityUpdateMemberRole(community.getId(), sender.getName(), info.UUID.toString(), role.toString()).publish();
} }
public void handleCreate(Player sender, int accountId, String name) public void handleCreate(Player sender, String senderName, int accountId, String name)
{ {
if (_creating.contains(sender.getUniqueId())) if (_creating.contains(sender.getUniqueId()))
{ {
@ -664,7 +668,7 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
} }
else else
{ {
new CommunityCreate(sender.getUniqueId().toString(), id).publish(); new CommunityCreate(sender.getUniqueId().toString(), senderName, accountId, id, name).publish();
runSync(() -> _creating.remove(sender.getUniqueId())); runSync(() -> _creating.remove(sender.getUniqueId()));
} }
}); });

View File

@ -1,15 +1,15 @@
package mineplex.core.communities; package mineplex.core.communities;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import mineplex.core.Managers; import mineplex.core.Managers;
public class CommunityMemberData public class CommunityMemberData
{ {
private final Map<Integer, CommunityRole> _communities = new HashMap<>(); private final Map<Integer, CommunityRole> _communities = new ConcurrentHashMap<>();
public final List<Integer> Invites = new ArrayList<>(); public final List<Integer> Invites = new ArrayList<>();

View File

@ -51,16 +51,17 @@ public class CommunityCreateCommand extends CommandBase<CommunityManager>
UtilPlayer.message(caller, F.main(Plugin.getName(), "That name is not allowed!")); UtilPlayer.message(caller, F.main(Plugin.getName(), "That name is not allowed!"));
return; return;
} }
final int accountId = Managers.get(CoreClientManager.class).getAccountId(caller);
final String senderName = Managers.get(CoreClientManager.class).Get(caller).getName();
Plugin.runAsync(() -> Plugin.runAsync(() ->
{ {
int accountId = Managers.get(CoreClientManager.class).getAccountId(caller);
if (Managers.get(Chat.class).getFilteredMessage(caller, args[0]).contains("*")) if (Managers.get(Chat.class).getFilteredMessage(caller, args[0]).contains("*"))
{ {
UtilPlayer.message(caller, F.main(Plugin.getName(), "That name is not allowed!")); UtilPlayer.message(caller, F.main(Plugin.getName(), "That name is not allowed!"));
} }
else else
{ {
Plugin.runSync(() -> Plugin.handleCreate(caller, accountId, args[0])); Plugin.runSync(() -> Plugin.handleCreate(caller, senderName, accountId, args[0]));
} }
}); });
} }

View File

@ -5,12 +5,18 @@ import mineplex.serverdata.commands.ServerCommand;
public class CommunityCreate extends ServerCommand public class CommunityCreate extends ServerCommand
{ {
private String _leaderUUID; private String _leaderUUID;
private String _leaderName;
private Integer _leaderId;
private Integer _communityId; private Integer _communityId;
private String _communityName;
public CommunityCreate(String leaderUUID, Integer communityId) public CommunityCreate(String leaderUUID, String leaderName, Integer leaderId, Integer communityId, String communityName)
{ {
_leaderUUID = leaderUUID; _leaderUUID = leaderUUID;
_leaderName = leaderName;
_leaderId = leaderId;
_communityId = communityId; _communityId = communityId;
_communityName = communityName;
} }
public String getLeaderUUID() public String getLeaderUUID()
@ -18,8 +24,23 @@ public class CommunityCreate extends ServerCommand
return _leaderUUID; return _leaderUUID;
} }
public String getLeaderName()
{
return _leaderName;
}
public Integer getLeaderId()
{
return _leaderId;
}
public Integer getCommunityId() public Integer getCommunityId()
{ {
return _communityId; return _communityId;
} }
public String getCommunityName()
{
return _communityName;
}
} }

View File

@ -23,8 +23,11 @@ public class CommunityCreateHandler implements CommandCallback
CommunityCreate update = ((CommunityCreate) command); CommunityCreate update = ((CommunityCreate) command);
UUID leaderUUID = UUID.fromString(update.getLeaderUUID()); UUID leaderUUID = UUID.fromString(update.getLeaderUUID());
Integer communityId = update.getCommunityId(); Integer communityId = update.getCommunityId();
String communityName = update.getCommunityName();
Integer leaderId = update.getLeaderId();
String leaderName = update.getLeaderName();
_manager.handleCommunityCreation(communityId, leaderUUID); _manager.handleCommunityCreation(communityId, communityName, leaderId, leaderUUID, leaderName);
} }
} }
} }