diff options
author | rmichela <deltahat@gmail.com> | 2012-03-15 02:28:01 -0400 |
---|---|---|
committer | EvilSeph <evilseph@gmail.com> | 2012-03-16 02:48:19 -0400 |
commit | f87e053c66cd229730bf8a4faba834ce7393f5f7 (patch) | |
tree | 14b3c2ae7f1b0778ba286bf043ff4982e6dc7e01 | |
parent | 184faf1f299df4f821c287bc179f106c78efbbc5 (diff) | |
download | craftbukkit-f87e053c66cd229730bf8a4faba834ce7393f5f7.tar craftbukkit-f87e053c66cd229730bf8a4faba834ce7393f5f7.tar.gz craftbukkit-f87e053c66cd229730bf8a4faba834ce7393f5f7.tar.lz craftbukkit-f87e053c66cd229730bf8a4faba834ce7393f5f7.tar.xz craftbukkit-f87e053c66cd229730bf8a4faba834ce7393f5f7.zip |
[Bleeding] Added automatically generated plugin-level sub-indexes to the master help index. Addresses BUKKIT-1180
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/help/HelpTopicComparator.java | 26 | ||||
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java | 61 |
2 files changed, 67 insertions, 20 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/help/HelpTopicComparator.java b/src/main/java/org/bukkit/craftbukkit/help/HelpTopicComparator.java index 7819bef9..9c0028ec 100644 --- a/src/main/java/org/bukkit/craftbukkit/help/HelpTopicComparator.java +++ b/src/main/java/org/bukkit/craftbukkit/help/HelpTopicComparator.java @@ -1,19 +1,29 @@ package org.bukkit.craftbukkit.help; +import org.bukkit.help.HelpTopic; + import java.util.Comparator; /** * Used to impose a custom total ordering on help topics. All topics are listed in alphabetic order, but topics * that start with a slash come after topics that don't. */ -public class HelpTopicComparator implements Comparator<String> { - public int compare(String lhs, String rhs) { - if (lhs.startsWith("/") && !rhs.startsWith("/")) { - return 1; - } else if (!lhs.startsWith("/") && rhs.startsWith("/")) { - return -1; - } else { - return lhs.compareToIgnoreCase(rhs); +public class HelpTopicComparator implements Comparator<HelpTopic> { + private TopicNameComparator tnc = new TopicNameComparator(); + + public int compare(HelpTopic lhs, HelpTopic rhs) { + return tnc.compare(lhs.getName(), rhs.getName()); + } + + public static class TopicNameComparator implements Comparator<String> { + public int compare(String lhs, String rhs) { + if (lhs.startsWith("/") && !rhs.startsWith("/")) { + return 1; + } else if (!lhs.startsWith("/") && rhs.startsWith("/")) { + return -1; + } else { + return lhs.compareToIgnoreCase(rhs); + } } } } diff --git a/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java b/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java index d7313ff1..d5696c84 100644 --- a/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java +++ b/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java @@ -2,11 +2,9 @@ package org.bukkit.craftbukkit.help; import com.google.common.base.Predicates; import com.google.common.collect.Collections2; +import org.bukkit.ChatColor; import org.bukkit.Server; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.MultipleCommandAlias; -import org.bukkit.command.PluginCommand; +import org.bukkit.command.*; import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.command.defaults.VanillaCommand; import org.bukkit.craftbukkit.CraftServer; @@ -21,11 +19,13 @@ public class SimpleHelpMap implements HelpMap { private HelpTopic defaultTopic; private Map<String, HelpTopic> helpTopics; + private Set<HelpTopic> pluginIndexes; private Map<Class, HelpTopicFactory<Command>> topicFactoryMap; private CraftServer server; public SimpleHelpMap(CraftServer server) { - helpTopics = new TreeMap<String, HelpTopic>(new HelpTopicComparator()); // Using a TreeMap for its explicit sorting on key + helpTopics = new TreeMap<String, HelpTopic>(new HelpTopicComparator.TopicNameComparator()); // Using a TreeMap for its explicit sorting on key + pluginIndexes = new TreeSet<HelpTopic>(new HelpTopicComparator()); defaultTopic = new IndexHelpTopic("Index", null, null, Collections2.filter(helpTopics.values(), Predicates.not(Predicates.instanceOf(CommandAliasHelpTopic.class)))); topicFactoryMap = new HashMap<Class, HelpTopicFactory<Command>>(); this.server = server; @@ -45,6 +45,10 @@ public class SimpleHelpMap implements HelpMap { return null; } + public Collection<HelpTopic> getHelpTopics() { + return helpTopics.values(); + } + public synchronized void addTopic(HelpTopic topic) { // Existing topics take priority if (!helpTopics.containsKey(topic.getName())) { @@ -89,11 +93,13 @@ public class SimpleHelpMap implements HelpMap { // Register a topic for (Class c : topicFactoryMap.keySet()) { if (c.isAssignableFrom(command.getClass())) { - addTopic(topicFactoryMap.get(c).createTopic(command)); + HelpTopic t = topicFactoryMap.get(c).createTopic(command); + if (t != null) addTopic(t); continue outer; } if (command instanceof PluginCommand && c.isAssignableFrom(((PluginCommand)command).getExecutor().getClass())) { - addTopic(topicFactoryMap.get(c).createTopic(command)); + HelpTopic t = topicFactoryMap.get(c).createTopic(command); + if (t != null) addTopic(t); continue outer; } } @@ -119,6 +125,15 @@ public class SimpleHelpMap implements HelpMap { // Add alias sub-index addTopic(new IndexHelpTopic("Aliases", "Lists command aliases", null, Collections2.filter(helpTopics.values(), Predicates.instanceOf(CommandAliasHelpTopic.class)))); + + // Initialize plugin-level sub-topics + Map<String, Set<HelpTopic>> pluginIndexes = new HashMap<String, Set<HelpTopic>>(); + fillPluginIndexes(pluginIndexes, server.getCommandMap().getCommands()); + fillPluginIndexes(pluginIndexes, server.getCommandMap().getFallbackCommands()); + + for (Map.Entry<String, Set<HelpTopic>> entry : pluginIndexes.entrySet()) { + addTopic(new IndexHelpTopic(entry.getKey(), "All commands for " + entry.getKey(), null, entry.getValue(), ChatColor.GRAY + "Below is a list of all " + entry.getKey() + " commands:")); + } // Amend help topics from the help.yml file for (HelpTopicAmendment amendment : helpYamlReader.getTopicAmendments()) { @@ -131,14 +146,36 @@ public class SimpleHelpMap implements HelpMap { } } - private boolean commandInIgnoredPlugin(Command command, List<String> ignoredPlugins) { - if (command instanceof BukkitCommand && ignoredPlugins.contains("Bukkit")) { - return true; + private void fillPluginIndexes(Map<String, Set<HelpTopic>> pluginIndexes, Collection<? extends Command> commands) { + for (Command command : commands) { + String pluginName = getCommandPluginName(command); + if (pluginName != null) { + HelpTopic topic = getHelpTopic("/" + command.getLabel()); + if (topic != null) { + if (!pluginIndexes.containsKey(pluginName)) { + pluginIndexes.put(pluginName, new TreeSet<HelpTopic>(new HelpTopicComparator())); //keep things in topic order + } + pluginIndexes.get(pluginName).add(topic); + } + } } - if (command instanceof VanillaCommand && ignoredPlugins.contains("Bukkit")) { + } + + private String getCommandPluginName(Command command) { + if (command instanceof BukkitCommand || command instanceof VanillaCommand) { + return "Bukkit"; + } + if (command instanceof PluginIdentifiableCommand) { + return ((PluginIdentifiableCommand)command).getPlugin().getName(); + } + return null; + } + + private boolean commandInIgnoredPlugin(Command command, List<String> ignoredPlugins) { + if ((command instanceof BukkitCommand || command instanceof VanillaCommand) && ignoredPlugins.contains("Bukkit")) { return true; } - if (command instanceof PluginCommand && ignoredPlugins.contains(((PluginCommand)command).getPlugin().getName())) { + if (command instanceof PluginIdentifiableCommand && ignoredPlugins.contains(((PluginIdentifiableCommand)command).getPlugin().getName())) { return true; } return false; |