Update the Communities miniplugin to contain a variety of redis and player interaction

This commit is contained in:
AlexTheCoder 2016-12-14 22:10:18 -05:00 committed by cnr
parent 2a971d616c
commit 39f2c94718
1 changed files with 500 additions and 32 deletions

View File

@ -9,31 +9,123 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import com.google.common.collect.Lists;
import mineplex.core.Managers;
import mineplex.core.MiniDbClientPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.common.util.C;
import mineplex.core.common.util.Callback;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.communities.Community.PrivacySetting;
import mineplex.core.communities.commands.CommunityCommand;
import mineplex.core.communities.redis.CommunityChat;
import mineplex.core.communities.redis.CommunityCloseJoinRequest;
import mineplex.core.communities.redis.CommunityCreate;
import mineplex.core.communities.redis.CommunityDisband;
import mineplex.core.communities.redis.CommunityInvite;
import mineplex.core.communities.redis.CommunityJoinRequest;
import mineplex.core.communities.redis.CommunityUnInvite;
import mineplex.core.communities.redis.CommunityUpdateMemberRole;
import mineplex.core.communities.redis.CommunityUpdateMembership;
import mineplex.core.communities.redis.CommunityUpdateName;
import mineplex.core.communities.redis.CommunityUpdateSetting;
import mineplex.core.communities.storage.CommunityRepository;
import mineplex.core.preferences.Preference;
import mineplex.core.preferences.PreferencesManager;
import mineplex.core.recharge.Recharge;
import mineplex.serverdata.Region;
import mineplex.serverdata.data.DataRepository;
import mineplex.serverdata.data.PlayerStatus;
import mineplex.serverdata.redis.RedisDataRepository;
import mineplex.serverdata.servers.ServerManager;
public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
{
private final CommunityRepository _repo;
private final List<Community> _communityBrowser;
private final Map<Integer, Community> _loadedCommunities;
public final List<Integer> BrowserIds = new LinkedList<>();
public final DataRepository<PlayerStatus> StatusRepository;
private boolean _cycling = false;
@SuppressWarnings("deprecation")
public CommunityManager(JavaPlugin plugin, CoreClientManager clientManager)
{
super("Communities", plugin, clientManager);
_repo = new CommunityRepository(plugin);
StatusRepository = new RedisDataRepository<PlayerStatus>(ServerManager.getMasterConnection(), ServerManager.getSlaveConnection(),
Region.currentRegion(), PlayerStatus.class, "playerStatus");
_repo = new CommunityRepository(plugin, StatusRepository);
_loadedCommunities = new HashMap<>();
_communityBrowser = new LinkedList<>();
_repo.loadCommunities(_communityBrowser);
Bukkit.getScheduler().scheduleAsyncRepeatingTask(plugin, () ->
{
if (_cycling)
{
return;
}
LinkedList<Community> communities = new LinkedList<>();
_loadedCommunities.values().forEach(community -> communities.add(community));
_repo.updateMembersAndJoinRequests(communities);
}, 0L, 20 * 5);
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () ->
{
cycleBrowser();
}, 0L, 20 * 30);
_repo.loadCommunities(_loadedCommunities);
addCommand(new CommunityCommand(this));
}
private void cycleBrowser()
{
if (_cycling)
{
return;
}
_cycling = true;
runAsync(() ->
{
List<Integer> resultant = Lists.newArrayList();
List<Integer> ids = new LinkedList<>();
_loadedCommunities.values().stream().filter(c -> c.getMembers().size() >= 5 && c.getPrivacySetting() != PrivacySetting.PRIVATE).forEach(c -> resultant.add(c.getId()));
while (!resultant.isEmpty())
{
if (UtilMath.random.nextBoolean())
{
ids.add(resultant.get(0));
}
}
BrowserIds.clear();
BrowserIds.addAll(ids);
_cycling = false;
runSync(() -> UtilServer.CallEvent(new CommunityBrowserUpdateEvent()));
});
}
public Community getLoadedCommunity(Integer id)
{
return _loadedCommunities.get(id);
}
public Community getLoadedCommunity(String name)
@ -49,47 +141,377 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
return null;
}
public void handleCommunitySettingUpdate(Integer id, CommunitySetting setting, String newValue)
public void handleCommunitySettingUpdate(Integer id, String sender, CommunitySetting setting, String newValue)
{
}
public void loadCommunity(Integer id, Callback<Community> callback)
{
runAsync(() ->
{
Community c = _repo.getCommunityById(id);
_loadedCommunities.remove(id);
_loadedCommunities.put(id, c);
if (callback != null)
{
runSync(() -> callback.run(c));
}
});
}
public void handleCommunityChat(Integer communityId, String senderName, String message)
{
Community community = _loadedCommunities.get(communityId);
Community community = _loadedCommunities.get(id);
if (community == null)
{
return;
}
for (Player player : UtilServer.getPlayers())
setting.parseValueInto(newValue, community);
community.message(F.main(getName(), F.name(sender) + " has changed settings in " + F.name(community.getName()) + "!"));
UtilServer.CallEvent(new CommunitySettingUpdateEvent(community));
}
public void handleCommunityNameUpdate(Integer id, String sender, String name)
{
Community community = _loadedCommunities.get(id);
if (community == null)
{
if (Get(player).isMemberOf(community))
return;
}
String oldName = community.getName();
community.message(F.main(getName(), F.name(sender) + " has changed the name of " + F.name(oldName) + " to " + F.name(community.getName()) + "!"));
UtilServer.CallEvent(new CommunityNameUpdateEvent(community));
}
public void handleCommunityMembershipRoleUpdate(Integer id, String sender, UUID uuid, CommunityRole role)
{
Community community = _loadedCommunities.get(id);
if (community == null)
{
return;
}
CommunityMemberInfo member = community.getMembers().get(uuid);
member.Role = role;
if (Bukkit.getPlayer(uuid) != null)
{
Get(Bukkit.getPlayer(uuid)).setRoleIn(community, role);
}
String name = member.Name;
community.message(F.main(getName(), F.name(sender) + " has changed " + F.name(name + "'s") + " role to " + F.elem(role.getDisplay()) + " in " + F.name(community.getName()) + "!"));
UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community));
}
public void handleCommunityMembershipUpdate(Integer id, String sender, String playerName, UUID playerUUID, Integer accountId, boolean kick, boolean leave)
{
Community community = _loadedCommunities.get(id);
if (community == null)
{
return;
}
if (kick)
{
community.message(F.main(getName(), F.name(sender) + " has kicked " + F.name(playerName) + " from " + F.name(community.getName()) + "!"));
community.getMembers().remove(playerUUID);
if (Bukkit.getPlayer(playerUUID) != null)
{
UtilPlayer.message(player, C.cBlueB + community.getName() + " " + community.getChatFormatting()[0] + C.Bold + senderName + " " + community.getChatFormatting()[1] + message);
Get(Bukkit.getPlayer(playerUUID)).leaveCommunity(community);
}
}
else if (leave)
{
community.message(F.main(getName(), F.name(playerName) + " has left " + F.name(community.getName()) + "!"));
community.getMembers().remove(playerUUID);
if (Bukkit.getPlayer(playerUUID) != null)
{
Get(Bukkit.getPlayer(playerUUID)).leaveCommunity(community);
}
}
else
{
community.getMembers().put(playerUUID, new CommunityMemberInfo(playerName, playerUUID, accountId, CommunityRole.MEMBER, System.currentTimeMillis()));
if (Bukkit.getPlayer(playerUUID) != null)
{
Get(Bukkit.getPlayer(playerUUID)).joinCommunity(community);
Get(Bukkit.getPlayer(playerUUID)).Invites.remove(community.getId());
}
community.message(F.main(getName(), F.name(playerName) + " has joined " + F.name(community.getName()) + "!"));
}
UtilServer.CallEvent(new CommunityMembershipUpdateEvent(community));
if (Bukkit.getPlayer(playerUUID) != null)
{
UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(playerUUID)));
}
}
public void handleCommunityInvite(Integer id, String sender, String targetName, UUID targetUUID)
{
Community community = _loadedCommunities.get(id);
if (community == null)
{
return;
}
if (Bukkit.getPlayer(targetUUID) != null)
{
Get(Bukkit.getPlayer(targetUUID)).Invites.add(community.getId());
if (Managers.get(PreferencesManager.class).get(Bukkit.getPlayer(targetUUID)).isActive(Preference.COMMUNITY_INVITES))
{
UtilPlayer.message(Bukkit.getPlayer(targetUUID), F.main(getName(), "You have been invited to join " + F.elem(community.getName()) + " by " + F.name(sender) + "!"));
}
}
community.message(F.main(getName(), F.name(sender) + " has invited " + F.name(targetName) + " to " + F.name(community.getName()) + "!"));
if (Bukkit.getPlayer(targetUUID) != null)
{
UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(targetUUID)));
}
}
public void handleCommunityUninvite(Integer id, String sender, String targetName, UUID targetUUID, boolean announce)
{
Community community = _loadedCommunities.get(id);
if (community == null)
{
return;
}
if (Bukkit.getPlayer(targetUUID) != null)
{
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) + "!"));
}
}
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) + "!"));
}
if (Bukkit.getPlayer(targetUUID) != null)
{
UtilServer.CallEvent(new CommunityMemberDataUpdateEvent(Bukkit.getPlayer(targetUUID)));
}
}
public void handleCommunityJoinRequest(Integer id, String playerName, UUID playerUUID, Integer accountId)
{
Community community = _loadedCommunities.get(id);
if (community == null)
{
return;
}
if (Bukkit.getPlayer(playerUUID) != null)
{
UtilPlayer.message(Bukkit.getPlayer(playerUUID), F.main(getName(), "You have requested to join " + F.elem(community.getName()) + "!"));
}
community.getJoinRequests().put(playerUUID, new CommunityJoinRequestInfo(playerName, playerUUID, accountId));
community.message(F.main(getName(), F.name(playerName) + " has requested to join " + F.name(community.getName()) + "!"), CommunityRole.COLEADER);
UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community));
}
public void handleCommunityCloseJoinRequest(Integer id, String sender, String playerName, UUID playerUUID, Integer accountId, boolean announce)
{
Community community = _loadedCommunities.get(id);
if (community == null)
{
return;
}
community.getJoinRequests().remove(playerUUID);
if (announce)
{
if (Bukkit.getPlayer(playerUUID) != null)
{
UtilPlayer.message(Bukkit.getPlayer(playerUUID), F.main(getName(), "Your request to join " + F.name(community.getName()) + " has been denied by " + F.name(sender) + "!"));
}
community.message(F.main(getName(), F.name(playerName) + "'s request to join " + F.name(community.getName()) + " has been denied by " + F.name(sender) + "!"), CommunityRole.COLEADER);
}
UtilServer.CallEvent(new CommunityJoinRequestsUpdateEvent(community));
}
public void handleCommunityCreation(Integer id, UUID leaderUUID)
{
runAsync(() ->
{
_repo.loadCommunity(id, _loadedCommunities);
runSync(() ->
{
Community community = _loadedCommunities.get(id);
if (Bukkit.getPlayer(leaderUUID) != null)
{
Player leader = Bukkit.getPlayer(leaderUUID);
UtilPlayer.message(leader, F.main(getName(), "You have created a community named " + F.name(community.getName()) + "!"));
Get(leader).joinCommunity(id, CommunityRole.LEADER);
}
});
});
}
public void handleCommunityDisband(Integer id, String senderName)
{
Community community = _loadedCommunities.get(id);
if (community == null)
{
return;
}
community.message(F.main(getName(), F.name(senderName) + " has disbanded community " + F.name(community.getName()) + "!"));
UtilServer.CallEvent(new CommunityDisbandEvent(community));
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));
_loadedCommunities.remove(community.getId());
BrowserIds.remove(community.getId());
}
public void handleCommunityChat(Integer id, String senderName, String message)
{
Community community = _loadedCommunities.get(id);
if (community == null)
{
return;
}
community.message(community.getChatFormatting()[0] + C.Bold + community.getName() + " " + community.getChatFormatting()[1] + C.Bold + senderName + " " + community.getChatFormatting()[2] + message);
}
public void handleInvite(Player sender, Community community, String target)
{
CommunityJoinRequestInfo[] jr = community.getJoinRequests().values().stream().filter(data -> data.Name.equalsIgnoreCase(target)).toArray(size -> new CommunityJoinRequestInfo[size]);
if (jr.length == 1)
{
UtilPlayer.message(sender, F.main(getName(), "You have accepted " + F.name(target) + "'s join request to " + F.name(community.getName()) + "!"));
runAsync(() ->
{
_repo.addToCommunity(jr[0].AccountId, community.getId());
_repo.removeJoinRequest(community.getId(), jr[0].AccountId);
});
new CommunityCloseJoinRequest(community.getId(), sender.getName(), jr[0].Name, jr[0].UUID.toString(), jr[0].AccountId, false).publish();
new CommunityUpdateMembership(community.getId(), sender.getName(), jr[0].Name, jr[0].UUID.toString(), jr[0].AccountId, false, false).publish();
return;
}
runAsync(() ->
{
if (_repo.inviteToCommunity(community.getId(), target))
{
UtilPlayer.message(sender, F.main(getName(), "You have invited " + F.name(target) + " to join " + F.name(community.getName()) + "!"));
new CommunityInvite(community.getId(), sender.getName(), target, Managers.get(CoreClientManager.class).loadUUIDFromDB(target).toString()).publish();
}
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()) + "!"));
}
});
}
public void handleUninvite(Player sender, Community community, String target)
{
runAsync(() ->
{
if (_repo.deleteInviteToCommunity(community.getId(), target))
{
UtilPlayer.message(sender, F.main(getName(), "You have revoked " + F.name(target) + "'s invitation to join " + F.name(community.getName()) + "!"));
new CommunityUnInvite(community.getId(), sender.getName(), target, Managers.get(CoreClientManager.class).loadUUIDFromDB(target).toString(), true).publish();
}
else
{
UtilPlayer.message(sender, F.main(getName(), "Either " + F.name(target) + " does not exist or you have not invited them to " + F.name(community.getName()) + "!"));
}
});
}
public void handleJoinRequest(Player sender, Community community)
{
final int accountId = Managers.get(CoreClientManager.class).getAccountId(sender);
if (Get(sender).Invites.contains(community.getId()))
{
String playerName = Managers.get(CoreClientManager.class).Get(sender).getName(); //Guarantee real name (important in this instance)
runAsync(() ->
{
_repo.addToCommunity(accountId, community.getId());
_repo.deleteInviteToCommunity(community.getId(), playerName);
});
new CommunityUnInvite(community.getId(), sender.getName(), sender.getName(), sender.getUniqueId().toString(), false).publish();
new CommunityUpdateMembership(community.getId(), sender.getName(), sender.getName(), sender.getUniqueId().toString(), accountId, false, false).publish();
return;
}
runAsync(() ->
{
_repo.addJoinRequest(community.getId(), accountId);
});
new CommunityJoinRequest(community.getId(), sender.getName(), sender.getUniqueId().toString(), accountId).publish();
}
public void handleCloseJoinRequest(Player sender, Community community, CommunityJoinRequestInfo info)
{
runAsync(() ->
{
_repo.removeJoinRequest(community.getId(), info.AccountId);
});
new CommunityCloseJoinRequest(community.getId(), sender.getName(), info.Name, info.UUID.toString(), info.AccountId, true).publish();
}
public void handleKick(Player sender, Community community, CommunityMemberInfo info)
{
runAsync(() ->
{
_repo.removeFromCommunity(info.AccountId, community.getId());
});
new CommunityUpdateMembership(community.getId(), sender.getName(), info.Name, info.UUID.toString(), info.AccountId, true, false).publish();
}
public void handleLeave(Player sender, Community community, CommunityMemberInfo info)
{
runAsync(() ->
{
_repo.removeFromCommunity(info.AccountId, community.getId());
});
new CommunityUpdateMembership(community.getId(), sender.getName(), info.Name, info.UUID.toString(), info.AccountId, false, true).publish();
}
public void handleSettingUpdate(Player sender, Community community, CommunitySetting setting, String newValue)
{
runAsync(() ->
{
_repo.updateCommunitySetting(setting, community.getId(), newValue);
});
new CommunityUpdateSetting(community.getId(), sender.getName(), setting.toString(), newValue).publish();
}
public void handleNameUpdate(Player sender, Community community, String newName)
{
runAsync(() ->
{
_repo.updateCommunityName(community.getId(), newName);
});
new CommunityUpdateName(community.getId(), sender.getName(), newName).publish();
}
public void handleRoleUpdate(Player sender, Community community, CommunityMemberInfo info, CommunityRole role)
{
runAsync(() ->
{
_repo.updateCommunityRole(info.AccountId, community.getId(), role);
});
new CommunityUpdateMemberRole(community.getId(), sender.getName(), info.UUID.toString(), role.toString()).publish();
}
public void handleCreate(Player sender, String name)
{
int accountId = Managers.get(CoreClientManager.class).getAccountId(sender);
runAsync(() ->
{
_repo.createCommunity(name, accountId, id ->
{
if (id == -1)
{
sender.sendMessage(F.main(getName(), "Failed to create community " + F.elem(name)));
}
else
{
new CommunityCreate(sender.getUniqueId().toString(), id).publish();
}
});
});
}
public void handleDisband(Player sender, Community community)
{
runAsync(() ->
{
_repo.deleteCommunity(community.getId());
});
new CommunityDisband(sender.getName(), community.getId()).publish();
}
@Override
public String getQuery(int accountId, String uuid, String name)
{
String query = "SELECT c.*, cm.communityId, cm.communityRole, cs.settingId, cs.settingValue FROM communityMembers cm INNER JOIN communities c ON c.id=cm.communityId INNER JOIN communitySettings cs ON cs.communityId=c.id;";
return query;
//return "SELECT communityId, communityRole FROM communityMembers WHERE accountId=" + accountId + ";";//BEING REPLACED WITH AN INNER JOIN TO LOAD THE COMMUNITY AS WELL
//String query = "SELECT c.*, cm.communityId, cm.communityRole, cs.settingId, cs.settingValue FROM communityMembers cm INNER JOIN communities c ON c.id=cm.communityId INNER JOIN communitySettings cs ON cs.communityId=c.id;";
//return query;
return "SELECT communityId, communityRole FROM communityMembers WHERE accountId=" + accountId + ";";//BEING REPLACED WITH AN INNER JOIN TO LOAD THE COMMUNITY AS WELL
}
@Override
@ -105,6 +527,52 @@ public class CommunityManager extends MiniDbClientPlugin<CommunityMemberData>
}
Set(uuid, data);
}
@EventHandler
public void loadInvites(PlayerJoinEvent event)
{
final int accountId = Managers.get(CoreClientManager.class).getAccountId(event.getPlayer());
final CommunityMemberData data = Get(event.getPlayer());
runAsync(() ->
{
_repo.loadInvites(accountId, data.Invites);
UtilPlayer.message(event.getPlayer(), F.main(getName(), "You have been invited to join " + F.elem(data.Invites.size()) + " communities!"));
});
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onChat(AsyncPlayerChatEvent event)
{
if (!event.getMessage().startsWith("!"))
{
return;
}
event.setCancelled(true);
Player sender = event.getPlayer();
if (Get(sender).getCommunityChattingTo() != -1)
{
Community target = _loadedCommunities.get(Get(sender).getCommunityChattingTo());
if (target == null || !target.getMembers().containsKey(event.getPlayer().getUniqueId()))
{
UtilPlayer.message(sender, F.main(getName(), "You are not in that community! Use " + F.elem("/com chat <community>") + " to select a new community to chat to!"));
}
else
{
if (Recharge.Instance.use(sender, "Community Chat to " + target.getId(), target.getChatDelay(), false, false))
{
new CommunityChat(sender.getName(), target.getId(), event.getMessage()).publish();
}
else
{
UtilPlayer.message(sender, F.main(getName(), "You cannot chat to " + F.name(target.getName()) + " that quickly!"));
}
}
}
else
{
UtilPlayer.message(sender, F.main(getName(), "You are not chatting to a specific community! Use " + F.elem("/com chat <community>") + " to select a community to chat to."));
}
}
@Override
protected CommunityMemberData addPlayer(UUID uuid)