[Bleeding] Added support for amending help topic visibility permissions in help.yml. Addresses BUKKIT-1113

This commit is contained in:
rmichela 2012-03-10 18:08:12 -05:00 committed by EvilSeph
parent a8b2c6d04e
commit 996832ff6c
6 changed files with 45 additions and 24 deletions

View file

@ -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);
}
}
}

View file

@ -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;
}
}

View file

@ -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;

View file

@ -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);
}
}
}

View file

@ -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) {

View file

@ -20,3 +20,4 @@
# /stop:
# shortText: Stops the server cold....in its tracks!
# fullText: <text> - This kills the server.
# permission: you.dont.have