From a0affddeedc84cea475b265ea0805e293844762b Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 21 Apr 2014 19:00:39 +0200 Subject: [PATCH] Refractored command handling to seperate class: TFM_CommandHandler Cleanup, refractoring --- buildnumber.properties | 4 +- .../Commands/Command_listsync.java | 4 +- .../TotalFreedomMod/Commands/Command_tfm.java | 2 +- .../Commands/TFM_CommandHandler.java | 82 ++++++++++++++++++ .../Commands/TFM_CommandLoader.java | 28 +++--- .../TotalFreedomMod/TFM_CommandBlocker.java | 2 +- .../TotalFreedomMod/TFM_FrontDoor.java | 7 +- .../TotalFreedomMod/TFM_Jumppads.java | 2 +- .../TotalFreedomMod/TFM_PlayerData.java | 4 +- .../TotalFreedomMod/TotalFreedomMod.java | 86 ++----------------- src/org/mcstats/Metrics.java | 4 +- src/plugin.yml | 2 +- src/superadmin.yml | 5 +- 13 files changed, 126 insertions(+), 106 deletions(-) create mode 100644 src/me/StevenLawson/TotalFreedomMod/Commands/TFM_CommandHandler.java diff --git a/buildnumber.properties b/buildnumber.properties index d581e15..3d55e33 100644 --- a/buildnumber.properties +++ b/buildnumber.properties @@ -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 diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_listsync.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_listsync.java index ee2d40c..ba36115 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_listsync.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_listsync.java @@ -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) diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tfm.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tfm.java index 2a31821..889bcce 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tfm.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tfm.java @@ -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 " diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_CommandHandler.java b/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_CommandHandler.java new file mode 100644 index 0000000..ef8bb88 --- /dev/null +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_CommandHandler.java @@ -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; + } +} diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_CommandLoader.java b/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_CommandLoader.java index 8c86eda..4e47c7f 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_CommandLoader.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_CommandLoader.java @@ -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 commandList = null; + public static final Pattern COMMAND_PATTERN; + private final List commandList; + + static + { + COMMAND_PATTERN = Pattern.compile(TFM_CommandHandler.COMMAND_PATH.replace('.', '/') + "/(" + TFM_CommandHandler.COMMAND_PREFIX + "[^\\$]+)\\.class"); + } private TFM_CommandLoader() { + commandList = new ArrayList(); } 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 getAliases() { - return aliases; + return Collections.unmodifiableList(aliases); } public Class getCommandClass() diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java b/src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java index c442718..c645f5e 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_CommandBlocker.java @@ -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; diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_FrontDoor.java b/src/me/StevenLawson/TotalFreedomMod/TFM_FrontDoor.java index 501661d..290497d 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_FrontDoor.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_FrontDoor.java @@ -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)) diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_Jumppads.java b/src/me/StevenLawson/TotalFreedomMod/TFM_Jumppads.java index 9af8f3e..b0adecb 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_Jumppads.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_Jumppads.java @@ -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; } diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerData.java b/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerData.java index ca66bc7..4e38b50 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerData.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_PlayerData.java @@ -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; diff --git a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java index a2ab297..4db326d 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java +++ b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java @@ -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() diff --git a/src/org/mcstats/Metrics.java b/src/org/mcstats/Metrics.java index 6cb7468..3569187 100644 --- a/src/org/mcstats/Metrics.java +++ b/src/org/mcstats/Metrics.java @@ -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(); } } -} \ No newline at end of file +} diff --git a/src/plugin.yml b/src/plugin.yml index 13d0724..c4d9d1a 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -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. diff --git a/src/superadmin.yml b/src/superadmin.yml index 324642c..bfb02e6 100644 --- a/src/superadmin.yml +++ b/src/superadmin.yml @@ -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: