Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 Add loop-counter & Improve loops #4595

Merged
merged 46 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
01fbfe0
🚀 Add `fails at` to while loop
AyhamAl-Ali Feb 11, 2022
d3397f3
Add iteration counter expr
AyhamAl-Ali Feb 12, 2022
5534c34
Merge branch 'master' into ench/while-fails-at
AyhamAl-Ali Feb 13, 2022
6ff2f24
🚀 Add loop-counter
AyhamAl-Ali Mar 30, 2022
691e37c
Extra protection
AyhamAl-Ali Mar 30, 2022
1c44447
Merge branch 'master' into ench/while-fails-at
AyhamAl-Ali Mar 30, 2022
72f4164
Should be ready now
AyhamAl-Ali Apr 5, 2022
6f9068c
Merge branch 'master' into ench/while-fails-at
AyhamAl-Ali Apr 5, 2022
d2314fd
Merge branch 'master' into ench/while-fails-at
AyhamAl-Ali Jun 24, 2022
58ccb26
Update src/main/java/ch/njol/skript/expressions/ExprLoopValue.java
AyhamAl-Ali Jul 1, 2022
56c5b68
Merge branch 'master' into ench/while-fails-at
AyhamAl-Ali Jul 1, 2022
674e7c2
RC - Update EffContinue with new LoopSection & Update loop-value
AyhamAl-Ali Jul 1, 2022
66244af
Update description
AyhamAl-Ali Jul 1, 2022
174fb7d
Merge branch 'master' into ench/while-fails-at
AyhamAl-Ali Jul 1, 2022
7ec6b41
Update src/main/java/ch/njol/skript/effects/EffContinue.java
AyhamAl-Ali Jul 1, 2022
4932f49
Merge branch 'master' into ench/while-fails-at
AyhamAl-Ali Jul 1, 2022
75e8f92
Improvements
AyhamAl-Ali Jul 1, 2022
14c8cb6
Merge remote-tracking branch 'AyhamAl-Ali/ench/while-fails-at' into e…
AyhamAl-Ali Jul 1, 2022
d3e3d6d
Annotations
AyhamAl-Ali Jul 1, 2022
cad44b3
Update src/main/java/ch/njol/skript/expressions/ExprLoopIteration.java
AyhamAl-Ali Jul 9, 2022
4161e68
Update src/main/java/ch/njol/skript/expressions/ExprLoopIteration.java
AyhamAl-Ali Jul 15, 2022
d7816d3
Update src/main/java/ch/njol/skript/expressions/ExprLoopIteration.java
AyhamAl-Ali Jul 18, 2022
85f1775
Update error message
AyhamAl-Ali Jul 18, 2022
f5e2a4c
Merge remote-tracking branch 'AyhamAl-Ali/ench/while-fails-at' into e…
AyhamAl-Ali Jul 18, 2022
8cd9c34
Update src/main/java/ch/njol/skript/expressions/ExprLoopIteration.java
AyhamAl-Ali Jul 22, 2022
875cb15
Update src/main/java/ch/njol/skript/lang/LoopSection.java
AyhamAl-Ali Aug 6, 2022
7c1e5b6
Update src/main/java/ch/njol/skript/expressions/ExprLoopValue.java
AyhamAl-Ali Aug 6, 2022
a71f19e
Update src/main/java/ch/njol/skript/sections/SecLoop.java
AyhamAl-Ali Aug 7, 2022
d43da59
Update src/main/java/ch/njol/skript/sections/SecLoop.java
AyhamAl-Ali Aug 7, 2022
a3e111d
Update src/main/java/ch/njol/skript/expressions/ExprLoopIteration.java
AyhamAl-Ali Aug 7, 2022
c98a4f3
Apply suggestions from code review
AyhamAl-Ali Aug 7, 2022
8d4c2e0
Formatting Improvements
AyhamAl-Ali Aug 26, 2022
a32d070
Update src/main/java/ch/njol/skript/effects/EffContinue.java
AyhamAl-Ali Nov 25, 2022
92cde7b
Better variable naming
AyhamAl-Ali Jan 1, 2023
1623b9d
Merge remote-tracking branch 'AyhamAl-Ali/ench/while-fails-at' into e…
AyhamAl-Ali Jan 1, 2023
60e31ba
Examples formatting
AyhamAl-Ali Jan 1, 2023
f7f7329
Requested Changes
AyhamAl-Ali Apr 18, 2023
9bf0f77
Add tests
AyhamAl-Ali Apr 18, 2023
c8c5ed3
Merge remote-tracking branch 'origin/master' into ench/while-fails-at
AyhamAl-Ali Apr 18, 2023
17c4ca5
Apply suggestions from code review
AyhamAl-Ali Apr 19, 2023
ab32194
Reorder methods
AyhamAl-Ali Apr 19, 2023
511d89b
Merge remote-tracking branch 'AyhamAl-Ali/ench/while-fails-at' into e…
AyhamAl-Ali Apr 19, 2023
8d609c2
Update src/test/skript/tests/regressions/4595-loop-iteration.sk
AyhamAl-Ali Apr 20, 2023
08351d2
Merge branch 'master' into ench/while-fails-at
TheLimeGlass Jun 6, 2023
48862ac
Merge branch 'master' into ench/while-fails-at
TheLimeGlass Jun 25, 2023
128bdc8
Merge branch 'master' into ench/while-fails-at
TheLimeGlass Jun 26, 2023
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
42 changes: 24 additions & 18 deletions src/main/java/ch/njol/skript/effects/EffContinue.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,33 @@
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.LoopSection;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.lang.TriggerSection;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.skript.sections.SecLoop;
import ch.njol.skript.sections.SecWhile;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import java.util.List;
import java.util.stream.Collectors;

