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).
This commit is contained in:
ryanhamshire 2015-10-28 20:13:33 -07:00
parent e98a123380
commit cf811cee86
3 changed files with 87 additions and 20 deletions

View File

@ -0,0 +1,8 @@
package me.ryanhamshire.GriefPrevention;
enum CommandCategory
{
Chat,
Whisper,
None
}

View File

@ -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_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_banOffenders = config.getBoolean("GriefPrevention.Spam.BanOffenders", true);
this.config_spam_banMessage = config.getString("GriefPrevention.Spam.BanMessage", "Banned for spam."); 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_spam_deathMessageCooldownSeconds = config.getInt("GriefPrevention.Spam.DeathMessageCooldownSeconds", 60);
this.config_pvp_protectFreshSpawns = config.getBoolean("GriefPrevention.PvP.ProtectFreshSpawns", true); 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_whisperNotifications = config.getBoolean("GriefPrevention.AdminsGetWhispers", true);
this.config_signNotifications = config.getBoolean("GriefPrevention.AdminsGetSignNotifications", 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_smartBan = config.getBoolean("GriefPrevention.SmartBan", true);
this.config_ipLimit = config.getInt("GriefPrevention.MaxPlayersPerIpAddress", 3); 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.Enabled", this.config_spam_enabled);
outConfig.set("GriefPrevention.Spam.LoginCooldownSeconds", this.config_spam_loginCooldownSeconds); 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.WarningMessage", this.config_spam_warningMessage);
outConfig.set("GriefPrevention.Spam.BanOffenders", this.config_spam_banOffenders); outConfig.set("GriefPrevention.Spam.BanOffenders", this.config_spam_banOffenders);
outConfig.set("GriefPrevention.Spam.BanMessage", this.config_spam_banMessage); 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.AdminsGetWhispers", this.config_whisperNotifications);
outConfig.set("GriefPrevention.AdminsGetSignNotifications", this.config_signNotifications); outConfig.set("GriefPrevention.AdminsGetSignNotifications", this.config_signNotifications);
outConfig.set("GriefPrevention.WhisperCommands", whisperCommandsToMonitor);
outConfig.set("GriefPrevention.SmartBan", this.config_smartBan); outConfig.set("GriefPrevention.SmartBan", this.config_smartBan);
outConfig.set("GriefPrevention.MaxPlayersPerIpAddress", this.config_ipLimit); outConfig.set("GriefPrevention.MaxPlayersPerIpAddress", this.config_ipLimit);
@ -851,7 +853,7 @@ public class GriefPrevention extends JavaPlugin
commands = slashCommandsToMonitor.split(";"); commands = slashCommandsToMonitor.split(";");
for(int i = 0; i < commands.length; i++) 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 //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(";"); commands = whisperCommandsToMonitor.split(";");
for(int i = 0; i < commands.length; i++) 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 //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(";"); commands = bannedPvPCommandsList.split(";");
for(int i = 0; i < commands.length; i++) 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());
} }
} }

View File

@ -47,6 +47,7 @@ import org.bukkit.World;
import org.bukkit.World.Environment; import org.bukkit.World.Environment;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.command.Command;
import org.bukkit.entity.Animals; import org.bukkit.entity.Animals;
import org.bukkit.entity.Boat; import org.bukkit.entity.Boat;
import org.bukkit.entity.Creature; 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.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.MetadataValue; import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.BlockIterator; import org.bukkit.util.BlockIterator;
class PlayerEventHandler implements Listener class PlayerEventHandler implements Listener
@ -484,11 +487,13 @@ class PlayerEventHandler implements Listener
String command = args[0].toLowerCase(); String command = args[0].toLowerCase();
CommandCategory category = this.getCommandCategory(command);
Player player = event.getPlayer(); Player player = event.getPlayer();
PlayerData playerData = null; PlayerData playerData = null;
//if a whisper //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 //determine target player, might be NULL
Player targetPlayer = GriefPrevention.instance.getServer().getPlayer(args[1]); 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) //if the slash command used is in the list of monitored commands, treat it like a chat message (see above)
boolean isMonitoredCommand = false; boolean isMonitoredCommand = (category == CommandCategory.Chat || category == CommandCategory.Whisper);
for(String monitoredCommand : GriefPrevention.instance.config_spam_monitorSlashCommands)
{
if(args[0].equalsIgnoreCase(monitoredCommand))
{
isMonitoredCommand = true;
break;
}
}
if(isMonitoredCommand) if(isMonitoredCommand)
{ {
//if anti spam enabled, check for spam //if anti spam enabled, check for spam
@ -578,7 +574,7 @@ class PlayerEventHandler implements Listener
builder.append(arg + " "); builder.append(arg + " ");
} }
this.makeSocialLogEntry(event.getPlayer().getName(), builder.toString()); makeSocialLogEntry(event.getPlayer().getName(), builder.toString());
} }
} }
@ -610,6 +606,67 @@ class PlayerEventHandler implements Listener
} }
} }
private ConcurrentHashMap<String, CommandCategory> commandCategoryMap = new ConcurrentHashMap<String, CommandCategory>();
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<String> aliases = new HashSet<String>();
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 int longestNameLength = 10;
static void makeSocialLogEntry(String name, String message) static void makeSocialLogEntry(String name, String message)
{ {