From aceeea8b0e9c88adc318cc548187433c7cedf988 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 10 Oct 2011 13:31:25 +0200 Subject: [PATCH] Fixed better color handling system --- .../massivecraft/factions/cmd/FCmdRoot.java | 2 +- .../massivecraft/factions/zcore/MCommand.java | 6 +- .../massivecraft/factions/zcore/MPlugin.java | 53 +++---- .../factions/zcore/util/TextUtil.java | 135 ++++++++++++------ 4 files changed, 116 insertions(+), 80 deletions(-) diff --git a/src/com/massivecraft/factions/cmd/FCmdRoot.java b/src/com/massivecraft/factions/cmd/FCmdRoot.java index 05f525b9..c4b6d14a 100644 --- a/src/com/massivecraft/factions/cmd/FCmdRoot.java +++ b/src/com/massivecraft/factions/cmd/FCmdRoot.java @@ -68,7 +68,7 @@ public class FCmdRoot extends FCommand this.disableOnLock = false; this.setHelpShort("The faction base command"); - this.helpLong.add(p.txt.tags("This command contains all faction stuff.")); + this.helpLong.add(p.txt.parseTags("This command contains all faction stuff.")); //this.subCommands.add(p.cmdHelp); diff --git a/src/com/massivecraft/factions/zcore/MCommand.java b/src/com/massivecraft/factions/zcore/MCommand.java index a2c30aeb..87007dc2 100644 --- a/src/com/massivecraft/factions/zcore/MCommand.java +++ b/src/com/massivecraft/factions/zcore/MCommand.java @@ -223,7 +223,7 @@ public abstract class MCommand public String getUseageTemplate(List> commandChain, boolean addShortHelp) { StringBuilder ret = new StringBuilder(); - ret.append(p.txt.tags("")); + ret.append(p.txt.parseTags("")); ret.append('/'); for (MCommand mc : commandChain) @@ -257,13 +257,13 @@ public abstract class MCommand if (args.size() > 0) { - ret.append(p.txt.tags("

")); + ret.append(p.txt.parseTags("

")); ret.append(TextUtil.implode(args, " ")); } if (addShortHelp) { - ret.append(p.txt.tags(" ")); + ret.append(p.txt.parseTags(" ")); ret.append(this.helpShort); } diff --git a/src/com/massivecraft/factions/zcore/MPlugin.java b/src/com/massivecraft/factions/zcore/MPlugin.java index 34ed9fd5..378d2af3 100644 --- a/src/com/massivecraft/factions/zcore/MPlugin.java +++ b/src/com/massivecraft/factions/zcore/MPlugin.java @@ -3,6 +3,7 @@ package com.massivecraft.factions.zcore; import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.util.*; +import java.util.Map.Entry; import java.util.logging.Level; import java.util.logging.Logger; @@ -66,6 +67,7 @@ public abstract class MPlugin extends JavaPlugin if ( ! lib.require("gson.jar", "http://search.maven.org/remotecontent?filepath=com/google/code/gson/gson/1.7.1/gson-1.7.1.jar")) return false; this.gson = this.getGsonBuilder().create(); + this.txt = new TextUtil(); initTXT(); // Create and register listeners @@ -131,52 +133,37 @@ public abstract class MPlugin extends JavaPlugin // These are not supposed to be used directly. // They are loaded and used through the TextUtil instance for the plugin. - public Map tags = new LinkedHashMap(); + public Map rawTags = new LinkedHashMap(); - public void addTags() + public void addRawTags() { - this.tags.put("black", "§0"); - this.tags.put("navy", "§1"); - this.tags.put("green", "§2"); - this.tags.put("teal", "§3"); - this.tags.put("red", "§4"); - this.tags.put("purple", "§5"); - this.tags.put("gold", "§6"); - this.tags.put("silver", "§7"); - this.tags.put("gray", "§8"); - this.tags.put("blue", "§9"); - this.tags.put("white", "§f"); - this.tags.put("lime", "§a"); - this.tags.put("aqua", "§b"); - this.tags.put("rose", "§c"); - this.tags.put("pink", "§d"); - this.tags.put("yellow", "§e"); - - this.tags.put("l", "§2"); // logo - this.tags.put("a", "§6"); // art - this.tags.put("n", "§7"); // notice - this.tags.put("i", "§e"); // info - this.tags.put("g", "§a"); // good - this.tags.put("b", "§c"); // bad - this.tags.put("h", "§d"); // highligh - this.tags.put("c", "§b"); // command - this.tags.put("p", "§3"); // parameter + this.rawTags.put("l", ""); // logo + this.rawTags.put("a", ""); // art + this.rawTags.put("n", ""); // notice + this.rawTags.put("i", ""); // info + this.rawTags.put("g", ""); // good + this.rawTags.put("b", ""); // bad + this.rawTags.put("h", ""); // highligh + this.rawTags.put("c", ""); // command + this.rawTags.put("p", ""); // parameter } public void initTXT() { - this.addTags(); + this.addRawTags(); Type type = new TypeToken>(){}.getType(); Map tagsFromFile = this.persist.load(type, "tags"); - if (tagsFromFile != null) this.tags.putAll(tagsFromFile); - this.persist.save(this.tags, "tags"); + if (tagsFromFile != null) this.rawTags.putAll(tagsFromFile); + this.persist.save(this.rawTags, "tags"); - this.txt = new TextUtil(this.tags); + for (Entry rawTag : this.rawTags.entrySet()) + { + this.txt.tags.put(rawTag.getKey(), TextUtil.parseColor(rawTag.getValue())); + } } - // -------------------------------------------- // // COMMAND HANDLING // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/zcore/util/TextUtil.java b/src/com/massivecraft/factions/zcore/util/TextUtil.java index 39be5c98..5916ae62 100644 --- a/src/com/massivecraft/factions/zcore/util/TextUtil.java +++ b/src/com/massivecraft/factions/zcore/util/TextUtil.java @@ -9,56 +9,36 @@ import org.bukkit.Material; public class TextUtil { - private Map tags = new HashMap(); - - public TextUtil(Map tags) + public Map tags; + public TextUtil() { - if (tags != null) - { - this.tags.putAll(tags); - } + this.tags = new HashMap(); } - // Get is supposed to be the way we reach registered lang - // TODO: Is the parse - /*public String get(String name) - { - String str = lang.get(name); - if (str == null) str = name; - - return this.parse(str); - } + // -------------------------------------------- // + // Top-level parsing functions. + // -------------------------------------------- // - public String get(String name, Object... args) - { - String str = lang.get(name); - if (str == null) str = name; - - return this.parse(str, args); - }*/ - - // Parse is used to handle non registered text public String parse(String str, Object... args) { - return String.format(this.tags(str), args); + return String.format(this.parse(str), args); } public String parse(String str) { - return this.tags(str); + return this.parseTags(parseColor(str)); } - public Map getTags() - { - return tags; - } + // -------------------------------------------- // + // Tag parsing + // -------------------------------------------- // - public String tags(String str) + public String parseTags(String str) { return replaceTags(str, this.tags); } - public static final transient Pattern patternTag = Pattern.compile("<([^<>]*)>"); + public static final transient Pattern patternTag = Pattern.compile("<([a-zA-Z0-9_]*)>"); public static String replaceTags(String str, Map tags) { StringBuffer ret = new StringBuffer(); @@ -80,6 +60,69 @@ public class TextUtil return ret.toString(); } + // -------------------------------------------- // + // Color parsing + // -------------------------------------------- // + + public static String parseColor(String string) + { + string = parseColorAmp(string); + string = parseColorAcc(string); + string = parseColorTags(string); + return string; + } + + public static String parseColorAmp(String string) + { + string = string.replaceAll("(§([a-z0-9]))", "\u00A7$2"); + string = string.replaceAll("(&([a-z0-9]))", "\u00A7$2"); + string = string.replace("&&", "&"); + return string; + } + + public static String parseColorAcc(String string) + { + return string.replace("`e", "") + .replace("`r", ChatColor.RED.toString()) .replace("`R", ChatColor.DARK_RED.toString()) + .replace("`y", ChatColor.YELLOW.toString()) .replace("`Y", ChatColor.GOLD.toString()) + .replace("`g", ChatColor.GREEN.toString()) .replace("`G", ChatColor.DARK_GREEN.toString()) + .replace("`a", ChatColor.AQUA.toString()) .replace("`A", ChatColor.DARK_AQUA.toString()) + .replace("`b", ChatColor.BLUE.toString()) .replace("`B", ChatColor.DARK_BLUE.toString()) + .replace("`p", ChatColor.LIGHT_PURPLE.toString()) .replace("`P", ChatColor.DARK_PURPLE.toString()) + .replace("`k", ChatColor.BLACK.toString()) .replace("`s", ChatColor.GRAY.toString()) + .replace("`S", ChatColor.DARK_GRAY.toString()) .replace("`w", ChatColor.WHITE.toString()); + } + + public static String parseColorTags(String string) + { + return string.replace("", "") + .replace("", "\u00A70") + .replace("", "\u00A71") + .replace("", "\u00A72") + .replace("", "\u00A73") + .replace("", "\u00A74") + .replace("", "\u00A75") + .replace("", "\u00A76") + .replace("", "\u00A77") + .replace("", "\u00A78") + .replace("", "\u00A79") + .replace("", "\u00A7a") + .replace("", "\u00A7b") + .replace("", "\u00A7c") + .replace("", "\u00A7d") + .replace("", "\u00A7e") + .replace("", "\u00A7f"); + } + + // -------------------------------------------- // + // Standard utils like UCFirst, implode and repeat. + // -------------------------------------------- // + + public static String upperCaseFirst(String string) + { + return string.substring(0, 1).toUpperCase()+string.substring(1); + } + public static String implode(List list, String glue) { StringBuilder ret = new StringBuilder(); @@ -100,6 +143,10 @@ public class TextUtil else return s + repeat(s, times-1); } + // -------------------------------------------- // + // Material name tools + // -------------------------------------------- // + public static String getMaterialName(Material material) { return material.toString().replace('_', ' ').toLowerCase(); @@ -110,26 +157,24 @@ public class TextUtil return getMaterialName(Material.getMaterial(materialId)); } - public static String upperCaseFirst(String string) - { - return string.substring(0, 1).toUpperCase()+string.substring(1); - } + // -------------------------------------------- // + // Paging and chrome-tools like titleize + // -------------------------------------------- // - // TODO: Make part of layout configuration. private final static String titleizeLine = repeat("_", 52); private final static int titleizeBalance = -1; public String titleize(String str) { - String center = ".[ "+ tags("") + str + tags("")+ " ]."; + String center = ".[ "+ parseTags("") + str + parseTags("")+ " ]."; int centerlen = ChatColor.stripColor(center).length(); int pivot = titleizeLine.length() / 2; int eatLeft = (centerlen / 2) - titleizeBalance; int eatRight = (centerlen - eatLeft) + titleizeBalance; if (eatLeft < pivot) - return tags("")+titleizeLine.substring(0, pivot - eatLeft) + center + titleizeLine.substring(pivot + eatRight); + return parseTags("")+titleizeLine.substring(0, pivot - eatLeft) + center + titleizeLine.substring(pivot + eatRight); else - return tags("")+center; + return parseTags("")+center; } public ArrayList getPage(List lines, int pageHumanBased, String title) @@ -143,12 +188,12 @@ public class TextUtil if (pagecount == 0) { - ret.add(this.tags("Sorry. No Pages available.")); + ret.add(this.parseTags("Sorry. No Pages available.")); return ret; } else if (pageZeroBased < 0 || pageHumanBased > pagecount) { - ret.add(this.tags("Invalid page. Must be between 1 and "+pagecount)); + ret.add(this.parseTags("Invalid page. Must be between 1 and "+pagecount)); return ret; } @@ -164,6 +209,10 @@ public class TextUtil return ret; } + // -------------------------------------------- // + // Describing Time + // -------------------------------------------- // + /** * Using this function you transform a delta in milliseconds * to a String like "2 weeks from now" or "7 days ago".