From db337a63285bfee737a950514b5c9ceda7d1691f Mon Sep 17 00:00:00 2001 From: _tud <98935832+UnderscoreTud@users.noreply.github.com> Date: Mon, 15 Jul 2024 20:28:55 +0300 Subject: [PATCH] Improve ReturnHandler#returnValues method (#6894) --- .../java/ch/njol/skript/effects/EffReturn.java | 3 +-- .../java/ch/njol/skript/lang/ReturnHandler.java | 6 ++++-- .../ch/njol/skript/lang/ReturnableTrigger.java | 5 +++-- .../njol/skript/lang/function/ScriptFunction.java | 14 ++++++++------ .../ch/njol/skript/test/runner/SecReturnable.java | 6 +++--- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffReturn.java b/src/main/java/ch/njol/skript/effects/EffReturn.java index 21ce3d4d475..7c4025b5d7a 100644 --- a/src/main/java/ch/njol/skript/effects/EffReturn.java +++ b/src/main/java/ch/njol/skript/effects/EffReturn.java @@ -19,7 +19,6 @@ package ch.njol.skript.effects; import ch.njol.skript.Skript; -import ch.njol.skript.classes.ClassInfo; import ch.njol.skript.doc.Description; import ch.njol.skript.doc.Examples; import ch.njol.skript.doc.Name; @@ -110,7 +109,7 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye protected TriggerItem walk(Event event) { debug(event, false); //noinspection rawtypes,unchecked - ((ReturnHandler) handler).returnValues(value.getArray(event)); + ((ReturnHandler) handler).returnValues(event, value); TriggerSection parent = getParent(); while (parent != null && parent != handler) { diff --git a/src/main/java/ch/njol/skript/lang/ReturnHandler.java b/src/main/java/ch/njol/skript/lang/ReturnHandler.java index f908fc68c51..d905599dc4b 100644 --- a/src/main/java/ch/njol/skript/lang/ReturnHandler.java +++ b/src/main/java/ch/njol/skript/lang/ReturnHandler.java @@ -130,9 +130,11 @@ default ReturnableTrigger loadReturnableTrigger(SectionNode node, String name } /** - * @param values the values to return + * Called when {@link ch.njol.skript.effects.EffReturn} is executed + * @param event the event providing context + * @param value an expression representing the value(s) to return */ - void returnValues(T @Nullable [] values); + void returnValues(Event event, Expression value); /** * @return whether this return handler may accept multiple return values diff --git a/src/main/java/ch/njol/skript/lang/ReturnableTrigger.java b/src/main/java/ch/njol/skript/lang/ReturnableTrigger.java index 51c025ef70c..4b8b601d990 100644 --- a/src/main/java/ch/njol/skript/lang/ReturnableTrigger.java +++ b/src/main/java/ch/njol/skript/lang/ReturnableTrigger.java @@ -18,6 +18,7 @@ */ package ch.njol.skript.lang; +import org.bukkit.event.Event; import org.eclipse.jdt.annotation.Nullable; import org.skriptlang.skript.lang.script.Script; @@ -36,8 +37,8 @@ public ReturnableTrigger(ReturnHandler handler, @Nullable Script script, Stri } @Override - public void returnValues(T @Nullable [] values) { - handler.returnValues(values); + public void returnValues(Event event, Expression value) { + handler.returnValues(event, value); } @Override diff --git a/src/main/java/ch/njol/skript/lang/function/ScriptFunction.java b/src/main/java/ch/njol/skript/lang/function/ScriptFunction.java index d3a1e85117c..c34f26fd448 100644 --- a/src/main/java/ch/njol/skript/lang/function/ScriptFunction.java +++ b/src/main/java/ch/njol/skript/lang/function/ScriptFunction.java @@ -19,13 +19,14 @@ package ch.njol.skript.lang.function; import ch.njol.skript.classes.ClassInfo; +import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ReturnHandler; +import org.bukkit.event.Event; import org.jetbrains.annotations.ApiStatus; import org.skriptlang.skript.lang.script.Script; import org.eclipse.jdt.annotation.Nullable; import ch.njol.skript.config.SectionNode; -import ch.njol.skript.effects.EffReturn; import ch.njol.skript.lang.Trigger; import ch.njol.skript.lang.util.SimpleEvent; import ch.njol.skript.variables.Variables; @@ -80,13 +81,14 @@ public ScriptFunction(Signature sign, SectionNode node) { } /** - * Should only be called by {@link EffReturn}. - * @deprecated Use {@link ScriptFunction#returnValues(Object[])} + * @deprecated Use {@link ScriptFunction#returnValues(Event, Expression)} */ @Deprecated @ApiStatus.Internal public final void setReturnValue(@Nullable T[] values) { - returnValues(values); + assert !returnValueSet; + returnValueSet = true; + this.returnValues = values; } @Override @@ -97,10 +99,10 @@ public boolean resetReturnValue() { } @Override - public final void returnValues(T @Nullable [] values) { + public final void returnValues(Event event, Expression value) { assert !returnValueSet; returnValueSet = true; - this.returnValues = values; + this.returnValues = value.getArray(event); } @Override diff --git a/src/main/java/ch/njol/skript/test/runner/SecReturnable.java b/src/main/java/ch/njol/skript/test/runner/SecReturnable.java index 436b6d282fb..03833d7132d 100644 --- a/src/main/java/ch/njol/skript/test/runner/SecReturnable.java +++ b/src/main/java/ch/njol/skript/test/runner/SecReturnable.java @@ -56,8 +56,8 @@ public boolean init(Expression[] expressions, int matchedPattern, Kleenean is } @Override - public void returnValues(Object @Nullable [] values) { - returnedValues = values; + public void returnValues(Event event, Expression value) { + returnedValues = value.getArray(event); } @Override @@ -110,5 +110,5 @@ public String toString(@Nullable Event event, boolean debug) { } } - + }