diff --git a/src/main/java/org/bukkit/craftbukkit/help/CommandAliasHelpTopic.java b/src/main/java/org/bukkit/craftbukkit/help/CommandAliasHelpTopic.java new file mode 100644 index 0000000000..b815b9a38f --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/help/CommandAliasHelpTopic.java @@ -0,0 +1,43 @@ +package org.bukkit.craftbukkit.help; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.help.HelpMap; +import org.bukkit.help.HelpTopic; + +public class CommandAliasHelpTopic extends HelpTopic { + + private String aliasFor; + private Command command; + private HelpMap helpMap; + + public CommandAliasHelpTopic(String alias, String aliasFor, Command command, HelpMap helpMap) { + this.aliasFor = aliasFor.startsWith("/") ? aliasFor : "/" + aliasFor; + this.helpMap = helpMap; + this.command = command; + this.name = alias.startsWith("/") ? alias : "/" + alias; + this.shortText = ChatColor.YELLOW + "Alias for " + ChatColor.WHITE + this.aliasFor; + } + + @Override + public String getFullText(CommandSender forWho) { + StringBuilder sb = new StringBuilder(shortText); + HelpTopic aliasForTopic = helpMap.getHelpTopic(aliasFor); + if (aliasForTopic != null) { + sb.append("\n"); + sb.append(aliasForTopic.getFullText(forWho)); + } + return sb.toString(); + } + + @Override + public boolean canSee(CommandSender commandSender) { + HelpTopic aliasForTopic = helpMap.getHelpTopic(aliasFor); + if (aliasForTopic != null) { + return aliasForTopic.canSee(commandSender); + } else { + return false; + } + } +} diff --git a/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java b/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java index ee2d05bf3c..ba228dbdcb 100644 --- a/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java +++ b/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java @@ -1,5 +1,7 @@ package org.bukkit.craftbukkit.help; +import com.google.common.base.Predicates; +import com.google.common.collect.Collections2; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.MultipleCommandAlias; @@ -21,7 +23,7 @@ public class SimpleHelpMap implements HelpMap { public SimpleHelpMap() { helpTopics = new TreeMap(new HelpTopicComparator()); // Using a TreeMap for its explicit sorting on key - defaultTopic = new IndexHelpTopic(helpTopics.values()); + defaultTopic = new IndexHelpTopic(null, null, null, Collections2.filter(helpTopics.values(), Predicates.not(Predicates.instanceOf(CommandAliasHelpTopic.class)))); topicFactoryMap = new HashMap(); registerHelpTopicFactory(MultipleCommandAlias.class, new MultipleCommandAliasHelpTopicFactory()); @@ -84,6 +86,13 @@ public class SimpleHelpMap implements HelpMap { } addTopic(new GenericCommandHelpTopic(command)); } + + // Initialize command alias help topics + for (Command command : server.getCommandMap().getCommands()) { + for (String alias : command.getAliases()) { + addTopic(new CommandAliasHelpTopic(alias, command.getLabel(), command, this)); + } + } // Initialize help topics from the server's fallback commands for (VanillaCommand command : server.getCommandMap().getFallbackCommands()) { @@ -97,6 +106,9 @@ public class SimpleHelpMap implements HelpMap { helpTopics.get(amendment.getTopicName()).amendTopic(amendment.getShortText(), amendment.getFullText()); } } + + // Add alias sub-index + addTopic(new IndexHelpTopic("Aliases", "Lists command aliases", null, Collections2.filter(helpTopics.values(), Predicates.instanceOf(CommandAliasHelpTopic.class)))); } public void registerHelpTopicFactory(Class commandClass, HelpTopicFactory factory) {