From 996832ff6c3d410e21bab149cf6197799468d6cd Mon Sep 17 00:00:00 2001 From: rmichela Date: Sat, 10 Mar 2012 18:08:12 -0500 Subject: [PATCH] [Bleeding] Added support for amending help topic visibility permissions in help.yml. Addresses BUKKIT-1113 --- .../help/CommandAliasHelpTopic.java | 19 ++++++++-------- .../craftbukkit/help/HelpTopicAmendment.java | 12 +++++++++- .../craftbukkit/help/HelpYamlReader.java | 3 ++- .../help/MultipleCommandAliasHelpTopic.java | 22 +++++++++++-------- .../craftbukkit/help/SimpleHelpMap.java | 12 ++++++---- src/main/resources/configurations/help.yml | 1 + 6 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/help/CommandAliasHelpTopic.java b/src/main/java/org/bukkit/craftbukkit/help/CommandAliasHelpTopic.java index b815b9a38f..8830462fdb 100644 --- a/src/main/java/org/bukkit/craftbukkit/help/CommandAliasHelpTopic.java +++ b/src/main/java/org/bukkit/craftbukkit/help/CommandAliasHelpTopic.java @@ -1,7 +1,6 @@ package org.bukkit.craftbukkit.help; import org.bukkit.ChatColor; -import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.help.HelpMap; import org.bukkit.help.HelpTopic; @@ -9,13 +8,11 @@ import org.bukkit.help.HelpTopic; public class CommandAliasHelpTopic extends HelpTopic { private String aliasFor; - private Command command; private HelpMap helpMap; - - public CommandAliasHelpTopic(String alias, String aliasFor, Command command, HelpMap helpMap) { + + public CommandAliasHelpTopic(String alias, String aliasFor, HelpMap helpMap) { this.aliasFor = aliasFor.startsWith("/") ? aliasFor : "/" + aliasFor; this.helpMap = helpMap; - this.command = command; this.name = alias.startsWith("/") ? alias : "/" + alias; this.shortText = ChatColor.YELLOW + "Alias for " + ChatColor.WHITE + this.aliasFor; } @@ -33,11 +30,15 @@ public class CommandAliasHelpTopic extends HelpTopic { @Override public boolean canSee(CommandSender commandSender) { - HelpTopic aliasForTopic = helpMap.getHelpTopic(aliasFor); - if (aliasForTopic != null) { - return aliasForTopic.canSee(commandSender); + if (amendedPermission == null) { + HelpTopic aliasForTopic = helpMap.getHelpTopic(aliasFor); + if (aliasForTopic != null) { + return aliasForTopic.canSee(commandSender); + } else { + return false; + } } else { - return false; + return commandSender.hasPermission(amendedPermission); } } } diff --git a/src/main/java/org/bukkit/craftbukkit/help/HelpTopicAmendment.java b/src/main/java/org/bukkit/craftbukkit/help/HelpTopicAmendment.java index f18e7afc2f..922a3e9a94 100644 --- a/src/main/java/org/bukkit/craftbukkit/help/HelpTopicAmendment.java +++ b/src/main/java/org/bukkit/craftbukkit/help/HelpTopicAmendment.java @@ -7,11 +7,13 @@ public class HelpTopicAmendment { private String topicName; private String shortText; private String fullText; + private String permission; - public HelpTopicAmendment(String topicName, String shortText, String fullText) { + public HelpTopicAmendment(String topicName, String shortText, String fullText, String permission) { this.fullText = fullText; this.shortText = shortText; this.topicName = topicName; + this.permission = permission; } /** @@ -37,4 +39,12 @@ public class HelpTopicAmendment { public String getTopicName() { return topicName; } + + /** + * Gets the amended permission + * @return the permission + */ + public String getPermission() { + return permission; + } } diff --git a/src/main/java/org/bukkit/craftbukkit/help/HelpYamlReader.java b/src/main/java/org/bukkit/craftbukkit/help/HelpYamlReader.java index dd5c3966e0..dfa8d9a461 100644 --- a/src/main/java/org/bukkit/craftbukkit/help/HelpYamlReader.java +++ b/src/main/java/org/bukkit/craftbukkit/help/HelpYamlReader.java @@ -64,7 +64,8 @@ public class HelpYamlReader { ConfigurationSection section = commandTopics.getConfigurationSection(topicName); String description = section.getString("shortText"); String usage = section.getString("fullText"); - amendments.add(new HelpTopicAmendment(topicName, description, usage)); + String permission = section.getString("permission"); + amendments.add(new HelpTopicAmendment(topicName, description, usage, permission)); } } return amendments; diff --git a/src/main/java/org/bukkit/craftbukkit/help/MultipleCommandAliasHelpTopic.java b/src/main/java/org/bukkit/craftbukkit/help/MultipleCommandAliasHelpTopic.java index ab18d0e78f..bd21c233d6 100644 --- a/src/main/java/org/bukkit/craftbukkit/help/MultipleCommandAliasHelpTopic.java +++ b/src/main/java/org/bukkit/craftbukkit/help/MultipleCommandAliasHelpTopic.java @@ -35,16 +35,20 @@ public class MultipleCommandAliasHelpTopic extends HelpTopic { } public boolean canSee(CommandSender sender) { - if (sender instanceof ConsoleCommandSender) { - return true; - } - - for (Command command : alias.getCommands()) { - if (!command.testPermissionSilent(sender)) { - return false; + if (amendedPermission == null) { + if (sender instanceof ConsoleCommandSender) { + return true; } - } - return true; + for (Command command : alias.getCommands()) { + if (!command.testPermissionSilent(sender)) { + return false; + } + } + + return true; + } else { + return sender.hasPermission(amendedPermission); + } } } diff --git a/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java b/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java index ba228dbdcb..753b29ab6b 100644 --- a/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java +++ b/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java @@ -69,6 +69,7 @@ public class SimpleHelpMap implements HelpMap { * Processes all the commands registered in the server and creates help topics for them. * @param server A reference to the server. */ + @SuppressWarnings("unchecked") public synchronized void initializeCommands(CraftServer server) { // ** Load topics from highest to lowest priority order ** @@ -90,7 +91,7 @@ public class SimpleHelpMap implements HelpMap { // Initialize command alias help topics for (Command command : server.getCommandMap().getCommands()) { for (String alias : command.getAliases()) { - addTopic(new CommandAliasHelpTopic(alias, command.getLabel(), command, this)); + addTopic(new CommandAliasHelpTopic(alias, command.getLabel(), this)); } } @@ -99,16 +100,19 @@ public class SimpleHelpMap implements HelpMap { addTopic(new GenericCommandHelpTopic(command)); } + // Add alias sub-index + addTopic(new IndexHelpTopic("Aliases", "Lists command aliases", null, Collections2.filter(helpTopics.values(), Predicates.instanceOf(CommandAliasHelpTopic.class)))); + // Amend help topics from the help.yml file HelpYamlReader reader = new HelpYamlReader(server); for (HelpTopicAmendment amendment : reader.getTopicAmendments()) { if (helpTopics.containsKey(amendment.getTopicName())) { helpTopics.get(amendment.getTopicName()).amendTopic(amendment.getShortText(), amendment.getFullText()); + if (amendment.getPermission() != null) { + helpTopics.get(amendment.getTopicName()).amendCanSee(amendment.getPermission()); + } } } - - // Add alias sub-index - addTopic(new IndexHelpTopic("Aliases", "Lists command aliases", null, Collections2.filter(helpTopics.values(), Predicates.instanceOf(CommandAliasHelpTopic.class)))); } public void registerHelpTopicFactory(Class commandClass, HelpTopicFactory factory) { diff --git a/src/main/resources/configurations/help.yml b/src/main/resources/configurations/help.yml index eba5ee34f3..715ddd3e4d 100644 --- a/src/main/resources/configurations/help.yml +++ b/src/main/resources/configurations/help.yml @@ -20,3 +20,4 @@ # /stop: # shortText: Stops the server cold....in its tracks! # fullText: - This kills the server. +# permission: you.dont.have