Skip to content

Commit

Permalink
Fix loop behaviour from super call. (#7537)
Browse files Browse the repository at this point in the history
* Fix loop behaviour from super call.

* Update src/test/skript/tests/syntaxes/expressions/ExprQueue.sk
  • Loading branch information
Moderocky authored Jan 26, 2025
1 parent 54a5ba1 commit 5c2ce78
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/sections/SecFor.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public boolean init(Expression<?>[] exprs,
}
//</editor-fold>
this.loadOptionalCode(sectionNode);
super.setNext(this);
this.setInternalNext(this);
return true;
}

Expand Down
17 changes: 12 additions & 5 deletions src/main/java/ch/njol/skript/sections/SecLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public boolean init(Expression<?>[] exprs,

guaranteedToLoop = guaranteedToLoop(expression);
loadOptionalCode(sectionNode);
super.setNext(this);
this.setInternalNext(this);

return true;
}
Expand All @@ -130,11 +130,11 @@ public boolean init(Expression<?>[] exprs,
if (iter == null) {
if (iterableSingle) {
Object value = expression.getSingle(event);
if (value instanceof Iterable<?> iterable) {
iter = iterable.iterator();
// Guaranteed to be ordered so we try it first
} else if (value instanceof Container<?> container) {
if (value instanceof Container<?> container) {
// Container may have special behaviour over regular iterator
iter = container.containerIterator();
} else if (value instanceof Iterable<?> iterable) {
iter = iterable.iterator();
} else {
iter = Collections.singleton(value).iterator();
}
Expand Down Expand Up @@ -210,6 +210,13 @@ public SecLoop setNext(@Nullable TriggerItem next) {
return this;
}

/**
* @see LoopSection#setNext(TriggerItem)
*/
protected void setInternalNext(TriggerItem item) {
super.setNext(item);
}

@Nullable
@Override
public TriggerItem getActualNext() {
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/org/skriptlang/skript/lang/util/SkriptQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,21 @@ public Object[] removeRangeSafely(int fromIndex, int toIndex) {

@Override
public Iterator<Object> containerIterator() {
return this.iterator();
return new Iterator<>() {
@Override
public boolean hasNext() {
return !SkriptQueue.this.isEmpty();
}

@Override
public Object next() {
return SkriptQueue.this.pollFirst();
}

@Override
public void remove() {
}
};
}

}
17 changes: 17 additions & 0 deletions src/test/skript/tests/regressions/7536-for-loop-ending.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using for each loops

test "for each loops ending (start)":
clear {7536 For Each::*}
for each {_word} in ("test", "test2"):
add {_word} to {7536 For Each::*}
for each {_word 2} in ("example", "example2"):
add {_word 2} to {7536 For Each::*}

assert the size of {7536 For Each::*} is 6 with "Wrong number of variables: %{7536 For Each::*}%"

# Need to make sure that trigger didn't just die
test "for each loops ending (result)":
assert the size of {7536 For Each::*} is 6 with "Wrong number of variables: %{7536 For Each::*}%"
assert {7536 For Each::*} is ("test", "example", "example2", "test2", "example", "example2") with "Wrong loop order: %{7536 For Each::*}%"

delete {7536 For Each::*}
4 changes: 4 additions & 0 deletions src/test/skript/tests/syntaxes/sections/SecFor.sk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test "for section":
delete {_value}

for {_key}, {_value} in {_list::*}:
set {_key} to {_key} parsed as integer
assert {_key} is greater than 0 with "Expected key > 0, found %{_key}%"
assert {_key} is less than 4 with "Expected key < 4, found %{_key}%"
assert {_value} is greater than 0 with "Expected value > 0, found %{_value}%"
Expand All @@ -27,6 +28,7 @@ test "for section":


for key {_key} and value {_value} in {_list::*}:
set {_key} to {_key} parsed as integer
assert {_key} is greater than 0 with "Expected key > 0, found %{_key}%"
assert {_key} is less than 4 with "Expected key < 4, found %{_key}%"
assert {_value} is greater than 0 with "Expected value > 0, found %{_value}%"
Expand All @@ -38,6 +40,7 @@ test "for section":
delete {_value}

for {_key} and {_value} in {_list::*}:
set {_key} to {_key} parsed as integer
assert {_key} is greater than 0 with "Expected key > 0, found %{_key}%"
assert {_key} is less than 4 with "Expected key < 4, found %{_key}%"
assert {_value} is greater than 0 with "Expected value > 0, found %{_value}%"
Expand All @@ -50,6 +53,7 @@ test "for section":

# 'loop' syntax alternative
loop {_key} and {_value} in {_list::*}:
set {_key} to {_key} parsed as integer
assert {_key} is greater than 0 with "Expected key > 0, found %{_key}%"
assert {_key} is less than 4 with "Expected key < 4, found %{_key}%"
assert {_value} is greater than 0 with "Expected value > 0, found %{_value}%"
Expand Down

0 comments on commit 5c2ce78

Please sign in to comment.