Skip to content

Commit a6a80ff

Browse files
committed
properly implement ExprArithmetic simplification
1 parent e9ba651 commit a6a80ff

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

src/main/java/ch/njol/skript/expressions/arithmetic/ExprArithmetic.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,35 @@ public String toString(@Nullable Event event, boolean debug) {
353353
@Override
354354
public Expression<T> simplify() {
355355
// simplify this expression IFF it's the top-level arithmetic expression
356-
if ( isTopLevel && first instanceof Literal && second instanceof Literal)
356+
if (isTopLevel)
357+
return simplifyInternal();
358+
return this;
359+
}
360+
361+
/**
362+
* Simplifies an arithmetic expression regardless of whether it is the top-level expression.
363+
* @return the simplified expression
364+
*/
365+
private Expression<T> simplifyInternal() {
366+
if (first instanceof ExprArithmetic<?,?,?> firstArith) {
367+
//noinspection unchecked
368+
first = (Expression<L>) firstArith.simplifyInternal();
369+
} else {
370+
//noinspection unchecked
371+
first = (Expression<L>) first.simplify();
372+
}
373+
374+
if (second instanceof ExprArithmetic<?,?,?> secondArith) {
375+
//noinspection unchecked
376+
second = (Expression<R>) secondArith.simplifyInternal();
377+
} else {
378+
//noinspection unchecked
379+
second = (Expression<R>) second.simplify();
380+
}
381+
382+
if (first instanceof Literal && second instanceof Literal)
357383
return getAsSimplifiedLiteral();
384+
358385
return this;
359386
}
360387

0 commit comments

Comments
 (0)