Skip to content

Commit

Permalink
Bump EvalEx to 3.0.5. (#327)
Browse files Browse the repository at this point in the history
* Updated EvalEx dependency to 3.0.5. Rewrote Evalex functions.

* Added Unit Tests for ConditionFunction, corrected some functions.

* Fixed date formatting in ConditionalFunctions

* Updated tests for ConditionFunctions
  • Loading branch information
FelixOBrien authored Oct 15, 2023
1 parent 9e9a274 commit 64004fc
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 136 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ subprojects {
relocate 'org.aopalliance.intercept', 'com.clubobsidian.dynamicgui.shaded.aopalliance.intercept'
relocate 'org.aopalliance.aop', 'com.clubobsidian.dynamicgui.shaded.aopalliance.aop'
relocate 'javax.inject', 'com.clubobsidian.dynamicgui.shaded.javax.inject'
relocate 'com.udojava.evalex', 'com.clubobsidian.dynamicgui.shaded.evalex'
relocate 'com.ezylang.evalex', 'com.clubobsidian.dynamicgui.shaded.evalex'
relocate 'com.typesafe.config', 'com.clubobsidian.dynamicgui.shaded.typesafeconfig'
relocate 'com.google.j2objc', 'com.clubobsidian.dynamicgui.shaded.j2objc'
relocate 'com.google.inject', 'com.clubobsidian.dynamicgui.shaded.guice'
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
api project(path: ':api')
api project(path: ':parser')
testImplementation project(path: ':mock')
api 'com.udojava:EvalEx:2.1'
api 'com.ezylang:EvalEx:3.0.5'
api 'com.github.ClubObsidian:FuzzUtil:1.1.0'
api("com.google.inject:guice:$guiceVersion")
api "net.kyori:adventure-text-minimessage:$adventureVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,32 @@
package com.clubobsidian.dynamicgui.core.function.condition;

import com.clubobsidian.dynamicgui.api.DynamicGui;
import com.udojava.evalex.AbstractLazyFunction;
import com.udojava.evalex.Expression.LazyNumber;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.List;

public class AfterDateLazyFunction extends AbstractLazyFunction {

protected AfterDateLazyFunction() {
super("AFTERDATE", 1, true);
}

@FunctionParameter(name = "AfterDate", isLazy = true)
public class AfterDateLazyFunction extends AbstractFunction {
@Override
public LazyNumber lazyEval(List<LazyNumber> lazyParams) {
public EvaluationValue evaluate(Expression expression, Token functionToken, EvaluationValue... parameterValues) {
try {
String format = DynamicGui.get().getConfig().getDateTimeFormat();
Date now = Date.from(Instant.now());
Date expected = new SimpleDateFormat(format).parse(lazyParams.get(0).getString());
Date expected = new SimpleDateFormat(format).parse(parameterValues[0].getExpressionNode().getToken().getValue());
if (now.after(expected)) {
return ConditionFunction.ONE;
return new EvaluationValue(BigDecimal.ONE);
}
} catch (ParseException ignore) {
DynamicGui.get().getLogger().error("Invalid Date: %s", lazyParams.get(0).getString());
DynamicGui.get().getLogger().error("Invalid Date: %s", parameterValues[0].getStringValue());
}
return ConditionFunction.ZERO;
return new EvaluationValue(BigDecimal.ZERO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,35 @@
package com.clubobsidian.dynamicgui.core.function.condition;

import com.clubobsidian.dynamicgui.api.DynamicGui;
import com.udojava.evalex.AbstractLazyFunction;
import com.udojava.evalex.Expression.LazyNumber;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.List;

public class BeforeDateLazyFunction extends AbstractLazyFunction {

protected BeforeDateLazyFunction() {
super("BEFOREDATE", 1, true);
}
@FunctionParameter(name = "BeforeDate", isLazy = true)
public class BeforeDateLazyFunction extends AbstractFunction {

@Override
public LazyNumber lazyEval(List<LazyNumber> lazyParams) {
public EvaluationValue evaluate(Expression expression, Token functionToken, EvaluationValue... parameterValues) {
try {

String format = DynamicGui.get().getConfig().getDateTimeFormat();
System.out.println(format);
Date now = Date.from(Instant.now());
Date expected = new SimpleDateFormat(format).parse(lazyParams.get(0).getString());
Date expected = new SimpleDateFormat(format).parse(parameterValues[0].getExpressionNode().getToken().getValue());
if (now.before(expected)) {
return ConditionFunction.ONE;
return new EvaluationValue(BigDecimal.ONE);
}
} catch (ParseException ignore) {
DynamicGui.get().getLogger().error("Invalid Date: %s", lazyParams.get(0).getString());
DynamicGui.get().getLogger().error("Invalid Date: %s", parameterValues[0].getExpressionNode().getToken().getValue());
}
return ConditionFunction.ZERO;
return new EvaluationValue(BigDecimal.ZERO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import com.clubobsidian.dynamicgui.api.function.Function;
import com.clubobsidian.dynamicgui.api.function.FunctionOwner;
import com.clubobsidian.dynamicgui.api.gui.Slot;
import com.udojava.evalex.Expression;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.config.ExpressionConfiguration;

import java.math.BigDecimal;
import java.util.Map;

public class CheckTickFunction extends Function {

Expand All @@ -45,14 +49,8 @@ public boolean function(PlayerWrapper<?> playerWrapper) {
String tickData = this.getData()
.replace("%tick%", String.valueOf(tick))
.replace("%frame%", String.valueOf(frame));
Expression expr = new Expression(tickData);
expr.addLazyFunction(new EqualLazyFunction());

if (!expr.isBoolean()) {
return false;
}

return expr.eval().intValue() == 1;
Expression expr = new Expression(tickData, ExpressionConfiguration.defaultConfiguration().withAdditionalFunctions(Map.entry("STREQUAL", new EqualLazyFunction())));
return expr.evaluate().getNumberValue().equals(BigDecimal.ONE);
} catch (Exception ex) {
ex.printStackTrace();
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

import com.clubobsidian.dynamicgui.api.entity.PlayerWrapper;
import com.clubobsidian.dynamicgui.api.function.Function;
import com.udojava.evalex.Expression;
import com.udojava.evalex.Expression.LazyNumber;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.config.ExpressionConfiguration;

import java.math.BigDecimal;
import java.util.Map;

public class ConditionFunction extends Function {

Expand All @@ -37,43 +38,20 @@ public ConditionFunction() {
@Override
public boolean function(PlayerWrapper<?> playerWrapper) {
try {
Expression expr = new Expression(this.getData());

expr.addLazyFunction(new EqualLazyFunction());
expr.addLazyFunction(new IgnoreCaseEqualLazyFunction());
expr.addLazyFunction(new ContainsLazyFunction());
expr.addLazyFunction(new EndsWithLazyFunction());
expr.addLazyFunction(new StartsWithLazyFunction());
expr.addLazyFunction(new AfterDateLazyFunction());
expr.addLazyFunction(new BeforeDateLazyFunction());

if (!expr.isBoolean())
return false;

return expr.eval().intValue() == 1;
ExpressionConfiguration config = ExpressionConfiguration.defaultConfiguration().withAdditionalFunctions(
Map.entry("STREQUAL", new EqualLazyFunction()),
Map.entry("STREQUALIGNORECASE", new IgnoreCaseEqualLazyFunction()),
Map.entry("STRCONTAINS", new ContainsLazyFunction()),
Map.entry("STRENDSWITH", new EndsWithLazyFunction()),
Map.entry("STRSTARTSWITH", new StartsWithLazyFunction()),
Map.entry("AFTERDATE", new AfterDateLazyFunction()),
Map.entry("BEFOREDATE", new BeforeDateLazyFunction())
);
Expression expr = new Expression(this.getData(), config);
return expr.evaluate().getNumberValue().equals(BigDecimal.ONE);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}

public static LazyNumber ZERO = new LazyNumber() {
public BigDecimal eval() {
return BigDecimal.ZERO;
}

public String getString() {
return "0";
}
};

public static LazyNumber ONE = new LazyNumber() {
public BigDecimal eval() {
return BigDecimal.ONE;
}

public String getString() {
return null;
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@

package com.clubobsidian.dynamicgui.core.function.condition;

import com.udojava.evalex.AbstractLazyFunction;
import com.udojava.evalex.Expression.LazyNumber;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;

import java.util.List;

public class ContainsLazyFunction extends AbstractLazyFunction {

protected ContainsLazyFunction() {
super("STRCONTAINS", 2, true);
}
import java.math.BigDecimal;

@FunctionParameter(name = "value", isLazy = true)
@FunctionParameter(name = "character", isLazy = true)
public class ContainsLazyFunction extends AbstractFunction {
@Override
public LazyNumber lazyEval(List<LazyNumber> lazyParams) {
if (lazyParams.get(0).getString().contains(lazyParams.get(1).getString())) {
return ConditionFunction.ONE;
public EvaluationValue evaluate(Expression expression, Token functionToken, EvaluationValue... parameterValues) {
if (parameterValues[0].getExpressionNode().getToken().getValue().contains(parameterValues[1].getExpressionNode().getToken().getValue())) {
return new EvaluationValue(BigDecimal.ONE);
}
return ConditionFunction.ZERO;
return new EvaluationValue(BigDecimal.ZERO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,21 @@

package com.clubobsidian.dynamicgui.core.function.condition;

import com.udojava.evalex.AbstractLazyFunction;
import com.udojava.evalex.Expression.LazyNumber;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;

import java.util.List;

public class EndsWithLazyFunction extends AbstractLazyFunction {

protected EndsWithLazyFunction() {
super("STRENDSWITH", 2, true);
}

@FunctionParameter(name = "value", isLazy = true)
@FunctionParameter(name = "endsWith", isLazy = true)
public class EndsWithLazyFunction extends AbstractFunction {
@Override
public LazyNumber lazyEval(List<LazyNumber> lazyParams) {
if (lazyParams.get(0).getString().endsWith(lazyParams.get(1).getString())) {
return ConditionFunction.ONE;
public EvaluationValue evaluate(Expression expression, Token functionToken, EvaluationValue... parameterValues) {
if (parameterValues[0].getExpressionNode().getToken().getValue().endsWith(parameterValues[1].getExpressionNode().getToken().getValue())) {
return new EvaluationValue(1);
}
return ConditionFunction.ZERO;
return new EvaluationValue(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@

package com.clubobsidian.dynamicgui.core.function.condition;

import com.udojava.evalex.AbstractLazyFunction;
import com.udojava.evalex.Expression.LazyNumber;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;

import java.util.List;
import java.math.BigDecimal;

public class EqualLazyFunction extends AbstractLazyFunction {

protected EqualLazyFunction() {
super("STREQUAL", 2, true);
}
@FunctionParameter(name = "stringA", isLazy = true)
@FunctionParameter(name = "stringB", isLazy = true)
public class EqualLazyFunction extends AbstractFunction {

@Override
public LazyNumber lazyEval(List<LazyNumber> lazyParams) {
if (lazyParams.get(0).getString().equals(lazyParams.get(1).getString())) {
return ConditionFunction.ONE;
public EvaluationValue evaluate(Expression expression, Token functionToken, EvaluationValue... parameterValues) {
if(parameterValues[0].getExpressionNode().getToken().getValue().equals(parameterValues[1].getExpressionNode().getToken().getValue())) {
return new EvaluationValue(BigDecimal.ONE);
}
return ConditionFunction.ZERO;
return new EvaluationValue(BigDecimal.ZERO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@

package com.clubobsidian.dynamicgui.core.function.condition;

import com.udojava.evalex.AbstractLazyFunction;
import com.udojava.evalex.Expression.LazyNumber;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;
import com.ezylang.evalex.functions.AbstractFunction;

import java.util.List;

public class IgnoreCaseEqualLazyFunction extends AbstractLazyFunction {

protected IgnoreCaseEqualLazyFunction() {
super("STREQUALIGNORECASE", 2, true);
}
import java.math.BigDecimal;

@FunctionParameter(name = "stringA", isLazy = true)
@FunctionParameter(name = "stringB", isLazy = true)
public class IgnoreCaseEqualLazyFunction extends AbstractFunction {
@Override
public LazyNumber lazyEval(List<LazyNumber> lazyParams) {
if (lazyParams.get(0).getString().equalsIgnoreCase(lazyParams.get(1).getString())) {
return ConditionFunction.ONE;
public EvaluationValue evaluate(Expression expression, Token functionToken, EvaluationValue... parameterValues) {
if(parameterValues[0].getExpressionNode().getToken().getValue().equalsIgnoreCase(parameterValues[1].getExpressionNode().getToken().getValue())){
return new EvaluationValue(BigDecimal.ONE);
}
return ConditionFunction.ZERO;
return new EvaluationValue(BigDecimal.ZERO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@

package com.clubobsidian.dynamicgui.core.function.condition;

import com.udojava.evalex.AbstractLazyFunction;
import com.udojava.evalex.Expression.LazyNumber;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;
import com.ezylang.evalex.functions.AbstractFunction;

import java.util.List;
import java.math.BigDecimal;

public class StartsWithLazyFunction extends AbstractLazyFunction {

protected StartsWithLazyFunction() {
super("STRSTARTSWITH", 2, true);
}
@FunctionParameter(name = "value", isLazy = true)
@FunctionParameter(name = "startsWith", isLazy = true)
public class StartsWithLazyFunction extends AbstractFunction {

@Override
public LazyNumber lazyEval(List<LazyNumber> lazyParams) {
if (lazyParams.get(0).getString().startsWith(lazyParams.get(1).getString())) {
return ConditionFunction.ONE;
public EvaluationValue evaluate(Expression expression, Token functionToken, EvaluationValue... parameterValues) {
if(parameterValues[0].getExpressionNode().getToken().getValue().startsWith(parameterValues[1].getExpressionNode().getToken().getValue())){
return new EvaluationValue(BigDecimal.ONE);
}
return ConditionFunction.ZERO;
return new EvaluationValue(BigDecimal.ZERO);
}
}
Loading

0 comments on commit 64004fc

Please sign in to comment.