Skip to content

Commit 665a02b

Browse files
committed
Add more wrapper methods to SimplifiedLiteral and implement simplify() on expressions capable of it.
1 parent 78431c7 commit 665a02b

57 files changed

Lines changed: 656 additions & 140 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/ch/njol/skript/expressions/ExprARGB.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package ch.njol.skript.expressions;
22

3-
import ch.njol.skript.doc.Description;
4-
import ch.njol.skript.doc.Examples;
5-
import ch.njol.skript.doc.Keywords;
6-
import ch.njol.skript.doc.Name;
7-
import ch.njol.skript.doc.Since;
3+
import ch.njol.skript.doc.*;
84
import ch.njol.skript.expressions.base.SimplePropertyExpression;
95
import ch.njol.skript.lang.Expression;
6+
import ch.njol.skript.lang.Literal;
107
import ch.njol.skript.lang.SkriptParser.ParseResult;
118
import ch.njol.skript.util.Color;
129
import ch.njol.util.Kleenean;
10+
import org.skriptlang.skript.lang.simplification.SimplifiedLiteral;
1311

1412
import java.util.Locale;
1513
import java.util.function.Function;
@@ -54,6 +52,13 @@ protected String getPropertyName() {
5452
return color.name().toLowerCase(Locale.ENGLISH);
5553
}
5654

55+
@Override
56+
public Expression<? extends Integer> simplify() {
57+
if (getExpr() instanceof Literal<? extends Color>)
58+
return SimplifiedLiteral.fromExpression(this);
59+
return this;
60+
}
61+
5762
/**
5863
* helper enum for getting argb values of {@link Color}s.
5964
*/

src/main/java/ch/njol/skript/expressions/ExprAlphabetList.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package ch.njol.skript.expressions;
22

3-
import java.util.Arrays;
4-
5-
import org.bukkit.event.Event;
6-
import org.jetbrains.annotations.Nullable;
7-
83
import ch.njol.skript.Skript;
94
import ch.njol.skript.doc.Description;
105
import ch.njol.skript.doc.Examples;
116
import ch.njol.skript.doc.Name;
127
import ch.njol.skript.doc.Since;
138
import ch.njol.skript.lang.Expression;
149
import ch.njol.skript.lang.ExpressionType;
10+
import ch.njol.skript.lang.Literal;
1511
import ch.njol.skript.lang.SkriptParser.ParseResult;
1612
import ch.njol.skript.lang.util.SimpleExpression;
1713
import ch.njol.util.Kleenean;
14+
import org.bukkit.event.Event;
15+
import org.jetbrains.annotations.Nullable;
16+
import org.skriptlang.skript.lang.simplification.SimplifiedLiteral;
17+
18+
import java.util.Arrays;
1819

1920
@Name("Alphabetical Sort")
2021
@Description("Sorts given strings in alphabetical order.")
@@ -38,8 +39,8 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
3839

3940
@Override
4041
@Nullable
41-
protected String[] get(Event e) {
42-
String[] sorted = texts.getAll(e).clone(); // Not yet sorted
42+
protected String[] get(Event event) {
43+
String[] sorted = texts.getAll(event).clone(); // Not yet sorted
4344
Arrays.sort(sorted); // Now sorted
4445
return sorted;
4546
}
@@ -55,8 +56,15 @@ public boolean isSingle() {
5556
}
5657

5758
@Override
58-
public String toString(@Nullable Event e, boolean debug) {
59-
return "alphabetically sorted strings: " + texts.toString(e, debug);
59+
public Expression<? extends String> simplify() {
60+
if (texts instanceof Literal<String>)
61+
return SimplifiedLiteral.fromExpression(this);
62+
return this;
63+
}
64+
65+
@Override
66+
public String toString(@Nullable Event event, boolean debug) {
67+
return "alphabetically sorted " + texts.toString(event, debug);
6068
}
6169

62-
}
70+
}

src/main/java/ch/njol/skript/expressions/ExprAltitude.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package ch.njol.skript.expressions;
22

3-
import org.bukkit.Location;
4-
53
import ch.njol.skript.doc.Description;
6-
import ch.njol.skript.doc.Examples;
4+
import ch.njol.skript.doc.Example;
75
import ch.njol.skript.doc.Name;
86
import ch.njol.skript.doc.Since;
97
import ch.njol.skript.expressions.base.SimplePropertyExpression;
8+
import ch.njol.skript.lang.Expression;
9+
import ch.njol.skript.lang.Literal;
10+
import org.bukkit.Location;
11+
import org.skriptlang.skript.lang.simplification.SimplifiedLiteral;
1012

11-
/**
12-
* @author Peter Güttinger
13-
*/
1413
@Name("Altitude")
1514
@Description("Effectively an alias of 'y-<a href='#ExprCoordinate'>coordinate</a> of …', it represents the height of some object above bedrock.")
16-
@Examples({"on damage:",
17-
" altitude of the attacker is higher than the altitude of the victim",
18-
" set damage to damage * 1.2"})
15+
@Example("""
16+
on damage:
17+
altitude of the attacker is higher than the altitude of the victim
18+
set damage to damage * 1.2
19+
""")
1920
@Since("1.4.3")
2021
public class ExprAltitude extends SimplePropertyExpression<Location, Number> {
2122

@@ -37,5 +38,14 @@ protected String getPropertyName() {
3738
public Class<? extends Number> getReturnType() {
3839
return Number.class;
3940
}
40-
41+
42+
@Override
43+
public Expression<? extends Number> simplify() {
44+
// as of INSERT VERSION, there are no literal locations but this is implemented for when pure functions can
45+
// be simplified.
46+
if (getExpr() instanceof Literal<? extends Location>)
47+
return SimplifiedLiteral.fromExpression(this);
48+
return this;
49+
}
50+
4151
}

src/main/java/ch/njol/skript/expressions/ExprAngle.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import ch.njol.skript.doc.Since;
88
import ch.njol.skript.lang.Expression;
99
import ch.njol.skript.lang.ExpressionType;
10+
import ch.njol.skript.lang.Literal;
1011
import ch.njol.skript.lang.SkriptParser.ParseResult;
1112
import ch.njol.skript.lang.util.SimpleExpression;
1213
import ch.njol.util.Kleenean;
1314
import org.bukkit.event.Event;
1415
import org.jetbrains.annotations.Nullable;
16+
import org.skriptlang.skript.lang.simplification.SimplifiedLiteral;
1517

1618
@Name("Angle")
1719
@Description({
@@ -72,6 +74,13 @@ public Class<? extends Number> getReturnType() {
7274
return Number.class;
7375
}
7476

77+
@Override
78+
public Expression<? extends Number> simplify() {
79+
if (angle instanceof Literal<?>)
80+
return SimplifiedLiteral.fromExpression(this);
81+
return this;
82+
}
83+
7584
@Override
7685
public String toString(@Nullable Event event, boolean debug) {
7786
return angle.toString(event, debug) + " in " + (isRadians ? "degrees" : "radians");

src/main/java/ch/njol/skript/expressions/ExprBannerItem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"set {_item} to thing banner pattern item"
3535
})
3636
@Since("2.10")
37+
// TODO: turn this into a Literal
3738
public class ExprBannerItem extends SimpleExpression<ItemType> {
3839

3940
private static final Map<Object, Material> bannerMaterials = new HashMap<>();

src/main/java/ch/njol/skript/expressions/ExprBiome.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ch.njol.skript.expressions;
22

3+
import ch.njol.skript.lang.Literal;
34
import org.bukkit.Location;
45
import org.bukkit.block.Biome;
56
import org.bukkit.event.Event;
@@ -17,6 +18,7 @@
1718
import ch.njol.skript.lang.SkriptParser.ParseResult;
1819
import ch.njol.skript.util.Direction;
1920
import ch.njol.util.Kleenean;
21+
import org.skriptlang.skript.lang.simplification.SimplifiedLiteral;
2022

2123
/**
2224
* @author Peter Güttinger
@@ -74,12 +76,18 @@ public Class<? extends Biome> getReturnType() {
7476
return Biome.class;
7577
}
7678

79+
@Override
80+
public Expression<? extends Biome> simplify() {
81+
if (getExpr() instanceof Literal<? extends Location>)
82+
return SimplifiedLiteral.fromExpression(this);
83+
return this;
84+
}
85+
7786
@Override
7887
public String toString(@Nullable Event event, boolean debug) {
7988
return "the biome at " + getExpr().toString(event, debug);
8089
}
8190

82-
@SuppressWarnings("unchecked")
8391
@Override
8492
public boolean setTime(int time) {
8593
super.setTime(time, getExpr());

src/main/java/ch/njol/skript/expressions/ExprCharacterFromCodepoint.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import ch.njol.skript.doc.Name;
77
import ch.njol.skript.doc.Since;
88
import ch.njol.skript.expressions.base.SimplePropertyExpression;
9+
import ch.njol.skript.lang.Expression;
910
import ch.njol.skript.lang.ExpressionType;
11+
import ch.njol.skript.lang.Literal;
1012
import org.bukkit.event.Event;
1113
import org.jetbrains.annotations.Nullable;
14+
import org.skriptlang.skript.lang.simplification.SimplifiedLiteral;
1215

1316
@Name("Character from Codepoint")
1417
@Description("Returns the character at the specified codepoint")
@@ -43,6 +46,13 @@ public Class<? extends String> getReturnType() {
4346
return String.class;
4447
}
4548

49+
@Override
50+
public Expression<? extends String> simplify() {
51+
if (getExpr() instanceof Literal<? extends Integer>)
52+
return SimplifiedLiteral.fromExpression(this);
53+
return this;
54+
}
55+
4656
@Override
4757
public String toString(@Nullable Event event, boolean debug) {
4858
return "character at codepoint " + getExpr().toString(event, debug);

src/main/java/ch/njol/skript/expressions/ExprCharacters.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import ch.njol.skript.doc.Since;
88
import ch.njol.skript.lang.Expression;
99
import ch.njol.skript.lang.ExpressionType;
10+
import ch.njol.skript.lang.Literal;
1011
import ch.njol.skript.lang.SkriptParser.ParseResult;
1112
import ch.njol.skript.lang.util.SimpleExpression;
1213
import ch.njol.util.Kleenean;
1314
import org.apache.commons.lang.ArrayUtils;
1415
import org.bukkit.event.Event;
1516
import org.jetbrains.annotations.Nullable;
17+
import org.skriptlang.skript.lang.simplification.SimplifiedLiteral;
1618

1719
@Name("Characters Between")
1820
@Description({
@@ -95,6 +97,13 @@ public Class<? extends String> getReturnType() {
9597
return String.class;
9698
}
9799

100+
@Override
101+
public Expression<? extends String> simplify() {
102+
if (start instanceof Literal<?> && end instanceof Literal<?>)
103+
return SimplifiedLiteral.fromExpression(this);
104+
return this;
105+
}
106+
98107
@Override
99108
public String toString(@Nullable Event event, boolean debug) {
100109
return "all the " + (isAlphanumeric ? "alphanumeric " : "") + "characters between " + start.toString(event, debug) + " and " + end.toString(event, debug);

src/main/java/ch/njol/skript/expressions/ExprCodepoint.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import ch.njol.skript.doc.Name;
66
import ch.njol.skript.doc.Since;
77
import ch.njol.skript.expressions.base.SimplePropertyExpression;
8+
import ch.njol.skript.lang.Expression;
9+
import ch.njol.skript.lang.Literal;
810
import org.jetbrains.annotations.Nullable;
11+
import org.skriptlang.skript.lang.simplification.SimplifiedLiteral;
912

1013
@Name("Character Codepoint")
1114
@Description("Returns the Unicode codepoint of a character")
@@ -48,6 +51,13 @@ public Class<? extends Integer> getReturnType() {
4851
return Integer.class;
4952
}
5053

54+
@Override
55+
public Expression<? extends Integer> simplify() {
56+
if (getExpr() instanceof Literal<? extends String>)
57+
return SimplifiedLiteral.fromExpression(this);
58+
return this;
59+
}
60+
5161
@Override
5262
protected String getPropertyName() {
5363
return "codepoint";

src/main/java/ch/njol/skript/expressions/ExprCoordinate.java

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package ch.njol.skript.expressions;
22

3-
import org.bukkit.Location;
4-
import org.bukkit.event.Event;
5-
import org.jetbrains.annotations.Nullable;
6-
7-
import ch.njol.skript.classes.Changer.ChangeMode;
8-
import ch.njol.skript.classes.Changer.ChangerUtils;
3+
import ch.njol.skript.classes.Changer;
94
import ch.njol.skript.doc.Description;
105
import ch.njol.skript.doc.Examples;
116
import ch.njol.skript.doc.Name;
127
import ch.njol.skript.doc.Since;
138
import ch.njol.skript.expressions.base.SimplePropertyExpression;
149
import ch.njol.skript.lang.Expression;
10+
import ch.njol.skript.lang.Literal;
1511
import ch.njol.skript.lang.SkriptParser.ParseResult;
1612
import ch.njol.util.Kleenean;
13+
import org.bukkit.Location;
14+
import org.bukkit.event.Event;
15+
import org.jetbrains.annotations.Nullable;
16+
import org.skriptlang.skript.lang.simplification.SimplifiedLiteral;
1717

1818
/**
1919
* @author Peter Güttinger
@@ -44,27 +44,17 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
4444
public Number convert(final Location l) {
4545
return axis == 0 ? l.getX() : axis == 1 ? l.getY() : l.getZ();
4646
}
47-
48-
@Override
49-
protected String getPropertyName() {
50-
return "the " + axes[axis] + "-coordinate";
51-
}
52-
53-
@Override
54-
public Class<? extends Number> getReturnType() {
55-
return Number.class;
56-
}
57-
47+
5848
@Override
5949
@Nullable
60-
public Class<?>[] acceptChange(final ChangeMode mode) {
61-
if ((mode == ChangeMode.SET || mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) && getExpr().isSingle() && ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, Location.class))
50+
public Class<?>[] acceptChange(final Changer.ChangeMode mode) {
51+
if ((mode == Changer.ChangeMode.SET || mode == Changer.ChangeMode.ADD || mode == Changer.ChangeMode.REMOVE) && getExpr().isSingle() && Changer.ChangerUtils.acceptsChange(getExpr(), Changer.ChangeMode.SET, Location.class))
6252
return new Class[] {Number.class};
6353
return null;
6454
}
65-
55+
6656
@Override
67-
public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) throws UnsupportedOperationException {
57+
public void change(final Event e, final @Nullable Object[] delta, final Changer.ChangeMode mode) throws UnsupportedOperationException {
6858
assert delta != null;
6959
final Location l = getExpr().getSingle(e);
7060
if (l == null)
@@ -82,7 +72,7 @@ public void change(final Event e, final @Nullable Object[] delta, final ChangeMo
8272
} else {
8373
l.setZ(l.getZ() + n);
8474
}
85-
getExpr().change(e, new Location[] {l}, ChangeMode.SET);
75+
getExpr().change(e, new Location[] {l}, Changer.ChangeMode.SET);
8676
break;
8777
case SET:
8878
if (axis == 0) {
@@ -92,13 +82,32 @@ public void change(final Event e, final @Nullable Object[] delta, final ChangeMo
9282
} else {
9383
l.setZ(n);
9484
}
95-
getExpr().change(e, new Location[] {l}, ChangeMode.SET);
85+
getExpr().change(e, new Location[] {l}, Changer.ChangeMode.SET);
9686
break;
9787
case DELETE:
9888
case REMOVE_ALL:
9989
case RESET:
10090
assert false;
10191
}
10292
}
93+
94+
@Override
95+
public Class<? extends Number> getReturnType() {
96+
return Number.class;
97+
}
98+
99+
@Override
100+
public Expression<? extends Number> simplify() {
101+
// implemented for future use with pure functions
102+
if (getExpr() instanceof Literal<? extends Location>)
103+
return SimplifiedLiteral.fromExpression(this);
104+
return this;
105+
}
106+
107+
@Override
108+
protected String getPropertyName() {
109+
return "the " + axes[axis] + "-coordinate";
110+
}
111+
103112

104113
}

0 commit comments

Comments
 (0)