diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/bukkit/help/GenericCommandHelpTopic.java | 75 | ||||
-rw-r--r-- | src/main/java/org/bukkit/help/HelpMap.java | 2 | ||||
-rw-r--r-- | src/main/java/org/bukkit/help/IndexHelpTopic.java | 63 |
3 files changed, 139 insertions, 1 deletions
diff --git a/src/main/java/org/bukkit/help/GenericCommandHelpTopic.java b/src/main/java/org/bukkit/help/GenericCommandHelpTopic.java new file mode 100644 index 00000000..be441862 --- /dev/null +++ b/src/main/java/org/bukkit/help/GenericCommandHelpTopic.java @@ -0,0 +1,75 @@ +package org.bukkit.help; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.apache.commons.lang.StringUtils; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.command.PluginCommand; +import org.bukkit.command.defaults.VanillaCommand; +import org.bukkit.help.HelpTopic; + +/** + * Lacking an alternative, the help system will create instances of GenericCommandHelpTopic for each command in the + * server's CommandMap. You can use this class as a base class for custom help topics, or as an example for how to + * write your own. + */ +public class GenericCommandHelpTopic extends HelpTopic { + + private Command command; + + public GenericCommandHelpTopic(Command command) { + this.command = command; + + if (command.getLabel().startsWith("/")) { + name = command.getLabel(); + } else { + name = "/" + command.getLabel(); + } + + // The short text is the first line of the description + int i = command.getDescription().indexOf("\n"); + if (i > 1) { + shortText = command.getDescription().substring(0, i - 1); + } else { + shortText = command.getDescription(); + } + + // Build full text + StringBuffer sb = new StringBuffer(); + + sb.append(ChatColor.GOLD); + sb.append("Description: "); + sb.append(ChatColor.WHITE); + sb.append(command.getDescription()); + + sb.append("\n"); + + sb.append(ChatColor.GOLD); + sb.append("Usage: "); + sb.append(ChatColor.WHITE); + sb.append(command.getUsage().replace("<command>", name.substring(1))); + + if (command.getAliases().size() > 0) { + sb.append("\n"); + sb.append(ChatColor.GOLD); + sb.append("Aliases: "); + sb.append(ChatColor.WHITE); + sb.append(ChatColor.WHITE + StringUtils.join(command.getAliases(), ", ")); + } + fullText = sb.toString(); + } + + public boolean canSee(CommandSender sender) { + if (!command.isRegistered() && !(command instanceof VanillaCommand)) { + // Unregistered commands should not show up in the help (ignore VanillaCommands) + return false; + } + + if (sender instanceof ConsoleCommandSender) { + return true; + } + + return command.testPermissionSilent(sender); + } +} diff --git a/src/main/java/org/bukkit/help/HelpMap.java b/src/main/java/org/bukkit/help/HelpMap.java index c1ce27be..6e4fc79c 100644 --- a/src/main/java/org/bukkit/help/HelpMap.java +++ b/src/main/java/org/bukkit/help/HelpMap.java @@ -42,5 +42,5 @@ public interface HelpMap { * @param factory The {@link HelpTopicFactory} implementation to associate with the {@code commandClass}. * @throws IllegalArgumentException Thrown if {@code commandClass} does not derive from a legal base class. */ - public void registerHelpTopicFactory(Class commandClass, HelpTopicFactory factory); + public void registerHelpTopicFactory(Class<?> commandClass, HelpTopicFactory<?> factory); } diff --git a/src/main/java/org/bukkit/help/IndexHelpTopic.java b/src/main/java/org/bukkit/help/IndexHelpTopic.java new file mode 100644 index 00000000..50053bcc --- /dev/null +++ b/src/main/java/org/bukkit/help/IndexHelpTopic.java @@ -0,0 +1,63 @@ +package org.bukkit.help; + +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.help.HelpTopic; +import org.bukkit.util.ChatPaginator; + +import java.util.Collection; + +/** + * This help topic generates a list of other help topics. This class is useful for adding your own + * index help topics. + */ +public class IndexHelpTopic extends HelpTopic { + + private Collection<HelpTopic> allTopics; + + /** + * Creates an index help topic from a collection of help topics. The index is displayed in the order of the + * topic collection's iterator. To enforce a particular order, use a sorted collection. + * @param topics The collection of topics to use display in an index. + */ + public IndexHelpTopic(Collection<HelpTopic> topics) { + this.allTopics = topics; + } + + public boolean canSee(CommandSender sender) { + return true; + } + + public String getName() { + return "Overall"; + } + + public String getShortText() { + return ""; + } + + public String getFullText(CommandSender sender) { + StringBuilder sb = new StringBuilder(); + for (HelpTopic topic : allTopics) { + if (topic.canSee(sender)) { + StringBuilder line = new StringBuilder(); + line.append(ChatColor.GOLD); + line.append(topic.getName()); + line.append(": "); + line.append(ChatColor.WHITE); + line.append(topic.getShortText()); + + String lineStr = line.toString().replace("\n", ". "); + if (sender instanceof Player && lineStr.length() > ChatPaginator.AVERAGE_CHAT_PAGE_WIDTH) { + sb.append(lineStr.substring(0, ChatPaginator.AVERAGE_CHAT_PAGE_WIDTH - 3)); + sb.append("..."); + } else { + sb.append(lineStr); + } + sb.append("\n"); + } + } + return sb.toString(); + } +} |