From 3333c826a55c28a268c281fe8a2029e9392fdb42 Mon Sep 17 00:00:00 2001 From: Steven Lawson Date: Mon, 8 Apr 2013 17:59:04 -0400 Subject: [PATCH] Some changes to command class loader and structure. --- appinfo.properties | 6 +- buildnumber.properties | 4 +- .../TotalFreedomMod/Commands/TFM_Command.java | 59 ++++++++++--------- .../TotalFreedomMod/TotalFreedomMod.java | 5 +- 4 files changed, 37 insertions(+), 37 deletions(-) diff --git a/appinfo.properties b/appinfo.properties index a4c93480..adfec82e 100644 --- a/appinfo.properties +++ b/appinfo.properties @@ -1,5 +1,5 @@ -#Sun, 24 Mar 2013 16:31:08 -0400 +#Mon, 08 Apr 2013 17:37:49 -0400 program.VERSION=2.12 -program.BUILDNUM=111 -program.BUILDDATE=03/24/2013 04\:31 PM +program.BUILDNUM=114 +program.BUILDDATE=04/08/2013 05\:37 PM diff --git a/buildnumber.properties b/buildnumber.properties index aa632381..13f1dc03 100644 --- a/buildnumber.properties +++ b/buildnumber.properties @@ -1,3 +1,3 @@ #Build Number for ANT. Do not edit! -#Sun Mar 24 16:31:08 EDT 2013 -build.number=112 +#Mon Apr 08 17:37:49 EDT 2013 +build.number=115 diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_Command.java b/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_Command.java index 92b154ee..839d01c6 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_Command.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/TFM_Command.java @@ -12,64 +12,61 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -public class TFM_Command +public abstract class TFM_Command { protected TotalFreedomMod plugin; protected Server server; - private CommandSender commandsender; + private CommandSender commandSender; + private Class commandClass; public TFM_Command() { } - public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) - { - TFM_Log.severe("Command Error: Command not implemented: " + cmd.getName()); - sender.sendMessage(ChatColor.RED + "Command Error: Command not implemented: " + cmd.getName()); - return false; - } + abstract public boolean run(final CommandSender sender, final Player sender_p, final Command cmd, final String commandLabel, final String[] args, final boolean senderIsConsole); - public void setPlugin(TotalFreedomMod plugin) + public void setup(final TotalFreedomMod plugin, final CommandSender commandSender, final Class commandClass) { this.plugin = plugin; - this.server = plugin.getServer(); + this.server = this.plugin.getServer(); + this.commandSender = commandSender; + this.commandClass = commandClass; } - public void setCommandsender(CommandSender commandsender) - { - this.commandsender = commandsender; - } - - public void playerMsg(CommandSender sender, String message, ChatColor color) // complete function + public void playerMsg(final CommandSender sender, final String message, final ChatColor color) { + if (sender == null) + { + return; + } sender.sendMessage(color + message); } - public void playerMsg(String message, ChatColor color) + public void playerMsg(final String message, final ChatColor color) { - playerMsg(commandsender, message, color); + playerMsg(commandSender, message, color); } - public void playerMsg(CommandSender sender, String message) + public void playerMsg(final CommandSender sender, final String message) { playerMsg(sender, message, ChatColor.GRAY); } - public void playerMsg(String message) + public void playerMsg(final String message) { - playerMsg(commandsender, message); + playerMsg(commandSender, message); } - public boolean senderHasPermission(Class cmd_class, CommandSender sender) + public boolean senderHasPermission() { - CommandPermissions permissions = cmd_class.getAnnotation(CommandPermissions.class); + CommandPermissions permissions = commandClass.getAnnotation(CommandPermissions.class); if (permissions != null) { - boolean is_super = TFM_SuperadminList.isUserSuperadmin(sender); + boolean is_super = TFM_SuperadminList.isUserSuperadmin(this.commandSender); boolean is_senior = false; if (is_super) { - is_senior = TFM_SuperadminList.isSeniorAdmin(sender); + is_senior = TFM_SuperadminList.isSeniorAdmin(this.commandSender); } AdminLevel level = permissions.level(); @@ -77,9 +74,9 @@ public class TFM_Command boolean block_host_console = permissions.block_host_console(); Player sender_p = null; - if (sender instanceof Player) + if (this.commandSender instanceof Player) { - sender_p = (Player) sender; + sender_p = (Player) this.commandSender; } if (sender_p == null) @@ -92,7 +89,7 @@ public class TFM_Command { return false; } - else if (block_host_console && TFM_Util.isFromHostConsole(sender.getName())) + else if (block_host_console && TFM_Util.isFromHostConsole(this.commandSender.getName())) { return false; } @@ -134,10 +131,14 @@ public class TFM_Command } return true; } + else + { + TFM_Log.warning(commandClass.getName() + " is missing permissions annotation."); + } return true; } - public Player getPlayer(String partialname) throws CantFindPlayerException + public Player getPlayer(final String partialname) throws CantFindPlayerException { List matches = server.matchPlayer(partialname); if (matches.isEmpty()) diff --git a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java index 6b95d0ce..6a13ce1d 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java +++ b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java @@ -154,8 +154,7 @@ public class TotalFreedomMod extends JavaPlugin { ClassLoader classLoader = TotalFreedomMod.class.getClassLoader(); dispatcher = (TFM_Command) classLoader.loadClass(String.format("%s.%s%s", COMMAND_PATH, COMMAND_PREFIX, cmd.getName().toLowerCase())).newInstance(); - dispatcher.setPlugin(this); - dispatcher.setCommandsender(sender); + dispatcher.setup(this, sender, dispatcher.getClass()); } catch (Throwable ex) { @@ -166,7 +165,7 @@ public class TotalFreedomMod extends JavaPlugin try { - if (dispatcher.senderHasPermission(dispatcher.getClass(), sender)) + if (dispatcher.senderHasPermission()) { return dispatcher.run(sender, sender_p, cmd, commandLabel, args, senderIsConsole); }