Skip to content

Commit 05b8cde

Browse files
committed
Fix local variable issues with EffRunSection
Update docs
1 parent 14c106c commit 05b8cde

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

docs/advanced/reflection/sections.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ the local variables from outside the section.
2828
{% code-tabs %}
2929
{% code-tabs-item title="Syntax" %}
3030
```text
31-
run section %section% [(1¦async)] [with [arguments] %-objects%] [and store [the] result in %-objects%] [(2¦and wait)]
31+
run section %section% [(1¦sync|2¦async)] [with [arguments] %-objects%] [and store [the] result in %-objects%] [(2¦and wait)]
3232
```
3333
{% endcode-tabs-item %}
3434
{% endcode-tabs %}
3535

3636
This effect will run the given section.
37-
If you run the section async, you can choose to wait for it to be done or not, by appending `and wait` or not.
37+
If you run the section (a)sync, you can choose to wait for it to be done or not, by appending `and wait` or not.
3838
{% hint style="info" %}
3939
Note that you can't get output from running an async section without waiting for it to return.
4040
{% endhint %}

src/main/java/com/btk5h/skriptmirror/skript/reflect/sections/EffRunSection.java

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,47 +67,42 @@ else if ((parseResult.mark & 0b0010) != 0)
6767

6868
@Override
6969
protected TriggerItem walk(Event e) {
70-
if (!runsAsync.isUnknown()) {
71-
Section section = sectionExpression.getSingle(e);
70+
if (runsAsync.isUnknown())
71+
return super.walk(e);
7272

73-
Object[][] args = getArgs(e);
73+
Section section = sectionExpression.getSingle(e);
7474

75-
Object localVars = SkriptReflection.removeLocals(e);
75+
if (section == null)
76+
return getNext();
7677

77-
boolean ranAsync = !Bukkit.isPrimaryThread();
78+
Object[][] args = getArgs(e);
7879

79-
if (section != null) {
80-
Runnable runnable = () -> {
81-
SkriptReflection.putLocals(localVars, e);
80+
// Whether the trigger needs to be continued abnormally
81+
boolean needsContinue = shouldWait && getNext() != null;
8282

83-
section.run(e, args);
84-
storeResult(section, e);
83+
// If the trigger needs abnormal continuation, remove locals and store whether this trigger was running async.
84+
Object localVars = needsContinue ? SkriptReflection.removeLocals(e) : null;
85+
boolean ranAsync = !Bukkit.isPrimaryThread();
8586

87+
Runnable runSection = () -> {
88+
section.run(e, args);
89+
storeResult(section, e);
8690

87-
if (shouldWait && getNext() != null) {
88-
Runnable continuation = () -> {
89-
TriggerItem.walk(getNext(), e);
90-
SkriptReflection.removeLocals(e);
91-
};
91+
if (needsContinue) {
92+
Runnable continuation = () -> {
93+
SkriptReflection.putLocals(localVars, e);
9294

93-
if (ranAsync)
94-
Bukkit.getScheduler().runTaskAsynchronously(SkriptMirror.getInstance(), continuation);
95-
else
96-
Bukkit.getScheduler().runTask(SkriptMirror.getInstance(), continuation);
97-
}
95+
TriggerItem.walk(getNext(), e);
96+
SkriptReflection.removeLocals(e);
9897
};
9998

100-
if (runsAsync.isTrue())
101-
Bukkit.getScheduler().runTaskAsynchronously(SkriptMirror.getInstance(), runnable);
102-
else
103-
Bukkit.getScheduler().runTask(SkriptMirror.getInstance(), runnable);
104-
} else {
105-
return getNext();
99+
runTask(continuation, ranAsync);
106100
}
107-
return shouldWait ? null : getNext();
108-
} else {
109-
return super.walk(e);
110-
}
101+
};
102+
103+
runTask(runSection, runsAsync.isTrue());
104+
105+
return needsContinue ? null : getNext();
111106
}
112107

113108
@Override
@@ -137,4 +132,11 @@ private void storeResult(Section section, Event event) {
137132
variableStorage.change(event, output, Changer.ChangeMode.SET);
138133
}
139134

135+
private void runTask(Runnable task, boolean async) {
136+
if (async)
137+
Bukkit.getScheduler().runTaskAsynchronously(SkriptMirror.getInstance(), task);
138+
else
139+
Bukkit.getScheduler().runTask(SkriptMirror.getInstance(), task);
140+
}
141+
140142
}

0 commit comments

Comments
 (0)