@@ -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