Bukkit  1.4.7-R1.0
 All Classes Namespaces Files Functions Variables Enumerator Pages
IndexHelpTopic.java
Go to the documentation of this file.
1 package org.bukkit.help;
2 
3 import org.bukkit.ChatColor;
4 import org.bukkit.command.CommandSender;
5 import org.bukkit.command.ConsoleCommandSender;
6 import org.bukkit.entity.Player;
7 import org.bukkit.util.ChatPaginator;
8 
9 import java.util.Collection;
10 
18 public class IndexHelpTopic extends HelpTopic {
19 
20  protected String permission;
21  protected String preamble;
22  protected Collection<HelpTopic> allTopics;
23 
24  public IndexHelpTopic(String name, String shortText, String permission, Collection<HelpTopic> topics) {
25  this(name, shortText, permission, topics, null);
26  }
27 
28  public IndexHelpTopic(String name, String shortText, String permission, Collection<HelpTopic> topics, String preamble) {
29  this.name = name;
30  this.shortText = shortText;
31  this.permission = permission;
32  this.preamble = preamble;
33  setTopicsCollection(topics);
34  }
35 
41  protected void setTopicsCollection(Collection<HelpTopic> topics) {
42  this.allTopics = topics;
43  }
44 
45  public boolean canSee(CommandSender sender) {
46  if (sender instanceof ConsoleCommandSender) {
47  return true;
48  }
49  if (permission == null) {
50  return true;
51  }
52  return sender.hasPermission(permission);
53  }
54 
55  @Override
56  public void amendCanSee(String amendedPermission) {
58  }
59 
60  public String getFullText(CommandSender sender) {
61  StringBuilder sb = new StringBuilder();
62 
63  if (preamble != null) {
64  sb.append(buildPreamble(sender));
65  sb.append("\n");
66  }
67 
68  for (HelpTopic topic : allTopics) {
69  if (topic.canSee(sender)) {
70  String lineStr = buildIndexLine(sender, topic).replace("\n", ". ");
71  if (sender instanceof Player && lineStr.length() > ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH) {
72  sb.append(lineStr.substring(0, ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH - 3));
73  sb.append("...");
74  } else {
75  sb.append(lineStr);
76  }
77  sb.append("\n");
78  }
79  }
80  return sb.toString();
81  }
82 
89  protected String buildPreamble(CommandSender sender) {
90  return ChatColor.GRAY + preamble;
91  }
92 
100  protected String buildIndexLine(CommandSender sender, HelpTopic topic) {
101  StringBuilder line = new StringBuilder();
102  line.append(ChatColor.GOLD);
103  line.append(topic.getName());
104  line.append(": ");
105  line.append(ChatColor.WHITE);
106  line.append(topic.getShortText());
107  return line.toString();
108  }
109 }