diff --git a/src/main/java/ch/njol/skript/sections/SecFor.java b/src/main/java/ch/njol/skript/sections/SecFor.java index e2b506e866d..3ea5bfd15d5 100644 --- a/src/main/java/ch/njol/skript/sections/SecFor.java +++ b/src/main/java/ch/njol/skript/sections/SecFor.java @@ -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 . - * + * 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 . + *

* Copyright Peter Güttinger, SkriptLang team and contributors */ package ch.njol.skript.sections; @@ -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::*}:", @@ -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; @@ -86,7 +87,7 @@ public boolean init(Expression[] exprs, ParseResult parseResult, SectionNode sectionNode, List triggerItems) { - if (!this.hasExperiment(Feature.FOR_EACH_LOOPS)) + if (!this.getParser().hasExperiment(Feature.FOR_EACH_LOOPS)) return false; // switch (matchedPattern) { @@ -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>) expression, type.value()); } if (expression.isSingle()) { @@ -140,14 +142,14 @@ protected void store(Event event, Object next) { if (next instanceof Map.Entry) { @SuppressWarnings("unchecked") Map.Entry entry = (Map.Entry) 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); } // } @@ -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); } } diff --git a/src/test/skript/tests/syntaxes/sections/SecFor.sk b/src/test/skript/tests/syntaxes/sections/SecFor.sk index e4ac211d5a3..cba4dc160e4 100644 --- a/src/test/skript/tests/syntaxes/sections/SecFor.sk +++ b/src/test/skript/tests/syntaxes/sections/SecFor.sk @@ -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}