Fix command blocker

This commit is contained in:
Seth 2020-07-07 19:10:21 -07:00
parent c1a7b1c141
commit 8b4a91b2e6
No known key found for this signature in database
GPG Key ID: A7BAB4E14F089CF3

View File

@ -2,21 +2,27 @@ package me.totalfreedom.totalfreedommod.blocking.command;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.command.FreedomCommand; import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.Bukkit;
import org.bukkit.plugin.SimplePluginManager;
public class CommandBlocker extends FreedomService public class CommandBlocker extends FreedomService
{ {
@ -38,11 +44,32 @@ public class CommandBlocker extends FreedomService
entryList.clear(); entryList.clear();
} }
public static CommandMap getCommandMap()
{
try
{
SimplePluginManager simplePluginManager = (SimplePluginManager) Bukkit.getServer().getPluginManager();
Field commandMapField = SimplePluginManager.class.getDeclaredField("commandMap");
commandMapField.setAccessible(true);
SimpleCommandMap simpleCommandMap = (SimpleCommandMap) commandMapField.get(simplePluginManager);
return simpleCommandMap;
}
catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e)
{
FLog.severe("Failed to get command map field (" + e.getMessage() + ")");
}
return null;
}
public void load() public void load()
{ {
entryList.clear(); entryList.clear();
unknownCommands.clear(); unknownCommands.clear();
final CommandMap commandMap = getCommandMap();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<String> blockedCommands = (List<String>)ConfigEntry.BLOCKED_COMMANDS.getList(); List<String> blockedCommands = (List<String>)ConfigEntry.BLOCKED_COMMANDS.getList();
for (String rawEntry : blockedCommands) for (String rawEntry : blockedCommands)
@ -73,7 +100,7 @@ public class CommandBlocker extends FreedomService
subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).trim().toLowerCase(); subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).trim().toLowerCase();
} }
final FreedomCommand command = plugin.cl.getByName(commandName); final Command command = commandMap.getCommand(commandName);
// Obtain command from alias // Obtain command from alias
if (command == null) if (command == null)
@ -91,12 +118,12 @@ public class CommandBlocker extends FreedomService
continue; continue;
} }
final CommandBlockerEntry blockedCommandEntry = new CommandBlockerEntry(rank, action, commandName, subCommand, message);
entryList.put(blockedCommandEntry.getCommand(), blockedCommandEntry);
final CommandBlockerEntry blockedCommandEntry = new CommandBlockerEntry(rank, action, commandName, subCommand, message);
entryList.put(commandName, blockedCommandEntry);
if (command != null) if (command != null)
{ {
for (String alias : command.getAliases().split(",")) for (String alias : command.getAliases())
{ {
entryList.put(alias.toLowerCase(), blockedCommandEntry); entryList.put(alias.toLowerCase(), blockedCommandEntry);
} }
@ -115,12 +142,6 @@ public class CommandBlocker extends FreedomService
// CommandBlocker handles messages and broadcasts // CommandBlocker handles messages and broadcasts
event.setCancelled(true); event.setCancelled(true);
} }
if (event.getMessage().contains("translation.test.invalid") || event.getMessage().contains("translation.test.invalid2"))
{
event.setCancelled(true);
FUtil.playerMsg(event.getPlayer(), ChatColor.RED + "No crishy crashy faggy");
}
} }
public boolean isCommandBlocked(String command, CommandSender sender) public boolean isCommandBlocked(String command, CommandSender sender)