diff options
-rw-r--r-- | Essentials/src/com/earth2me/essentials/utils/FormatUtil.java | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/Essentials/src/com/earth2me/essentials/utils/FormatUtil.java b/Essentials/src/com/earth2me/essentials/utils/FormatUtil.java index 9d3ef7cb0..82f106ac9 100644 --- a/Essentials/src/com/earth2me/essentials/utils/FormatUtil.java +++ b/Essentials/src/com/earth2me/essentials/utils/FormatUtil.java @@ -6,15 +6,19 @@ import java.util.regex.Pattern; 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_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_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])"); + //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 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 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])$"); @@ -27,7 +31,7 @@ public class FormatUtil } return stripColor(input, VANILLA_PATTERN); } - + //This method is used to simply strip the & convention colour codes public static String stripEssentialsFormat(final String input) { @@ -35,7 +39,7 @@ public class FormatUtil { 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. @@ -60,12 +64,12 @@ public class FormatUtil { 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) { - 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. @@ -78,45 +82,45 @@ public class FormatUtil String message; if (user.isAuthorized(permBase + ".color")) { - message = FormatUtil.replaceColor(input, REPLACE_COLOR_PATTERN); + message = replaceColor(input, REPLACE_COLOR_PATTERN); } else { - message = FormatUtil.stripColor(input, VANILLA_COLOR_PATTERN); + message = stripColor(input, VANILLA_COLOR_PATTERN); } if (user.isAuthorized(permBase + ".magic")) { - message = FormatUtil.replaceColor(message, REPLACE_MAGIC_PATTERN); + message = replaceColor(message, REPLACE_MAGIC_PATTERN); } else { - message = FormatUtil.stripColor(message, VANILLA_MAGIC_PATTERN); + message = stripColor(message, VANILLA_MAGIC_PATTERN); } if (user.isAuthorized(permBase + ".format")) { - message = FormatUtil.replaceColor(message, REPLACE_FORMAT_PATTERN); + message = replaceColor(message, REPLACE_FORMAT_PATTERN); } else { - message = FormatUtil.stripColor(message, VANILLA_FORMAT_PATTERN); + message = stripColor(message, VANILLA_FORMAT_PATTERN); } return message; } - + public static String stripLogColorFormat(final String input) { if (input == null) { return null; } - return LOGCOLOR_PATTERN.matcher(input).replaceAll(""); + return stripColor(input, LOGCOLOR_PATTERN); } - + static String stripColor(final String input, final Pattern pattern) { return pattern.matcher(input).replaceAll(""); } - + public static String lastCode(final String input) { int pos = input.lastIndexOf("\u00a7"); @@ -126,7 +130,7 @@ public class FormatUtil } return input.substring(pos, pos + 2); } - + static String blockURL(final String input) { if (input == null) @@ -140,7 +144,7 @@ public class FormatUtil } return text; } - + public static boolean validIP(String ipAddress) { return IPPATTERN.matcher(ipAddress).matches(); |