Skip to content

Commit

Permalink
Create SectionExitHandler interface (#6349)
Browse files Browse the repository at this point in the history
  • Loading branch information
takejohn authored Mar 9, 2024
1 parent 9699415 commit f603e9d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/main/java/ch/njol/skript/effects/EffExit.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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--;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ch/njol/skript/effects/EffReturn.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/ch/njol/skript/lang/LoopSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event, Long> currentLoopCounter = new WeakHashMap<>();

Expand All @@ -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);
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/ch/njol/skript/lang/SectionExitHandler.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
* 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);

}

0 comments on commit f603e9d

Please sign in to comment.