Skip to content

Commit

Permalink
Fix changes from upstream.
Browse files Browse the repository at this point in the history
  • Loading branch information
Moderocky committed Sep 3, 2024
1 parent ba3be6f commit 36c1e09
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 deletions.
83 changes: 42 additions & 41 deletions src/main/java/ch/njol/skript/sections/SecFor.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* This file is part of Skript.
* <p>
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
* <p>
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.sections;
Expand All @@ -31,35 +31,36 @@
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.lang.Variable;
import ch.njol.skript.lang.util.ContainerExpression;
import ch.njol.skript.registrations.Feature;
import ch.njol.skript.util.Container;
import ch.njol.skript.util.Container.ContainerType;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.experiment.Feature;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Name("For Each Loop (Experimental)")
@Description({
"A specialised loop section run for each element in a list.",
"Unlike the basic loop, this is designed for extracting the key & value from pairs.",
"The loop element's key/index and value can be stored in a variable for convenience."
})
@Description("""
A specialised loop section run for each element in a list.
Unlike the basic loop, this is designed for extracting the key & value from pairs.
The loop element's key/index and value can be stored in a variable for convenience.
When looping a simple (non-indexed) set of values, e.g. all players, the index will be the loop counter number."""
)
@Examples({
"for each {_player} in players:",
"\tsend \"Hello %{_player}%!\" to {_player}",
"",
"for each {_item} in {list of items::*}:",
"loop {_item} in {list of items::*}:",
"\tbroadcast {_item}'s name",
"",
"for each key {_index} in {list of items::*}:",
"\tbroadcast {_index}",
"",
"for each key {_index} and value {_value} in {list of items::*}:",
"loop key {_index} and value {_value} in {list of items::*}:",
"\tbroadcast \"%{_index}% = %{_value}%\"",
"",
"for each {_index} = {_value} in {my list::*}:",
Expand All @@ -70,10 +71,10 @@ public class SecFor extends SecLoop {

static {
Skript.registerSection(SecFor.class,
"for [each] [value] %~object% in %objects%",
"for [each] (key|index) %~object% in %objects%",
"for [each] [key|index] %~object% (=|and) [value] %~object% in %objects%"
);
"(for [each]|loop) [value] %~object% in %objects%",
"(for [each]|loop) (key|index) %~object% in %objects%",
"(for [each]|loop) [key|index] %~object% (=|and) [value] %~object% in %objects%"
);
}

private @Nullable Expression<?> keyStore, valueStore;
Expand All @@ -86,7 +87,7 @@ public boolean init(Expression<?>[] exprs,
ParseResult parseResult,
SectionNode sectionNode,
List<TriggerItem> triggerItems) {
if (!this.hasExperiment(Feature.FOR_EACH_LOOPS))
if (!this.getParser().hasExperiment(Feature.FOR_EACH_LOOPS))
return false;
//<editor-fold desc="Set the key/value expressions based on the pattern" defaultstate="collapsed">
switch (matchedPattern) {
Expand Down Expand Up @@ -120,7 +121,8 @@ public boolean init(Expression<?>[] exprs,
if (Container.class.isAssignableFrom(expression.getReturnType())) {
ContainerType type = expression.getReturnType().getAnnotation(ContainerType.class);
if (type == null)
throw new SkriptAPIException(expression.getReturnType().getName() + " implements Container but is missing the required @ContainerType annotation");
throw new SkriptAPIException(expression.getReturnType()
.getName() + " implements Container but is missing the required @ContainerType annotation");
this.expression = new ContainerExpression((Expression<? extends Container<?>>) expression, type.value());
}
if (expression.isSingle()) {
Expand All @@ -140,14 +142,14 @@ protected void store(Event event, Object next) {
if (next instanceof Map.Entry) {
@SuppressWarnings("unchecked") Map.Entry<String, Object> entry = (Map.Entry<String, Object>) next;
if (keyStore != null)
this.keyStore.change(event, new Object[]{entry.getKey()}, Changer.ChangeMode.SET);
this.keyStore.change(event, new Object[] {entry.getKey()}, Changer.ChangeMode.SET);
if (valueStore != null)
this.valueStore.change(event, new Object[]{entry.getValue()}, Changer.ChangeMode.SET);
this.valueStore.change(event, new Object[] {entry.getValue()}, Changer.ChangeMode.SET);
} else {
if (keyStore != null)
this.keyStore.change(event, new Object[]{this.getLoopCounter(event)}, Changer.ChangeMode.SET);
this.keyStore.change(event, new Object[] {this.getLoopCounter(event)}, Changer.ChangeMode.SET);
if (valueStore != null)
this.valueStore.change(event, new Object[]{next}, Changer.ChangeMode.SET);
this.valueStore.change(event, new Object[] {next}, Changer.ChangeMode.SET);
}
//</editor-fold>
}
Expand All @@ -158,14 +160,13 @@ public String toString(@Nullable Event event, boolean debug) {
return "for each key " + keyStore.toString(event, debug)
+ " and value " + valueStore.toString(event, debug) + " in "
+ super.expression.toString(event, debug);
} else if (keyStore != null) {
return "for each key " + keyStore.toString(event, debug)
+ " in " + super.expression.toString(event, debug);
}
else if (keyStore != null) {
return "for each key " + keyStore.toString(event, debug) + " in " +
super.expression.toString(event, debug);
}
assert valueStore != null: "How did we get here?";
return "for each value " + valueStore.toString(event, debug) + " in " +
super.expression.toString(event, debug);
assert valueStore != null : "How did we get here?";
return "for each value " + valueStore.toString(event, debug)
+ " in " + super.expression.toString(event, debug);
}

}
12 changes: 12 additions & 0 deletions src/test/skript/tests/syntaxes/sections/SecFor.sk
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ test "for section":

delete {_key}
delete {_value}

# 'loop' syntax alternative
loop {_key} and {_value} in {_list::*}:
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}%"

assert {_key} is 3 with "Expected key = 3, found %{_key}%"
assert {_value} is 10 with "Expected value = 10, found %{_value}%"

delete {_key}
delete {_value}

0 comments on commit 36c1e09

Please sign in to comment.