Skip to content
This repository was archived by the owner on Apr 9, 2024. It is now read-only.

Commit 6392929

Browse files
authored
Merge pull request #3 from Andre601/feature/further-condition-improvements
Further improvements of condition parsing
2 parents afc32da + 0f7fb9b commit 6392929

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

core/src/main/java/ch/andre601/advancedserverlist/core/profiles/ServerListProfile.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,12 @@ private List<Expression> createExpressions(List<String> list, PluginLogger logge
9696
for(String str : list){
9797
Expression expression = new Expression(str);
9898

99-
switch(expression.getResult()){
100-
case VALID -> expressions.add(expression);
101-
case INVALID_EMPTY_PARTS -> logInvalid(logger, str, "Either left or right part of condition was empty.");
102-
case INVALID_NO_EXPRESSION -> logInvalid(logger, str, "Empty conditions are not allowed.");
103-
case INVALID_DOUBLE_OPERATOR -> logInvalid(logger, str, "Condition had two operands!");
104-
case INVALID_BROKEN_NOT_EQUAL -> logInvalid(logger, str, "Found '!' without '=' following it.");
105-
default -> logInvalid(logger, str, "Encountered unknown issue.");
99+
if(expression.getResult() != Expression.ExpressionResult.VALID){
100+
logger.warn("Detected Invalid condition '%s'! Cause: %s", str, expression.getResult().getMessage());
101+
continue;
106102
}
103+
104+
expressions.add(expression);
107105
}
108106

109107
return expressions;
@@ -125,8 +123,4 @@ private List<String> getList(ConfigurationNode node, String key, boolean trim){
125123

126124
return list;
127125
}
128-
129-
private void logInvalid(PluginLogger logger, String expression, String reason){
130-
logger.warn("Invalid Condition '%s'! %s", expression, reason);
131-
}
132126
}

core/src/main/java/ch/andre601/advancedserverlist/core/profiles/conditions/Expression.java

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public Expression(String expression){
4242
}
4343

4444
public boolean evaluate(Map<String, Object> replacements){
45-
String newLeft = StringReplacer.replace(left, replacements);
46-
String newRight = StringReplacer.replace(right, replacements);
45+
String newLeft = StringReplacer.replace(left, replacements).trim();
46+
String newRight = StringReplacer.replace(right, replacements).trim();
4747

4848
return operator.evaluate(newLeft, newRight);
4949
}
@@ -67,8 +67,10 @@ private void resolveExpression(String expression){
6767

6868
for(int i = 0; i < chars.length; i++){
6969
char c = chars[i];
70+
char next = (i == (chars.length - 1)) ? '\0' : chars[i + 1];
7071

71-
if((c != '<' && c != '>' && c != '=' && c != '!') || i + 1 >= chars.length){
72+
Operator tmp = Operator.getOperand(c, next);
73+
if(tmp == Operator.UNKNOWN){
7274
if(foundOperator){
7375
right.append(c);
7476
}else{
@@ -82,17 +84,8 @@ private void resolveExpression(String expression){
8284
return;
8385
}
8486

87+
operator = tmp;
8588
foundOperator = true;
86-
87-
char next = chars[i + 1];
88-
if(c == '!' && next != '='){
89-
result = ExpressionResult.INVALID_BROKEN_NOT_EQUAL;
90-
return;
91-
}
92-
93-
operator = Operator.getOperand(c, next);
94-
if(operator.hasSecond() && operator != Operator.UNKNOWN)
95-
i++;
9689
}
9790

9891
if(left.isEmpty() || right.isEmpty()){
@@ -198,11 +191,32 @@ private static int getInt(String text){
198191
}
199192

200193
public enum ExpressionResult{
201-
VALID,
194+
VALID{
195+
@Override
196+
public String getMessage(){
197+
return null;
198+
}
199+
},
200+
201+
INVALID_NO_EXPRESSION{
202+
@Override
203+
public String getMessage(){
204+
return "Received empty condition which isn't allowed!";
205+
}
206+
},
207+
INVALID_DOUBLE_OPERATOR{
208+
@Override
209+
public String getMessage(){
210+
return "Condition contained two operands.";
211+
}
212+
},
213+
INVALID_EMPTY_PARTS{
214+
@Override
215+
public String getMessage(){
216+
return "Received condition with either left or right part being empty.";
217+
}
218+
};
202219

203-
INVALID_NO_EXPRESSION,
204-
INVALID_DOUBLE_OPERATOR,
205-
INVALID_BROKEN_NOT_EQUAL,
206-
INVALID_EMPTY_PARTS
220+
public abstract String getMessage();
207221
}
208222
}

0 commit comments

Comments
 (0)