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

Create SectionExitHandler interface #6349

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
10 changes: 3 additions & 7 deletions src/main/java/ch/njol/skript/effects/EffExit.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
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.*;
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;
Expand Down Expand Up @@ -122,8 +118,8 @@ protected TriggerItem walk(Event event) {
assert false : this;
return null;
}
if (node instanceof LoopSection)
((LoopSection) node).exit(event);
if (node instanceof ExitHandlingSection)
((ExitHandlingSection) node).exit(event);

if (type == EVERYTHING || type == CONDITIONALS && node instanceof SecConditional || type == LOOPS && (node instanceof LoopSection))
i--;
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/ch/njol/skript/effects/EffReturn.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
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.*;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.lang.TriggerSection;
import ch.njol.skript.lang.function.FunctionEvent;
import ch.njol.skript.lang.function.Functions;
import ch.njol.skript.lang.function.ScriptFunction;
Expand Down Expand Up @@ -117,8 +113,8 @@ protected TriggerItem walk(Event event) {

TriggerSection parent = getParent();
while (parent != null) {
if (parent instanceof LoopSection)
((LoopSection) parent).exit(event);
if (parent instanceof ExitHandlingSection)
((ExitHandlingSection) parent).exit(event);

parent = parent.getParent();
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/ch/njol/skript/lang/ExitHandlingSection.java
takejohn marked this conversation as resolved.
Show resolved Hide resolved
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 ExitHandlingSection {

/**
* Exits the section
* @param event The involved event
*/
void exit(Event event);

}
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, ExitHandlingSection {

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