6
6
7
7
public class FormatUtil
8
8
{
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]" );
10
12
static final transient Pattern VANILLA_MAGIC_PATTERN = Pattern .compile ("\u00a7 +[Kk]" );
11
13
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
15
21
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]" );
18
22
static final transient Pattern URL_PATTERN = Pattern .compile ("((?:(?:https?)://)?[\\ w-_\\ .]{2,})\\ .([a-z]{2,3}(?:/\\ S+)?)" );
19
23
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])$" );
20
24
@@ -27,15 +31,15 @@ public static String stripFormat(final String input)
27
31
}
28
32
return stripColor (input , VANILLA_PATTERN );
29
33
}
30
-
34
+
31
35
//This method is used to simply strip the & convention colour codes
32
36
public static String stripEssentialsFormat (final String input )
33
37
{
34
38
if (input == null )
35
39
{
36
40
return null ;
37
41
}
38
- return stripColor (input , REPLACE_PATTERN );
42
+ return stripColor (input , REPLACE_ALL_PATTERN );
39
43
}
40
44
41
45
//This is the general permission sensitive message format function, checks for urls.
@@ -60,12 +64,12 @@ public static String replaceFormat(final String input)
60
64
{
61
65
return null ;
62
66
}
63
- return REPLACE_PATTERN . matcher (input ). replaceAll ( " \u00a7 $1" );
67
+ return replaceColor (input , REPLACE_ALL_PATTERN );
64
68
}
65
-
69
+
66
70
static String replaceColor (final String input , final Pattern pattern )
67
71
{
68
- return pattern .matcher (input ).replaceAll ("\u00a7 $1" );
72
+ return REPLACE_PATTERN . matcher ( pattern .matcher (input ).replaceAll ("\u00a7 $1" )). replaceAll ( "& " );
69
73
}
70
74
71
75
//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
78
82
String message ;
79
83
if (user .isAuthorized (permBase + ".color" ))
80
84
{
81
- message = FormatUtil . replaceColor (input , REPLACE_COLOR_PATTERN );
85
+ message = replaceColor (input , REPLACE_COLOR_PATTERN );
82
86
}
83
87
else
84
88
{
85
- message = FormatUtil . stripColor (input , VANILLA_COLOR_PATTERN );
89
+ message = stripColor (input , VANILLA_COLOR_PATTERN );
86
90
}
87
91
if (user .isAuthorized (permBase + ".magic" ))
88
92
{
89
- message = FormatUtil . replaceColor (message , REPLACE_MAGIC_PATTERN );
93
+ message = replaceColor (message , REPLACE_MAGIC_PATTERN );
90
94
}
91
95
else
92
96
{
93
- message = FormatUtil . stripColor (message , VANILLA_MAGIC_PATTERN );
97
+ message = stripColor (message , VANILLA_MAGIC_PATTERN );
94
98
}
95
99
if (user .isAuthorized (permBase + ".format" ))
96
100
{
97
- message = FormatUtil . replaceColor (message , REPLACE_FORMAT_PATTERN );
101
+ message = replaceColor (message , REPLACE_FORMAT_PATTERN );
98
102
}
99
103
else
100
104
{
101
- message = FormatUtil . stripColor (message , VANILLA_FORMAT_PATTERN );
105
+ message = stripColor (message , VANILLA_FORMAT_PATTERN );
102
106
}
103
107
return message ;
104
108
}
105
-
109
+
106
110
public static String stripLogColorFormat (final String input )
107
111
{
108
112
if (input == null )
109
113
{
110
114
return null ;
111
115
}
112
- return LOGCOLOR_PATTERN . matcher (input ). replaceAll ( "" );
116
+ return stripColor (input , LOGCOLOR_PATTERN );
113
117
}
114
-
118
+
115
119
static String stripColor (final String input , final Pattern pattern )
116
120
{
117
121
return pattern .matcher (input ).replaceAll ("" );
118
122
}
119
-
123
+
120
124
public static String lastCode (final String input )
121
125
{
122
126
int pos = input .lastIndexOf ("\u00a7 " );
@@ -126,7 +130,7 @@ public static String lastCode(final String input)
126
130
}
127
131
return input .substring (pos , pos + 2 );
128
132
}
129
-
133
+
130
134
static String blockURL (final String input )
131
135
{
132
136
if (input == null )
@@ -140,7 +144,7 @@ static String blockURL(final String input)
140
144
}
141
145
return text ;
142
146
}
143
-
147
+
144
148
public static boolean validIP (String ipAddress )
145
149
{
146
150
return IPPATTERN .matcher (ipAddress ).matches ();
0 commit comments