001    package org.bukkit.help;
002    
003    import org.bukkit.ChatColor;
004    import org.bukkit.command.Command;
005    import org.bukkit.command.CommandSender;
006    import org.apache.commons.lang.StringUtils;
007    import org.bukkit.command.ConsoleCommandSender;
008    import org.bukkit.command.PluginCommand;
009    import org.bukkit.command.defaults.VanillaCommand;
010    import org.bukkit.help.HelpTopic;
011    
012    /**
013     * Lacking an alternative, the help system will create instances of
014     * GenericCommandHelpTopic for each command in the server's CommandMap. You
015     * can use this class as a base class for custom help topics, or as an example
016     * for how to write your own.
017     */
018    public class GenericCommandHelpTopic extends HelpTopic {
019    
020        protected Command command;
021    
022        public GenericCommandHelpTopic(Command command) {
023            this.command = command;
024    
025            if (command.getLabel().startsWith("/")) {
026                name = command.getLabel();
027            } else {
028                name = "/" + command.getLabel();
029            }
030    
031            // The short text is the first line of the description
032            int i = command.getDescription().indexOf("\n");
033            if (i > 1) {
034                shortText = command.getDescription().substring(0, i - 1);
035            } else {
036                shortText = command.getDescription();
037            }
038    
039            // Build full text
040            StringBuffer sb = new StringBuffer();
041    
042            sb.append(ChatColor.GOLD);
043            sb.append("Description: ");
044            sb.append(ChatColor.WHITE);
045            sb.append(command.getDescription());
046    
047            sb.append("\n");
048    
049            sb.append(ChatColor.GOLD);
050            sb.append("Usage: ");
051            sb.append(ChatColor.WHITE);
052            sb.append(command.getUsage().replace("<command>", name.substring(1)));
053    
054            if (command.getAliases().size() > 0) {
055                sb.append("\n");
056                sb.append(ChatColor.GOLD);
057                sb.append("Aliases: ");
058                sb.append(ChatColor.WHITE);
059                sb.append(ChatColor.WHITE + StringUtils.join(command.getAliases(), ", "));
060            }
061            fullText = sb.toString();
062        }
063    
064        public boolean canSee(CommandSender sender) {
065            if (!command.isRegistered() && !(command instanceof VanillaCommand)) {
066                // Unregistered commands should not show up in the help (ignore VanillaCommands)
067                return false;
068            }
069    
070            if (sender instanceof ConsoleCommandSender) {
071                return true;
072            }
073    
074            if (amendedPermission != null) {
075                return sender.hasPermission(amendedPermission);
076            } else {
077                return command.testPermissionSilent(sender);
078            }
079        }
080    }