Compare commits

...
4 Commits
Author SHA1 Message Date
Andre_601 6392929298 Merge pull request #3 from Andre601/feature/further-condition-improvements
Further improvements of condition parsing
2022-08-03 13:37:03 +02:00
Andre601 0f7fb9b180 Better message 2022-08-02 23:00:54 +02:00
Andre601 4b24da8409 Further improve condition handling 2022-08-02 22:59:08 +02:00
Andre_601 afc32da0c3 Update README.md 2022-08-02 02:59:50 +02:00
3 changed files with 38 additions and 29 deletions

No files matched your search

+1
View File
@@ -46,6 +46,7 @@ You can download AdvancedServerList on the following locations:
- Custom MOTD.
- Custom Player count text.
- Custom hover for the player count hover.
- Hide player count (Turn to `???`)
- MiniMessage formatting for MOTD, player count and player count hover [[Read More][minimessage]]
- Placeholder support [[Read More][placeholders]]
@@ -96,14 +96,12 @@ public class ServerListProfile{
for(String str : list){
Expression expression = new Expression(str);
switch(expression.getResult()){
case VALID -> expressions.add(expression);
case INVALID_EMPTY_PARTS -> logInvalid(logger, str, "Either left or right part of condition was empty.");
case INVALID_NO_EXPRESSION -> logInvalid(logger, str, "Empty conditions are not allowed.");
case INVALID_DOUBLE_OPERATOR -> logInvalid(logger, str, "Condition had two operands!");
case INVALID_BROKEN_NOT_EQUAL -> logInvalid(logger, str, "Found '!' without '=' following it.");
default -> logInvalid(logger, str, "Encountered unknown issue.");
if(expression.getResult() != Expression.ExpressionResult.VALID){
logger.warn("Detected Invalid condition '%s'! Cause: %s", str, expression.getResult().getMessage());
continue;
}
expressions.add(expression);
}
return expressions;
@@ -125,8 +123,4 @@ public class ServerListProfile{
return list;
}
private void logInvalid(PluginLogger logger, String expression, String reason){
logger.warn("Invalid Condition '%s'! %s", expression, reason);
}
}
@@ -42,8 +42,8 @@ public class Expression{
}
public boolean evaluate(Map<String, Object> replacements){
String newLeft = StringReplacer.replace(left, replacements);
String newRight = StringReplacer.replace(right, replacements);
String newLeft = StringReplacer.replace(left, replacements).trim();
String newRight = StringReplacer.replace(right, replacements).trim();
return operator.evaluate(newLeft, newRight);
}
@@ -67,8 +67,10 @@ public class Expression{
for(int i = 0; i < chars.length; i++){
char c = chars[i];
char next = (i == (chars.length - 1)) ? '\0' : chars[i + 1];
if((c != '<' && c != '>' && c != '=' && c != '!') || i + 1 >= chars.length){
Operator tmp = Operator.getOperand(c, next);
if(tmp == Operator.UNKNOWN){
if(foundOperator){
right.append(c);
}else{
@@ -82,17 +84,8 @@ public class Expression{
return;
}
operator = tmp;
foundOperator = true;
char next = chars[i + 1];
if(c == '!' && next != '='){
result = ExpressionResult.INVALID_BROKEN_NOT_EQUAL;
return;
}
operator = Operator.getOperand(c, next);
if(operator.hasSecond() && operator != Operator.UNKNOWN)
i++;
}
if(left.isEmpty() || right.isEmpty()){
@@ -198,11 +191,32 @@ public class Expression{
}
public enum ExpressionResult{
VALID,
VALID{
@Override
public String getMessage(){
return null;
}
},
INVALID_NO_EXPRESSION,
INVALID_DOUBLE_OPERATOR,
INVALID_BROKEN_NOT_EQUAL,
INVALID_EMPTY_PARTS
INVALID_NO_EXPRESSION{
@Override
public String getMessage(){
return "Received empty condition which isn't allowed!";
}
},
INVALID_DOUBLE_OPERATOR{
@Override
public String getMessage(){
return "Condition contained two operands.";
}
},
INVALID_EMPTY_PARTS{
@Override
public String getMessage(){
return "Received condition with either left or right part being empty.";
}
};
public abstract String getMessage();
}
}