diff options
author | rmichela <deltahat@gmail.com> | 2012-03-22 00:20:33 -0400 |
---|---|---|
committer | EvilSeph <evilseph@gmail.com> | 2012-03-22 00:48:48 -0400 |
commit | 816d7d850c5b7a163417ca984731bd222dfa7839 (patch) | |
tree | 5851167bc1382d2b3e327fe969dcb251c54801db | |
parent | 45d5f691b164129744155d443d39b92045eddb29 (diff) | |
download | bukkit-816d7d850c5b7a163417ca984731bd222dfa7839.tar bukkit-816d7d850c5b7a163417ca984731bd222dfa7839.tar.gz bukkit-816d7d850c5b7a163417ca984731bd222dfa7839.tar.lz bukkit-816d7d850c5b7a163417ca984731bd222dfa7839.tar.xz bukkit-816d7d850c5b7a163417ca984731bd222dfa7839.zip |
[Bleeding] Made IndexHelpTopic more conducive to subclassing. Addresses BUKKIT-1263
-rw-r--r-- | src/main/java/org/bukkit/help/IndexHelpTopic.java | 61 |
1 files changed, 46 insertions, 15 deletions
diff --git a/src/main/java/org/bukkit/help/IndexHelpTopic.java b/src/main/java/org/bukkit/help/IndexHelpTopic.java index ce468969..61db6c8d 100644 --- a/src/main/java/org/bukkit/help/IndexHelpTopic.java +++ b/src/main/java/org/bukkit/help/IndexHelpTopic.java @@ -4,7 +4,6 @@ import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; -import org.bukkit.help.HelpTopic; import org.bukkit.util.ChatPaginator; import java.util.Collection; @@ -12,23 +11,35 @@ 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. To enforce a particular order, use a sorted collection. + * <p/> + * If a preamble is provided to the constructor, that text will be displayed before the first item + * in the index. */ public class IndexHelpTopic extends HelpTopic { - private String permission; - private String preamble; - private Collection<HelpTopic> allTopics; + protected String permission; + protected String preamble; + protected Collection<HelpTopic> allTopics; public IndexHelpTopic(String name, String shortText, String permission, Collection<HelpTopic> topics) { this(name, shortText, permission, topics, null); } - + public IndexHelpTopic(String name, String shortText, String permission, Collection<HelpTopic> topics, String preamble) { this.name = name; this.shortText = shortText; this.permission = permission; - this.allTopics = topics; this.preamble = preamble; + setTopicsCollection(topics); + } + + /** + * Sets the contents of the internal allTopics collection. + * + * @param topics The topics to set. + */ + protected void setTopicsCollection(Collection<HelpTopic> topics) { + this.allTopics = topics; } public boolean canSee(CommandSender sender) { @@ -50,20 +61,13 @@ public class IndexHelpTopic extends HelpTopic { StringBuilder sb = new StringBuilder(); if (preamble != null) { - sb.append(preamble); + sb.append(buildPreamble(sender)); sb.append("\n"); } 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", ". "); + String lineStr = buildIndexLine(sender, topic).replace("\n", ". "); if (sender instanceof Player && lineStr.length() > ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH) { sb.append(lineStr.substring(0, ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH - 3)); sb.append("..."); @@ -75,4 +79,31 @@ public class IndexHelpTopic extends HelpTopic { } return sb.toString(); } + + /** + * Builds the topic preamble. Override this method to change how the index preamble looks. + * + * @param sender The command sender requesting the preamble. + * @return The topic preamble. + */ + protected String buildPreamble(CommandSender sender) { + return ChatColor.GRAY + preamble; + } + + /** + * Builds individual lines in the index topic. Override this method to change how index lines are rendered. + * + * @param sender The command sender requesting the index line. + * @param topic The topic to render into an index line. + * @return The rendered index line. + */ + protected String buildIndexLine(CommandSender sender, HelpTopic topic) { + StringBuilder line = new StringBuilder(); + line.append(ChatColor.GOLD); + line.append(topic.getName()); + line.append(": "); + line.append(ChatColor.WHITE); + line.append(topic.getShortText()); + return line.toString(); + } } |