2013-07-22 02:48:34 +00:00
|
|
|
package me.StevenLawson.TotalFreedomMod;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2013-08-18 19:52:32 +00:00
|
|
|
import java.util.List;
|
2013-07-22 02:48:34 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
|
2014-12-21 09:23:50 +00:00
|
|
|
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
|
2015-01-19 21:51:02 +00:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2013-07-22 02:48:34 +00:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandMap;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
2013-08-18 20:45:35 +00:00
|
|
|
public class TFM_CommandBlocker
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2014-05-19 17:32:25 +00:00
|
|
|
private static final Map<String, CommandBlockerEntry> BLOCKED_COMMANDS;
|
|
|
|
|
|
|
|
static
|
|
|
|
{
|
|
|
|
BLOCKED_COMMANDS = new HashMap<String, CommandBlockerEntry>();
|
|
|
|
}
|
2013-07-22 02:48:34 +00:00
|
|
|
|
2013-08-18 20:45:35 +00:00
|
|
|
private TFM_CommandBlocker()
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2014-05-19 17:32:25 +00:00
|
|
|
throw new AssertionError();
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
public static void load()
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2014-05-19 17:32:25 +00:00
|
|
|
BLOCKED_COMMANDS.clear();
|
2013-07-22 02:48:34 +00:00
|
|
|
|
2014-05-19 17:32:25 +00:00
|
|
|
final CommandMap commandMap = TFM_CommandLoader.getCommandMap();
|
2013-07-22 02:48:34 +00:00
|
|
|
if (commandMap == null)
|
|
|
|
{
|
|
|
|
TFM_Log.severe("Error loading commandMap.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-20 01:02:00 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2014-12-21 09:23:50 +00:00
|
|
|
List<String> blockedCommands = (List<String>) TFM_ConfigEntry.BLOCKED_COMMANDS.getList();
|
|
|
|
for (String rawEntry : blockedCommands)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2014-03-18 15:07:51 +00:00
|
|
|
final String[] parts = rawEntry.split(":");
|
2013-07-22 02:48:34 +00:00
|
|
|
if (parts.length < 3 || parts.length > 4)
|
|
|
|
{
|
2015-01-19 21:51:02 +00:00
|
|
|
TFM_Log.warning("Invalid command blocker entry: " + rawEntry);
|
2013-07-22 02:48:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-18 15:07:51 +00:00
|
|
|
final CommandBlockerRank rank = CommandBlockerRank.fromToken(parts[0]);
|
2015-01-19 21:51:02 +00:00
|
|
|
final CommandBlockerAction action = CommandBlockerAction.fromToken(parts[1]);
|
|
|
|
String commandName = parts[2].toLowerCase().substring(1);
|
|
|
|
final String message = (parts.length > 3 ? parts[3] : null);
|
|
|
|
|
|
|
|
if (rank == null || action == null || commandName == null || commandName.isEmpty())
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2015-01-19 21:51:02 +00:00
|
|
|
TFM_Log.warning("Invalid command blocker entry: " + rawEntry);
|
2013-07-22 02:48:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
final String[] commandParts = commandName.split(" ");
|
|
|
|
String subCommand = null;
|
|
|
|
if (commandParts.length > 1)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2015-01-19 21:51:02 +00:00
|
|
|
commandName = commandParts[0];
|
|
|
|
subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).trim().toLowerCase();
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
final Command command = commandMap.getCommand(commandName);
|
|
|
|
|
|
|
|
// Obtain command from alias
|
|
|
|
if (command == null)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2015-01-19 21:51:02 +00:00
|
|
|
TFM_Log.info("Blocking unknown command: /" + commandName);
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-19 21:51:02 +00:00
|
|
|
commandName = command.getName().toLowerCase();
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
if (BLOCKED_COMMANDS.containsKey(commandName))
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2015-01-19 21:51:02 +00:00
|
|
|
TFM_Log.warning("Not blocking: /" + commandName + " - Duplicate entry exists!");
|
|
|
|
continue;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
final CommandBlockerEntry blockedCommandEntry = new CommandBlockerEntry(rank, action, commandName, subCommand, message);
|
|
|
|
BLOCKED_COMMANDS.put(blockedCommandEntry.getCommand(), blockedCommandEntry);
|
2013-07-22 02:48:34 +00:00
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
if (command != null)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2015-01-19 21:51:02 +00:00
|
|
|
for (String alias : command.getAliases())
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2014-05-19 17:32:25 +00:00
|
|
|
BLOCKED_COMMANDS.put(alias.toLowerCase(), blockedCommandEntry);
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 15:26:10 +00:00
|
|
|
|
2014-08-25 11:57:13 +00:00
|
|
|
TFM_Log.info("Loaded " + BLOCKED_COMMANDS.size() + " blocked commands");
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 17:32:25 +00:00
|
|
|
public static boolean isCommandBlocked(String command, CommandSender sender)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2015-05-10 21:19:23 +00:00
|
|
|
return isCommandBlocked(command, sender, false);
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 17:32:25 +00:00
|
|
|
public static boolean isCommandBlocked(String command, CommandSender sender, boolean doAction)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
if (command == null || command.isEmpty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-10 21:19:23 +00:00
|
|
|
if (command.split(" ")[0].contains(":"))
|
2014-03-18 15:07:51 +00:00
|
|
|
{
|
|
|
|
TFM_Util.playerMsg(sender, "Plugin-specific commands are disabled.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
final String[] commandParts = command.split(" ");
|
|
|
|
String subCommand = null;
|
|
|
|
if (commandParts.length > 1)
|
|
|
|
{
|
|
|
|
command = commandParts[0].substring(1);
|
|
|
|
subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).toLowerCase();
|
|
|
|
}
|
|
|
|
|
2014-05-19 17:32:25 +00:00
|
|
|
final CommandBlockerEntry entry = BLOCKED_COMMANDS.get(command);
|
2013-07-22 02:48:34 +00:00
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
if (entry == null)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2015-01-19 21:51:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-22 02:48:34 +00:00
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
if (entry.getSubCommand() != null)
|
|
|
|
{
|
|
|
|
if (subCommand == null || !subCommand.startsWith(entry.getSubCommand()))
|
|
|
|
{
|
|
|
|
return false;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
if (entry.getRank().hasPermission(sender))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doAction)
|
|
|
|
{
|
|
|
|
entry.doActions(sender);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
public static enum CommandBlockerRank
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
ANYONE("a", 0),
|
|
|
|
OP("o", 1),
|
|
|
|
SUPER("s", 2),
|
|
|
|
TELNET("t", 3),
|
|
|
|
SENIOR("c", 4),
|
|
|
|
NOBODY("n", 5);
|
2015-01-19 21:51:02 +00:00
|
|
|
//
|
2013-07-22 02:48:34 +00:00
|
|
|
private final String token;
|
|
|
|
private final int level;
|
|
|
|
|
|
|
|
private CommandBlockerRank(String token, int level)
|
|
|
|
{
|
|
|
|
this.token = token;
|
|
|
|
this.level = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getToken()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.token;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasPermission(CommandSender sender)
|
|
|
|
{
|
2015-01-19 21:51:02 +00:00
|
|
|
return fromSender(sender).level >= this.level;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
public static CommandBlockerRank fromSender(CommandSender sender)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2014-04-09 20:33:03 +00:00
|
|
|
if (!TFM_AdminList.isSuperAdmin(sender))
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
if (sender.isOp())
|
|
|
|
{
|
|
|
|
return OP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ANYONE;
|
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
if (TFM_AdminList.isSeniorAdmin(sender))
|
|
|
|
{
|
|
|
|
return SENIOR;
|
|
|
|
}
|
2013-07-22 02:48:34 +00:00
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
if (!(sender instanceof Player))
|
|
|
|
{
|
|
|
|
return TELNET;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
2015-01-19 21:51:02 +00:00
|
|
|
|
|
|
|
return SUPER;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static CommandBlockerRank fromToken(String token)
|
|
|
|
{
|
|
|
|
for (CommandBlockerRank rank : CommandBlockerRank.values())
|
|
|
|
{
|
|
|
|
if (rank.getToken().equalsIgnoreCase(token))
|
|
|
|
{
|
|
|
|
return rank;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ANYONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
public static enum CommandBlockerAction
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
2013-12-17 15:24:56 +00:00
|
|
|
BLOCK("b"),
|
|
|
|
BLOCK_AND_EJECT("a"),
|
|
|
|
BLOCK_UNKNOWN("u");
|
2013-07-22 02:48:34 +00:00
|
|
|
private final String token;
|
|
|
|
|
|
|
|
private CommandBlockerAction(String token)
|
|
|
|
{
|
|
|
|
this.token = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getToken()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.token;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static CommandBlockerAction fromToken(String token)
|
|
|
|
{
|
|
|
|
for (CommandBlockerAction action : CommandBlockerAction.values())
|
|
|
|
{
|
|
|
|
if (action.getToken().equalsIgnoreCase(token))
|
|
|
|
{
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
public static class CommandBlockerEntry
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
private final CommandBlockerRank rank;
|
|
|
|
private final CommandBlockerAction action;
|
2015-01-19 21:51:02 +00:00
|
|
|
private final String command;
|
|
|
|
private final String subCommand;
|
2013-07-22 02:48:34 +00:00
|
|
|
private final String message;
|
|
|
|
|
2014-04-21 17:00:39 +00:00
|
|
|
private CommandBlockerEntry(CommandBlockerRank rank, CommandBlockerAction action, String command, String message)
|
2015-01-19 21:51:02 +00:00
|
|
|
{
|
|
|
|
this(rank, action, command, null, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
private CommandBlockerEntry(CommandBlockerRank rank, CommandBlockerAction action, String command, String subCommand, String message)
|
2013-07-22 02:48:34 +00:00
|
|
|
{
|
|
|
|
this.rank = rank;
|
|
|
|
this.action = action;
|
|
|
|
this.command = command;
|
2015-01-19 21:51:02 +00:00
|
|
|
this.subCommand = (subCommand == null ? null : subCommand.toLowerCase().trim());
|
|
|
|
this.message = (message == null || message.equals("_") ? "That command is blocked." : message);
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public CommandBlockerAction getAction()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.action;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getCommand()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.command;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
public String getSubCommand()
|
|
|
|
{
|
|
|
|
return this.subCommand;
|
|
|
|
}
|
|
|
|
|
2013-07-22 02:48:34 +00:00
|
|
|
public String getMessage()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.message;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public CommandBlockerRank getRank()
|
|
|
|
{
|
2013-07-26 23:48:18 +00:00
|
|
|
return this.rank;
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 23:48:18 +00:00
|
|
|
private void doActions(CommandSender sender)
|
|
|
|
{
|
2015-01-19 21:51:02 +00:00
|
|
|
if (action == CommandBlockerAction.BLOCK_AND_EJECT && sender instanceof Player)
|
2013-07-26 23:48:18 +00:00
|
|
|
{
|
2015-01-19 21:51:02 +00:00
|
|
|
TFM_Util.autoEject((Player) sender, "You used a prohibited command: " + command);
|
2013-07-26 23:48:18 +00:00
|
|
|
TFM_Util.bcastMsg(sender.getName() + " was automatically kicked for using harmful commands.", ChatColor.RED);
|
2015-01-19 21:51:02 +00:00
|
|
|
return;
|
2013-07-26 23:48:18 +00:00
|
|
|
}
|
|
|
|
|
2015-01-19 21:51:02 +00:00
|
|
|
if (action == CommandBlockerAction.BLOCK_UNKNOWN)
|
|
|
|
{
|
|
|
|
TFM_Util.playerMsg(sender, "Unknown command. Type \"help\" for help.", ChatColor.RESET);
|
|
|
|
return;
|
2013-07-26 23:48:18 +00:00
|
|
|
}
|
2015-01-19 21:51:02 +00:00
|
|
|
|
|
|
|
TFM_Util.playerMsg(sender, TFM_Util.colorize(message));
|
2013-07-26 23:48:18 +00:00
|
|
|
}
|
2013-07-22 02:48:34 +00:00
|
|
|
}
|
|
|
|
}
|