66
77public class FormatUtil
88{
9- static final transient Pattern REPLACE_COLOR_PATTERN = Pattern .compile ("&([0-9a-fA-F])" );
9+ //Vanilla patterns used to strip existing formats
10+ static final transient Pattern VANILLA_PATTERN = Pattern .compile ("\u00a7 +[0-9A-FK-ORa-fk-or]?" );
11+ static final transient Pattern VANILLA_COLOR_PATTERN = Pattern .compile ("\u00a7 +[0-9A-Fa-f]" );
1012 static final transient Pattern VANILLA_MAGIC_PATTERN = Pattern .compile ("\u00a7 +[Kk]" );
1113 static final transient Pattern VANILLA_FORMAT_PATTERN = Pattern .compile ("\u00a7 +[L-ORl-or]" );
12- static final transient Pattern REPLACE_FORMAT_PATTERN = Pattern .compile ("&([l-orL-OR])" );
13- static final transient Pattern REPLACE_MAGIC_PATTERN = Pattern .compile ("&([Kk])" );
14- static final transient Pattern REPLACE_PATTERN = Pattern .compile ("&([0-9a-fk-orA-FK-OR])" );
14+ //Essentials '&' convention colour codes
15+ static final transient Pattern REPLACE_ALL_PATTERN = Pattern .compile ("(?<!&)&([0-9a-fk-orA-FK-OR])" );
16+ static final transient Pattern REPLACE_COLOR_PATTERN = Pattern .compile ("(?<!&)&([0-9a-fA-F])" );
17+ static final transient Pattern REPLACE_MAGIC_PATTERN = Pattern .compile ("(?<!&)&([Kk])" );
18+ static final transient Pattern REPLACE_FORMAT_PATTERN = Pattern .compile ("(?<!&)&([l-orL-OR])" );
19+ static final transient Pattern REPLACE_PATTERN = Pattern .compile ("&&(?=[0-9a-fk-orA-FK-OR])" );
20+ //Used to prepare xmpp output
1521 static final transient Pattern LOGCOLOR_PATTERN = Pattern .compile ("\\ x1B\\ [([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]" );
16- static final transient Pattern VANILLA_PATTERN = Pattern .compile ("\u00a7 +[0-9A-FK-ORa-fk-or]?" );
17- static final transient Pattern VANILLA_COLOR_PATTERN = Pattern .compile ("\u00a7 +[0-9A-Fa-f]" );
1822 static final transient Pattern URL_PATTERN = Pattern .compile ("((?:(?:https?)://)?[\\ w-_\\ .]{2,})\\ .([a-z]{2,3}(?:/\\ S+)?)" );
1923 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])$" );
2024
@@ -27,15 +31,15 @@ public static String stripFormat(final String input)
2731 }
2832 return stripColor (input , VANILLA_PATTERN );
2933 }
30-
34+
3135 //This method is used to simply strip the & convention colour codes
3236 public static String stripEssentialsFormat (final String input )
3337 {
3438 if (input == null )
3539 {
3640 return null ;
3741 }
38- return stripColor (input , REPLACE_PATTERN );
42+ return stripColor (input , REPLACE_ALL_PATTERN );
3943 }
4044
4145 //This is the general permission sensitive message format function, checks for urls.
@@ -60,12 +64,12 @@ public static String replaceFormat(final String input)
6064 {
6165 return null ;
6266 }
63- return REPLACE_PATTERN . matcher (input ). replaceAll ( " \u00a7 $1" );
67+ return replaceColor (input , REPLACE_ALL_PATTERN );
6468 }
65-
69+
6670 static String replaceColor (final String input , final Pattern pattern )
6771 {
68- return pattern .matcher (input ).replaceAll ("\u00a7 $1" );
72+ return REPLACE_PATTERN . matcher ( pattern .matcher (input ).replaceAll ("\u00a7 $1" )). replaceAll ( "& " );
6973 }
7074
7175 //This is the general permission sensitive message format function, does not touch urls.
@@ -78,45 +82,45 @@ public static String formatString(final IUser user, final String permBase, final
7882 String message ;
7983 if (user .isAuthorized (permBase + ".color" ))
8084 {
81- message = FormatUtil . replaceColor (input , REPLACE_COLOR_PATTERN );
85+ message = replaceColor (input , REPLACE_COLOR_PATTERN );
8286 }
8387 else
8488 {
85- message = FormatUtil . stripColor (input , VANILLA_COLOR_PATTERN );
89+ message = stripColor (input , VANILLA_COLOR_PATTERN );
8690 }
8791 if (user .isAuthorized (permBase + ".magic" ))
8892 {
89- message = FormatUtil . replaceColor (message , REPLACE_MAGIC_PATTERN );
93+ message = replaceColor (message , REPLACE_MAGIC_PATTERN );
9094 }
9195 else
9296 {
93- message = FormatUtil . stripColor (message , VANILLA_MAGIC_PATTERN );
97+ message = stripColor (message , VANILLA_MAGIC_PATTERN );
9498 }
9599 if (user .isAuthorized (permBase + ".format" ))
96100 {
97- message = FormatUtil . replaceColor (message , REPLACE_FORMAT_PATTERN );
101+ message = replaceColor (message , REPLACE_FORMAT_PATTERN );
98102 }
99103 else
100104 {
101- message = FormatUtil . stripColor (message , VANILLA_FORMAT_PATTERN );
105+ message = stripColor (message , VANILLA_FORMAT_PATTERN );
102106 }
103107 return message ;
104108 }
105-
109+
106110 public static String stripLogColorFormat (final String input )
107111 {
108112 if (input == null )
109113 {
110114 return null ;
111115 }
112- return LOGCOLOR_PATTERN . matcher (input ). replaceAll ( "" );
116+ return stripColor (input , LOGCOLOR_PATTERN );
113117 }
114-
118+
115119 static String stripColor (final String input , final Pattern pattern )
116120 {
117121 return pattern .matcher (input ).replaceAll ("" );
118122 }
119-
123+
120124 public static String lastCode (final String input )
121125 {
122126 int pos = input .lastIndexOf ("\u00a7 " );
@@ -126,7 +130,7 @@ public static String lastCode(final String input)
126130 }
127131 return input .substring (pos , pos + 2 );
128132 }
129-
133+
130134 static String blockURL (final String input )
131135 {
132136 if (input == null )
@@ -140,7 +144,7 @@ static String blockURL(final String input)
140144 }
141145 return text ;
142146 }
143-
147+
144148 public static boolean validIP (String ipAddress )
145149 {
146150 return IPPATTERN .matcher (ipAddress ).matches ();
0 commit comments