Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
IndexHelpTopic.java
1 package org.bukkit.help;
2 
3 import org.bukkit.ChatColor;
6 import org.bukkit.entity.Player;
8 
9 import java.util.Collection;
10 
11 /**
12  * This help topic generates a list of other help topics. This class is useful
13  * for adding your own index help topics. To enforce a particular order, use a
14  * sorted collection.
15  * <p>
16  * If a preamble is provided to the constructor, that text will be displayed
17  * before the first item in the index.
18  */
19 public class IndexHelpTopic extends HelpTopic {
20 
21  protected String permission;
22  protected String preamble;
23  protected Collection<HelpTopic> allTopics;
24 
25  public IndexHelpTopic(String name, String shortText, String permission, Collection<HelpTopic> topics) {
26  this(name, shortText, permission, topics, null);
27  }
28 
29  public IndexHelpTopic(String name, String shortText, String permission, Collection<HelpTopic> topics, String preamble) {
30  this.name = name;
31  this.shortText = shortText;
32  this.permission = permission;
33  this.preamble = preamble;
34  setTopicsCollection(topics);
35  }
36 
37  /**
38  * Sets the contents of the internal allTopics collection.
39  *
40  * @param topics The topics to set.
41  */
42  protected void setTopicsCollection(Collection<HelpTopic> topics) {
43  this.allTopics = topics;
44  }
45 
46  public boolean canSee(CommandSender sender) {
47  if (sender instanceof ConsoleCommandSender) {
48  return true;
49  }
50  if (permission == null) {
51  return true;
52  }
53  return sender.hasPermission(permission);
54  }
55 
56  @Override
57  public void amendCanSee(String amendedPermission) {
58  permission = amendedPermission;
59  }
60 
61  public String getFullText(CommandSender sender) {
62  StringBuilder sb = new StringBuilder();
63 
64  if (preamble != null) {
65  sb.append(buildPreamble(sender));
66  sb.append("\n");
67  }
68 
69  for (HelpTopic topic : allTopics) {
70  if (topic.canSee(sender)) {
71  String lineStr = buildIndexLine(sender, topic).replace("\n", ". ");
72  if (sender instanceof Player && lineStr.length() > ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH) {
73  sb.append(lineStr.substring(0, ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH - 3));
74  sb.append("...");
75  } else {
76  sb.append(lineStr);
77  }
78  sb.append("\n");
79  }
80  }
81  return sb.toString();
82  }
83 
84  /**
85  * Builds the topic preamble. Override this method to change how the index
86  * preamble looks.
87  *
88  * @param sender The command sender requesting the preamble.
89  * @return The topic preamble.
90  */
91  protected String buildPreamble(CommandSender sender) {
92  return ChatColor.GRAY + preamble;
93  }
94 
95  /**
96  * Builds individual lines in the index topic. Override this method to
97  * change how index lines are rendered.
98  *
99  * @param sender The command sender requesting the index line.
100  * @param topic The topic to render into an index line.
101  * @return The rendered index line.
102  */
103  protected String buildIndexLine(CommandSender sender, HelpTopic topic) {
104  StringBuilder line = new StringBuilder();
105  line.append(ChatColor.GOLD);
106  line.append(topic.getName());
107  line.append(": ");
108  line.append(ChatColor.WHITE);
109  line.append(topic.getShortText());
110  return line.toString();
111  }
112 }
boolean hasPermission(String name)
String buildPreamble(CommandSender sender)
void setTopicsCollection(Collection< HelpTopic > topics)
String buildIndexLine(CommandSender sender, HelpTopic topic)