diff --git a/src/main/java/ch/njol/skript/effects/EffExit.java b/src/main/java/ch/njol/skript/effects/EffExit.java index eecbb7efec1..b5e2be7e04a 100644 --- a/src/main/java/ch/njol/skript/effects/EffExit.java +++ b/src/main/java/ch/njol/skript/effects/EffExit.java @@ -25,6 +25,7 @@ import ch.njol.skript.doc.Since; import ch.njol.skript.lang.Effect; import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SectionExitHandler; import ch.njol.skript.lang.LoopSection; import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.lang.TriggerItem; @@ -122,8 +123,8 @@ protected TriggerItem walk(Event event) { assert false : this; return null; } - if (node instanceof LoopSection) - ((LoopSection) node).exit(event); + if (node instanceof SectionExitHandler) + ((SectionExitHandler) node).exit(event); if (type == EVERYTHING || type == CONDITIONALS && node instanceof SecConditional || type == LOOPS && (node instanceof LoopSection)) i--; diff --git a/src/main/java/ch/njol/skript/effects/EffReturn.java b/src/main/java/ch/njol/skript/effects/EffReturn.java index e9184ac69d5..d0b4d220514 100644 --- a/src/main/java/ch/njol/skript/effects/EffReturn.java +++ b/src/main/java/ch/njol/skript/effects/EffReturn.java @@ -26,7 +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.SectionExitHandler; import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.lang.TriggerItem; import ch.njol.skript.lang.TriggerSection; @@ -117,8 +117,8 @@ protected TriggerItem walk(Event event) { TriggerSection parent = getParent(); while (parent != null) { - if (parent instanceof LoopSection) - ((LoopSection) parent).exit(event); + if (parent instanceof SectionExitHandler) + ((SectionExitHandler) parent).exit(event); parent = parent.getParent(); } diff --git a/src/main/java/ch/njol/skript/lang/LoopSection.java b/src/main/java/ch/njol/skript/lang/LoopSection.java index f733ad2eaaf..b126a8889c6 100644 --- a/src/main/java/ch/njol/skript/lang/LoopSection.java +++ b/src/main/java/ch/njol/skript/lang/LoopSection.java @@ -29,7 +29,7 @@ * @see ch.njol.skript.sections.SecWhile * @see ch.njol.skript.sections.SecLoop */ -public abstract class LoopSection extends Section implements SyntaxElement, Debuggable { +public abstract class LoopSection extends Section implements SyntaxElement, Debuggable, SectionExitHandler { protected final transient Map currentLoopCounter = new WeakHashMap<>(); @@ -50,6 +50,7 @@ public long getLoopCounter(Event event) { * Exit the loop, used to reset the loop properties such as iterations counter * @param event The event where the loop is used to reset its relevant properties */ + @Override public void exit(Event event) { currentLoopCounter.remove(event); } diff --git a/src/main/java/ch/njol/skript/lang/SectionExitHandler.java b/src/main/java/ch/njol/skript/lang/SectionExitHandler.java new file mode 100644 index 00000000000..3b081245fdb --- /dev/null +++ b/src/main/java/ch/njol/skript/lang/SectionExitHandler.java @@ -0,0 +1,36 @@ +/** + * 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.lang; + +import org.bukkit.event.Event; + +/** + * A {@link Section} implementing this interface can execute a task when + * it is exited by an {@link ch.njol.skript.effects.EffExit 'exit'} or + * {@link ch.njol.skript.effects.EffReturn 'return'} effect. + */ +public interface SectionExitHandler { + + /** + * Exits the section + * @param event The involved event + */ + void exit(Event event); + +}