Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/main/java/ch/njol/skript/sections/SecWhile.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.WeakHashMap;

@Name("While Loop")
@Description("While Loop sections are loops that will just keep repeating as long as a condition is met.")
Expand Down Expand Up @@ -46,7 +49,7 @@ public class SecWhile extends LoopSection {
private TriggerItem actualNext;

private boolean doWhile;
private boolean ranDoWhile = false;
private final Set<Event> ranDoWhile = Collections.newSetFromMap(new WeakHashMap<>());

@Override
public boolean init(Expression<?>[] exprs,
Expand All @@ -70,8 +73,7 @@ public boolean init(Expression<?>[] exprs,
@Nullable
@Override
protected TriggerItem walk(Event event) {
if ((doWhile && !ranDoWhile) || condition.check(event)) {
ranDoWhile = true;
if ((doWhile && ranDoWhile.add(event)) || condition.check(event)) {
currentLoopCounter.put(event, (currentLoopCounter.getOrDefault(event, 0L)) + 1);
return walk(event, true);
} else {
Expand Down Expand Up @@ -104,7 +106,7 @@ public String toString(@Nullable Event event, boolean debug) {

@Override
public void exit(Event event) {
ranDoWhile = false;
ranDoWhile.remove(event);
super.exit(event);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local function test(id: integer):
do while true is true:
add {_id} to {-%script%::*}
wait 1 tick
exit

test "concurrent do while loops":
test(1)
test(2)
assert size of {-%script%::*} is 2 with "Didn't run multiple do-while loops concurrently"