Skip to content

Commit ae22719

Browse files
committed
Merge pull request #376 from PseudoKnight/fixes
Fixes
2 parents dd4d647 + b9cfb0d commit ae22719

File tree

2 files changed

+106
-109
lines changed

2 files changed

+106
-109
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@
459459
<!--<mainClass>com.laytonsmith.core.MainSandbox</mainClass>-->
460460
</manifest>
461461
<manifestEntries>
462-
<Class-Path>WorldEdit.jar CommandHelper/WorldEdit.jar lib/WorldEdit.jar ../lib/WorldEdit.jar ../WorldEdit.jar ../bukkit.jar CommandHelper/bukkit.jar</Class-Path>
462+
<Class-Path>../bukkit.jar CommandHelper/bukkit.jar</Class-Path>
463463
</manifestEntries>
464464
</archive>
465465
</configuration>

src/main/java/com/laytonsmith/core/events/Prefilters.java

Lines changed: 105 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
*
2424
*/
2525
public final class Prefilters {
26-
26+
2727
private Prefilters(){}
28-
28+
2929
public enum PrefilterType{
3030
/**
3131
* 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,
101101
* If it doesn't, it throws an exception. If the value is not provided, or it does
102102
* match, it returns void, which means that the test passed, and the event matches.
103103
*/
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);
125142
break;
126143
case BOOLEAN_MATCH:
127144
BooleanMatch(map.get(key), actualValue);
128145
break;
129146
case LOCATION_MATCH:
130147
LocationMatch(map.get(key), actualValue);
131148
break;
132-
}
133-
}
134-
}
149+
}
150+
}
151+
}
135152

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+
}
151160

152161
private static void BooleanMatch(Construct bool1, Construct bool2) throws PrefilterNonMatchException {
153162
if (Static.getBoolean(bool1) != Static.getBoolean(bool2)) {
@@ -181,76 +190,64 @@ private static void MathMatch(Construct one, Construct two) throws PrefilterNonM
181190
}
182191
}
183192

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();
213218
}
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();
221222
}
222223
}
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+
}
242234

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+
}
252241

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+
}
256253
}

0 commit comments

Comments
 (0)