From 89e5e16aadd8432e64d8e09956e518adcd13a89f Mon Sep 17 00:00:00 2001 From: GJ Date: Fri, 1 Feb 2013 12:27:24 -0500 Subject: [PATCH] This is a much better way to work with our commands dynamically. It allows for localized description strings, aliases, etc. With this addition, our "ugly alias" method in CommandPreProcessEvent is no longer needed, nor is our alias map. This also makes us more friendly with Essentials - if Essentials is enabled, the /repair command will be changed to /mcrepair for compatibility reasons. --- .../commands/CommandRegistrationHelper.java | 112 ++++++++++++++++++ .../nossr50/listeners/PlayerListener.java | 24 ---- src/main/java/com/gmail/nossr50/mcMMO.java | 71 +---------- .../resources/locale/locale_en_US.properties | 3 + src/main/resources/plugin.yml | 26 ---- 5 files changed, 118 insertions(+), 118 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/commands/CommandRegistrationHelper.java diff --git a/src/main/java/com/gmail/nossr50/commands/CommandRegistrationHelper.java b/src/main/java/com/gmail/nossr50/commands/CommandRegistrationHelper.java new file mode 100644 index 000000000..056d3b917 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/commands/CommandRegistrationHelper.java @@ -0,0 +1,112 @@ +package com.gmail.nossr50.commands; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.command.PluginCommand; + +import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.skills.acrobatics.AcrobaticsCommand; +import com.gmail.nossr50.skills.archery.ArcheryCommand; +import com.gmail.nossr50.skills.axes.AxesCommand; +import com.gmail.nossr50.skills.excavation.ExcavationCommand; +import com.gmail.nossr50.skills.fishing.FishingCommand; +import com.gmail.nossr50.skills.herbalism.HerbalismCommand; +import com.gmail.nossr50.skills.mining.MiningCommand; +import com.gmail.nossr50.skills.repair.RepairCommand; +import com.gmail.nossr50.skills.smelting.SmeltingCommand; +import com.gmail.nossr50.skills.swords.SwordsCommand; +import com.gmail.nossr50.skills.taming.TamingCommand; +import com.gmail.nossr50.skills.unarmed.UnarmedCommand; +import com.gmail.nossr50.skills.utilities.SkillType; +import com.gmail.nossr50.skills.woodcutting.WoodcuttingCommand; +import com.gmail.nossr50.util.Misc; + +public final class CommandRegistrationHelper { + private CommandRegistrationHelper() {}; + + public static void registerSkillCommands() { + for (SkillType skill : SkillType.values()) { + if (skill != SkillType.ALL) { + String commandName = skill.toString().toLowerCase(); + String localizedName = LocaleLoader.getString(Misc.getCapitalized(commandName) + ".SkillName").toLowerCase(); + + List aliasList = new ArrayList(); + aliasList.add(localizedName); + + PluginCommand command; + + // Make us play nice with Essentials + if (skill == SkillType.REPAIR && mcMMO.p.getServer().getPluginManager().isPluginEnabled("Essentials")) { + command = mcMMO.p.getCommand("mcrepair"); + } + else { + command = mcMMO.p.getCommand(commandName); + } + + command.setAliases(aliasList); + command.setDescription(LocaleLoader.getString("Commands.Description.Skill", new Object[] { Misc.getCapitalized(localizedName) })); + command.setPermission("mcmmo.skills." + commandName); + command.setPermissionMessage(LocaleLoader.getString("mcMMO.NoPermission")); + + switch (skill) { + case ACROBATICS: + command.setExecutor(new AcrobaticsCommand()); + break; + + case ARCHERY: + command.setExecutor(new ArcheryCommand()); + break; + + case AXES: + command.setExecutor(new AxesCommand()); + break; + + case EXCAVATION: + command.setExecutor(new ExcavationCommand()); + break; + + case FISHING: + command.setExecutor(new FishingCommand()); + break; + + case HERBALISM: + command.setExecutor(new HerbalismCommand()); + break; + + case MINING: + command.setExecutor(new MiningCommand()); + break; + + case REPAIR: + command.setExecutor(new RepairCommand()); + break; + + case SMELTING: + command.setExecutor(new SmeltingCommand()); + break; + + case SWORDS: + command.setExecutor(new SwordsCommand()); + break; + + case TAMING: + command.setExecutor(new TamingCommand()); + break; + + case UNARMED: + command.setExecutor(new UnarmedCommand()); + break; + + case WOODCUTTING: + command.setExecutor(new WoodcuttingCommand()); + break; + + default: + break; + } + } + } + } +} diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index b479aabfc..b49452e53 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -9,7 +9,6 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerChangedWorldEvent; -import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerFishEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; @@ -348,27 +347,4 @@ public class PlayerListener implements Listener { event.setCancelled(true); } } - - /** - * Monitor PlayerCommandPreprocess events. - * - * @param event The event to watch - */ - @EventHandler(priority = EventPriority.LOWEST) - public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) { - String message = event.getMessage(); - String command = message.substring(1).split(" ")[0]; - String lowerCaseCommand = command.toLowerCase(); - - if (plugin.commandIsAliased(lowerCaseCommand)) { - String commandAlias = plugin.getCommandAlias(lowerCaseCommand); - - //TODO: We should find a better way to avoid string replacement where the alias is equals to the command - if (command.equals(commandAlias)) { - return; - } - - event.setMessage(message.replace(command, plugin.getCommandAlias(lowerCaseCommand))); - } - } } diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 660bbdfa8..aa4ba8c26 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -18,6 +18,7 @@ import org.bukkit.scheduler.BukkitScheduler; import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManager; import com.gmail.nossr50.util.blockmeta.chunkmeta.ChunkManagerFactory; +import com.gmail.nossr50.commands.CommandRegistrationHelper; import com.gmail.nossr50.commands.general.AddlevelsCommand; import com.gmail.nossr50.commands.general.AddxpCommand; import com.gmail.nossr50.commands.general.InspectCommand; @@ -48,7 +49,6 @@ import com.gmail.nossr50.listeners.HardcoreListener; import com.gmail.nossr50.listeners.InventoryListener; import com.gmail.nossr50.listeners.PlayerListener; import com.gmail.nossr50.listeners.WorldListener; -import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.mods.config.CustomArmorConfig; import com.gmail.nossr50.mods.config.CustomBlocksConfig; import com.gmail.nossr50.mods.config.CustomToolsConfig; @@ -59,25 +59,12 @@ import com.gmail.nossr50.party.commands.PartyCommand; import com.gmail.nossr50.party.commands.PtpCommand; import com.gmail.nossr50.runnables.MobStoreCleaner; import com.gmail.nossr50.runnables.SaveTimer; -import com.gmail.nossr50.skills.acrobatics.AcrobaticsCommand; -import com.gmail.nossr50.skills.archery.ArcheryCommand; -import com.gmail.nossr50.skills.axes.AxesCommand; -import com.gmail.nossr50.skills.excavation.ExcavationCommand; -import com.gmail.nossr50.skills.fishing.FishingCommand; -import com.gmail.nossr50.skills.herbalism.HerbalismCommand; -import com.gmail.nossr50.skills.mining.MiningCommand; -import com.gmail.nossr50.skills.repair.RepairCommand; import com.gmail.nossr50.skills.repair.RepairManager; import com.gmail.nossr50.skills.repair.RepairManagerFactory; import com.gmail.nossr50.skills.repair.Repairable; import com.gmail.nossr50.skills.repair.config.RepairConfigManager; import com.gmail.nossr50.skills.runnables.BleedTimer; import com.gmail.nossr50.skills.runnables.SkillMonitor; -import com.gmail.nossr50.skills.smelting.SmeltingCommand; -import com.gmail.nossr50.skills.swords.SwordsCommand; -import com.gmail.nossr50.skills.taming.TamingCommand; -import com.gmail.nossr50.skills.unarmed.UnarmedCommand; -import com.gmail.nossr50.skills.woodcutting.WoodcuttingCommand; import com.gmail.nossr50.spout.SpoutConfig; import com.gmail.nossr50.spout.SpoutTools; import com.gmail.nossr50.spout.commands.MchudCommand; @@ -96,7 +83,6 @@ public class mcMMO extends JavaPlugin { private final WorldListener worldListener = new WorldListener(); private final HardcoreListener hardcoreListener = new HardcoreListener(); - private HashMap aliasMap = new HashMap(); //Alias - Command private HashMap tntTracker = new HashMap(); private HashMap furnaceTracker = new HashMap(); @@ -129,7 +115,7 @@ public class mcMMO extends JavaPlugin { setupFilePaths(); // Check for Spout - if (getServer().getPluginManager().getPlugin("Spout") != null) { + if (getServer().getPluginManager().isPluginEnabled("Spout")) { spoutEnabled = true; SpoutConfig.getInstance(); @@ -337,38 +323,7 @@ public class mcMMO extends JavaPlugin { * Register the commands. */ private void registerCommands() { - // Register aliases with the aliasmap (used in the playercommandpreprocessevent to ugly alias them to actual commands) - // Skills commands - aliasMap.put(LocaleLoader.getString("Acrobatics.SkillName").toLowerCase(), "acrobatics"); - aliasMap.put(LocaleLoader.getString("Archery.SkillName").toLowerCase(), "archery"); - aliasMap.put(LocaleLoader.getString("Axes.SkillName").toLowerCase(), "axes"); - aliasMap.put(LocaleLoader.getString("Excavation.SkillName").toLowerCase(), "excavation"); - aliasMap.put(LocaleLoader.getString("Fishing.SkillName").toLowerCase(), "fishing"); - aliasMap.put(LocaleLoader.getString("Herbalism.SkillName").toLowerCase(), "herbalism"); - aliasMap.put(LocaleLoader.getString("Mining.SkillName").toLowerCase(), "mining"); - aliasMap.put(LocaleLoader.getString("Repair.SkillName").toLowerCase(), "repair"); - aliasMap.put(LocaleLoader.getString("Smelting.SkillName").toLowerCase(), "smelting"); - aliasMap.put(LocaleLoader.getString("Swords.SkillName").toLowerCase(), "swords"); - aliasMap.put(LocaleLoader.getString("Taming.SkillName").toLowerCase(), "taming"); - aliasMap.put(LocaleLoader.getString("Unarmed.SkillName").toLowerCase(), "unarmed"); - aliasMap.put(LocaleLoader.getString("Woodcutting.SkillName").toLowerCase(), "woodcutting"); - - // Register commands - // Skills commands - getCommand("acrobatics").setExecutor(new AcrobaticsCommand()); - getCommand("archery").setExecutor(new ArcheryCommand()); - getCommand("axes").setExecutor(new AxesCommand()); - getCommand("excavation").setExecutor(new ExcavationCommand()); - getCommand("fishing").setExecutor(new FishingCommand()); - getCommand("herbalism").setExecutor(new HerbalismCommand()); - getCommand("mining").setExecutor(new MiningCommand()); - getCommand("repair").setExecutor(new RepairCommand()); - getCommand("smelting").setExecutor(new SmeltingCommand()); - getCommand("swords").setExecutor(new SwordsCommand()); - getCommand("taming").setExecutor(new TamingCommand()); - getCommand("unarmed").setExecutor(new UnarmedCommand()); - getCommand("woodcutting").setExecutor(new WoodcuttingCommand()); - + CommandRegistrationHelper.registerSkillCommands(); Config configInstance = Config.getInstance(); // mc* commands @@ -463,26 +418,6 @@ public class mcMMO extends JavaPlugin { getCommand("mchud").setExecutor(new MchudCommand()); } - /** - * Checks to see if the alias map contains the given key. - * - * @param command The command to check - * @return true if the command is in the map, false otherwise - */ - public boolean commandIsAliased(String command) { - return aliasMap.containsKey(command); - } - - /** - * Get the alias of a given command. - * - * @param command The command to retrieve the alias of - * @return the alias of the command - */ - public String getCommandAlias(String command) { - return aliasMap.get(command); - } - /** * Add a set of values to the TNT tracker. * diff --git a/src/main/resources/locale/locale_en_US.properties b/src/main/resources/locale/locale_en_US.properties index 087684425..379cc2419 100644 --- a/src/main/resources/locale/locale_en_US.properties +++ b/src/main/resources/locale/locale_en_US.properties @@ -684,3 +684,6 @@ Smelting.Effect.7=Chance for ores to be instantly smelted while mining Smelting.FluxMining.Success=[[GREEN]]That ore smelted itself! Smelting.Listener=Smelting: Smelting.SkillName=SMELTING + +#COMMAND DESCRIPTIONS +Commands.Description.Skill=Detailed skill info for {0} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index a9f3455ad..3da6348f5 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -83,44 +83,18 @@ commands: aliases: [] description: Reset the level of one or all of your skills excavation: - aliases: [] - description: Detailed skill info herbalism: - aliases: [] - description: Detailed skill info mining: - aliases: [] - description: Detailed skill info woodcutting: - aliases: [] - description: Detailed skill info axes: - aliases: [] - description: Detailed skill info archery: - aliases: [] - description: Detailed skill info swords: - aliases: [] - description: Detailed skill info taming: - aliases: [] - description: Detailed skill info unarmed: - aliases: [] - description: Detailed skill info acrobatics: - aliases: [] - description: Detailed skill info repair: - aliases: [mcrepair] - description: Detailed skill info fishing: - aliases: [] - description: Detailed skill info smelting: - aliases: [] - description: Detailed skill info a: aliases: [ac] description: Toggle Admin chat or send admin chat messages