Text wrapping improvements etc.

This commit is contained in:
Olof Larsson 2011-12-12 22:32:06 +01:00
parent 65538d0789
commit b49494d559
4 changed files with 65 additions and 14 deletions

View File

@ -70,6 +70,7 @@ public class MCore extends JavaPlugin
// -------------------------------------------- // // -------------------------------------------- //
// TXT // TXT
// -------------------------------------------- // // -------------------------------------------- //
public static Txt txt = new Txt();
private static Map<Object, Txt> txtInstances = new HashMap<Object, Txt>(); private static Map<Object, Txt> txtInstances = new HashMap<Object, Txt>();
public static Map<Object, Txt> getTxtInstances() { return txtInstances; } public static Map<Object, Txt> getTxtInstances() { return txtInstances; }
public static Txt getTxt(Object owner) { return txtInstances.get(owner); } public static Txt getTxt(Object owner) { return txtInstances.get(owner); }
@ -83,6 +84,7 @@ public class MCore extends JavaPlugin
// -------------------------------------------- // // -------------------------------------------- //
// PERM // PERM
// -------------------------------------------- // // -------------------------------------------- //
public static Perm perm = new Perm(txt);
private static Map<Object, Perm> permInstances = new HashMap<Object, Perm>(); private static Map<Object, Perm> permInstances = new HashMap<Object, Perm>();
public static Map<Object, Perm> getPermInstances() { return permInstances; } public static Map<Object, Perm> getPermInstances() { return permInstances; }
public static Perm getPerm(Object owner) { return permInstances.get(owner); } public static Perm getPerm(Object owner) { return permInstances.get(owner); }

View File

@ -129,6 +129,6 @@ public abstract class MPlugin extends JavaPlugin
} }
public void log(Level level, Object... msg) public void log(Level level, Object... msg)
{ {
Logger.getLogger("Minecraft").log(level, this.logPrefix+txt.implode(msg, " ")); Logger.getLogger("Minecraft").log(level, this.logPrefix + MCore.txt.implode(msg, " "));
} }
} }

View File

@ -159,7 +159,7 @@ public class Persist
} }
// Clean args // Clean args
if (fromIndex < 0) if (fromIndex <= 0)
{ {
fromIndex = 0; fromIndex = 0;
} }

View File

@ -46,14 +46,19 @@ public class Txt
// Top-level parsing functions. // Top-level parsing functions.
// -------------------------------------------- // // -------------------------------------------- //
public String parse(String str, Object... args) public String parse(String string, Object... args)
{ {
return String.format(this.parse(str), args); return String.format(this.parse(string), args);
} }
public String parse(String str) public String parse(String string)
{ {
return this.parseTags(parseColor(str)); return this.parseTags(parseColor(string));
}
public ArrayList<String> parse(Collection<String> strings)
{
return this.parseTags(parseColor(strings));
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -61,10 +66,10 @@ public class Txt
// -------------------------------------------- // // -------------------------------------------- //
public static final transient Pattern patternTag = Pattern.compile("<([a-zA-Z0-9_]*)>"); public static final transient Pattern patternTag = Pattern.compile("<([a-zA-Z0-9_]*)>");
public String replaceTags(String str, Map<String, String> tags) public String replaceTags(String string, Map<String, String> tags)
{ {
StringBuffer ret = new StringBuffer(); StringBuffer ret = new StringBuffer();
Matcher matcher = patternTag.matcher(str); Matcher matcher = patternTag.matcher(string);
while (matcher.find()) while (matcher.find())
{ {
String tag = matcher.group(1); String tag = matcher.group(1);
@ -82,9 +87,19 @@ public class Txt
return ret.toString(); return ret.toString();
} }
public String parseTags(String str) public String parseTags(String string)
{ {
return replaceTags(str, this.getDesign().getTags()); return replaceTags(string, this.getDesign().getTags());
}
public ArrayList<String> parseTags(Collection<String> strings)
{
ArrayList<String> ret = new ArrayList<String>(strings.size());
for (String string : strings)
{
ret.add(this.parseTags(string));
}
return ret;
} }
// -------------------------------------------- // // -------------------------------------------- //
@ -99,6 +114,16 @@ public class Txt
return string; return string;
} }
public ArrayList<String> parseColor(Collection<String> strings)
{
ArrayList<String> ret = new ArrayList<String>(strings.size());
for (String string : strings)
{
ret.add(this.parseColor(string));
}
return ret;
}
public String parseColorAmp(String string) public String parseColorAmp(String string)
{ {
string = string.replaceAll("(§([a-z0-9]))", "\u00A7$2"); string = string.replaceAll("(§([a-z0-9]))", "\u00A7$2");
@ -150,10 +175,10 @@ public class Txt
return string.substring(0, 1).toUpperCase()+string.substring(1); return string.substring(0, 1).toUpperCase()+string.substring(1);
} }
public String repeat(String s, int times) public String repeat(String string, int times)
{ {
if (times <= 0) return ""; if (times <= 0) return "";
else return s + repeat(s, times-1); else return string + repeat(string, times-1);
} }
public String implode(List<String> list, String glue) public String implode(List<String> list, String glue)
@ -363,8 +388,32 @@ public class Txt
// -------------------------------------------- // // -------------------------------------------- //
// Wrapping the Craftbukkit TextWrapper // Wrapping the Craftbukkit TextWrapper
// -------------------------------------------- // // -------------------------------------------- //
public ArrayList<String> wrap(final String text) public ArrayList<String> wrap(final String string)
{ {
return new ArrayList<String>(Arrays.asList(TextWrapper.wrapText(text))); return new ArrayList<String>(Arrays.asList(TextWrapper.wrapText(string)));
}
public ArrayList<String> wrap(final Collection<String> strings)
{
ArrayList<String> ret = new ArrayList<String>();
for (String line : strings)
{
ret.addAll(this.wrap(line));
}
return ret;
}
// -------------------------------------------- //
// Parse and Wrap combo
// -------------------------------------------- //
public ArrayList<String> parseWrap(final String string)
{
return this.wrap(this.parse(string));
}
public ArrayList<String> parseWrap(final Collection<String> strings)
{
return this.wrap(this.parse(strings));
} }
} }