From ce76aa9ce1582fe11670ce97201203d1a5332691 Mon Sep 17 00:00:00 2001 From: spacerocket62 Date: Thu, 5 Nov 2020 13:17:14 -0800 Subject: [PATCH] admin cmd done --- src/main/java/me/totalfreedom/plex/Plex.java | 5 + .../me/totalfreedom/plex/admin/Admin.java | 31 ++++ .../me/totalfreedom/plex/admin/AdminList.java | 75 +++++++++ .../me/totalfreedom/plex/cache/DataUtils.java | 47 ++++++ .../plex/cache/MongoPlayerData.java | 1 + .../totalfreedom/plex/cache/PlayerCache.java | 5 +- .../plex/command/PlexCommand.java | 11 +- .../plex/command/impl/AdminCMD.java | 158 ++++++++++++++++++ .../plex/command/impl/FionnCMD.java | 3 +- .../plex/command/impl/FreezeCMD.java | 3 +- .../plex/command/impl/NameHistoryCMD.java | 6 +- .../plex/command/impl/OpAllCMD.java | 3 +- .../totalfreedom/plex/command/impl/OpCMD.java | 3 +- .../plex/command/impl/PlexCMD.java | 5 +- .../plex/event/AdminAddEvent.java | 27 +++ .../plex/event/AdminRemoveEvent.java | 27 +++ .../plex/event/AdminSetRankEvent.java | 34 ++++ .../plex/handlers/CommandHandler.java | 1 + .../plex/listener/impl/PlayerListener.java | 14 ++ .../plex/listener/impl/WorldListener.java | 2 +- .../me/totalfreedom/plex/util/PlexUtils.java | 15 ++ src/main/resources/config.yml | 2 + 22 files changed, 461 insertions(+), 17 deletions(-) create mode 100644 src/main/java/me/totalfreedom/plex/admin/Admin.java create mode 100644 src/main/java/me/totalfreedom/plex/admin/AdminList.java create mode 100644 src/main/java/me/totalfreedom/plex/cache/DataUtils.java create mode 100644 src/main/java/me/totalfreedom/plex/command/impl/AdminCMD.java create mode 100644 src/main/java/me/totalfreedom/plex/event/AdminAddEvent.java create mode 100644 src/main/java/me/totalfreedom/plex/event/AdminRemoveEvent.java create mode 100644 src/main/java/me/totalfreedom/plex/event/AdminSetRankEvent.java diff --git a/src/main/java/me/totalfreedom/plex/Plex.java b/src/main/java/me/totalfreedom/plex/Plex.java index cf642e2..ada3b87 100644 --- a/src/main/java/me/totalfreedom/plex/Plex.java +++ b/src/main/java/me/totalfreedom/plex/Plex.java @@ -2,6 +2,7 @@ package me.totalfreedom.plex; import lombok.Getter; import lombok.Setter; +import me.totalfreedom.plex.admin.AdminList; import me.totalfreedom.plex.cache.MongoPlayerData; import me.totalfreedom.plex.cache.SQLPlayerData; import me.totalfreedom.plex.config.Config; @@ -37,6 +38,8 @@ public class Plex extends JavaPlugin private RankManager rankManager; + private AdminList adminList; + public static Plex get() { return plugin; @@ -96,6 +99,8 @@ public class Plex extends JavaPlugin rankManager.importDefaultRanks(); PlexLog.log("Rank Manager initialized"); + adminList = new AdminList(); + generateWorlds(); } diff --git a/src/main/java/me/totalfreedom/plex/admin/Admin.java b/src/main/java/me/totalfreedom/plex/admin/Admin.java new file mode 100644 index 0000000..4c2b9eb --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/admin/Admin.java @@ -0,0 +1,31 @@ +package me.totalfreedom.plex.admin; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; +import me.totalfreedom.plex.rank.enums.Rank; + +import java.util.UUID; + +@Getter +@Setter +public class Admin +{ + @Setter(AccessLevel.NONE) + private UUID uuid; + + private Rank rank; + + private boolean commandSpy = true; + private boolean staffChat = false; + + public Admin(UUID uuid) + { + this.uuid = uuid; + this.rank = Rank.ADMIN; + } + + + + +} diff --git a/src/main/java/me/totalfreedom/plex/admin/AdminList.java b/src/main/java/me/totalfreedom/plex/admin/AdminList.java new file mode 100644 index 0000000..ac6b503 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/admin/AdminList.java @@ -0,0 +1,75 @@ +package me.totalfreedom.plex.admin; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import dev.morphia.Datastore; +import dev.morphia.query.Query; +import dev.morphia.query.experimental.filters.Filters; +import dev.morphia.query.internal.MorphiaCursor; +import me.totalfreedom.plex.Plex; +import me.totalfreedom.plex.player.PlexPlayer; +import me.totalfreedom.plex.rank.enums.Rank; +import me.totalfreedom.plex.storage.StorageType; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +public class AdminList +{ + + private final Map admins = Maps.newHashMap(); + + public void addToCache(Admin admin) + { + admins.put(admin.getUuid(), admin); + } + + public void removeFromCache(UUID uuid) + { + admins.remove(uuid); + } + + + public List getAllAdmins() + { + List admins = Lists.newArrayList(); + if (Plex.get().getStorageType() == StorageType.MONGO) + { + Datastore store = Plex.get().getMongoConnection().getDatastore(); + Query query = store.find(PlexPlayer.class); + MorphiaCursor cursor = query.iterator(); + while (cursor.hasNext()) + { + PlexPlayer player = cursor.next(); + if (player.getRankFromString().isAtLeast(Rank.ADMIN)) + { + admins.add(player.getName()); + } + } + } else { + try (Connection con = Plex.get().getSqlConnection().getCon()) + { + PreparedStatement statement = con.prepareStatement("SELECT * FROM `players` WHERE rank IN(?, ?, ?)"); + statement.setString(1, Rank.ADMIN.name().toLowerCase()); + statement.setString(2, Rank.SENIOR_ADMIN.name().toLowerCase()); + statement.setString(3, Rank.EXECUTIVE.name().toLowerCase()); + + ResultSet set = statement.executeQuery(); + while (set.next()) + { + admins.add(set.getString("name")); + } + + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } + return admins; + } + +} diff --git a/src/main/java/me/totalfreedom/plex/cache/DataUtils.java b/src/main/java/me/totalfreedom/plex/cache/DataUtils.java new file mode 100644 index 0000000..ec26f38 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/cache/DataUtils.java @@ -0,0 +1,47 @@ +package me.totalfreedom.plex.cache; + +import me.totalfreedom.plex.Plex; +import me.totalfreedom.plex.player.PlexPlayer; +import me.totalfreedom.plex.storage.StorageType; + +import java.util.UUID; + +public class DataUtils +{ + + public static boolean hasPlayedBefore(UUID uuid) + { + if (Plex.get().getStorageType() == StorageType.MONGO) + { + return Plex.get().getMongoPlayerData().exists(uuid); + } else { + return Plex.get().getSqlPlayerData().exists(uuid); + } + } + + public static PlexPlayer getPlayer(UUID uuid) + { + if (PlayerCache.getPlexPlayerMap().containsKey(uuid)) + { + return PlayerCache.getPlexPlayerMap().get(uuid); + } + + if (Plex.get().getStorageType() == StorageType.MONGO) + { + return Plex.get().getMongoPlayerData().getByUUID(uuid); + } else { + return Plex.get().getSqlPlayerData().getByUUID(uuid); + } + } + + public static void update(PlexPlayer plexPlayer) + { + if (Plex.get().getStorageType() == StorageType.MONGO) + { + Plex.get().getMongoPlayerData().update(plexPlayer); + } else { + Plex.get().getSqlPlayerData().update(plexPlayer); + } + } + +} diff --git a/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java b/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java index cb24742..e4499c5 100644 --- a/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java +++ b/src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java @@ -55,6 +55,7 @@ public class MongoPlayerData updateOps.execute(); } + public void save(PlexPlayer plexPlayer) { datastore.save(plexPlayer); diff --git a/src/main/java/me/totalfreedom/plex/cache/PlayerCache.java b/src/main/java/me/totalfreedom/plex/cache/PlayerCache.java index be3bda9..f7f50ac 100644 --- a/src/main/java/me/totalfreedom/plex/cache/PlayerCache.java +++ b/src/main/java/me/totalfreedom/plex/cache/PlayerCache.java @@ -1,11 +1,12 @@ package me.totalfreedom.plex.cache; import com.google.common.collect.Maps; -import java.util.Map; -import java.util.UUID; import me.totalfreedom.plex.player.PlexPlayer; import me.totalfreedom.plex.player.PunishedPlayer; +import java.util.Map; +import java.util.UUID; + public class PlayerCache { private static final Map plexPlayerMap = Maps.newHashMap(); diff --git a/src/main/java/me/totalfreedom/plex/command/PlexCommand.java b/src/main/java/me/totalfreedom/plex/command/PlexCommand.java index db2a657..7c3dcc7 100644 --- a/src/main/java/me/totalfreedom/plex/command/PlexCommand.java +++ b/src/main/java/me/totalfreedom/plex/command/PlexCommand.java @@ -73,21 +73,21 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC PlexLog.log(this.sender.getSender().getName()); if (commandSource == RequiredCommandSource.CONSOLE && sender instanceof Player) { - send(tl("noPermissionInGame")); + sender.sendMessage(tl("noPermissionInGame")); return true; } if (commandSource == RequiredCommandSource.IN_GAME) { if (sender instanceof ConsoleCommandSender) { - send(tl("noPermissionConsole")); + sender.sendMessage(tl("noPermissionConsole")); return true; } Player player = (Player) sender; PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId()); if (!plexPlayer.getRankFromString().isAtLeast(getLevel())) { - send(tl("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMSG()))); + sender.sendMessage(tl("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMSG()))); return true; } } @@ -161,6 +161,11 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC player.sendMessage(s); } + protected String usage(String s) + { + return ChatColor.YELLOW + "Correct Usage: " + ChatColor.GRAY + s; + } + protected void send(String s) { if (sender == null) diff --git a/src/main/java/me/totalfreedom/plex/command/impl/AdminCMD.java b/src/main/java/me/totalfreedom/plex/command/impl/AdminCMD.java new file mode 100644 index 0000000..25fb77a --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/command/impl/AdminCMD.java @@ -0,0 +1,158 @@ +package me.totalfreedom.plex.command.impl; + +import com.google.common.collect.ImmutableList; +import me.totalfreedom.plex.cache.DataUtils; +import me.totalfreedom.plex.command.PlexCommand; +import me.totalfreedom.plex.command.annotation.CommandParameters; +import me.totalfreedom.plex.command.annotation.CommandPermissions; +import me.totalfreedom.plex.command.source.CommandSource; +import me.totalfreedom.plex.command.source.RequiredCommandSource; +import me.totalfreedom.plex.event.AdminAddEvent; +import me.totalfreedom.plex.event.AdminRemoveEvent; +import me.totalfreedom.plex.event.AdminSetRankEvent; +import me.totalfreedom.plex.player.PlexPlayer; +import me.totalfreedom.plex.rank.enums.Rank; +import me.totalfreedom.plex.util.PlexUtils; +import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; + +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +@CommandPermissions(level = Rank.SENIOR_ADMIN, source = RequiredCommandSource.CONSOLE) +@CommandParameters(usage = "/ [player] [rank]", aliases = "adminconfig,adminmanage", description = "Manages all admins") +public class AdminCMD extends PlexCommand +{ + public AdminCMD() { + super("admin"); + } + + @Override + public void execute(CommandSource sender, String[] args) + { + if (args.length == 0) + { + sender.send(usage(getUsage())); + return; + } + + if (args[0].equalsIgnoreCase("add")) + { + if (args.length != 2) + { + sender.send(usage("/admin add ")); + return; + } + + UUID targetUUID = PlexUtils.getFromName(args[1]); + + if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID)) + { + sender.send(PlexUtils.tl("playerNotFound")); + return; + } + PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID); + plexPlayer.setRank(Rank.ADMIN.name()); + DataUtils.update(plexPlayer); + Bukkit.getServer().getPluginManager().callEvent(new AdminAddEvent(plexPlayer)); + return; + } + if (args[0].equalsIgnoreCase("remove")) + { + if (args.length != 2) + { + sender.send(usage("/admin remove ")); + return; + } + + UUID targetUUID = PlexUtils.getFromName(args[1]); + + if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID)) + { + sender.send(PlexUtils.tl("playerNotFound")); + return; + } + PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID); + plexPlayer.setRank(""); + DataUtils.update(plexPlayer); + Bukkit.getServer().getPluginManager().callEvent(new AdminRemoveEvent(plexPlayer)); + return; + } + + if (args[0].equalsIgnoreCase("setrank")) + { + if (args.length != 3) + { + sender.send(usage("/admin setrank ")); + return; + } + + UUID targetUUID = PlexUtils.getFromName(args[1]); + + if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID)) + { + sender.send(PlexUtils.tl("playerNotFound")); + return; + } + + if (!rankExists(args[2])) + { + sender.send("Rank not found"); + return; + } + + Rank rank = Rank.valueOf(args[2].toUpperCase()); + + if (!rank.isAtLeast(Rank.ADMIN)) + { + sender.send("Must be admin+"); + return; + } + + PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID); + plexPlayer.setRank(rank.name().toLowerCase()); + DataUtils.update(plexPlayer); + + Bukkit.getServer().getPluginManager().callEvent(new AdminSetRankEvent(plexPlayer, rank)); + + return; + } + + if (args[0].equalsIgnoreCase("list")) + { + if (args.length != 1) + { + sender.send(usage("/admin list")); + return; + } + + sender.send("Admins: " + StringUtils.join(plugin.getAdminList().getAllAdmins(), ", ")); + return; + } + } + + @Override + public List onTabComplete(CommandSource sender, String[] args) { + if (args.length == 1) + { + return Arrays.asList("add", "remove", "setrank", "list"); + } else if (args.length == 2 && !args[0].equalsIgnoreCase("list")) + { + return PlexUtils.getPlayerNameList(); + } + return ImmutableList.of(); + } + + private boolean rankExists(String rank) + { + for (Rank ranks : Rank.values()) + { + if (ranks.name().equalsIgnoreCase(rank)) + { + return true; + } + } + return false; + } +} diff --git a/src/main/java/me/totalfreedom/plex/command/impl/FionnCMD.java b/src/main/java/me/totalfreedom/plex/command/impl/FionnCMD.java index 5fedb84..8761880 100644 --- a/src/main/java/me/totalfreedom/plex/command/impl/FionnCMD.java +++ b/src/main/java/me/totalfreedom/plex/command/impl/FionnCMD.java @@ -1,5 +1,6 @@ package me.totalfreedom.plex.command.impl; +import com.google.common.collect.ImmutableList; import me.totalfreedom.plex.cache.PlayerCache; import me.totalfreedom.plex.command.PlexCommand; import me.totalfreedom.plex.command.annotation.CommandParameters; @@ -119,7 +120,7 @@ public class FionnCMD extends PlexCommand @Override public List onTabComplete(CommandSource sender, String[] args) { - return null; + return ImmutableList.of(); } public static void lateFakeChat(String name, String message, ChatColor color, int delay) diff --git a/src/main/java/me/totalfreedom/plex/command/impl/FreezeCMD.java b/src/main/java/me/totalfreedom/plex/command/impl/FreezeCMD.java index d07c4b1..e39c744 100644 --- a/src/main/java/me/totalfreedom/plex/command/impl/FreezeCMD.java +++ b/src/main/java/me/totalfreedom/plex/command/impl/FreezeCMD.java @@ -1,5 +1,6 @@ package me.totalfreedom.plex.command.impl; +import com.google.common.collect.ImmutableList; import me.totalfreedom.plex.cache.PlayerCache; import me.totalfreedom.plex.command.PlexCommand; import me.totalfreedom.plex.command.annotation.CommandParameters; @@ -39,6 +40,6 @@ public class FreezeCMD extends PlexCommand @Override public List onTabComplete(CommandSource sender, String[] args) { - return args.length == 1 ? PlexUtils.getPlayerNameList() : null; + return args.length == 1 ? PlexUtils.getPlayerNameList() : ImmutableList.of(); } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/plex/command/impl/NameHistoryCMD.java b/src/main/java/me/totalfreedom/plex/command/impl/NameHistoryCMD.java index c886e65..1d59f40 100644 --- a/src/main/java/me/totalfreedom/plex/command/impl/NameHistoryCMD.java +++ b/src/main/java/me/totalfreedom/plex/command/impl/NameHistoryCMD.java @@ -1,5 +1,6 @@ package me.totalfreedom.plex.command.impl; +import com.google.common.collect.ImmutableList; import me.totalfreedom.plex.command.PlexCommand; import me.totalfreedom.plex.command.annotation.CommandParameters; import me.totalfreedom.plex.command.annotation.CommandPermissions; @@ -7,9 +8,6 @@ import me.totalfreedom.plex.command.exception.CommandArgumentException; import me.totalfreedom.plex.command.source.CommandSource; import me.totalfreedom.plex.rank.enums.Rank; import me.totalfreedom.plex.util.PlexUtils; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.entity.Player; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.ParseException; @@ -73,6 +71,6 @@ public class NameHistoryCMD extends PlexCommand @Override public List onTabComplete(CommandSource sender, String[] args) { - return args.length == 1 ? PlexUtils.getPlayerNameList() : null; + return args.length == 1 ? PlexUtils.getPlayerNameList() : ImmutableList.of(); } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/plex/command/impl/OpAllCMD.java b/src/main/java/me/totalfreedom/plex/command/impl/OpAllCMD.java index 7557150..a746597 100644 --- a/src/main/java/me/totalfreedom/plex/command/impl/OpAllCMD.java +++ b/src/main/java/me/totalfreedom/plex/command/impl/OpAllCMD.java @@ -1,5 +1,6 @@ package me.totalfreedom.plex.command.impl; +import com.google.common.collect.ImmutableList; import me.totalfreedom.plex.command.PlexCommand; import me.totalfreedom.plex.command.annotation.CommandParameters; import me.totalfreedom.plex.command.annotation.CommandPermissions; @@ -32,6 +33,6 @@ public class OpAllCMD extends PlexCommand @Override public List onTabComplete(CommandSource sender, String[] args) { - return null; + return ImmutableList.of(); } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/plex/command/impl/OpCMD.java b/src/main/java/me/totalfreedom/plex/command/impl/OpCMD.java index 1b0dcf5..623709b 100644 --- a/src/main/java/me/totalfreedom/plex/command/impl/OpCMD.java +++ b/src/main/java/me/totalfreedom/plex/command/impl/OpCMD.java @@ -1,5 +1,6 @@ package me.totalfreedom.plex.command.impl; +import com.google.common.collect.ImmutableList; import me.totalfreedom.plex.command.PlexCommand; import me.totalfreedom.plex.command.annotation.CommandParameters; import me.totalfreedom.plex.command.annotation.CommandPermissions; @@ -35,6 +36,6 @@ public class OpCMD extends PlexCommand @Override public List onTabComplete(CommandSource sender, String[] args) { - return args.length == 1 ? PlexUtils.getPlayerNameList() : null; + return args.length == 1 ? PlexUtils.getPlayerNameList() : ImmutableList.of(); } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/plex/command/impl/PlexCMD.java b/src/main/java/me/totalfreedom/plex/command/impl/PlexCMD.java index ce47aee..9c81d94 100644 --- a/src/main/java/me/totalfreedom/plex/command/impl/PlexCMD.java +++ b/src/main/java/me/totalfreedom/plex/command/impl/PlexCMD.java @@ -1,18 +1,17 @@ package me.totalfreedom.plex.command.impl; +import me.totalfreedom.plex.command.PlexCommand; import me.totalfreedom.plex.command.annotation.CommandParameters; import me.totalfreedom.plex.command.annotation.CommandPermissions; -import me.totalfreedom.plex.command.PlexCommand; import me.totalfreedom.plex.command.source.CommandSource; import me.totalfreedom.plex.command.source.RequiredCommandSource; import me.totalfreedom.plex.rank.enums.Rank; -import org.bukkit.command.CommandSender; import java.util.Arrays; import java.util.List; @CommandPermissions(level = Rank.OP, source = RequiredCommandSource.ANY) -@CommandParameters(usage = "/", aliases = "plexhelp", description = "Help with plex") +@CommandParameters(aliases = "plexhelp", description = "Help with plex") public class PlexCMD extends PlexCommand { public PlexCMD() { diff --git a/src/main/java/me/totalfreedom/plex/event/AdminAddEvent.java b/src/main/java/me/totalfreedom/plex/event/AdminAddEvent.java new file mode 100644 index 0000000..cdca741 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/event/AdminAddEvent.java @@ -0,0 +1,27 @@ +package me.totalfreedom.plex.event; + +import me.totalfreedom.plex.player.PlexPlayer; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class AdminAddEvent extends Event +{ + private final HandlerList handlers = new HandlerList(); + + private PlexPlayer plexPlayer; + + public AdminAddEvent(PlexPlayer plexPlayer) + { + this.plexPlayer = plexPlayer; + } + + @Override + public HandlerList getHandlers() + { + return handlers; + } + + public PlexPlayer getPlexPlayer() { + return plexPlayer; + } +} diff --git a/src/main/java/me/totalfreedom/plex/event/AdminRemoveEvent.java b/src/main/java/me/totalfreedom/plex/event/AdminRemoveEvent.java new file mode 100644 index 0000000..e78bc2f --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/event/AdminRemoveEvent.java @@ -0,0 +1,27 @@ +package me.totalfreedom.plex.event; + +import me.totalfreedom.plex.player.PlexPlayer; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class AdminRemoveEvent extends Event +{ + private final HandlerList handlers = new HandlerList(); + + private PlexPlayer plexPlayer; + + public AdminRemoveEvent(PlexPlayer plexPlayer) + { + this.plexPlayer = plexPlayer; + } + + @Override + public HandlerList getHandlers() + { + return handlers; + } + + public PlexPlayer getPlexPlayer() { + return plexPlayer; + } +} diff --git a/src/main/java/me/totalfreedom/plex/event/AdminSetRankEvent.java b/src/main/java/me/totalfreedom/plex/event/AdminSetRankEvent.java new file mode 100644 index 0000000..7099f49 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/event/AdminSetRankEvent.java @@ -0,0 +1,34 @@ +package me.totalfreedom.plex.event; + +import me.totalfreedom.plex.player.PlexPlayer; +import me.totalfreedom.plex.rank.enums.Rank; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class AdminSetRankEvent extends Event +{ + private final HandlerList handlers = new HandlerList(); + + private PlexPlayer plexPlayer; + private Rank rank; + + public AdminSetRankEvent(PlexPlayer plexPlayer, Rank rank) + { + this.plexPlayer = plexPlayer; + this.rank = rank; + } + + @Override + public HandlerList getHandlers() + { + return handlers; + } + + public PlexPlayer getPlexPlayer() { + return plexPlayer; + } + + public Rank getRank() { + return rank; + } +} diff --git a/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java b/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java index e86eb55..9de1218 100644 --- a/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java +++ b/src/main/java/me/totalfreedom/plex/handlers/CommandHandler.java @@ -22,6 +22,7 @@ public class CommandHandler commands.add(new OpCMD()); commands.add(new FreezeCMD()); commands.add(new NameHistoryCMD()); + commands.add(new AdminCMD()); PlexLog.log(String.format("Registered %s commands!", commands.size())); } diff --git a/src/main/java/me/totalfreedom/plex/listener/impl/PlayerListener.java b/src/main/java/me/totalfreedom/plex/listener/impl/PlayerListener.java index b0ac300..693ad95 100644 --- a/src/main/java/me/totalfreedom/plex/listener/impl/PlayerListener.java +++ b/src/main/java/me/totalfreedom/plex/listener/impl/PlayerListener.java @@ -1,7 +1,10 @@ package me.totalfreedom.plex.listener.impl; import java.util.Arrays; +import java.util.UUID; + import me.totalfreedom.plex.Plex; +import me.totalfreedom.plex.admin.Admin; import me.totalfreedom.plex.cache.MongoPlayerData; import me.totalfreedom.plex.cache.PlayerCache; import me.totalfreedom.plex.cache.SQLPlayerData; @@ -73,6 +76,11 @@ public class PlayerListener extends PlexListener if (plugin.getRankManager().isAdmin(plexPlayer)) { + Admin admin = new Admin(UUID.fromString(plexPlayer.getUuid())); + admin.setRank(plexPlayer.getRankFromString()); + + plugin.getAdminList().addToCache(admin); + if (!plexPlayer.getLoginMSG().isEmpty()) { event.setJoinMessage(ChatColor.AQUA + player.getName() + " is " + plexPlayer.getLoginMSG()); @@ -89,6 +97,11 @@ public class PlayerListener extends PlexListener { PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId()); //get the player because it's literally impossible for them to not have an object + if (plugin.getRankManager().isAdmin(plexPlayer)) + { + plugin.getAdminList().removeFromCache(UUID.fromString(plexPlayer.getUuid())); + } + if (mongoPlayerData != null) //back to mongo checking { mongoPlayerData.update(plexPlayer); //update the player's document @@ -103,6 +116,7 @@ public class PlayerListener extends PlexListener PlayerCache.getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache PlayerCache.getPunishedPlayerMap().remove(event.getPlayer().getUniqueId()); + } // unrelated player quitting diff --git a/src/main/java/me/totalfreedom/plex/listener/impl/WorldListener.java b/src/main/java/me/totalfreedom/plex/listener/impl/WorldListener.java index 004c9bc..bcd1a54 100644 --- a/src/main/java/me/totalfreedom/plex/listener/impl/WorldListener.java +++ b/src/main/java/me/totalfreedom/plex/listener/impl/WorldListener.java @@ -21,7 +21,7 @@ public class WorldListener extends PlexListener Player player = e.getPlayer(); PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId()); World world = player.getWorld(); - switch (world.getName()) + switch (world.getName().toLowerCase()) { case "adminworld": { diff --git a/src/main/java/me/totalfreedom/plex/util/PlexUtils.java b/src/main/java/me/totalfreedom/plex/util/PlexUtils.java index 12916ba..5cf14a1 100644 --- a/src/main/java/me/totalfreedom/plex/util/PlexUtils.java +++ b/src/main/java/me/totalfreedom/plex/util/PlexUtils.java @@ -8,6 +8,7 @@ import java.net.URL; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.UUID; import java.util.stream.Collectors; import me.totalfreedom.plex.Plex; @@ -18,6 +19,7 @@ import org.bukkit.command.Command; import org.bukkit.command.PluginCommandYamlParser; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; +import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -157,4 +159,17 @@ public class PlexUtils connection.disconnect(); return new JSONParser().parse(content.toString()); } + + public static UUID getFromName(String name) + { + JSONObject profile; + try { + profile = (JSONObject) PlexUtils.simpleGET("https://api.ashcon.app/mojang/v2/user/" + name); + } catch (IOException | ParseException e) { + e.printStackTrace(); + return null; + } + String uuidString = (String) profile.get("uuid"); + return UUID.fromString(uuidString); + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 7d1f945..1987e76 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,9 @@ # Plex Configuration File server: + name: "TotalFreedom" test: "test string" + motd: "&~%servername% &8- %mcversion%" data: central: