Fixed better color handling system

This commit is contained in:
Olof Larsson 2011-10-10 13:31:25 +02:00
parent e402797c54
commit aceeea8b0e
4 changed files with 116 additions and 80 deletions

View File

@ -68,7 +68,7 @@ public class FCmdRoot extends FCommand
this.disableOnLock = false;
this.setHelpShort("The faction base command");
this.helpLong.add(p.txt.tags("<i>This command contains all faction stuff."));
this.helpLong.add(p.txt.parseTags("<i>This command contains all faction stuff."));
//this.subCommands.add(p.cmdHelp);

View File

@ -223,7 +223,7 @@ public abstract class MCommand<T extends MPlugin>
public String getUseageTemplate(List<MCommand<?>> commandChain, boolean addShortHelp)
{
StringBuilder ret = new StringBuilder();
ret.append(p.txt.tags("<c>"));
ret.append(p.txt.parseTags("<c>"));
ret.append('/');
for (MCommand<?> mc : commandChain)
@ -257,13 +257,13 @@ public abstract class MCommand<T extends MPlugin>
if (args.size() > 0)
{
ret.append(p.txt.tags("<p> "));
ret.append(p.txt.parseTags("<p> "));
ret.append(TextUtil.implode(args, " "));
}
if (addShortHelp)
{
ret.append(p.txt.tags(" <i>"));
ret.append(p.txt.parseTags(" <i>"));
ret.append(this.helpShort);
}

View File

@ -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,51 +133,36 @@ 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<String, String> tags = new LinkedHashMap<String, String>();
public Map<String, String> rawTags = new LinkedHashMap<String, String>();
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", "<green>"); // logo
this.rawTags.put("a", "<gold>"); // art
this.rawTags.put("n", "<silver>"); // notice
this.rawTags.put("i", "<yellow>"); // info
this.rawTags.put("g", "<lime>"); // good
this.rawTags.put("b", "<rose>"); // bad
this.rawTags.put("h", "<pink>"); // highligh
this.rawTags.put("c", "<aqua>"); // command
this.rawTags.put("p", "<teal>"); // parameter
}
public void initTXT()
{
this.addTags();
this.addRawTags();
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> 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<String, String> rawTag : this.rawTags.entrySet())
{
this.txt.tags.put(rawTag.getKey(), TextUtil.parseColor(rawTag.getValue()));
}
}
// -------------------------------------------- //
// COMMAND HANDLING

View File

@ -9,56 +9,36 @@ import org.bukkit.Material;
public class TextUtil
{
private Map<String, String> tags = new HashMap<String, String>();
public TextUtil(Map<String, String> tags)
public Map<String, String> tags;
public TextUtil()
{
if (tags != null)
{
this.tags.putAll(tags);
}
this.tags = new HashMap<String, String>();
}
// 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;
// -------------------------------------------- //
// Top-level parsing functions.
// -------------------------------------------- //
return this.parse(str);
}
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<String, String> 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<String, String> 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("<empty>", "")
.replace("<black>", "\u00A70")
.replace("<navy>", "\u00A71")
.replace("<green>", "\u00A72")
.replace("<teal>", "\u00A73")
.replace("<red>", "\u00A74")
.replace("<purple>", "\u00A75")
.replace("<gold>", "\u00A76")
.replace("<silver>", "\u00A77")
.replace("<gray>", "\u00A78")
.replace("<blue>", "\u00A79")
.replace("<lime>", "\u00A7a")
.replace("<aqua>", "\u00A7b")
.replace("<rose>", "\u00A7c")
.replace("<pink>", "\u00A7d")
.replace("<yellow>", "\u00A7e")
.replace("<white>", "\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<String> 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("<l>") + str + tags("<a>")+ " ].";
String center = ".[ "+ parseTags("<l>") + str + parseTags("<a>")+ " ].";
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("<a>")+titleizeLine.substring(0, pivot - eatLeft) + center + titleizeLine.substring(pivot + eatRight);
return parseTags("<a>")+titleizeLine.substring(0, pivot - eatLeft) + center + titleizeLine.substring(pivot + eatRight);
else
return tags("<a>")+center;
return parseTags("<a>")+center;
}
public ArrayList<String> getPage(List<String> lines, int pageHumanBased, String title)
@ -143,12 +188,12 @@ public class TextUtil
if (pagecount == 0)
{
ret.add(this.tags("<i>Sorry. No Pages available."));
ret.add(this.parseTags("<i>Sorry. No Pages available."));
return ret;
}
else if (pageZeroBased < 0 || pageHumanBased > pagecount)
{
ret.add(this.tags("<i>Invalid page. Must be between 1 and "+pagecount));
ret.add(this.parseTags("<i>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".