|
23 | 23 | * |
24 | 24 | */ |
25 | 25 | public final class Prefilters { |
26 | | - |
| 26 | + |
27 | 27 | private Prefilters(){} |
28 | | - |
| 28 | + |
29 | 29 | public enum PrefilterType{ |
30 | 30 | /** |
31 | 31 | * Item matches are fuzzy matches for item notation. Red wool and black wool |
@@ -101,53 +101,62 @@ public static void match(Map<String, Construct> map, String key, |
101 | 101 | * If it doesn't, it throws an exception. If the value is not provided, or it does |
102 | 102 | * match, it returns void, which means that the test passed, and the event matches. |
103 | 103 | */ |
104 | | - public static void match(Map<String, Construct> map, String key, |
105 | | - Construct actualValue, PrefilterType type) throws PrefilterNonMatchException{ |
106 | | - if(map.containsKey(key)){ |
107 | | - switch(type){ |
108 | | - case ITEM_MATCH: |
109 | | - ItemMatch(map.get(key), actualValue); |
110 | | - break; |
111 | | - case STRING_MATCH: |
112 | | - StringMatch(map.get(key).val(), actualValue.val()); |
113 | | - break; |
114 | | - case MATH_MATCH: |
115 | | - MathMatch(map.get(key), actualValue); |
116 | | - break; |
117 | | - case EXPRESSION: |
118 | | - ExpressionMatch(MathReplace(key, map.get(key), actualValue), actualValue); |
119 | | - break; |
120 | | - case REGEX: |
121 | | - RegexMatch(map.get(key), actualValue); |
122 | | - break; |
123 | | - case MACRO: |
124 | | - MacroMatch(key, map.get(key), actualValue); |
| 104 | + public static void match(Map<String, Construct> map, String key, |
| 105 | + Construct actualValue, PrefilterType type) throws PrefilterNonMatchException{ |
| 106 | + if(map.containsKey(key)){ |
| 107 | + switch(type){ |
| 108 | + case ITEM_MATCH: |
| 109 | + ItemMatch(map.get(key), actualValue); |
| 110 | + break; |
| 111 | + case STRING_MATCH: |
| 112 | + StringMatch(map.get(key).val(), actualValue.val()); |
| 113 | + break; |
| 114 | + case MATH_MATCH: |
| 115 | + MathMatch(map.get(key), actualValue); |
| 116 | + break; |
| 117 | + case EXPRESSION: |
| 118 | + Construct exp = map.get(key); |
| 119 | + if(!exp.val().isEmpty() |
| 120 | + && exp.val().charAt(0) == '(' && exp.val().charAt(exp.val().length() - 1) == ')'){ |
| 121 | + ExpressionMatch(exp, key, actualValue); |
| 122 | + } else { |
| 123 | + throw new ConfigRuntimeException("Prefilter expecting expression type, and \"" |
| 124 | + + exp.val() + "\" does not follow expression format. " |
| 125 | + + "(Did you surround it in parenthesis?)", |
| 126 | + ExceptionType.FormatException, exp.getTarget()); |
| 127 | + } |
| 128 | + break; |
| 129 | + case REGEX: |
| 130 | + String regex = map.get(key).val(); |
| 131 | + if(!regex.isEmpty() |
| 132 | + && regex.charAt(0) == '/' && regex.charAt(regex.length() - 1) == '/'){ |
| 133 | + RegexMatch(regex, actualValue); |
| 134 | + } else { |
| 135 | + throw new ConfigRuntimeException("Prefilter expecting regex type, and \"" |
| 136 | + + regex + "\" does not follow regex format", |
| 137 | + ExceptionType.FormatException, map.get(key).getTarget()); |
| 138 | + } |
| 139 | + break; |
| 140 | + case MACRO: |
| 141 | + MacroMatch(key, map.get(key), actualValue); |
125 | 142 | break; |
126 | 143 | case BOOLEAN_MATCH: |
127 | 144 | BooleanMatch(map.get(key), actualValue); |
128 | 145 | break; |
129 | 146 | case LOCATION_MATCH: |
130 | 147 | LocationMatch(map.get(key), actualValue); |
131 | 148 | break; |
132 | | - } |
133 | | - } |
134 | | - } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
135 | 152 |
|
136 | | - private static void ItemMatch(Construct item1, Construct item2) throws PrefilterNonMatchException{ |
137 | | - String i1 = item1.val(); |
138 | | - String i2 = item2.val(); |
139 | | - if(item1.val().contains(":")){ |
140 | | - String[] split = item1.val().split(":"); |
141 | | - i1 = split[0].trim(); |
142 | | - } |
143 | | - if(item2.val().contains(":")){ |
144 | | - String[] split = item2.val().split(":"); |
145 | | - i2 = split[0].trim(); |
146 | | - } |
147 | | - if(!i1.trim().equalsIgnoreCase(i2.trim())){ |
148 | | - throw new PrefilterNonMatchException(); |
149 | | - } |
150 | | - } |
| 153 | + private static void ItemMatch(Construct item1, Construct item2) throws PrefilterNonMatchException{ |
| 154 | + String i1 = item1.val().split(":")[0]; |
| 155 | + String i2 = item2.val().split(":")[0]; |
| 156 | + if (!i1.trim().equals(i2)) { |
| 157 | + throw new PrefilterNonMatchException(); |
| 158 | + } |
| 159 | + } |
151 | 160 |
|
152 | 161 | private static void BooleanMatch(Construct bool1, Construct bool2) throws PrefilterNonMatchException { |
153 | 162 | if (Static.getBoolean(bool1) != Static.getBoolean(bool2)) { |
@@ -181,76 +190,64 @@ private static void MathMatch(Construct one, Construct two) throws PrefilterNonM |
181 | 190 | } |
182 | 191 | } |
183 | 192 |
|
184 | | - private static void ExpressionMatch(Construct expression, Construct dvalue) throws PrefilterNonMatchException{ |
185 | | - if(expression.val().matches("\\(.*\\)")){ |
186 | | - String exp = expression.val().substring(1, expression.val().length() - 1); |
187 | | - boolean inequalityMode = false; |
188 | | - if(exp.contains("<") || exp.contains(">") || exp.contains("==")){ |
189 | | - inequalityMode = true; |
190 | | - } |
191 | | - String eClass = "com.sk89q.worldedit.internal.expression.Expression"; |
192 | | - String errClass = "com.sk89q.worldedit.internal.expression.ExpressionException"; |
193 | | - Class eClazz, errClazz; |
194 | | - try { |
195 | | - eClazz = Class.forName(eClass); |
196 | | - errClazz = Class.forName(errClass); |
197 | | - } catch (ClassNotFoundException cnf) { |
198 | | - throw new ConfigRuntimeException("You are missing a required dependency: " + eClass, |
199 | | - ExceptionType.PluginInternalException, expression.getTarget(), cnf); |
200 | | - } |
201 | | - try { |
202 | | - Object e = ReflectionUtils.invokeMethod(eClazz, null, "compile", |
203 | | - new Class[]{String.class}, new Object[]{exp}); |
204 | | - double val = (double) ReflectionUtils.invokeMethod(eClazz, e, "evaluate"); |
205 | | - if (inequalityMode) { |
206 | | - if (val == 0) { |
207 | | - throw new PrefilterNonMatchException(); |
208 | | - } |
209 | | - } else { |
210 | | - if (val != Static.getDouble(dvalue, Target.UNKNOWN)) { |
211 | | - throw new PrefilterNonMatchException(); |
212 | | - } |
| 193 | + private static void ExpressionMatch(Construct expression, String key, Construct dvalue) throws PrefilterNonMatchException{ |
| 194 | + String exp = expression.val().substring(1, expression.val().length() - 1); |
| 195 | + boolean inequalityMode = false; |
| 196 | + if(exp.contains("<") || exp.contains(">") || exp.contains("==")){ |
| 197 | + inequalityMode = true; |
| 198 | + } |
| 199 | + String eClass = "com.sk89q.worldedit.internal.expression.Expression"; |
| 200 | + String errClass = "com.sk89q.worldedit.internal.expression.ExpressionException"; |
| 201 | + Class eClazz, errClazz; |
| 202 | + try { |
| 203 | + eClazz = Class.forName(eClass); |
| 204 | + errClazz = Class.forName(errClass); |
| 205 | + } catch (ClassNotFoundException cnf) { |
| 206 | + throw new ConfigRuntimeException("You are missing a required dependency: " + eClass, |
| 207 | + ExceptionType.PluginInternalException, expression.getTarget(), cnf); |
| 208 | + } |
| 209 | + try { |
| 210 | + Object e = ReflectionUtils.invokeMethod(eClazz, null, "compile", |
| 211 | + new Class[]{String.class, String[].class}, new Object[]{exp, new String[]{key}}); |
| 212 | + double val = (double) ReflectionUtils.invokeMethod(eClazz, e, "evaluate", |
| 213 | + new Class[]{double[].class}, |
| 214 | + new Object[]{new double[]{Static.getDouble(dvalue, Target.UNKNOWN)}}); |
| 215 | + if (inequalityMode) { |
| 216 | + if (val == 0) { |
| 217 | + throw new PrefilterNonMatchException(); |
213 | 218 | } |
214 | | - } catch (ReflectionUtils.ReflectionException rex) { |
215 | | - if (rex.getCause().getClass().isAssignableFrom(errClazz)) { |
216 | | - throw new ConfigRuntimeException("Your expression was invalidly formatted", |
217 | | - ExceptionType.PluginInternalException, expression.getTarget(), rex.getCause()); |
218 | | - } else { |
219 | | - throw new ConfigRuntimeException(rex.getMessage(), ExceptionType.PluginInternalException, |
220 | | - expression.getTarget(), rex.getCause()); |
| 219 | + } else { |
| 220 | + if (val != Static.getDouble(dvalue, Target.UNKNOWN)) { |
| 221 | + throw new PrefilterNonMatchException(); |
221 | 222 | } |
222 | 223 | } |
223 | | - } else { |
224 | | - throw new ConfigRuntimeException("Prefilter expecting expression type, and \"" |
225 | | - + expression.val() + "\" does not follow expression format. " |
226 | | - + "(Did you surround it in parenthesis?)", |
227 | | - ExceptionType.FormatException, expression.getTarget()); |
228 | | - } |
229 | | - } |
230 | | - |
231 | | - private static void RegexMatch(Construct expression, Construct value) throws PrefilterNonMatchException{ |
232 | | - if(expression.val().matches("/.*/")){ |
233 | | - String exp = expression.val().substring(1, expression.val().length() - 1); |
234 | | - if(!value.val().matches(exp)){ |
235 | | - throw new PrefilterNonMatchException(); |
236 | | - } |
237 | | - } else { |
238 | | - throw new ConfigRuntimeException("Prefilter expecting regex type, and \"" |
239 | | - + expression.val() + "\" does not follow regex format", ExceptionType.FormatException, expression.getTarget()); |
240 | | - } |
241 | | - } |
| 224 | + } catch (ReflectionUtils.ReflectionException rex) { |
| 225 | + if (rex.getCause().getClass().isAssignableFrom(errClazz)) { |
| 226 | + throw new ConfigRuntimeException("Your expression was invalidly formatted", |
| 227 | + ExceptionType.PluginInternalException, expression.getTarget(), rex.getCause()); |
| 228 | + } else { |
| 229 | + throw new ConfigRuntimeException(rex.getMessage(), ExceptionType.PluginInternalException, |
| 230 | + expression.getTarget(), rex.getCause()); |
| 231 | + } |
| 232 | + } |
| 233 | + } |
242 | 234 |
|
243 | | - private static void MacroMatch(String key, Construct expression, Construct value) throws PrefilterNonMatchException{ |
244 | | - if(expression.val().matches("\\(.*\\)")){ |
245 | | - ExpressionMatch(MathReplace(key, expression, value), value); |
246 | | - } else if(expression.val().matches("/.*/")){ |
247 | | - RegexMatch(expression, value); |
248 | | - } else { |
249 | | - StringMatch(expression.val(), value.val()); |
250 | | - } |
251 | | - } |
| 235 | + private static void RegexMatch(String regex, Construct value) throws PrefilterNonMatchException{ |
| 236 | + regex = regex.substring(1, regex.length() - 1); |
| 237 | + if(!value.val().matches(regex)){ |
| 238 | + throw new PrefilterNonMatchException(); |
| 239 | + } |
| 240 | + } |
252 | 241 |
|
253 | | - private static Construct MathReplace(String key, Construct expression, Construct value){ |
254 | | - return new CString(expression.val().replaceAll(key, value.val()), expression.getTarget()); |
255 | | - } |
| 242 | + private static void MacroMatch(String key, Construct expression, Construct value) throws PrefilterNonMatchException{ |
| 243 | + if(expression.val().isEmpty()) { |
| 244 | + throw new PrefilterNonMatchException(); |
| 245 | + } else if (expression.val().charAt(0) == '(' && expression.val().charAt(expression.val().length() - 1) == ')') { |
| 246 | + ExpressionMatch(expression, key, value); |
| 247 | + } else if (expression.val().charAt(0) == '/' && expression.val().charAt(expression.val().length() - 1) == '/') { |
| 248 | + RegexMatch(expression.val(), value); |
| 249 | + } else { |
| 250 | + StringMatch(expression.val(), value.val()); |
| 251 | + } |
| 252 | + } |
256 | 253 | } |
0 commit comments