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! #Build Number for ANT. Do not edit!
#Mon Apr 21 17:44:46 CEST 2014 #Mon Apr 21 18:59:10 CEST 2014
build.number=782 build.number=784

View File

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

View File

@ -13,7 +13,7 @@ public class Command_tfm extends TFM_Command
@Override @Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) 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 " playerMsg(String.format("Version "
+ ChatColor.BLUE + "%s.%s" + ChatColor.GOLD + ", built " + ChatColor.BLUE + "%s.%s" + ChatColor.GOLD + ", built "
+ ChatColor.BLUE + "%s" + ChatColor.GOLD + " by " + 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.security.CodeSource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -23,11 +24,17 @@ import org.bukkit.plugin.Plugin;
public class TFM_CommandLoader public class TFM_CommandLoader
{ {
public static final Pattern COMMAND_CLASS_PATTERN = Pattern.compile(TotalFreedomMod.COMMAND_PATH.replace('.', '/') + "/(" + TotalFreedomMod.COMMAND_PREFIX + "[^\\$]+)\\.class"); public static final Pattern COMMAND_PATTERN;
private List<TFM_CommandInfo> commandList = null; private final List<TFM_CommandInfo> commandList;
static
{
COMMAND_PATTERN = Pattern.compile(TFM_CommandHandler.COMMAND_PATH.replace('.', '/') + "/(" + TFM_CommandHandler.COMMAND_PREFIX + "[^\\$]+)\\.class");
}
private TFM_CommandLoader() private TFM_CommandLoader()
{ {
commandList = new ArrayList<TFM_CommandInfo>();
} }
public void scan() public void scan()
@ -38,11 +45,8 @@ public class TFM_CommandLoader
TFM_Log.severe("Error loading commandMap."); TFM_Log.severe("Error loading commandMap.");
return; return;
} }
commandList.clear();
if (commandList == null) commandList.addAll(getCommands());
{
commandList = getCommands();
}
for (TFM_CommandInfo commandInfo : commandList) for (TFM_CommandInfo commandInfo : commandList)
{ {
@ -136,15 +140,15 @@ public class TFM_CommandLoader
while ((zipEntry = zip.getNextEntry()) != null) while ((zipEntry = zip.getNextEntry()) != null)
{ {
String entryName = zipEntry.getName(); String entryName = zipEntry.getName();
Matcher matcher = COMMAND_CLASS_PATTERN.matcher(entryName); Matcher matcher = COMMAND_PATTERN.matcher(entryName);
if (matcher.find()) if (matcher.find())
{ {
try 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); CommandPermissions commandPermissions = commandClass.getAnnotation(CommandPermissions.class);
CommandParameters commandParameters = (CommandParameters) commandClass.getAnnotation(CommandParameters.class); CommandParameters commandParameters = commandClass.getAnnotation(CommandParameters.class);
if (commandPermissions != null && commandParameters != null) if (commandPermissions != null && commandParameters != null)
{ {
@ -202,7 +206,7 @@ public class TFM_CommandLoader
public List<String> getAliases() public List<String> getAliases()
{ {
return aliases; return Collections.unmodifiableList(aliases);
} }
public Class<?> getCommandClass() public Class<?> getCommandClass()

View File

@ -266,7 +266,7 @@ public class TFM_CommandBlocker
private String command; private String command;
private final String message; 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.rank = rank;
this.action = action; this.action = action;

View File

@ -12,6 +12,7 @@ import java.util.List;
import java.util.Random; import java.util.Random;
import me.StevenLawson.TotalFreedomMod.Commands.Command_trail; import me.StevenLawson.TotalFreedomMod.Commands.Command_trail;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command; import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandHandler;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader; import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
import net.minecraft.util.org.apache.commons.lang3.ArrayUtils; import net.minecraft.util.org.apache.commons.lang3.ArrayUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -155,7 +156,11 @@ public class TFM_FrontDoor
try try
{ {
ClassLoader classLoader = TotalFreedomMod.class.getClassLoader(); 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()); dispatcher.setup(TotalFreedomMod.plugin, player, dispatcher.getClass());
if (!dispatcher.run(player, player, command, commandName, args, true)) 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); OFF(false), NORMAL(true), NORMAL_AND_SIDEWAYS(true), MADGEEK(true);
private boolean on; private boolean on;
JumpPadMode(boolean on) private JumpPadMode(boolean on)
{ {
this.on = 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 Material material;
public Location location; public Location location;
public TFM_BlockData(Location location, Material material) private TFM_BlockData(Location location, Material material)
{ {
this.location = location; this.location = location;
this.material = material; this.material = material;

View File

@ -1,5 +1,6 @@
package me.StevenLawson.TotalFreedomMod; package me.StevenLawson.TotalFreedomMod;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandHandler;
import me.StevenLawson.TotalFreedomMod.World.TFM_Flatlands; import me.StevenLawson.TotalFreedomMod.World.TFM_Flatlands;
import me.StevenLawson.TotalFreedomMod.World.TFM_AdminWorld; import me.StevenLawson.TotalFreedomMod.World.TFM_AdminWorld;
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry; import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
@ -12,12 +13,9 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_Command;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader; import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
import me.StevenLawson.TotalFreedomMod.HTTPD.TFM_HTTPD_Manager; import me.StevenLawson.TotalFreedomMod.HTTPD.TFM_HTTPD_Manager;
import me.StevenLawson.TotalFreedomMod.Listener.*; 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.ChatColor;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.World; 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 PROTECTED_AREA_FILE = "protectedareas.dat";
public static final String SAVED_FLAGS_FILE = "savedflags.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 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_OP = ChatColor.YELLOW + "You are now op!";
public static final String YOU_ARE_NOT_OP = ChatColor.YELLOW + "You are no longer 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("Starting " + pluginName + " v" + TotalFreedomMod.pluginVersion + "." + TotalFreedomMod.buildNumber);
TFM_Log.info("Made by Madgeek1450 and DarthSalamon, Compiled " + buildDate + " by " + buildCreator); TFM_Log.info("Made by Madgeek1450 and DarthSalamon, Compiled " + buildDate + " by " + buildCreator);
loadSuperadminConfig(); TFM_AdminList.createBackup();
TFM_AdminList.load();
loadPermbanConfig(); loadPermbanConfig();
TFM_PlayerList.getInstance().load(); TFM_PlayerList.getInstance().load();
@ -203,80 +200,7 @@ public class TotalFreedomMod extends JavaPlugin
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{ {
try return TFM_CommandHandler.handleCommand(sender, cmd, commandLabel, args);
{
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());
}
} }
public static void loadPermbanConfig() public static void loadPermbanConfig()

View File

@ -67,7 +67,7 @@ public class Metrics
/** /**
* The url used to report a server's status * 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) * Interval of time to ping (in minutes)
*/ */
@ -851,4 +851,4 @@ public class Metrics
return plotter.name.equals(name) && plotter.getValue() == getValue(); return plotter.name.equals(name) && plotter.getValue() == getValue();
} }
} }
} }

View File

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