mirror of
https://github.com/AtlasMediaGroup/TotalFreedomMod.git
synced 2025-06-29 19:46:42 +00:00
Mavenized project
This commit is contained in:
@ -0,0 +1,206 @@
|
||||
package me.totalfreedom.totalfreedommod.blocking.command;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
||||
import me.totalfreedom.totalfreedommod.util.FLog;
|
||||
import me.totalfreedom.totalfreedommod.util.FUtil;
|
||||
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
||||
import net.pravian.aero.command.CommandReflection;
|
||||
import net.pravian.aero.component.service.AbstractService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
|
||||
public class CommandBlocker extends AbstractService<TotalFreedomMod>
|
||||
{
|
||||
public static Pattern NUMBER_FLAG_PATTERN = Pattern.compile("(:([0-9]){5,})");
|
||||
//
|
||||
private final Map<String, CommandBlockerEntry> entryList = Maps.newHashMap();
|
||||
|
||||
public CommandBlocker(TotalFreedomMod plugin)
|
||||
{
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart()
|
||||
{
|
||||
load();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop()
|
||||
{
|
||||
entryList.clear();
|
||||
}
|
||||
|
||||
public void load()
|
||||
{
|
||||
entryList.clear();
|
||||
|
||||
final CommandMap commandMap = CommandReflection.getCommandMap();
|
||||
if (commandMap == null)
|
||||
{
|
||||
FLog.severe("Error loading commandMap.");
|
||||
return;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
List<String> blockedCommands = (List<String>) ConfigEntry.BLOCKED_COMMANDS.getList();
|
||||
for (String rawEntry : blockedCommands)
|
||||
{
|
||||
final String[] parts = rawEntry.split(":");
|
||||
if (parts.length < 3 || parts.length > 4)
|
||||
{
|
||||
FLog.warning("Invalid command blocker entry: " + rawEntry);
|
||||
continue;
|
||||
}
|
||||
|
||||
final CommandBlockerRank rank = CommandBlockerRank.fromToken(parts[0]);
|
||||
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())
|
||||
{
|
||||
FLog.warning("Invalid command blocker entry: " + rawEntry);
|
||||
continue;
|
||||
}
|
||||
|
||||
final String[] commandParts = commandName.split(" ");
|
||||
String subCommand = null;
|
||||
if (commandParts.length > 1)
|
||||
{
|
||||
commandName = commandParts[0];
|
||||
subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).trim().toLowerCase();
|
||||
}
|
||||
|
||||
final Command command = commandMap.getCommand(commandName);
|
||||
|
||||
// Obtain command from alias
|
||||
if (command == null)
|
||||
{
|
||||
FLog.info("Blocking unknown command: /" + commandName);
|
||||
}
|
||||
else
|
||||
{
|
||||
commandName = command.getName().toLowerCase();
|
||||
}
|
||||
|
||||
if (entryList.containsKey(commandName))
|
||||
{
|
||||
FLog.warning("Not blocking: /" + commandName + " - Duplicate entry exists!");
|
||||
continue;
|
||||
}
|
||||
|
||||
final CommandBlockerEntry blockedCommandEntry = new CommandBlockerEntry(rank, action, commandName, subCommand, message);
|
||||
entryList.put(blockedCommandEntry.getCommand(), blockedCommandEntry);
|
||||
|
||||
if (command != null)
|
||||
{
|
||||
for (String alias : command.getAliases())
|
||||
{
|
||||
entryList.put(alias.toLowerCase(), blockedCommandEntry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FLog.info("Loaded " + blockedCommands.size() + " blocked commands");
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
|
||||
{
|
||||
// Blocked commands
|
||||
if (isCommandBlocked(event.getMessage(), event.getPlayer(), true))
|
||||
{
|
||||
// CommandBlocker handles messages and broadcasts
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isCommandBlocked(String command, CommandSender sender)
|
||||
{
|
||||
return isCommandBlocked(command, sender, false);
|
||||
}
|
||||
|
||||
public boolean isCommandBlocked(String command, CommandSender sender, boolean doAction)
|
||||
{
|
||||
if (command == null || command.isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Format
|
||||
command = command.toLowerCase().trim();
|
||||
command = command.startsWith("/") ? command.substring(1) : command;
|
||||
|
||||
// Check for plugin specific commands
|
||||
final String[] commandParts = command.split(" ");
|
||||
if (commandParts[0].contains(":"))
|
||||
{
|
||||
if (doAction)
|
||||
{
|
||||
FUtil.playerMsg(sender, "Plugin specific commands are disabled.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
for (String part : commandParts)
|
||||
{
|
||||
Matcher matcher = NUMBER_FLAG_PATTERN.matcher(part);
|
||||
if (!matcher.matches())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (doAction)
|
||||
{
|
||||
FUtil.playerMsg(sender, "That command contains an illegal number: " + matcher.group(1));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Obtain sub command, if it exists
|
||||
String subCommand = null;
|
||||
if (commandParts.length > 1)
|
||||
{
|
||||
subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).toLowerCase();
|
||||
}
|
||||
|
||||
// Obtain entry
|
||||
final CommandBlockerEntry entry = entryList.get(commandParts[0]);
|
||||
if (entry == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Validate sub command
|
||||
if (entry.getSubCommand() != null)
|
||||
{
|
||||
if (subCommand == null || !subCommand.startsWith(entry.getSubCommand()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (entry.getRank().hasPermission(sender))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (doAction)
|
||||
{
|
||||
entry.doActions(sender);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package me.totalfreedom.totalfreedommod.blocking.command;
|
||||
|
||||
public enum CommandBlockerAction
|
||||
{
|
||||
BLOCK("b"),
|
||||
BLOCK_AND_EJECT("a"),
|
||||
BLOCK_UNKNOWN("u");
|
||||
private final String token;
|
||||
|
||||
private CommandBlockerAction(String token)
|
||||
{
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public String getToken()
|
||||
{
|
||||
return this.token;
|
||||
}
|
||||
|
||||
public static CommandBlockerAction fromToken(String token)
|
||||
{
|
||||
for (CommandBlockerAction action : CommandBlockerAction.values())
|
||||
{
|
||||
if (action.getToken().equalsIgnoreCase(token))
|
||||
{
|
||||
return action;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package me.totalfreedom.totalfreedommod.blocking.command;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.totalfreedom.totalfreedommod.util.FUtil;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class CommandBlockerEntry
|
||||
{
|
||||
@Getter
|
||||
private final CommandBlockerRank rank;
|
||||
@Getter
|
||||
private final CommandBlockerAction action;
|
||||
@Getter
|
||||
private final String command;
|
||||
@Getter
|
||||
private final String subCommand;
|
||||
@Getter
|
||||
private final String message;
|
||||
|
||||
public CommandBlockerEntry(CommandBlockerRank rank, CommandBlockerAction action, String command, String message)
|
||||
{
|
||||
this(rank, action, command, null, message);
|
||||
}
|
||||
|
||||
public CommandBlockerEntry(CommandBlockerRank rank, CommandBlockerAction action, String command, String subCommand, String message)
|
||||
{
|
||||
this.rank = rank;
|
||||
this.action = action;
|
||||
this.command = command;
|
||||
this.subCommand = (subCommand == null ? null : subCommand.toLowerCase().trim());
|
||||
this.message = (message == null || message.equals("_") ? "That command is blocked." : message);
|
||||
}
|
||||
|
||||
public void doActions(CommandSender sender)
|
||||
{
|
||||
if (action == CommandBlockerAction.BLOCK_AND_EJECT && sender instanceof Player)
|
||||
{
|
||||
FUtil.autoEject((Player) sender, "You used a prohibited command: " + command);
|
||||
FUtil.bcastMsg(sender.getName() + " was automatically kicked for using harmful commands.", ChatColor.RED);
|
||||
return;
|
||||
}
|
||||
|
||||
if (action == CommandBlockerAction.BLOCK_UNKNOWN)
|
||||
{
|
||||
FUtil.playerMsg(sender, "Unknown command. Type \"help\" for help.", ChatColor.RESET);
|
||||
return;
|
||||
}
|
||||
|
||||
FUtil.playerMsg(sender, FUtil.colorize(message));
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package me.totalfreedom.totalfreedommod.blocking.command;
|
||||
|
||||
import me.totalfreedom.totalfreedommod.admin.Admin;
|
||||
import me.totalfreedom.totalfreedommod.rank.PlayerRank;
|
||||
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public enum CommandBlockerRank
|
||||
{
|
||||
ANYONE("a"),
|
||||
OP("o"),
|
||||
SUPER("s"),
|
||||
TELNET("t"),
|
||||
SENIOR("c"),
|
||||
NOBODY("n");
|
||||
//
|
||||
private final String token;
|
||||
|
||||
private CommandBlockerRank(String token)
|
||||
{
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public String getToken()
|
||||
{
|
||||
return this.token;
|
||||
}
|
||||
|
||||
public boolean hasPermission(CommandSender sender)
|
||||
{
|
||||
return fromSender(sender).ordinal() >= ordinal();
|
||||
}
|
||||
|
||||
public static CommandBlockerRank fromSender(CommandSender sender)
|
||||
{
|
||||
if (!(sender instanceof Player))
|
||||
{
|
||||
return TELNET;
|
||||
}
|
||||
|
||||
Admin admin = TotalFreedomMod.plugin.al.getAdmin(sender);
|
||||
if (admin != null)
|
||||
{
|
||||
if (admin.getRank() == PlayerRank.SENIOR_ADMIN)
|
||||
{
|
||||
return SENIOR;
|
||||
}
|
||||
return SUPER;
|
||||
}
|
||||
|
||||
if (sender.isOp())
|
||||
{
|
||||
return OP;
|
||||
}
|
||||
|
||||
return ANYONE;
|
||||
|
||||
}
|
||||
|
||||
public static CommandBlockerRank fromToken(String token)
|
||||
{
|
||||
for (CommandBlockerRank rank : CommandBlockerRank.values())
|
||||
{
|
||||
if (rank.getToken().equalsIgnoreCase(token))
|
||||
{
|
||||
return rank;
|
||||
}
|
||||
}
|
||||
return ANYONE;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user