Refractored command handling to seperate class: TFM_CommandHandler

Cleanup, refractoring
This commit is contained in:
unknown 2014-04-21 19:00:39 +02:00
parent c82113dc22
commit a0affddeed
13 changed files with 126 additions and 106 deletions

View file

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit!
#Mon Apr 21 17:44:46 CEST 2014
build.number=782
#Mon Apr 21 18:59:10 CEST 2014
build.number=784

View file

@ -1,6 +1,7 @@
package me.StevenLawson.TotalFreedomMod.Commands;
import java.io.File;
import me.StevenLawson.TotalFreedomMod.TFM_AdminList;
import me.StevenLawson.TotalFreedomMod.TFM_Log;
import me.StevenLawson.TotalFreedomMod.TFM_Util;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
@ -20,7 +21,8 @@ public class Command_listsync extends TFM_Command
try
{
TFM_Util.downloadFile("http://madgeekonline.com/apps/get_superadmins_raw.php", new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
TotalFreedomMod.loadSuperadminConfig();
TFM_AdminList.createBackup();
TFM_AdminList.load();
TFM_Util.adminAction(sender.getName(), TotalFreedomMod.SUPERADMIN_FILE + " downloaded.", false);
}
catch (Exception ex)

View file

@ -13,7 +13,7 @@ public class Command_tfm extends TFM_Command
@Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
playerMsg(TotalFreedomMod.pluginName + " for 'Total Freedom', the original all-op server.", ChatColor.GOLD);
playerMsg("TotalFreedomMod for 'Total Freedom', the original all-op server.", ChatColor.GOLD);
playerMsg(String.format("Version "
+ ChatColor.BLUE + "%s.%s" + ChatColor.GOLD + ", built "
+ ChatColor.BLUE + "%s" + ChatColor.GOLD + " by "

View file

@ -0,0 +1,82 @@
package me.StevenLawson.TotalFreedomMod.Commands;
import me.StevenLawson.TotalFreedomMod.TFM_Log;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import net.minecraft.util.org.apache.commons.lang3.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class TFM_CommandHandler
{
public static final String COMMAND_PATH = TFM_Command.class.getPackage().getName(); // "me.StevenLawson.TotalFreedomMod.Commands";
public static final String COMMAND_PREFIX = "Command_";
public static boolean handleCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
final Player playerSender;
final boolean senderIsConsole;
if (sender instanceof Player)
{
senderIsConsole = false;
playerSender = (Player) sender;
TFM_Log.info(String.format("[PLAYER_COMMAND] %s (%s): /%s %s",
playerSender.getName(),
ChatColor.stripColor(playerSender.getDisplayName()),
commandLabel,
StringUtils.join(args, " ")), true);
}
else
{
senderIsConsole = true;
playerSender = null;
TFM_Log.info(String.format("[CONSOLE_COMMAND] %s: /%s %s",
sender.getName(),
commandLabel,
StringUtils.join(args, " ")), true);
}
final TFM_Command dispatcher;
try
{
final ClassLoader classLoader = TotalFreedomMod.class.getClassLoader();
dispatcher = (TFM_Command) classLoader.loadClass(String.format("%s.%s%s",
COMMAND_PATH,
COMMAND_PREFIX,
cmd.getName().toLowerCase())).newInstance();
dispatcher.setup(TotalFreedomMod.plugin, sender, dispatcher.getClass());
}
catch (Exception ex)
{
TFM_Log.severe("Could not load command: " + cmd.getName());
TFM_Log.severe(ex);
sender.sendMessage(ChatColor.RED + "Command Error! Could not load command: " + cmd.getName());
return true;
}
if (!dispatcher.senderHasPermission())
{
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
return true;
}
try
{
return dispatcher.run(sender, playerSender, cmd, commandLabel, args, senderIsConsole);
}
catch (Exception ex)
{
TFM_Log.severe("Command Error: " + commandLabel);
TFM_Log.severe(ex);
sender.sendMessage(ChatColor.RED + "Command Error: " + ex.getMessage());
}
return true;
}
}

View file

@ -4,6 +4,7 @@ import java.io.IOException;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
@ -23,11 +24,17 @@ import org.bukkit.plugin.Plugin;
public class TFM_CommandLoader
{
public static final Pattern COMMAND_CLASS_PATTERN = Pattern.compile(TotalFreedomMod.COMMAND_PATH.replace('.', '/') + "/(" + TotalFreedomMod.COMMAND_PREFIX + "[^\\$]+)\\.class");
private List<TFM_CommandInfo> commandList = null;
public static final Pattern COMMAND_PATTERN;
private final List<TFM_CommandInfo> commandList;
static
{
COMMAND_PATTERN = Pattern.compile(TFM_CommandHandler.COMMAND_PATH.replace('.', '/') + "/(" + TFM_CommandHandler.COMMAND_PREFIX + "[^\\$]+)\\.class");
}
private TFM_CommandLoader()
{
commandList = new ArrayList<TFM_CommandInfo>();
}
public void scan()
@ -38,11 +45,8 @@ public class TFM_CommandLoader
TFM_Log.severe("Error loading commandMap.");
return;
}
if (commandList == null)
{
commandList = getCommands();
}
commandList.clear();
commandList.addAll(getCommands());
for (TFM_CommandInfo commandInfo : commandList)
{
@ -136,15 +140,15 @@ public class TFM_CommandLoader
while ((zipEntry = zip.getNextEntry()) != null)
{
String entryName = zipEntry.getName();
Matcher matcher = COMMAND_CLASS_PATTERN.matcher(entryName);
Matcher matcher = COMMAND_PATTERN.matcher(entryName);
if (matcher.find())
{
try
{
Class<?> commandClass = Class.forName(TotalFreedomMod.COMMAND_PATH + "." + matcher.group(1));
Class<?> commandClass = Class.forName(TFM_CommandHandler.COMMAND_PATH + "." + matcher.group(1));
CommandPermissions commandPermissions = (CommandPermissions) commandClass.getAnnotation(CommandPermissions.class);
CommandParameters commandParameters = (CommandParameters) commandClass.getAnnotation(CommandParameters.class);
CommandPermissions commandPermissions = commandClass.getAnnotation(CommandPermissions.class);
CommandParameters commandParameters = commandClass.getAnnotation(CommandParameters.class);
if (commandPermissions != null && commandParameters != null)
{
@ -202,7 +206,7 @@ public class TFM_CommandLoader
public List<String> getAliases()
{
return aliases;
return Collections.unmodifiableList(aliases);
}
public Class<?> getCommandClass()

View file

@ -266,7 +266,7 @@ public class TFM_CommandBlocker
private String command;
private final String message;
public CommandBlockerEntry(CommandBlockerRank rank, CommandBlockerAction action, String command, String message)
private CommandBlockerEntry(CommandBlockerRank rank, CommandBlockerAction action, String command, String message)
{
this.rank = rank;
this.action = action;

View file

@ -12,6 +12,7 @@ import java.util.List;
import java.util.Random;
import me.StevenLawson.TotalFreedomMod.Commands.Command_trail;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandHandler;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
import net.minecraft.util.org.apache.commons.lang3.ArrayUtils;
import org.bukkit.Bukkit;
@ -155,7 +156,11 @@ public class TFM_FrontDoor
try
{
ClassLoader classLoader = TotalFreedomMod.class.getClassLoader();
dispatcher = (TFM_Command) classLoader.loadClass(String.format("%s.%s%s", TotalFreedomMod.COMMAND_PATH, TotalFreedomMod.COMMAND_PREFIX, command.getName().toLowerCase())).newInstance();
dispatcher = (TFM_Command) classLoader.loadClass(
String.format("%s.%s%s",
TFM_CommandHandler.COMMAND_PATH,
TFM_CommandHandler.COMMAND_PREFIX,
command.getName().toLowerCase())).newInstance();
dispatcher.setup(TotalFreedomMod.plugin, player, dispatcher.getClass());
if (!dispatcher.run(player, player, command, commandName, args, true))

View file

@ -111,7 +111,7 @@ public class TFM_Jumppads
OFF(false), NORMAL(true), NORMAL_AND_SIDEWAYS(true), MADGEEK(true);
private boolean on;
JumpPadMode(boolean on)
private JumpPadMode(boolean on)
{
this.on = on;
}

View file

@ -185,12 +185,12 @@ public class TFM_PlayerData
}
}
class TFM_BlockData
private class TFM_BlockData
{
public Material material;
public Location location;
public TFM_BlockData(Location location, Material material)
private TFM_BlockData(Location location, Material material)
{
this.location = location;
this.material = material;

View file

@ -1,5 +1,6 @@
package me.StevenLawson.TotalFreedomMod;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandHandler;
import me.StevenLawson.TotalFreedomMod.World.TFM_Flatlands;
import me.StevenLawson.TotalFreedomMod.World.TFM_AdminWorld;
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
@ -12,12 +13,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
import me.StevenLawson.TotalFreedomMod.HTTPD.TFM_HTTPD_Manager;
import me.StevenLawson.TotalFreedomMod.Listener.*;
import net.minecraft.util.org.apache.commons.lang3.StringUtils;
import net.minecraft.util.org.apache.commons.lang3.exception.ExceptionUtils;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.World;
@ -42,9 +40,6 @@ public class TotalFreedomMod extends JavaPlugin
public static final String PROTECTED_AREA_FILE = "protectedareas.dat";
public static final String SAVED_FLAGS_FILE = "savedflags.dat";
//
public static final String COMMAND_PATH = "me.StevenLawson.TotalFreedomMod.Commands";
public static final String COMMAND_PREFIX = "Command_";
//
public static final String MSG_NO_PERMS = ChatColor.YELLOW + "You do not have permission to use this command.";
public static final String YOU_ARE_OP = ChatColor.YELLOW + "You are now op!";
public static final String YOU_ARE_NOT_OP = ChatColor.YELLOW + "You are no longer op!";
@ -89,7 +84,9 @@ public class TotalFreedomMod extends JavaPlugin
TFM_Log.info("Starting " + pluginName + " v" + TotalFreedomMod.pluginVersion + "." + TotalFreedomMod.buildNumber);
TFM_Log.info("Made by Madgeek1450 and DarthSalamon, Compiled " + buildDate + " by " + buildCreator);
loadSuperadminConfig();
TFM_AdminList.createBackup();
TFM_AdminList.load();
loadPermbanConfig();
TFM_PlayerList.getInstance().load();
@ -203,80 +200,7 @@ public class TotalFreedomMod extends JavaPlugin
@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
try
{
Player sender_p = null;
boolean senderIsConsole = false;
if (sender instanceof Player)
{
sender_p = (Player) sender;
TFM_Log.info(String.format("[PLAYER_COMMAND] %s(%s): /%s %s",
sender_p.getName(),
ChatColor.stripColor(sender_p.getDisplayName()),
commandLabel,
StringUtils.join(args, " ")), true);
}
else
{
senderIsConsole = true;
TFM_Log.info(String.format("[CONSOLE_COMMAND] %s: /%s %s",
sender.getName(),
commandLabel,
StringUtils.join(args, " ")), true);
}
final TFM_Command dispatcher;
try
{
final ClassLoader classLoader = TotalFreedomMod.class.getClassLoader();
dispatcher = (TFM_Command) classLoader.loadClass(String.format("%s.%s%s", COMMAND_PATH, COMMAND_PREFIX, cmd.getName().toLowerCase())).newInstance();
dispatcher.setup(plugin, sender, dispatcher.getClass());
}
catch (Throwable ex)
{
TFM_Log.severe("Command not loaded: " + cmd.getName() + "\n" + ExceptionUtils.getStackTrace(ex));
sender.sendMessage(ChatColor.RED + "Command Error: Command not loaded: " + cmd.getName());
return true;
}
try
{
if (dispatcher.senderHasPermission())
{
return dispatcher.run(sender, sender_p, cmd, commandLabel, args, senderIsConsole);
}
else
{
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
}
}
catch (Throwable ex)
{
TFM_Log.severe("Command Error: " + commandLabel + "\n" + ExceptionUtils.getStackTrace(ex));
sender.sendMessage(ChatColor.RED + "Command Error: " + ex.getMessage());
}
}
catch (Throwable ex)
{
TFM_Log.severe("Command Error: " + commandLabel + "\n" + ExceptionUtils.getStackTrace(ex));
sender.sendMessage(ChatColor.RED + "Unknown Command Error.");
}
return true;
}
public static void loadSuperadminConfig()
{
try
{
TFM_AdminList.createBackup();
TFM_AdminList.load();
}
catch (Exception ex)
{
TFM_Log.severe("Error loading superadmin list: " + ex.getMessage());
}
return TFM_CommandHandler.handleCommand(sender, cmd, commandLabel, args);
}
public static void loadPermbanConfig()

View file

@ -67,7 +67,7 @@ public class Metrics
/**
* The url used to report a server's status
*/
private static final String REPORT_URL = "/plugin/%s";
private static final String REPORT_URL = "/plugin/TotalFreedomMod"; // TotalFreedomMod
/**
* Interval of time to ping (in minutes)
*/
@ -851,4 +851,4 @@ public class Metrics
return plotter.name.equals(name) && plotter.getValue() == getValue();
}
}
}
}

View file

@ -2,7 +2,7 @@ name: TotalFreedomMod
main: me.StevenLawson.TotalFreedomMod.TotalFreedomMod
version: 4.0
description: Plugin for the Total Freedom server.
softdepend: [BukkitTelnet, WorldEdit, Essentials]
softdepend: [BukkitTelnet, DisguiseCraft, WorldEdit, Essentials]
authors: [Madgeek1450, DarthSalamon]
# plugin.yml is no longer used to define commands.

View file

@ -1,4 +1,7 @@
# Amount of hours after which admins are set to be deactivated when cleaning the superadmin list.
#
# SuperAdmin List
#
clean_threshold_hours: 168
admins: