Skip to content

Commit 3983fa7

Browse files
authored
Runtime error for when EffSort aborts due to null value (#8206)
* Swap reset and delete errors in EffChange (#8177) swap reset and delete errors * Fix improper grammar in update block syntax (#8072) Co-authored-by: sovdee <[email protected]> * Fix Incorrect Variable Change Queue Processing (#8182) Fix incorrect change processing order * Change registerExpression() parameter name (#8180) change parameter name * Move EvtRealTime to main thread (#8185) * Fix type-aware function parsing for functions with only optional arguments (#8189) Account for functions with all optional parameters * Properly parse exprsecs in function calls (#8199) modify section context when parsing functions * add runtime error when EffSort aborts due to null values. * catch runtime errors
1 parent 6070e7c commit 3983fa7

File tree

11 files changed

+62
-32
lines changed

11 files changed

+62
-32
lines changed

src/main/java/ch/njol/skript/Skript.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,19 +1588,19 @@ public static <E extends Section> void registerSection(Class<E> sectionClass, St
15881588
/**
15891589
* Registers an expression.
15901590
*
1591-
* @param expressionType The expression's class
1591+
* @param expressionClass The expression's class
15921592
* @param returnType The superclass of all values returned by the expression
15931593
* @param type The expression's {@link ExpressionType type}. This is used to determine in which order to try to parse expressions.
15941594
* @param patterns Skript patterns that match this expression
15951595
* @throws IllegalArgumentException if returnType is not a normal class
15961596
*/
15971597
public static <E extends Expression<T>, T> void registerExpression(
1598-
Class<E> expressionType, Class<T> returnType, ExpressionType type, String... patterns
1598+
Class<E> expressionClass, Class<T> returnType, ExpressionType type, String... patterns
15991599
) throws IllegalArgumentException {
16001600
checkAcceptRegistrations();
1601-
skript.syntaxRegistry().register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(expressionType, returnType)
1601+
skript.syntaxRegistry().register(SyntaxRegistry.EXPRESSION, SyntaxInfo.Expression.builder(expressionClass, returnType)
16021602
.priority(type.priority())
1603-
.origin(getSyntaxOrigin(expressionType))
1603+
.origin(getSyntaxOrigin(expressionClass))
16041604
.addPatterns(patterns)
16051605
.build()
16061606
);

src/main/java/ch/njol/skript/effects/EffBlockUpdate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class EffBlockUpdate extends Effect {
3232

3333
static {
3434
Skript.registerEffect(EffBlockUpdate.class,
35-
"update %blocks% (as|to be) %blockdata% [physics:without [neighbo[u]r[ing]|adjacent] [physic[s]] update[s]]");
35+
"update %blocks% (as|to be) %blockdata% [physics:without [neighbo[u]r[ing]|adjacent] [physics] update[s]]");
3636
}
3737

3838
private boolean physics;

src/main/java/ch/njol/skript/effects/EffChange.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
145145
}
146146
yield what + " can't have anything removed from it";
147147
}
148-
case DELETE -> {
148+
case RESET -> {
149149
String error = what + " can't be reset";
150150
if (changed.acceptChange(ChangeMode.DELETE) != null) {
151151
error += ". However, it can be deleted which might result in the desired effect";
152152
}
153153
yield error;
154154
}
155-
case RESET -> {
155+
case DELETE -> {
156156
String error = what + " can't be deleted";
157157
if (changed.acceptChange(ChangeMode.RESET) != null) {
158158
error += ". However, it can be reset which might result in the desired effect";

src/main/java/ch/njol/skript/effects/EffSort.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ protected void execute(Event event) {
9595
currentIndex = keyedValue.key();
9696
currentValue = keyedValue.value();
9797
Object mappedValue = mappingExpr.getSingle(event);
98-
if (mappedValue == null)
98+
if (mappedValue == null) {
99+
error("Sorting failed because Skript cannot sort null values. "
100+
+ "The mapping expression '" + mappingExpr.toString(event, false)
101+
+ "' returned a null value when given the input '"+currentValue+"'.");
99102
return;
103+
}
100104
mappedValues.add(new MappedValue(currentValue, mappedValue));
101105
}
102106
try {

src/main/java/ch/njol/skript/events/EvtRealTime.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ch.njol.skript.lang.SkriptEvent;
77
import ch.njol.skript.lang.SkriptParser.ParseResult;
88
import ch.njol.skript.util.Time;
9+
import org.bukkit.Bukkit;
910
import org.bukkit.event.Event;
1011
import org.bukkit.event.HandlerList;
1112
import org.jetbrains.annotations.NotNull;
@@ -82,8 +83,9 @@ public void run() {
8283
@Override
8384
public void unload() {
8485
unloaded = true;
85-
for (TimerTask task : timerTasks)
86+
for (TimerTask task : timerTasks) {
8687
task.cancel();
88+
}
8789
TIMER.purge();
8890
}
8991

@@ -94,14 +96,18 @@ public boolean check(Event event) {
9496

9597
private void execute() {
9698
// Ensure this element wasn't unloaded
97-
if (unloaded)
99+
if (unloaded) {
98100
return;
99-
RealTimeEvent event = new RealTimeEvent();
100-
SkriptEventHandler.logEventStart(event);
101-
SkriptEventHandler.logTriggerStart(trigger);
102-
trigger.execute(event);
103-
SkriptEventHandler.logTriggerEnd(trigger);
104-
SkriptEventHandler.logEventEnd();
101+
}
102+
103+
Bukkit.getScheduler().scheduleSyncDelayedTask(Skript.getInstance(), () -> {
104+
RealTimeEvent event = new RealTimeEvent();
105+
SkriptEventHandler.logEventStart(event);
106+
SkriptEventHandler.logTriggerStart(trigger);
107+
trigger.execute(event);
108+
SkriptEventHandler.logTriggerEnd(trigger);
109+
SkriptEventHandler.logEventEnd();
110+
});
105111
}
106112

107113
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ record SignatureData(ClassInfo<?> classInfo, boolean plural) { }
12341234
boolean trySingle = false;
12351235
boolean trySinglePlural = false;
12361236
for (var signature : signatures) {
1237-
trySingle |= signature.getMinParameters() == 1 || signature.getMaxParameters() == 1;
1237+
trySingle |= signature.getMinParameters() <= 1 || signature.getMaxParameters() == 1;
12381238
trySinglePlural |= trySingle && !signature.getParameter(0).isSingleValue();
12391239
for (int i = 0; i < signature.getMaxParameters(); i++) {
12401240
if (signatureDatas.size() <= i) {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ public abstract class Statement extends TriggerItem implements SyntaxElement {
3030

3131
public static @Nullable Statement parse(String input, @Nullable String defaultError, @Nullable SectionNode node, @Nullable List<TriggerItem> items) {
3232
try (ParseLogHandler log = SkriptLogger.startParseLogHandler()) {
33-
EffFunctionCall functionCall = EffFunctionCall.parse(input);
33+
Section.SectionContext sectionContext = ParserInstance.get().getData(Section.SectionContext.class);
34+
EffFunctionCall functionCall;
35+
if (node != null) {
36+
functionCall = sectionContext.modify(node, items, () -> EffFunctionCall.parse(input));
37+
} else {
38+
functionCall = EffFunctionCall.parse(input);
39+
}
3440
if (functionCall != null) {
3541
log.printLog();
3642
return functionCall;
@@ -49,7 +55,6 @@ public abstract class Statement extends TriggerItem implements SyntaxElement {
4955

5056
Statement statement;
5157
var iterator = Skript.instance().syntaxRegistry().syntaxes(org.skriptlang.skript.registration.SyntaxRegistry.STATEMENT).iterator();
52-
Section.SectionContext sectionContext = ParserInstance.get().getData(Section.SectionContext.class);
5358
if (node != null) {
5459
var wrappedIterator = new Iterator<>() {
5560
@Override

src/main/java/ch/njol/skript/variables/Variables.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,14 @@ public static void setVariable(String name, @Nullable Object value, @Nullable Ev
602602
* @param value the value, or {@code null} to delete the variable.
603603
*/
604604
static void setVariable(String name, @Nullable Object value) {
605-
boolean gotLock = variablesLock.writeLock().tryLock();
606-
if (gotLock) {
605+
if (variablesLock.writeLock().tryLock()) {
607606
try {
608-
// Set the variable
607+
if (!changeQueue.isEmpty()) { // Process older, queued changes if available
608+
processChangeQueue();
609+
}
610+
// Process and save requested change
609611
variables.setVariable(name, value);
610-
// ..., save the variable change
611612
saveVariableChange(name, value);
612-
// ..., and process all previously queued changes
613-
processChangeQueue();
614613
} finally {
615614
variablesLock.writeLock().unlock();
616615
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local function test(x: number = 3, y: number = 5) returns number:
2+
return {_x} + {_y}
3+
4+
test "functions behave wrong with all default args":
5+
assert test() is 8 with "no args did not use both default values"
6+
assert test(1) is 6 with "only one arg did not use the second default value"
7+
assert test(1, 2) is 3 with "both args still used a default value"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local function f(x: worldborder):
2+
assert worldborder warning time of {_x} is 1 second with "Failed to modify worldborder before calling function. Check Statement.parse() for sectioncontext handling."
3+
4+
test "load expr secs in functions":
5+
f(a worldborder):
6+
set worldborder warning time to 20 ticks

0 commit comments

Comments
 (0)