Skip to content

Commit fc14b86

Browse files
committed
Add ExecutionIntent to EffContinue, and fix its toString
1 parent 18591ad commit fc14b86

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

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

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@
2323
import ch.njol.skript.doc.Examples;
2424
import ch.njol.skript.doc.Name;
2525
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.*;
3027
import ch.njol.skript.lang.SkriptParser.ParseResult;
31-
import ch.njol.skript.lang.TriggerItem;
3228
import ch.njol.util.Kleenean;
3329
import ch.njol.util.StringUtils;
3430
import org.bukkit.event.Event;
35-
import org.eclipse.jdt.annotation.Nullable;
31+
import org.jetbrains.annotations.Nullable;
32+
import org.jetbrains.annotations.UnknownNullability;
3633

34+
import java.util.ArrayList;
3735
import java.util.List;
3836

3937
@Name("Continue")
@@ -65,35 +63,56 @@ public class EffContinue extends Effect {
6563
);
6664
}
6765

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;
7269

7370
@Override
7471
@SuppressWarnings("unchecked")
7572
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;
7777

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");
8181
return false;
8282
}
8383

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");
87102
return false;
88103
}
89-
if (level > size) {
104+
105+
if (level > loopLevels) {
90106
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");
92108
return false;
93109
}
94110

95-
loop = currentLoops.get(level - 1);
96-
innerLoops = currentLoops.subList(level, size);
111+
if (level == -1) {
112+
loop = lastLoop;
113+
breakLevels++;
114+
}
115+
97116
return true;
98117
}
99118

@@ -110,9 +129,14 @@ protected TriggerItem walk(Event event) {
110129
return loop;
111130
}
112131

132+
@Override
133+
public @Nullable ExecutionIntent executionIntent() {
134+
return ExecutionIntent.stopSections(breakLevels);
135+
}
136+
113137
@Override
114138
public String toString(@Nullable Event event, boolean debug) {
115-
return "continue";
139+
return "continue" + (loop == null ? "" : " the " + StringUtils.fancyOrderNumber(innerLoops.size() + 1) + " loop");
116140
}
117141

118142
}

src/test/skript/tests/syntaxes/effects/EffContinue.sk

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ test "continue effect":
44
continue
55
assert loop-value is not 5 with "continue in loop failed"
66
set {_i} to 0
7+
78
while {_i} is smaller than 10:
89
increase {_i} by 1
910
if {_i} is equal to 5:
1011
continue
1112
assert {_i} is not 5 with "continue in while failed"
13+
1214
loop integers from 1 to 10:
1315
continue this loop if loop-value is 5
14-
assert loop-value is not 5 with "leveled continue failed"
16+
assert loop-value is not 5 with "leveled continue failed #1"
1517
loop integers from 11 to 20:
1618
continue 2nd loop if loop-value-2 is 15
17-
assert loop-value-2 is not 15 with "leveled continue failed"
19+
assert loop-value-2 is not 15 with "leveled continue failed #2"
1820
continue 1st loop if loop-value-1 is 10
19-
assert loop-value is not 10 with "leveled continue failed"
21+
assert loop-value is not 10 with "leveled continue failed #3"

0 commit comments

Comments
 (0)