From ff0f9073dbf7d6b7733d659be166ce046e1d8bd2 Mon Sep 17 00:00:00 2001 From: zml2008 Date: Thu, 12 Apr 2012 19:29:20 -0700 Subject: [PATCH] Improved command help format a bit --- .../bukkit/util/CommandRegistration.java | 4 +-- .../bukkit/util/DynamicPluginCommand.java | 14 ++++++-- .../util/DynamicPluginCommandHelpTopic.java | 35 ++++++++++++++++--- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/sk89q/bukkit/util/CommandRegistration.java b/src/main/java/com/sk89q/bukkit/util/CommandRegistration.java index d5c58b987..8c5b5f8ca 100644 --- a/src/main/java/com/sk89q/bukkit/util/CommandRegistration.java +++ b/src/main/java/com/sk89q/bukkit/util/CommandRegistration.java @@ -61,7 +61,7 @@ public class CommandRegistration { } for (CommandInfo command : registered) { DynamicPluginCommand cmd = new DynamicPluginCommand(command.getAliases(), - command.getDesc(), "/" + command.getAliases()[0] + " " + command.getUsage(), executor, command.getRegisteredWith()); + command.getDesc(), "/" + command.getAliases()[0] + " " + command.getUsage(), executor, command.getRegisteredWith(), plugin); cmd.setPermissions(command.getPermissions()); commandMap.register(plugin.getDescription().getName(), cmd); } @@ -93,7 +93,7 @@ public class CommandRegistration { } for (Iterator i = knownCommands.values().iterator(); i.hasNext();) { org.bukkit.command.Command cmd = i.next(); - if (cmd instanceof DynamicPluginCommand && ((DynamicPluginCommand) cmd).getOwner().equals(plugin)) { + if (cmd instanceof DynamicPluginCommand && ((DynamicPluginCommand) cmd).getOwner().equals(executor)) { i.remove(); for (String alias : cmd.getAliases()) { org.bukkit.command.Command aliasCmd = knownCommands.get(alias); diff --git a/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java b/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java index d8b62df9b..ae81710ce 100644 --- a/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java +++ b/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommand.java @@ -22,20 +22,25 @@ package com.sk89q.bukkit.util; import com.sk89q.util.StringUtil; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginIdentifiableCommand; +import org.bukkit.plugin.Plugin; + import java.util.Arrays; /** * @author zml2008 */ -public class DynamicPluginCommand extends org.bukkit.command.Command { +public class DynamicPluginCommand extends org.bukkit.command.Command implements PluginIdentifiableCommand { protected final CommandExecutor owner; protected final Object registeredWith; + protected final Plugin owningPlugin; protected String[] permissions = new String[0]; - public DynamicPluginCommand(String[] aliases, String desc, String usage, CommandExecutor owner, Object registeredWith) { + public DynamicPluginCommand(String[] aliases, String desc, String usage, CommandExecutor owner, Object registeredWith, Plugin plugin) { super(aliases[0], desc, usage, Arrays.asList(aliases)); this.owner = owner; + this.owningPlugin = plugin; this.registeredWith = registeredWith; } @@ -62,4 +67,9 @@ public class DynamicPluginCommand extends org.bukkit.command.Command { public String[] getPermissions() { return permissions; } + + @Override + public Plugin getPlugin() { + return owningPlugin; + } } diff --git a/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommandHelpTopic.java b/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommandHelpTopic.java index 936a47879..80439b3c6 100644 --- a/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommandHelpTopic.java +++ b/src/main/java/com/sk89q/bukkit/util/DynamicPluginCommandHelpTopic.java @@ -21,6 +21,7 @@ package com.sk89q.bukkit.util; import com.sk89q.minecraft.util.commands.CommandsManager; import com.sk89q.wepif.PermissionsResolverManager; import com.sk89q.wepif.WEPIFRuntimeException; +import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.help.HelpTopic; @@ -38,23 +39,49 @@ public class DynamicPluginCommandHelpTopic extends HelpTopic { this.cmd = cmd; this.name = "/" + cmd.getName(); + String fullTextTemp = null; + StringBuilder fullText = new StringBuilder(); + if (cmd.getRegisteredWith() instanceof CommandsManager) { Map helpText = ((CommandsManager) cmd.getRegisteredWith()).getHelpMessages(); final String lookupName = cmd.getName().replaceAll("/", ""); - if (helpText.containsKey(lookupName)) { - this.fullText = helpText.get(lookupName); + if (helpText.containsKey(lookupName)) { // We have full help text for this command + fullTextTemp = helpText.get(lookupName); } + // No full help text, assemble help text from info helpText = ((CommandsManager) cmd.getRegisteredWith()).getCommands(); if (helpText.containsKey(cmd.getName())) { final String shortText = helpText.get(cmd.getName()); - if (this.fullText == null) { - this.fullText = this.name + " " + shortText; + if (fullTextTemp == null) { + fullTextTemp = this.name + " " + shortText; } this.shortText = shortText; } } else { this.shortText = cmd.getDescription(); } + + // Put the usage in the format: Usage string (newline) Aliases (newline) Help text + String[] split = fullTextTemp == null ? new String[2] : fullTextTemp.split("\n", 2); + fullText.append(ChatColor.BOLD).append(ChatColor.GOLD).append("Usage: ").append(ChatColor.WHITE); + fullText.append(split[0]).append("\n"); + + if (cmd.getAliases().size() > 0) { + fullText.append(ChatColor.BOLD).append(ChatColor.GOLD).append("Aliases: ").append(ChatColor.WHITE); + boolean first = true; + for (String alias : cmd.getAliases()) { + if (!first) { + fullText.append(", "); + } + fullText.append(alias); + first = false; + } + fullText.append("\n"); + } + if (split.length > 1) { + fullText.append(split[1]); + } + this.fullText = fullText.toString(); } @Override