From e716cee5bc808d3d0b4b1444b28ecd440541eb9d Mon Sep 17 00:00:00 2001 From: spacerocket62 Date: Wed, 28 Oct 2020 12:07:02 -0700 Subject: [PATCH] so apparently mongo methods needed to be updated, weird amirite added chat listener added login msgs in a way ?? added setters to the ranks changed the default rank obj so it puts prefix, loginmsg, and perms only instead rankmanager imports any rank changes on startup plex main class kinda just vibing --- src/main/java/me/totalfreedom/plex/Plex.java | 3 + .../plex/cache/MongoPlayerData.java | 34 ++++--- .../plex/listeners/ChatListener.java | 28 ++++++ .../plex/listeners/PlayerListener.java | 24 ++++- .../plex/rank/DefaultRankObj.java | 7 +- .../totalfreedom/plex/rank/RankManager.java | 92 +++++++++++++------ .../me/totalfreedom/plex/rank/enums/Rank.java | 37 +++++++- 7 files changed, 174 insertions(+), 51 deletions(-) create mode 100644 src/main/java/me/totalfreedom/plex/listeners/ChatListener.java diff --git a/src/main/java/me/totalfreedom/plex/Plex.java b/src/main/java/me/totalfreedom/plex/Plex.java index 0cb0083..184965f 100644 --- a/src/main/java/me/totalfreedom/plex/Plex.java +++ b/src/main/java/me/totalfreedom/plex/Plex.java @@ -6,6 +6,7 @@ import lombok.Setter; import me.totalfreedom.plex.cache.MongoPlayerData; import me.totalfreedom.plex.cache.SQLPlayerData; import me.totalfreedom.plex.config.MainConfig; +import me.totalfreedom.plex.listeners.ChatListener; import me.totalfreedom.plex.listeners.PlayerListener; import me.totalfreedom.plex.rank.RankManager; import me.totalfreedom.plex.storage.MongoConnection; @@ -78,9 +79,11 @@ public class Plex extends JavaPlugin } getServer().getPluginManager().registerEvents(new PlayerListener(), this); + getServer().getPluginManager().registerEvents(new ChatListener(), this); rankManager = new RankManager(); rankManager.generateDefaultRanks(); + rankManager.importDefaultRanks(); } diff --git a/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java b/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java index 4ad54cc..cb24742 100644 --- a/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java +++ b/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java @@ -2,7 +2,9 @@ package me.totalfreedom.plex.cache; import dev.morphia.Datastore; import dev.morphia.query.Query; -import dev.morphia.query.UpdateOperations; +import dev.morphia.query.Update; +import dev.morphia.query.experimental.filters.Filters; +import dev.morphia.query.experimental.updates.UpdateOperators; import me.totalfreedom.plex.Plex; import me.totalfreedom.plex.player.PlexPlayer; @@ -19,36 +21,38 @@ public class MongoPlayerData public boolean exists(UUID uuid) { - Query query = datastore.createQuery(PlexPlayer.class); + Query query = datastore.find(PlexPlayer.class) + .filter(Filters.eq("uuid", uuid.toString())); - return query.field("uuid").exists().field("uuid").equal(uuid.toString()).first() != null; + return query.first() != null; } public PlexPlayer getByUUID(UUID uuid) { - if (PlayerCache.getPlexPlayerMap().containsKey(uuid)) { return PlayerCache.getPlexPlayerMap().get(uuid); } - Query query2 = datastore.createQuery(PlexPlayer.class).field("uuid").exists().field("uuid").equal(uuid.toString()); + + Query query2 = datastore.find(PlexPlayer.class).filter(Filters.eq("uuid", uuid.toString())); return query2.first(); } public void update(PlexPlayer player) { - Query filter = datastore.createQuery(PlexPlayer.class) - .field("uuid").equal(player.getUuid()); + Query filter = datastore.find(PlexPlayer.class) + .filter(Filters.eq("uuid", player.getUuid())); - UpdateOperations updateOps = datastore.createUpdateOperations(PlexPlayer.class); + Update updateOps = filter + .update( + UpdateOperators.set("name", player.getName()), + UpdateOperators.set("loginMSG", player.getLoginMSG()), + UpdateOperators.set("prefix", player.getPrefix()), + UpdateOperators.set("rank", player.getRank().toLowerCase()), + UpdateOperators.set("ips", player.getIps()), + UpdateOperators.set("coins", player.getCoins())); - updateOps.set("name", player.getName()); - updateOps.set("loginMSG", player.getLoginMSG()); - updateOps.set("prefix", player.getPrefix()); - updateOps.set("rank", player.getRank().toLowerCase()); - updateOps.set("ips", player.getIps()); - updateOps.set("coins", player.getCoins()); - datastore.update(filter, updateOps); + updateOps.execute(); } public void save(PlexPlayer plexPlayer) diff --git a/src/main/java/me/totalfreedom/plex/listeners/ChatListener.java b/src/main/java/me/totalfreedom/plex/listeners/ChatListener.java new file mode 100644 index 0000000..f83f432 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/listeners/ChatListener.java @@ -0,0 +1,28 @@ +package me.totalfreedom.plex.listeners; + +import me.totalfreedom.plex.Plex; +import me.totalfreedom.plex.cache.PlayerCache; +import me.totalfreedom.plex.player.PlexPlayer; +import me.totalfreedom.plex.rank.RankManager; +import net.md_5.bungee.api.ChatColor; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.AsyncPlayerChatEvent; + +public class ChatListener implements Listener +{ + + @EventHandler + public void onChat(AsyncPlayerChatEvent event) + { + PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId()); + if (!plexPlayer.getPrefix().isEmpty()) + { + event.setFormat(String.format("%s %s §7» %s", plexPlayer.getPrefix(), ChatColor.RESET + plexPlayer.getName(), event.getMessage())); + } else if (Plex.get().getRankManager().isAdmin(plexPlayer)) + { + event.setFormat(String.format("%s %s §7» %s", plexPlayer.getRankFromString().getPrefix(), ChatColor.RESET + plexPlayer.getName(), event.getMessage())); + } + } + +} diff --git a/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java b/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java index 591b00d..0a31d32 100644 --- a/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java +++ b/src/main/java/me/totalfreedom/plex/listeners/PlayerListener.java @@ -8,8 +8,10 @@ import me.totalfreedom.plex.cache.SQLPlayerData; import me.totalfreedom.plex.player.PlexPlayer; import me.totalfreedom.plex.player.PunishedPlayer; import me.totalfreedom.plex.util.PlexLog; +import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; @@ -19,8 +21,8 @@ public class PlayerListener implements Listener private final MongoPlayerData mongoPlayerData = Plex.get().getMongoPlayerData() != null ? Plex.get().getMongoPlayerData() : null; private final SQLPlayerData sqlPlayerData = Plex.get().getSqlPlayerData() != null ? Plex.get().getSqlPlayerData() : null; - @EventHandler - public void onJoin(PlayerJoinEvent event) + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerSetup(PlayerJoinEvent event) { Player player = event.getPlayer(); @@ -62,10 +64,23 @@ public class PlayerListener implements Listener PlayerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache PlayerCache.getPunishedPlayerMap().put(player.getUniqueId(), new PunishedPlayer(player.getUniqueId())); + + assert plexPlayer != null; + + if (Plex.get().getRankManager().isAdmin(plexPlayer)) + { + if (!plexPlayer.getLoginMSG().isEmpty()) + { + event.setJoinMessage(ChatColor.AQUA + player.getName() + " is " + plexPlayer.getLoginMSG()); + } else { + event.setJoinMessage(ChatColor.AQUA + player.getName() + " is " + plexPlayer.getRankFromString().getLoginMSG()); + } + + } } - @EventHandler - public void onQuit(PlayerQuitEvent event) + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerSave(PlayerQuitEvent event) { PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId()); //get the player because it's literally impossible for them to not have an object @@ -81,4 +96,5 @@ public class PlayerListener implements Listener PlayerCache.getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache PlayerCache.getPunishedPlayerMap().remove(event.getPlayer().getUniqueId()); } + } diff --git a/src/main/java/me/totalfreedom/plex/rank/DefaultRankObj.java b/src/main/java/me/totalfreedom/plex/rank/DefaultRankObj.java index ea3c9aa..b370a46 100644 --- a/src/main/java/me/totalfreedom/plex/rank/DefaultRankObj.java +++ b/src/main/java/me/totalfreedom/plex/rank/DefaultRankObj.java @@ -8,12 +8,15 @@ import me.totalfreedom.plex.rank.enums.Rank; @Getter public class DefaultRankObj { - private final String name; + private final String prefix; + private final String loginMSG; private final List permissions; public DefaultRankObj(Rank rank) { - this.name = rank.name().toUpperCase(); + this.prefix = rank.getPrefix(); + this.loginMSG = rank.getLoginMSG(); this.permissions = Lists.newArrayList(); + permissions.add("example.permission"); } } diff --git a/src/main/java/me/totalfreedom/plex/rank/RankManager.java b/src/main/java/me/totalfreedom/plex/rank/RankManager.java index 7817a4a..a980d2a 100644 --- a/src/main/java/me/totalfreedom/plex/rank/RankManager.java +++ b/src/main/java/me/totalfreedom/plex/rank/RankManager.java @@ -1,14 +1,19 @@ package me.totalfreedom.plex.rank; -import com.google.common.collect.Lists; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.List; +import com.google.common.collect.Maps; import me.totalfreedom.plex.Plex; +import me.totalfreedom.plex.player.PlexPlayer; import me.totalfreedom.plex.rank.enums.Rank; import me.totalfreedom.plex.util.PlexLog; import org.json.JSONObject; +import org.json.JSONTokener; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Map; +import java.util.stream.Collectors; public class RankManager { @@ -25,34 +30,65 @@ public class RankManager { return; } - else + try { - try + defaultRanks.createNewFile(); + + Map rankMap = Maps.newHashMap(); + for (Rank rank : Rank.values()) { - defaultRanks.createNewFile(); - - List ranks = Lists.newArrayList(); - for (Rank rank : Rank.values()) - { - ranks.add(new DefaultRankObj(rank)); - } - - JSONObject obj = new JSONObject(); - if (obj.length() == 0) - { - obj.put("ranks", ranks); - - FileWriter writer = new FileWriter(defaultRanks); - writer.append(obj.toString(4)); - writer.flush(); - writer.close(); - PlexLog.log("Generating default-ranks.json"); - } + rankMap.put(rank.name().toUpperCase(), new DefaultRankObj(rank)); } - catch (IOException e) + + JSONObject obj = new JSONObject(); + if (obj.length() == 0) { - e.printStackTrace(); + obj.put("ranks", rankMap); + + FileWriter writer = new FileWriter(defaultRanks); + writer.append(obj.toString(4)); + writer.flush(); + writer.close(); + PlexLog.log("Generating default-ranks.json"); } } + catch (IOException e) + { + e.printStackTrace(); + } + } + + public void importDefaultRanks() + { + if (!defaultRanks.exists()) + { + return; + } + + try + { + FileInputStream stream = new FileInputStream(defaultRanks); + JSONTokener tokener = new JSONTokener(stream); + JSONObject object = new JSONObject(tokener); + JSONObject rankObj = object.getJSONObject("ranks"); + for (Rank rank : Rank.values()) + { + if (rankObj.isNull(rank.name().toUpperCase())) continue; + rank.setLoginMessage(rankObj.getJSONObject(rank.name().toUpperCase()).getString("loginMSG")); + rank.setPrefix(rankObj.getJSONObject(rank.name().toUpperCase()).getString("prefix")); //should i even be doing this + rank.setPermissions(rankObj.getJSONObject(rank.name().toUpperCase()).getJSONArray("permissions").toList().stream().map(Object::toString).collect(Collectors.toList())); + + } + } + catch (IOException e) + { + e.printStackTrace(); + } + + } + + public boolean isAdmin(PlexPlayer plexPlayer) + { + return !plexPlayer.getRank().isEmpty() && plexPlayer.getRankFromString().isAtleast(Rank.ADMIN); } } diff --git a/src/main/java/me/totalfreedom/plex/rank/enums/Rank.java b/src/main/java/me/totalfreedom/plex/rank/enums/Rank.java index 539e182..782dc34 100644 --- a/src/main/java/me/totalfreedom/plex/rank/enums/Rank.java +++ b/src/main/java/me/totalfreedom/plex/rank/enums/Rank.java @@ -1,7 +1,10 @@ package me.totalfreedom.plex.rank.enums; +import com.google.common.collect.Lists; import org.bukkit.ChatColor; +import java.util.List; + public enum Rank { IMPOSTER(-1, ChatColor.AQUA + "an " + ChatColor.YELLOW + "Imposter", ChatColor.YELLOW + "[Imp]"), @@ -11,13 +14,17 @@ public enum Rank SENIOR_ADMIN(3, ChatColor.GOLD + "a " + ChatColor.GOLD + "Senior Admin", ChatColor.GOLD + "[SrA]"), EXECUTIVE(4, ChatColor.RED + "an " + ChatColor.RED + "Executive", ChatColor.RED + "[Exec]"); - private final String loginMessage; - private final String prefix; + private String loginMessage; + private String prefix; + private int level; + private List permissions; Rank(int level, String loginMessage, String prefix) { + this.level = level; this.loginMessage = loginMessage; this.prefix = prefix; + this.permissions = Lists.newArrayList(); } public String getPrefix() @@ -29,4 +36,30 @@ public enum Rank { return loginMessage; } + + public int getLevel() { + return level; + } + + public void setLoginMessage(String msg) + { + this.loginMessage = msg; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public boolean isAtleast(Rank rank) + { + return getLevel() >= rank.getLevel(); + } + + public List getPermissions() { + return permissions; + } + + public void setPermissions(List permissions) { + this.permissions = permissions; + } }