23
23
import ch .njol .skript .doc .Examples ;
24
24
import ch .njol .skript .doc .Name ;
25
25
import ch .njol .skript .doc .Since ;
26
- import ch .njol .skript .lang .Effect ;
27
- import ch .njol .skript .lang .Expression ;
28
- import ch .njol .skript .lang .Literal ;
29
- import ch .njol .skript .lang .LoopSection ;
26
+ import ch .njol .skript .lang .*;
30
27
import ch .njol .skript .lang .SkriptParser .ParseResult ;
31
- import ch .njol .skript .lang .TriggerItem ;
32
28
import ch .njol .util .Kleenean ;
33
29
import ch .njol .util .StringUtils ;
34
30
import org .bukkit .event .Event ;
35
- import org .eclipse .jdt .annotation .Nullable ;
31
+ import org .jetbrains .annotations .Nullable ;
32
+ import org .jetbrains .annotations .UnknownNullability ;
36
33
34
+ import java .util .ArrayList ;
37
35
import java .util .List ;
38
36
39
37
@ Name ("Continue" )
@@ -65,35 +63,56 @@ public class EffContinue extends Effect {
65
63
);
66
64
}
67
65
68
- @ SuppressWarnings ("NotNullFieldNotInitialized" )
69
- private LoopSection loop ;
70
- @ SuppressWarnings ("NotNullFieldNotInitialized" )
71
- private List <LoopSection > innerLoops ;
66
+ private @ UnknownNullability LoopSection loop ;
67
+ private @ UnknownNullability List <LoopSection > innerLoops ;
68
+ private int breakLevels ;
72
69
73
70
@ Override
74
71
@ SuppressWarnings ("unchecked" )
75
72
public boolean init (Expression <?>[] exprs , int matchedPattern , Kleenean isDelayed , ParseResult parseResult ) {
76
- List <LoopSection > currentLoops = getParser ().getCurrentSections (LoopSection .class );
73
+ List <TriggerSection > sections = getParser ().getCurrentSections ();
74
+ innerLoops = new ArrayList <>();
75
+ int loopLevels = 0 ;
76
+ LoopSection lastLoop = null ;
77
77
78
- int size = currentLoops . size ();
79
- if (size == 0 ) {
80
- Skript .error ("The ' continue' effect may only be used in loops " );
78
+ int level = matchedPattern == 0 ? - 1 : (( Literal < Integer >) exprs [ 0 ]). getSingle ();
79
+ if (matchedPattern != 0 && level < 1 ) {
80
+ Skript .error ("Can't continue the " + StringUtils . fancyOrderNumber ( level ) + " loop " );
81
81
return false ;
82
82
}
83
83
84
- int level = matchedPattern == 0 ? size : ((Literal <Integer >) exprs [0 ]).getSingle ();
85
- if (level < 1 ) {
86
- Skript .error ("Can't continue the " + StringUtils .fancyOrderNumber (level ) + " loop" );
84
+ for (TriggerSection section : sections ) {
85
+ if (loop != null )
86
+ breakLevels ++;
87
+ if (!(section instanceof LoopSection loopSection ))
88
+ continue ;
89
+ loopLevels ++;
90
+ if (level == -1 ) {
91
+ lastLoop = loopSection ;
92
+ } else if (loopLevels == level ) {
93
+ loop = loopSection ;
94
+ breakLevels ++;
95
+ } else if (loopLevels > level ) {
96
+ innerLoops .add (loopSection );
97
+ }
98
+ }
99
+
100
+ if (loopLevels == 0 ) {
101
+ Skript .error ("The 'continue' effect may only be used in loops" );
87
102
return false ;
88
103
}
89
- if (level > size ) {
104
+
105
+ if (level > loopLevels ) {
90
106
Skript .error ("Can't continue the " + StringUtils .fancyOrderNumber (level ) + " loop as there " +
91
- (size == 1 ? "is only 1 loop" : "are only " + size + " loops" ) + " present" );
107
+ (loopLevels == 1 ? "is only 1 loop" : "are only " + loopLevels + " loops" ) + " present" );
92
108
return false ;
93
109
}
94
110
95
- loop = currentLoops .get (level - 1 );
96
- innerLoops = currentLoops .subList (level , size );
111
+ if (level == -1 ) {
112
+ loop = lastLoop ;
113
+ breakLevels ++;
114
+ }
115
+
97
116
return true ;
98
117
}
99
118
@@ -110,9 +129,14 @@ protected TriggerItem walk(Event event) {
110
129
return loop ;
111
130
}
112
131
132
+ @ Override
133
+ public @ Nullable ExecutionIntent executionIntent () {
134
+ return ExecutionIntent .stopSections (breakLevels );
135
+ }
136
+
113
137
@ Override
114
138
public String toString (@ Nullable Event event , boolean debug ) {
115
- return "continue" ;
139
+ return "continue" + ( loop == null ? "" : " the " + StringUtils . fancyOrderNumber ( innerLoops . size () + 1 ) + " loop" ) ;
116
140
}
117
141
118
142
}
0 commit comments