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.
This commit is contained in:
GJ 2013-02-01 12:27:24 -05:00
parent cec132092f
commit 89e5e16aad
5 changed files with 118 additions and 118 deletions

View File

@ -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<String> aliasList = new ArrayList<String>();
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;
}
}
}
}
}

View File

@ -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)));
}
}
}

View File

@ -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<String, String> aliasMap = new HashMap<String, String>(); //Alias - Command
private HashMap<Integer, String> tntTracker = new HashMap<Integer, String>();
private HashMap<Block, String> furnaceTracker = new HashMap<Block, String>();
@ -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.
*

View File

@ -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}

View File

@ -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