From 9297eb6703341c6be755794a1c904c2ebd3c3f0e Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Fri, 8 Apr 2022 16:57:09 -0500 Subject: [PATCH] Try to improve things --- .../cmdblocker/CommandBlockerManager.java | 52 +++++++------------ .../plex/listener/impl/CommandListener.java | 9 ++-- src/main/java/dev/plex/player/PlexPlayer.java | 9 +--- .../dev/plex/services/ServiceManager.java | 2 +- ...ervice.java => CommandBlockerService.java} | 4 +- 5 files changed, 29 insertions(+), 47 deletions(-) rename src/main/java/dev/plex/services/impl/{CmdBlockerService.java => CommandBlockerService.java} (80%) diff --git a/src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java b/src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java index 8dfdd39..833bda8 100644 --- a/src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java +++ b/src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java @@ -1,10 +1,12 @@ package dev.plex.cmdblocker; import dev.plex.Plex; +import dev.plex.PlexBase; import dev.plex.rank.enums.Rank; import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; import lombok.Getter; +import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommandYamlParser; @@ -16,18 +18,18 @@ import java.util.List; import java.util.regex.Pattern; @Getter -public class CommandBlockerManager +public class CommandBlockerManager extends PlexBase { private List blockedCommands = new ArrayList<>(); - public boolean loadedYet = false; + public boolean loadedYet; public void syncCommands() { loadedYet = false; blockedCommands.clear(); - List raw = Plex.get().blockedCommands.getStringList("blockedCommands"); + List raw = plugin.blockedCommands.getStringList("blockedCommands"); for (String cmd : raw) { @@ -46,31 +48,12 @@ public class CommandBlockerManager message = PlexUtils.messageString("commandBlocked"); } - Rank rank; - - switch (rawRank) - { - case "i": - rank = Rank.IMPOSTOR; - break; - case "n": - rank = Rank.NONOP; - break; - case "o": - rank = Rank.OP; - break; - case "a": - rank = Rank.ADMIN; - break; - case "s": - rank = Rank.SENIOR_ADMIN; - break; - case "e": - rank = Rank.EXECUTIVE; - break; - default: - rank = Rank.EXECUTIVE; - } + Rank rank = switch (rawRank) + { + case "e" -> Rank.IMPOSTOR; + case "a" -> Rank.ADMIN; + case "s" -> Rank.SENIOR_ADMIN; + }; if (rawType.equals("r")) { @@ -82,7 +65,7 @@ public class CommandBlockerManager if (ind == -1 && regexOrMatch.endsWith(":")) { String pluginName = regexOrMatch.substring(0, regexOrMatch.length() - 1); - Plugin plugin = Arrays.stream(Plex.get().getServer().getPluginManager().getPlugins()).filter(pl -> pl.getName().equalsIgnoreCase(pluginName)).findAny().orElse(null); + Plugin plugin = Arrays.stream(Bukkit.getServer().getPluginManager().getPlugins()).filter(pl -> pl.getName().equalsIgnoreCase(pluginName)).findAny().orElse(null); if (plugin != null) { List commandList = PluginCommandYamlParser.parse(plugin); @@ -90,7 +73,8 @@ public class CommandBlockerManager { blockedCommands.add(new MatchCommand(command.getName(), rank, message)); blockedCommands.add(new MatchCommand(pluginName + ":" + command.getName(), rank, message)); - for (String alias : command.getAliases()) { + for (String alias : command.getAliases()) + { blockedCommands.add(new MatchCommand(alias, rank, message)); blockedCommands.add(new MatchCommand(pluginName + ":" + alias, rank, message)); } @@ -98,7 +82,10 @@ public class CommandBlockerManager } } String blockedArgs = ind == -1 ? "" : regexOrMatch.substring(ind + 1); - if (!blockedArgs.isEmpty()) blockedArgs = " " + blockedArgs; // necessary in case no args + if (!blockedArgs.isEmpty()) + { + blockedArgs = " " + blockedArgs; // necessary in case no args + } PluginCommand pluginCommand = Plex.get().getServer().getPluginCommand(ind == -1 ? regexOrMatch : regexOrMatch.substring(0, ind)); if (pluginCommand != null) { @@ -116,7 +103,8 @@ public class CommandBlockerManager } PlexLog.debug("Blocked commands:"); - for (BaseCommand blockedCommand : blockedCommands) { + for (BaseCommand blockedCommand : blockedCommands) + { PlexLog.debug(" - {0}", blockedCommand); } diff --git a/src/main/java/dev/plex/listener/impl/CommandListener.java b/src/main/java/dev/plex/listener/impl/CommandListener.java index 60c7f69..0ba5a0a 100644 --- a/src/main/java/dev/plex/listener/impl/CommandListener.java +++ b/src/main/java/dev/plex/listener/impl/CommandListener.java @@ -38,9 +38,10 @@ public class CommandListener extends PlexListener } Player player = event.getPlayer(); String message = event.getMessage().substring(1).stripLeading(); // stripLeading() is VITAL for workaround blocking (/ minecraft:summon) - for (BaseCommand blockedCommand : plugin.getCommandBlockerManager().getBlockedCommands()) { + for (BaseCommand blockedCommand : plugin.getCommandBlockerManager().getBlockedCommands()) + { PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId()); - if (!plexPlayer.getRepresentedRank().isAtMost(blockedCommand.getRank())) + if (!plexPlayer.getRankFromString().isAtMost(blockedCommand.getRank())) { continue; } @@ -53,9 +54,9 @@ public class CommandListener extends PlexListener isBlocked = true; } } - else if(blockedCommand instanceof MatchCommand matchCommand) + else if (blockedCommand instanceof MatchCommand matchCommand) { - if (message.toLowerCase().startsWith(matchCommand.getMatch().toLowerCase()) /*message.equalsIgnoreCase(matchCommand.getMatch()) || message.toLowerCase().startsWith(matchCommand.getMatch().toLowerCase() + " ")*/) + if (message.toLowerCase().startsWith(matchCommand.getMatch().toLowerCase())) { isBlocked = true; } diff --git a/src/main/java/dev/plex/player/PlexPlayer.java b/src/main/java/dev/plex/player/PlexPlayer.java index 9e9c3e3..4ae582b 100644 --- a/src/main/java/dev/plex/player/PlexPlayer.java +++ b/src/main/java/dev/plex/player/PlexPlayer.java @@ -98,7 +98,7 @@ public class PlexPlayer public Rank getRankFromString() { OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); - if (rank.isEmpty()) + if (rank.isEmpty() || !isAdminActive()) { if (player.isOp()) { @@ -115,13 +115,6 @@ public class PlexPlayer } } - public Rank getRepresentedRank() - { - int representedLevel = this.getRankFromString().getLevel(); - if (!this.adminActive) representedLevel = Math.min(Rank.OP.getLevel(), representedLevel); - return Rank.values()[representedLevel]; - } - public void loadPunishments() { if (Plex.get().getStorageType() != StorageType.MONGODB) diff --git a/src/main/java/dev/plex/services/ServiceManager.java b/src/main/java/dev/plex/services/ServiceManager.java index fc73390..2253f48 100644 --- a/src/main/java/dev/plex/services/ServiceManager.java +++ b/src/main/java/dev/plex/services/ServiceManager.java @@ -18,7 +18,7 @@ public class ServiceManager registerService(new GameRuleService()); registerService(new UpdateCheckerService()); registerService(new AutoWipeService()); - registerService(new CmdBlockerService()); + registerService(new CommandBlockerService()); } public void startServices() diff --git a/src/main/java/dev/plex/services/impl/CmdBlockerService.java b/src/main/java/dev/plex/services/impl/CommandBlockerService.java similarity index 80% rename from src/main/java/dev/plex/services/impl/CmdBlockerService.java rename to src/main/java/dev/plex/services/impl/CommandBlockerService.java index fc9a4eb..6116927 100644 --- a/src/main/java/dev/plex/services/impl/CmdBlockerService.java +++ b/src/main/java/dev/plex/services/impl/CommandBlockerService.java @@ -3,9 +3,9 @@ package dev.plex.services.impl; import dev.plex.services.AbstractService; import dev.plex.util.PlexLog; -public class CmdBlockerService extends AbstractService +public class CommandBlockerService extends AbstractService { - public CmdBlockerService() + public CommandBlockerService() { super(false, true); }