From 583394d22e9af1fd6e4ed6139d556291e78f4bf5 Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Fri, 8 Apr 2022 01:16:05 -0400 Subject: [PATCH] part 1 --- src/main/java/dev/plex/Plex.java | 8 ++++ .../java/dev/plex/cmdblocker/BaseCommand.java | 17 +++++++ .../cmdblocker/CommandBlockerManager.java | 46 +++++++++++++++++++ .../dev/plex/cmdblocker/MatchCommand.java | 16 +++++++ .../dev/plex/cmdblocker/RegexCommand.java | 18 ++++++++ .../java/dev/plex/command/impl/PlexCMD.java | 6 ++- .../listener/impl/CmdBlockerListener.java | 19 ++++++++ src/main/resources/commands.yml | 19 ++++++++ src/main/resources/messages.yml | 3 +- 9 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 src/main/java/dev/plex/cmdblocker/BaseCommand.java create mode 100644 src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java create mode 100644 src/main/java/dev/plex/cmdblocker/MatchCommand.java create mode 100644 src/main/java/dev/plex/cmdblocker/RegexCommand.java create mode 100644 src/main/java/dev/plex/listener/impl/CmdBlockerListener.java create mode 100644 src/main/resources/commands.yml diff --git a/src/main/java/dev/plex/Plex.java b/src/main/java/dev/plex/Plex.java index cd19578..cdbf957 100644 --- a/src/main/java/dev/plex/Plex.java +++ b/src/main/java/dev/plex/Plex.java @@ -8,6 +8,7 @@ import dev.plex.cache.player.PlayerCache; import dev.plex.cache.player.SQLPlayerData; import dev.plex.cache.sql.SQLNotes; import dev.plex.cache.sql.SQLPunishment; +import dev.plex.cmdblocker.CommandBlockerManager; import dev.plex.config.Config; import dev.plex.handlers.CommandHandler; import dev.plex.handlers.ListenerHandler; @@ -44,6 +45,7 @@ public class Plex extends JavaPlugin public Config config; public Config messages; public Config indefBans; + public Config blockedCommands; public File modulesFolder; private StorageType storageType = StorageType.SQLITE; @@ -62,6 +64,7 @@ public class Plex extends JavaPlugin private RankManager rankManager; private ServiceManager serviceManager; private PunishmentManager punishmentManager; + private CommandBlockerManager commandBlockerManager; private AdminList adminList; private UpdateChecker updateChecker; @@ -79,6 +82,7 @@ public class Plex extends JavaPlugin config = new Config(this, "config.yml"); messages = new Config(this, "messages.yml"); indefBans = new Config(this, "indefbans.yml"); + blockedCommands = new Config(this, "commands.yml"); build.load(this); modulesFolder = new File(this.getDataFolder() + File.separator + "modules"); @@ -99,6 +103,7 @@ public class Plex extends JavaPlugin messages.load(); // Don't add default entries to indefinite ban file indefBans.load(false); + blockedCommands.load(); sqlConnection = new SQLConnection(); mongoConnection = new MongoConnection(); @@ -158,6 +163,9 @@ public class Plex extends JavaPlugin punishmentManager.mergeIndefiniteBans(); PlexLog.log("Punishment System initialized"); + commandBlockerManager = new CommandBlockerManager(); + PlexLog.log("Command Blocker initialized"); + generateWorlds(); serviceManager = new ServiceManager(); diff --git a/src/main/java/dev/plex/cmdblocker/BaseCommand.java b/src/main/java/dev/plex/cmdblocker/BaseCommand.java new file mode 100644 index 0000000..21d148a --- /dev/null +++ b/src/main/java/dev/plex/cmdblocker/BaseCommand.java @@ -0,0 +1,17 @@ +package dev.plex.cmdblocker; + +import dev.plex.rank.enums.Rank; +import lombok.Getter; + +@Getter +public class BaseCommand +{ + public final Rank rank; + public final String message; + + public BaseCommand(Rank r, String m) + { + rank = r; + message = m; + } +} diff --git a/src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java b/src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java new file mode 100644 index 0000000..5d56df2 --- /dev/null +++ b/src/main/java/dev/plex/cmdblocker/CommandBlockerManager.java @@ -0,0 +1,46 @@ +package dev.plex.cmdblocker; + +import dev.plex.Plex; +import dev.plex.rank.enums.Rank; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +public class CommandBlockerManager +{ + private List blockedCommands = new ArrayList<>(); + + public void syncCommands() + { + blockedCommands.clear(); + + List raw = Plex.get().blockedCommands.getStringList("blockedCommands"); + + for (String cmd : raw) + { + List pieces = new ArrayList<>(); + + int lastDelim = cmd.lastIndexOf(':'); + + String cmdWithoutMsg = cmd.substring(0, lastDelim); + String[] rawPieces = cmdWithoutMsg.split(":", 3); + + pieces.add(rawPieces[0].toLowerCase()); // type + pieces.add(rawPieces[1].toLowerCase()); // rank + pieces.add(rawPieces[2]); // RegEx or match + pieces.add(cmd.substring(lastDelim + 1)); // Message (w/o : in it) + + Rank rank = Plex.get().getRankManager().getRankFromString(pieces.get(1)); + + if (pieces.get(0).equals("r")) + { + blockedCommands.add(new RegexCommand(Pattern.compile(pieces.get(2)), rank, pieces.get(3))); + } + else if (pieces.get(0).equals("m")) + { + blockedCommands.add(new MatchCommand(pieces.get(2), rank, pieces.get(3))); + } + } + } +} diff --git a/src/main/java/dev/plex/cmdblocker/MatchCommand.java b/src/main/java/dev/plex/cmdblocker/MatchCommand.java new file mode 100644 index 0000000..96ec6cf --- /dev/null +++ b/src/main/java/dev/plex/cmdblocker/MatchCommand.java @@ -0,0 +1,16 @@ +package dev.plex.cmdblocker; + +import dev.plex.rank.enums.Rank; +import lombok.Getter; + +@Getter +public class MatchCommand extends BaseCommand +{ + public final String match; + + public MatchCommand(String r1, Rank r2, String m1) + { + super(r2, m1); + match = r1; + } +} diff --git a/src/main/java/dev/plex/cmdblocker/RegexCommand.java b/src/main/java/dev/plex/cmdblocker/RegexCommand.java new file mode 100644 index 0000000..fd2c027 --- /dev/null +++ b/src/main/java/dev/plex/cmdblocker/RegexCommand.java @@ -0,0 +1,18 @@ +package dev.plex.cmdblocker; + +import dev.plex.rank.enums.Rank; +import lombok.Getter; + +import java.util.regex.Pattern; + +@Getter +public class RegexCommand extends BaseCommand +{ + public final Pattern regex; + + public RegexCommand(Pattern r1, Rank r2, String m1) + { + super(r2, m1); + regex = r1; + } +} \ No newline at end of file diff --git a/src/main/java/dev/plex/command/impl/PlexCMD.java b/src/main/java/dev/plex/command/impl/PlexCMD.java index c6baa8e..1bcb86d 100644 --- a/src/main/java/dev/plex/command/impl/PlexCMD.java +++ b/src/main/java/dev/plex/command/impl/PlexCMD.java @@ -18,6 +18,7 @@ import dev.plex.util.PlexLog; import dev.plex.util.PlexUtils; import net.kyori.adventure.text.Component; import org.apache.commons.lang.StringUtils; +import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -51,13 +52,16 @@ public class PlexCMD extends PlexCommand plugin.indefBans.load(false); plugin.getPunishmentManager().mergeIndefiniteBans(); send(sender, "Reloaded indefinite bans"); + plugin.blockedCommands.load(); + plugin.getCommandBlockerManager().syncCommands(); + send(sender, "Reloaded blocked commands file"); plugin.getRankManager().importDefaultRanks(); send(sender, "Imported ranks"); - send(sender, "Plex successfully reloaded."); plugin.setSystem(plugin.config.getString("system")); plugin.getServiceManager().endServices(); plugin.getServiceManager().startServices(); PlexLog.debug("Restarted services"); + send(sender, "Plex successfully reloaded."); return null; } else if (args[0].equalsIgnoreCase("redis")) diff --git a/src/main/java/dev/plex/listener/impl/CmdBlockerListener.java b/src/main/java/dev/plex/listener/impl/CmdBlockerListener.java new file mode 100644 index 0000000..831ab4b --- /dev/null +++ b/src/main/java/dev/plex/listener/impl/CmdBlockerListener.java @@ -0,0 +1,19 @@ +package dev.plex.listener.impl; + +import dev.plex.listener.PlexListener; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; + +public class CmdBlockerListener extends PlexListener +{ + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) + { + Player player = event.getPlayer(); + String message = event.getMessage().substring(1); + // check if commands are blocked here + // DEFAULT message is named "commandBlocked" + } +} diff --git a/src/main/resources/commands.yml b/src/main/resources/commands.yml new file mode 100644 index 0000000..f095f74 --- /dev/null +++ b/src/main/resources/commands.yml @@ -0,0 +1,19 @@ +# +# Command Blocker +# +# Format: +# - "::command name no slash:Block message" +# +# Symbols to use: +# - r for RegEx +# - m for matching +# - The ranks are "e" for everyone, "a" for admin and "s" for senior admin +# - The command is just the command without slashes. Optional arguments are specified as well +# - Finally the block message. MUST NOT CONTAIN ":". Use _ to use the default command blocked message as specified in messages.yml, or you can optionally put your own in +# +# So these would be valid: +# - "m:e:mail sendall:You cannot send messages to everyone on the server" +# - "r:e:(.*:):Plugin specific commands are disabled" +blockedCommands: + - "m:e:mail sendall:You cannot send messages to everyone on the server" + - "r:e:(.*:):Plugin specific commands are disabled" \ No newline at end of file diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 275138d..ac87e6f 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -166,4 +166,5 @@ noRemovedEntities: "No entities were removed." removedMobs: "{0} - Removed {1} mobs" autoWipeDisabled: "Item wiping is currently disabled in the config!" allowDropsDisabled: "No longer allowing drops from players." -allowDropsEnabled: "Now allowing drops from players." \ No newline at end of file +allowDropsEnabled: "Now allowing drops from players." +commandBlocked: "This command is blocked" \ No newline at end of file