From cf811cee863e3f105d2b0cdceb358733a708937a Mon Sep 17 00:00:00 2001 From: ryanhamshire Date: Wed, 28 Oct 2015 20:13:33 -0700 Subject: [PATCH] Smarter slash command lists. Whisper and chat slash command lists now auto-fill themselves with all possible aliases. So if you put /tell in your whisper commands list, GP will be smart enough to also add /minecraft:tell (and similarly for plugins, including all aliases registered via plugin.yml for those commands by those plugins). --- .../GriefPrevention/CommandCategory.java | 8 ++ .../GriefPrevention/GriefPrevention.java | 16 ++-- .../GriefPrevention/PlayerEventHandler.java | 83 ++++++++++++++++--- 3 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 src/me/ryanhamshire/GriefPrevention/CommandCategory.java diff --git a/src/me/ryanhamshire/GriefPrevention/CommandCategory.java b/src/me/ryanhamshire/GriefPrevention/CommandCategory.java new file mode 100644 index 0000000..417c1ac --- /dev/null +++ b/src/me/ryanhamshire/GriefPrevention/CommandCategory.java @@ -0,0 +1,8 @@ +package me.ryanhamshire.GriefPrevention; + +enum CommandCategory +{ + Chat, + Whisper, + None +} diff --git a/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java b/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java index eb91a39..106857d 100644 --- a/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java +++ b/src/me/ryanhamshire/GriefPrevention/GriefPrevention.java @@ -529,7 +529,8 @@ public class GriefPrevention extends JavaPlugin this.config_spam_allowedIpAddresses = config.getString("GriefPrevention.Spam.AllowedIpAddresses", "1.2.3.4; 5.6.7.8"); this.config_spam_banOffenders = config.getBoolean("GriefPrevention.Spam.BanOffenders", true); this.config_spam_banMessage = config.getString("GriefPrevention.Spam.BanMessage", "Banned for spam."); - String slashCommandsToMonitor = config.getString("GriefPrevention.Spam.MonitorSlashCommands", "/me;/tell;/global;/local;/w;/msg;/r;/t"); + String slashCommandsToMonitor = config.getString("GriefPrevention.Spam.MonitorSlashCommands", "/me;/global;/local"); + slashCommandsToMonitor = config.getString("GriefPrevention.Spam.ChatSlashCommands", slashCommandsToMonitor); this.config_spam_deathMessageCooldownSeconds = config.getInt("GriefPrevention.Spam.DeathMessageCooldownSeconds", 60); this.config_pvp_protectFreshSpawns = config.getBoolean("GriefPrevention.PvP.ProtectFreshSpawns", true); @@ -556,7 +557,8 @@ public class GriefPrevention extends JavaPlugin this.config_whisperNotifications = config.getBoolean("GriefPrevention.AdminsGetWhispers", true); this.config_signNotifications = config.getBoolean("GriefPrevention.AdminsGetSignNotifications", true); - String whisperCommandsToMonitor = config.getString("GriefPrevention.WhisperCommands", "/tell;/pm;/r;/w;/whisper;/t;/msg"); + String whisperCommandsToMonitor = config.getString("GriefPrevention.WhisperCommands", "/tell;/pm;/r;/whisper;/msg"); + whisperCommandsToMonitor = config.getString("GriefPrevention.Spam.WhisperSlashCommands", whisperCommandsToMonitor); this.config_smartBan = config.getBoolean("GriefPrevention.SmartBan", true); this.config_ipLimit = config.getInt("GriefPrevention.MaxPlayersPerIpAddress", 3); @@ -753,7 +755,8 @@ public class GriefPrevention extends JavaPlugin outConfig.set("GriefPrevention.Spam.Enabled", this.config_spam_enabled); outConfig.set("GriefPrevention.Spam.LoginCooldownSeconds", this.config_spam_loginCooldownSeconds); - outConfig.set("GriefPrevention.Spam.MonitorSlashCommands", slashCommandsToMonitor); + outConfig.set("GriefPrevention.Spam.ChatSlashCommands", slashCommandsToMonitor); + outConfig.set("GriefPrevention.Spam.WhisperSlashCommands", whisperCommandsToMonitor); outConfig.set("GriefPrevention.Spam.WarningMessage", this.config_spam_warningMessage); outConfig.set("GriefPrevention.Spam.BanOffenders", this.config_spam_banOffenders); outConfig.set("GriefPrevention.Spam.BanMessage", this.config_spam_banMessage); @@ -794,7 +797,6 @@ public class GriefPrevention extends JavaPlugin outConfig.set("GriefPrevention.AdminsGetWhispers", this.config_whisperNotifications); outConfig.set("GriefPrevention.AdminsGetSignNotifications", this.config_signNotifications); - outConfig.set("GriefPrevention.WhisperCommands", whisperCommandsToMonitor); outConfig.set("GriefPrevention.SmartBan", this.config_smartBan); outConfig.set("GriefPrevention.MaxPlayersPerIpAddress", this.config_ipLimit); @@ -851,7 +853,7 @@ public class GriefPrevention extends JavaPlugin commands = slashCommandsToMonitor.split(";"); for(int i = 0; i < commands.length; i++) { - this.config_spam_monitorSlashCommands.add(commands[i].trim()); + this.config_spam_monitorSlashCommands.add(commands[i].trim().toLowerCase()); } //try to parse the list of commands which should be included in eavesdropping @@ -859,7 +861,7 @@ public class GriefPrevention extends JavaPlugin commands = whisperCommandsToMonitor.split(";"); for(int i = 0; i < commands.length; i++) { - this.config_eavesdrop_whisperCommands.add(commands[i].trim()); + this.config_eavesdrop_whisperCommands.add(commands[i].trim().toLowerCase()); } //try to parse the list of commands which should be banned during pvp combat @@ -867,7 +869,7 @@ public class GriefPrevention extends JavaPlugin commands = bannedPvPCommandsList.split(";"); for(int i = 0; i < commands.length; i++) { - this.config_pvp_blockedCommands.add(commands[i].trim()); + this.config_pvp_blockedCommands.add(commands[i].trim().toLowerCase()); } } diff --git a/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java b/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java index d442102..c8afd19 100644 --- a/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java +++ b/src/me/ryanhamshire/GriefPrevention/PlayerEventHandler.java @@ -47,6 +47,7 @@ import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; +import org.bukkit.command.Command; import org.bukkit.entity.Animals; import org.bukkit.entity.Boat; import org.bukkit.entity.Creature; @@ -71,6 +72,8 @@ import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import org.bukkit.metadata.MetadataValue; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.util.BlockIterator; class PlayerEventHandler implements Listener @@ -484,11 +487,13 @@ class PlayerEventHandler implements Listener String command = args[0].toLowerCase(); + CommandCategory category = this.getCommandCategory(command); + Player player = event.getPlayer(); PlayerData playerData = null; //if a whisper - if(GriefPrevention.instance.config_eavesdrop_whisperCommands.contains(command) && args.length > 1) + if(category == CommandCategory.Whisper && args.length > 1) { //determine target player, might be NULL Player targetPlayer = GriefPrevention.instance.getServer().getPlayer(args[1]); @@ -551,16 +556,7 @@ class PlayerEventHandler implements Listener } //if the slash command used is in the list of monitored commands, treat it like a chat message (see above) - boolean isMonitoredCommand = false; - for(String monitoredCommand : GriefPrevention.instance.config_spam_monitorSlashCommands) - { - if(args[0].equalsIgnoreCase(monitoredCommand)) - { - isMonitoredCommand = true; - break; - } - } - + boolean isMonitoredCommand = (category == CommandCategory.Chat || category == CommandCategory.Whisper); if(isMonitoredCommand) { //if anti spam enabled, check for spam @@ -578,7 +574,7 @@ class PlayerEventHandler implements Listener builder.append(arg + " "); } - this.makeSocialLogEntry(event.getPlayer().getName(), builder.toString()); + makeSocialLogEntry(event.getPlayer().getName(), builder.toString()); } } @@ -610,7 +606,68 @@ class PlayerEventHandler implements Listener } } - static int longestNameLength = 10; + private ConcurrentHashMap commandCategoryMap = new ConcurrentHashMap(); + private CommandCategory getCommandCategory(String commandName) + { + if(commandName.startsWith("/")) commandName = commandName.substring(1); + + //if we've seen this command or alias before, return the category determined previously + CommandCategory category = this.commandCategoryMap.get(commandName); + if(category != null) return category; + + //otherwise build a list of all the aliases of this command across all installed plugins + HashSet aliases = new HashSet(); + aliases.add(commandName); + for(Plugin plugin : Bukkit.getServer().getPluginManager().getPlugins()) + { + JavaPlugin javaPlugin = (JavaPlugin)plugin; + Command command = javaPlugin.getCommand(commandName); + if(command != null) + { + aliases.add(command.getName().toLowerCase()); + aliases.add(plugin.getName().toLowerCase() + ":" + command.getName().toLowerCase()); + for(String alias : command.getAliases()) + { + aliases.add(alias.toLowerCase()); + aliases.add(plugin.getName().toLowerCase() + ":" + alias.toLowerCase()); + } + } + } + + //also consider vanilla commands + Command command = Bukkit.getServer().getPluginCommand(commandName); + if(command != null) + { + aliases.add(command.getName().toLowerCase()); + aliases.add("minecraft:" + command.getName().toLowerCase()); + for(String alias : command.getAliases()) + { + aliases.add(alias.toLowerCase()); + aliases.add("minecraft:" + alias.toLowerCase()); + } + } + + //if any of those aliases are in the chat list or whisper list, then we know the category for that command + category = CommandCategory.None; + for(String alias : aliases) + { + if(GriefPrevention.instance.config_eavesdrop_whisperCommands.contains("/" + alias)) + { + category = CommandCategory.Whisper; + } + else if(GriefPrevention.instance.config_spam_monitorSlashCommands.contains("/" + alias)) + { + category = CommandCategory.Chat; + } + + //remember the categories for later + this.commandCategoryMap.put(alias.toLowerCase(), category); + } + + return category; + } + + static int longestNameLength = 10; static void makeSocialLogEntry(String name, String message) { StringBuilder entryBuilder = new StringBuilder(name);