001    package org.bukkit.help;
002    
003    import org.bukkit.ChatColor;
004    import org.bukkit.command.CommandSender;
005    import org.bukkit.command.ConsoleCommandSender;
006    import org.bukkit.entity.Player;
007    import org.bukkit.util.ChatPaginator;
008    
009    import java.util.Collection;
010    
011    /**
012     * This help topic generates a list of other help topics. This class is useful
013     * for adding your own index help topics. To enforce a particular order, use a
014     * sorted collection.
015     * <p>
016     * If a preamble is provided to the constructor, that text will be displayed
017     * before the first item in the index.
018     */
019    public class IndexHelpTopic extends HelpTopic {
020    
021        protected String permission;
022        protected String preamble;
023        protected Collection<HelpTopic> allTopics;
024    
025        public IndexHelpTopic(String name, String shortText, String permission, Collection<HelpTopic> topics) {
026            this(name, shortText, permission, topics, null);
027        }
028    
029        public IndexHelpTopic(String name, String shortText, String permission, Collection<HelpTopic> topics, String preamble) {
030            this.name = name;
031            this.shortText = shortText;
032            this.permission = permission;
033            this.preamble = preamble;
034            setTopicsCollection(topics);
035        }
036    
037        /**
038         * Sets the contents of the internal allTopics collection.
039         *
040         * @param topics The topics to set.
041         */
042        protected void setTopicsCollection(Collection<HelpTopic> topics) {
043            this.allTopics = topics;
044        }
045    
046        public boolean canSee(CommandSender sender) {
047            if (sender instanceof ConsoleCommandSender) {
048                return true;
049            }
050            if (permission == null) {
051                return true;
052            }
053            return sender.hasPermission(permission);
054        }
055    
056        @Override
057        public void amendCanSee(String amendedPermission) {
058            permission = amendedPermission;
059        }
060    
061        public String getFullText(CommandSender sender) {
062            StringBuilder sb = new StringBuilder();
063    
064            if (preamble != null) {
065                sb.append(buildPreamble(sender));
066                sb.append("\n");
067            }
068    
069            for (HelpTopic topic : allTopics) {
070                if (topic.canSee(sender)) {
071                    String lineStr = buildIndexLine(sender, topic).replace("\n", ". ");
072                    if (sender instanceof Player && lineStr.length() > ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH) {
073                        sb.append(lineStr.substring(0, ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH - 3));
074                        sb.append("...");
075                    } else {
076                        sb.append(lineStr);
077                    }
078                    sb.append("\n");
079                }
080            }
081            return sb.toString();
082        }
083    
084        /**
085         * Builds the topic preamble. Override this method to change how the index
086         * preamble looks.
087         *
088         * @param sender The command sender requesting the preamble.
089         * @return The topic preamble.
090         */
091        protected String buildPreamble(CommandSender sender) {
092            return ChatColor.GRAY + preamble;
093        }
094    
095        /**
096         * Builds individual lines in the index topic. Override this method to
097         * change how index lines are rendered.
098         *
099         * @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    }