@Name("Continue")
@Description("Skips the value currently being looped, moving on to the next value if it exists.")
@Examples({"loop all players:",
"\tif loop-value does not have permission \"moderator\":",
"\t\tcontinue # filter out non moderators",
"\tbroadcast \"%loop-player% is a moderator!\" # Only moderators get broadcast"})
@Description("Immediately moves the (while) loop on to the next iteration.")
@Examples({
"# Broadcast online moderators",
"loop all players:",
"\tif loop-value does not have permission \"moderator\":",
"\t\tcontinue # filter out non moderators",
"\tbroadcast \"%loop-player% is a moderator!\" # Only moderators get broadcast",
" ",
"# Game starting counter",
"set {_counter} to 11",
"while {_counter} > 0:",
"\tremove 1 from {_counter}",
"\twait a second",
"\tif {_counter} != 1, 2, 3, 5 or 10:",
"\t\tcontinue # only print when counter is 1, 2, 3, 5 or 10",
"\tbroadcast \"Game starting in %{_counter}% second(s)\"",
})
@Since("2.2-dev37, INSERT VERSION (while loops)")
public class EffContinue extends Effect {

Expand All @@ -52,20 +60,18 @@ public class EffContinue extends Effect {
}

@SuppressWarnings("NotNullFieldNotInitialized")
private TriggerSection section;
private LoopSection loop;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
List<TriggerSection> currentSections = ParserInstance.get().getCurrentSections().stream()
.filter(s -> s instanceof SecLoop || s instanceof SecWhile)
.collect(Collectors.toList());
List<LoopSection> currentLoops = getParser().getCurrentSections(LoopSection.class);

if (currentSections.isEmpty()) {
Skript.error("Continue may only be used in while or loops");
if (currentLoops.isEmpty()) {
Skript.error("'Continue' effect may only be used in while loops and regular loops");
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

section = currentSections.get(currentSections.size() - 1);
loop = currentLoops.get(currentLoops.size() - 1);
TPGamesNL marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

Expand All @@ -77,7 +83,7 @@ protected void execute(Event e) {
@Nullable
@Override
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
protected TriggerItem walk(Event e) {
return section;
return loop;
}

@Override
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/ch/njol/skript/effects/EffExit.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.LoopSection;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.lang.TriggerSection;
import ch.njol.skript.lang.parser.ParserInstance;
import ch.njol.skript.log.ErrorQuality;
import ch.njol.skript.sections.SecConditional;
import ch.njol.skript.sections.SecLoop;
import ch.njol.skript.sections.SecWhile;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -104,7 +103,7 @@ private static int numLevels(int type) {
return currentSections.size();
int r = 0;
for (TriggerSection s : currentSections) {
if (type == CONDITIONALS ? s instanceof SecConditional : s instanceof SecLoop || s instanceof SecWhile)
if (type == CONDITIONALS ? s instanceof SecConditional : s instanceof LoopSection)
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
r++;
}
return r;
Expand All @@ -121,16 +120,13 @@ protected TriggerItem walk(final Event e) {
assert false : this;
return null;
}
if (n instanceof SecLoop) {
((SecLoop) n).exit(e);
} else if (n instanceof SecWhile) {
((SecWhile) n).reset();
}
if (n instanceof LoopSection)
((LoopSection) n).exit(e);
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved

if (type == EVERYTHING || type == CONDITIONALS && n instanceof SecConditional || type == LOOPS && (n instanceof SecLoop || n instanceof SecWhile))
if (type == EVERYTHING || type == CONDITIONALS && n instanceof SecConditional || type == LOOPS && (n instanceof LoopSection))
i--;
}
return n instanceof SecLoop ? ((SecLoop) n).getActualNext() : n instanceof SecWhile ? ((SecWhile) n).getActualNext() : n.getNext();
return n instanceof LoopSection ? ((LoopSection) n).getActualNext() : n.getNext();
}

@Override
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/ch/njol/skript/effects/EffReturn.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.LoopSection;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.lang.TriggerSection;
Expand All @@ -34,8 +35,6 @@
import ch.njol.skript.lang.function.ScriptFunction;
import ch.njol.skript.log.RetainingLogHandler;
import ch.njol.skript.log.SkriptLogger;
import ch.njol.skript.sections.SecLoop;
import ch.njol.skript.sections.SecWhile;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -116,11 +115,9 @@ protected TriggerItem walk(final Event e) {

TriggerSection parent = getParent();
while (parent != null) {
if (parent instanceof SecLoop) {
((SecLoop) parent).exit(e);
} else if (parent instanceof SecWhile) {
((SecWhile) parent).reset();
}
if (parent instanceof LoopSection)
((LoopSection) parent).exit(e);
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved

parent = parent.getParent();
}

Expand Down
119 changes: 119 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprLoopIteration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* 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/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.LoopSection;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.TriggerSection;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

@Name("Loop Iteration")
@Description("Returns the loop's current iteration count (for both normal and while loop).")
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
@Examples({
"while player is online:",
"\tgive player 1 stone",
"\twait 5 ticks",
"\tif loop-counter > 30:",
"\t\tstop loop",
"",
"loop {top-balances::*}:",
"\tif loop-iteration <= 10:",
"\t\tbroadcast \"##%loop-iteration% %loop-index% has $%loop-value%\"",
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
})
@Since("INSERT VERSION")
public class ExprLoopIteration extends SimpleExpression<Long> {

static {
Skript.registerExpression(ExprLoopIteration.class, Long.class, ExpressionType.SIMPLE, "[the] loop(-| )(counter|iteration)[-%-*number%]");
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
APickledWalrus marked this conversation as resolved.
Show resolved Hide resolved
}

@SuppressWarnings("NotNullFieldNotInitialized")
private LoopSection loop;

private int loopNumber;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
loopNumber = -1;
if (exprs[0] != null) {
loopNumber = ((Literal<Number>) exprs[0]).getSingle().intValue();
}
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved

int i = 1;
LoopSection loop = null;

for (LoopSection l : getParser().getCurrentSections(LoopSection.class)) {

AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
if (i < loopNumber) {
i++;
continue;
}

AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
if (loop != null) {
Skript.error("There are multiple loops. Use loop-iteration-1/2/3/etc. to specify which loop-iteration you want.");
return false;
}
loop = l;

AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
if (i == loopNumber)
break;
}

if (loop == null) {
Skript.error("The loop iteration expression must be used in a loop");
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

this.loop = loop;
return true;
}

@Override
protected Long[] get(Event e) {
return new Long[] { loop.getLoopCounter(e) };
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public Class<? extends Long> getReturnType() {
return Long.class;
}

@Override
public boolean isSingle() {
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "loop-iteration" + (loopNumber != -1 ? ("-" + loopNumber) : "");
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
}

}
Loading