Fix text file color code escaping (&&6 -> &6) Add support for color escaping in ingame chat Other formatting class cleanup

This commit is contained in:
KHobbits 2013-08-19 21:28:05 +01:00
parent a06a81c15d
commit 7f27de4bb7

View File

@ -6,15 +6,19 @@ import java.util.regex.Pattern;
public class FormatUtil public class FormatUtil
{ {
static final transient Pattern REPLACE_COLOR_PATTERN = Pattern.compile("&([0-9a-fA-F])"); //Vanilla patterns used to strip existing formats
static final transient Pattern VANILLA_MAGIC_PATTERN = Pattern.compile("\u00a7+[Kk]");
static final transient Pattern VANILLA_FORMAT_PATTERN = Pattern.compile("\u00a7+[L-ORl-or]");
static final transient Pattern REPLACE_FORMAT_PATTERN = Pattern.compile("&([l-orL-OR])");
static final transient Pattern REPLACE_MAGIC_PATTERN = Pattern.compile("&([Kk])");
static final transient Pattern REPLACE_PATTERN = Pattern.compile("&([0-9a-fk-orA-FK-OR])");
static final transient Pattern LOGCOLOR_PATTERN = Pattern.compile("\\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]");
static final transient Pattern VANILLA_PATTERN = Pattern.compile("\u00a7+[0-9A-FK-ORa-fk-or]?"); static final transient Pattern VANILLA_PATTERN = Pattern.compile("\u00a7+[0-9A-FK-ORa-fk-or]?");
static final transient Pattern VANILLA_COLOR_PATTERN = Pattern.compile("\u00a7+[0-9A-Fa-f]"); static final transient Pattern VANILLA_COLOR_PATTERN = Pattern.compile("\u00a7+[0-9A-Fa-f]");
static final transient Pattern VANILLA_MAGIC_PATTERN = Pattern.compile("\u00a7+[Kk]");
static final transient Pattern VANILLA_FORMAT_PATTERN = Pattern.compile("\u00a7+[L-ORl-or]");
//Essentials '&' convention colour codes
static final transient Pattern REPLACE_ALL_PATTERN = Pattern.compile("(?<!&)&([0-9a-fk-orA-FK-OR])");
static final transient Pattern REPLACE_COLOR_PATTERN = Pattern.compile("(?<!&)&([0-9a-fA-F])");
static final transient Pattern REPLACE_MAGIC_PATTERN = Pattern.compile("(?<!&)&([Kk])");
static final transient Pattern REPLACE_FORMAT_PATTERN = Pattern.compile("(?<!&)&([l-orL-OR])");
static final transient Pattern REPLACE_PATTERN = Pattern.compile("&&(?=[0-9a-fk-orA-FK-OR])");
//Used to prepare xmpp output
static final transient Pattern LOGCOLOR_PATTERN = Pattern.compile("\\x1B\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]");
static final transient Pattern URL_PATTERN = Pattern.compile("((?:(?:https?)://)?[\\w-_\\.]{2,})\\.([a-z]{2,3}(?:/\\S+)?)"); static final transient Pattern URL_PATTERN = Pattern.compile("((?:(?:https?)://)?[\\w-_\\.]{2,})\\.([a-z]{2,3}(?:/\\S+)?)");
public static final Pattern IPPATTERN = Pattern.compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"); public static final Pattern IPPATTERN = Pattern.compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
@ -35,7 +39,7 @@ public class FormatUtil
{ {
return null; return null;
} }
return stripColor(input, REPLACE_PATTERN); return stripColor(input, REPLACE_ALL_PATTERN);
} }
//This is the general permission sensitive message format function, checks for urls. //This is the general permission sensitive message format function, checks for urls.
@ -60,12 +64,12 @@ public class FormatUtil
{ {
return null; return null;
} }
return REPLACE_PATTERN.matcher(input).replaceAll("\u00a7$1"); return replaceColor(input, REPLACE_ALL_PATTERN);
} }
static String replaceColor(final String input, final Pattern pattern) static String replaceColor(final String input, final Pattern pattern)
{ {
return pattern.matcher(input).replaceAll("\u00a7$1"); return REPLACE_PATTERN.matcher(pattern.matcher(input).replaceAll("\u00a7$1")).replaceAll("&");
} }
//This is the general permission sensitive message format function, does not touch urls. //This is the general permission sensitive message format function, does not touch urls.
@ -78,27 +82,27 @@ public class FormatUtil
String message; String message;
if (user.isAuthorized(permBase + ".color")) if (user.isAuthorized(permBase + ".color"))
{ {
message = FormatUtil.replaceColor(input, REPLACE_COLOR_PATTERN); message = replaceColor(input, REPLACE_COLOR_PATTERN);
} }
else else
{ {
message = FormatUtil.stripColor(input, VANILLA_COLOR_PATTERN); message = stripColor(input, VANILLA_COLOR_PATTERN);
} }
if (user.isAuthorized(permBase + ".magic")) if (user.isAuthorized(permBase + ".magic"))
{ {
message = FormatUtil.replaceColor(message, REPLACE_MAGIC_PATTERN); message = replaceColor(message, REPLACE_MAGIC_PATTERN);
} }
else else
{ {
message = FormatUtil.stripColor(message, VANILLA_MAGIC_PATTERN); message = stripColor(message, VANILLA_MAGIC_PATTERN);
} }
if (user.isAuthorized(permBase + ".format")) if (user.isAuthorized(permBase + ".format"))
{ {
message = FormatUtil.replaceColor(message, REPLACE_FORMAT_PATTERN); message = replaceColor(message, REPLACE_FORMAT_PATTERN);
} }
else else
{ {
message = FormatUtil.stripColor(message, VANILLA_FORMAT_PATTERN); message = stripColor(message, VANILLA_FORMAT_PATTERN);
} }
return message; return message;
} }
@ -109,7 +113,7 @@ public class FormatUtil
{ {
return null; return null;
} }
return LOGCOLOR_PATTERN.matcher(input).replaceAll(""); return stripColor(input, LOGCOLOR_PATTERN);
} }
static String stripColor(final String input, final Pattern pattern) static String stripColor(final String input, final Pattern pattern)