From 4589e943f6796f96a3cab0b1ea2563bc9a357b15 Mon Sep 17 00:00:00 2001 From: rmichela Date: Fri, 16 Mar 2012 00:41:13 -0400 Subject: [Bleeding] Added support for color codes in help.yml. Addresses BUKKIT-1191 --- .../craftbukkit/help/HelpTopicComparator.java | 23 +++++++++++++++++--- .../bukkit/craftbukkit/help/HelpYamlReader.java | 12 ++++++----- .../org/bukkit/craftbukkit/help/SimpleHelpMap.java | 25 +++++++++++----------- src/main/resources/configurations/help.yml | 23 ++++++++++---------- 4 files changed, 52 insertions(+), 31 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/bukkit/craftbukkit/help/HelpTopicComparator.java b/src/main/java/org/bukkit/craftbukkit/help/HelpTopicComparator.java index 9c0028ec..c88bfafb 100644 --- a/src/main/java/org/bukkit/craftbukkit/help/HelpTopicComparator.java +++ b/src/main/java/org/bukkit/craftbukkit/help/HelpTopicComparator.java @@ -9,17 +9,34 @@ import java.util.Comparator; * that start with a slash come after topics that don't. */ public class HelpTopicComparator implements Comparator { - private TopicNameComparator tnc = new TopicNameComparator(); + + // Singleton implementations + private static final TopicNameComparator tnc = new TopicNameComparator(); + public static TopicNameComparator topicNameComparatorInstance() { + return tnc; + } + + private static final HelpTopicComparator htc = new HelpTopicComparator(); + public static HelpTopicComparator helpTopicComparatorInstance() { + return htc; + } + + private HelpTopicComparator() {} public int compare(HelpTopic lhs, HelpTopic rhs) { return tnc.compare(lhs.getName(), rhs.getName()); } public static class TopicNameComparator implements Comparator { + private TopicNameComparator(){} + public int compare(String lhs, String rhs) { - if (lhs.startsWith("/") && !rhs.startsWith("/")) { + boolean lhsStartSlash = lhs.startsWith("/"); + boolean rhsStartSlash = rhs.startsWith("/"); + + if (lhsStartSlash && !rhsStartSlash) { return 1; - } else if (!lhs.startsWith("/") && rhs.startsWith("/")) { + } else if (!lhsStartSlash && rhsStartSlash) { return -1; } else { return lhs.compareToIgnoreCase(rhs); diff --git a/src/main/java/org/bukkit/craftbukkit/help/HelpYamlReader.java b/src/main/java/org/bukkit/craftbukkit/help/HelpYamlReader.java index abef75e7..158a781c 100644 --- a/src/main/java/org/bukkit/craftbukkit/help/HelpYamlReader.java +++ b/src/main/java/org/bukkit/craftbukkit/help/HelpYamlReader.java @@ -1,5 +1,6 @@ package org.bukkit.craftbukkit.help; +import org.bukkit.ChatColor; import org.bukkit.Server; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; @@ -16,7 +17,8 @@ import java.util.logging.Level; */ public class HelpYamlReader { - private YamlConfiguration helpYaml; + private final YamlConfiguration helpYaml; + private final char ALT_COLOR_CODE = '&'; public HelpYamlReader(Server server) { File helpYamlFile = new File("help.yml"); @@ -43,8 +45,8 @@ public class HelpYamlReader { if (generalTopics != null) { for (String topicName : generalTopics.getKeys(false)) { ConfigurationSection section = generalTopics.getConfigurationSection(topicName); - String shortText = section.getString("shortText"); - String fullText = section.getString("fullText"); + String shortText = ChatColor.translateAlternateColorCodes(ALT_COLOR_CODE, section.getString("shortText")); + String fullText = ChatColor.translateAlternateColorCodes(ALT_COLOR_CODE, section.getString("fullText")); String permission = section.getString("permission"); topics.add(new CustomHelpTopic(topicName, shortText, fullText, permission)); } @@ -62,8 +64,8 @@ public class HelpYamlReader { if (commandTopics != null) { for (String topicName : commandTopics.getKeys(false)) { ConfigurationSection section = commandTopics.getConfigurationSection(topicName); - String description = section.getString("shortText"); - String usage = section.getString("fullText"); + String description = ChatColor.translateAlternateColorCodes(ALT_COLOR_CODE, section.getString("shortText")); + String usage = ChatColor.translateAlternateColorCodes(ALT_COLOR_CODE, section.getString("fullText")); String permission = section.getString("permission"); amendments.add(new HelpTopicAmendment(topicName, description, usage, permission)); } diff --git a/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java b/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java index 1fc57a09..ec25c351 100644 --- a/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java +++ b/src/main/java/org/bukkit/craftbukkit/help/SimpleHelpMap.java @@ -10,7 +10,6 @@ import org.bukkit.command.defaults.VanillaCommand; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.help.*; -import javax.annotation.Nullable; import java.util.*; /** @@ -18,16 +17,16 @@ import java.util.*; */ public class SimpleHelpMap implements HelpMap { - private HelpTopic defaultTopic; - private Map helpTopics; - private Set pluginIndexes; - private Map> topicFactoryMap; - private CraftServer server; + private final HelpTopic defaultTopic; + private final Map helpTopics; + private final Set pluginIndexes; + private final Map> topicFactoryMap; + private final CraftServer server; private HelpYamlReader yaml; public SimpleHelpMap(CraftServer server) { - this.helpTopics = new TreeMap(new HelpTopicComparator.TopicNameComparator()); // Using a TreeMap for its explicit sorting on key - this.pluginIndexes = new TreeSet(new HelpTopicComparator()); + this.helpTopics = new TreeMap(HelpTopicComparator.topicNameComparatorInstance()); // Using a TreeMap for its explicit sorting on key + this.pluginIndexes = new TreeSet(HelpTopicComparator.helpTopicComparatorInstance()); this.topicFactoryMap = new HashMap>(); this.server = server; this.yaml = new HelpYamlReader(server); @@ -77,6 +76,8 @@ public class SimpleHelpMap implements HelpMap { * Reads the general topics from help.yml and adds them to the help index. */ public synchronized void initializeGeneralTopics() { + yaml = new HelpYamlReader(server); + // Initialize general help topics from the help.yml file for (HelpTopic topic : yaml.getGeneralTopics()) { addTopic(topic); @@ -88,7 +89,7 @@ public class SimpleHelpMap implements HelpMap { */ public synchronized void initializeCommands() { // ** Load topics from highest to lowest priority order ** - List ignoredPlugins = yaml.getIgnoredPlugins(); + Set ignoredPlugins = new HashSet(yaml.getIgnoredPlugins()); // Initialize help topics from the server's command map outer: for (Command command : server.getCommandMap().getCommands()) { @@ -159,7 +160,7 @@ public class SimpleHelpMap implements HelpMap { HelpTopic topic = getHelpTopic("/" + command.getLabel()); if (topic != null) { if (!pluginIndexes.containsKey(pluginName)) { - pluginIndexes.put(pluginName, new TreeSet(new HelpTopicComparator())); //keep things in topic order + pluginIndexes.put(pluginName, new TreeSet(HelpTopicComparator.helpTopicComparatorInstance())); //keep things in topic order } pluginIndexes.get(pluginName).add(topic); } @@ -177,7 +178,7 @@ public class SimpleHelpMap implements HelpMap { return null; } - private boolean commandInIgnoredPlugin(Command command, List ignoredPlugins) { + private boolean commandInIgnoredPlugin(Command command, Set ignoredPlugins) { if ((command instanceof BukkitCommand || command instanceof VanillaCommand) && ignoredPlugins.contains("Bukkit")) { return true; } @@ -196,7 +197,7 @@ public class SimpleHelpMap implements HelpMap { private class IsCommandTopicPredicate implements Predicate { - public boolean apply(@Nullable HelpTopic topic) { + public boolean apply(HelpTopic topic) { return topic.getName().charAt(0) == '/'; } } diff --git a/src/main/resources/configurations/help.yml b/src/main/resources/configurations/help.yml index 5e6ed5b9..d7f363b1 100644 --- a/src/main/resources/configurations/help.yml +++ b/src/main/resources/configurations/help.yml @@ -1,36 +1,37 @@ # This is the help configuration file for Bukkit. +# # By default you do not need to modify this file. Help topics for all plugin commands are automatically provided by # or extracted from your installed plugins. You only need to modify this file if you wish to add new help pages to # your server or override the help pages of existing plugin commands. -# -- +# # This file is divided up into the following parts: # -- general-topics: lists admin defined topics # -- amend-topics: lists topic amendments to apply to existing help topics # -- ignore-plugins: lists any plugins that should be excluded from help -# general-topics and command-topics respectively. -# Examples are given below. Color codes are allowed. When amending command topic, the string will be replaced -# with the existing value in the help topic. -# -- +# +# Examples are given below. When amending command topic, the string will be replaced with the existing value +# in the help topic. Color codes can be used in topic text. The color code character is & followed by 0-F. +# # Set this to true to list the individual command help topics in the master help. # command-topics-in-master-index: true -# -- +# # Each general topic will show up as a separate topic in the help index along with all the plugin command topics. # general-topics: # rules: # shortText: Rules of the server # fullText: | -# 1. Be kind to your fellow players. -# 2. No griefing. -# 3. No swearing. +# &61. Be kind to your fellow players. +# &B2. No griefing. +# &D3. No swearing. # permission: topics.rules -# -- +# # Topic amendments are used to change the content of automatically generated plugin command topics. # amended-topics: # /stop: # shortText: Stops the server cold....in its tracks! # fullText: - This kills the server. # permission: you.dont.have -# -- +# # Any plugin in the ignored plugins list will be excluded from help. The name must match the name displayed by # the /plugins command. Ignore "Bukkit" to remove the standard bukkit commands from the index. # ignore-plugins: -- cgit v1.2.3