Skip to content

Commit f91a30b

Browse files
committed
Fix Direction and ConvertedExpression simplification
1 parent a6a80ff commit f91a30b

6 files changed

Lines changed: 48 additions & 16 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.skriptlang.skript.lang.arithmetic.Arithmetics;
2424
import org.skriptlang.skript.lang.arithmetic.OperationInfo;
2525
import org.skriptlang.skript.lang.arithmetic.Operator;
26+
import org.skriptlang.skript.lang.simplification.SimplifiedLiteral;
2627

2728
import java.lang.reflect.Array;
2829
import java.util.ArrayList;
@@ -380,7 +381,7 @@ private Expression<T> simplifyInternal() {
380381
}
381382

382383
if (first instanceof Literal && second instanceof Literal)
383-
return getAsSimplifiedLiteral();
384+
return SimplifiedLiteral.fromExpression(this);
384385

385386
return this;
386387
}

src/main/java/ch/njol/skript/lang/Expression.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import ch.njol.skript.classes.Changer.ChangeMode;
66
import ch.njol.skript.classes.Changer.ChangerUtils;
77
import ch.njol.skript.conditions.CondIsSet;
8-
import ch.njol.skript.lang.util.ContextlessEvent;
98
import ch.njol.skript.lang.util.ConvertedExpression;
109
import ch.njol.skript.lang.util.SimpleExpression;
1110
import ch.njol.skript.log.ErrorQuality;
@@ -18,7 +17,6 @@
1817
import org.jetbrains.annotations.Nullable;
1918
import org.skriptlang.skript.lang.converter.Converter;
2019
import org.skriptlang.skript.lang.simplification.Simplifiable;
21-
import org.skriptlang.skript.lang.simplification.SimplifiedLiteral;
2220

2321
import java.util.*;
2422
import java.util.function.Function;
@@ -249,18 +247,6 @@ default boolean canReturn(Class<?> returnType) {
249247
*/
250248
Expression<?> getSource();
251249

252-
/**
253-
* Attempts to create a {@link SimplifiedLiteral} by evaluating the expression with a {@link ContextlessEvent}.
254-
* This should only be attempted IFF the expression's children are all literals and
255-
* {@link #getAll(Event)} would always return the exact same value, no matter the context in which it is called.
256-
* The value of {@link #toString(Event, boolean)} will be evaluated and captured for the literal's toString methods.
257-
*
258-
* @return A simplified literal with the data from this expression's evaluation.
259-
*/
260-
default Literal<T> getAsSimplifiedLiteral() {
261-
return SimplifiedLiteral.fromExpression(this);
262-
}
263-
264250
/**
265251
* Tests whether this expression supports the given mode, and if yes what type it expects the <code>delta</code> to be.
266252
* <p>

src/main/java/ch/njol/skript/lang/util/ConvertedExpression.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ public Expression<?> getSource() {
265265

266266
@Override
267267
public Expression<? extends T> simplify() {
268+
source = source.simplify();
268269
return this;
269270
}
270271

src/main/java/ch/njol/skript/util/Direction.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,37 @@ public String toString(final @Nullable Event e, final boolean debug) {
420420
@Override
421421
public Expression<? extends Location> simplify() {
422422
if (dirs instanceof Literal && dirs.isSingle() && Direction.ZERO.equals(((Literal<?>) dirs).getSingle())) {
423-
return locs;
423+
return new SimpleExpression<>() {
424+
@Override
425+
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
426+
throw new UnsupportedOperationException();
427+
}
428+
429+
@Override
430+
protected Location @Nullable [] get(Event event) {
431+
return locs.getAll(event);
432+
}
433+
434+
@Override
435+
public boolean getAnd() {
436+
return locs.getAnd();
437+
}
438+
439+
@Override
440+
public boolean isSingle() {
441+
return locs.isSingle();
442+
}
443+
444+
@Override
445+
public Class<? extends Location> getReturnType() {
446+
return Location.class;
447+
}
448+
449+
@Override
450+
public String toString(@Nullable Event event, boolean debug) {
451+
return "at " + locs.toString(event, debug);
452+
}
453+
};
424454
}
425455
return this;
426456
}

src/main/java/org/skriptlang/skript/lang/simplification/Simplifiable.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public interface Simplifiable<S extends SyntaxElement> {
1515
* Any returned object should attempt to maintain the original value of {@link Debuggable#toString(Event, boolean)}.
1616
* An addition indicating that the value was simplified can be added in the debug string. See {@link SimplifiedLiteral}
1717
* for an example.
18+
* <br>
19+
* Simplification should never widen contracts. For example, any simplified expression should take care to return
20+
* the same or a more specific type than the original expression, never a more generic type. Likewise, be sure to
21+
* maintain the behavior of change() and acceptsChange(). Failure to do so can result in unexpected behavior and
22+
* tricky bugs.
1823
*
1924
* @return the simplified object.
2025
* @see SimplifiedLiteral

src/main/java/org/skriptlang/skript/lang/simplification/SimplifiedLiteral.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
*/
1515
public class SimplifiedLiteral<T> extends SimpleLiteral<T> {
1616

17+
/**
18+
* Creates a new simplified literal from an expression by evaluating it with a {@link ContextlessEvent}.
19+
* Any expression that requires specific event data cannot be safely simplified to a literal.
20+
* The original expression is stored for later toString generation.
21+
*
22+
* @param original the original expression to simplify
23+
* @param <T> the type of the literal
24+
* @return a new simplified literal
25+
*/
1726
public static <T> SimplifiedLiteral<T> fromExpression(Expression<T> original) {
1827
Event event = ContextlessEvent.get();
1928

0 commit comments

Comments
 (0)