diff --git a/src/main/java/ch/njol/skript/expressions/ExprVelocity.java b/src/main/java/ch/njol/skript/expressions/ExprVelocity.java index d776264bcd7..1745acabbe3 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprVelocity.java +++ b/src/main/java/ch/njol/skript/expressions/ExprVelocity.java @@ -51,6 +51,7 @@ public Vector convert(Entity e) { } @Override + @Nullable @SuppressWarnings("null") public Class[] acceptChange(ChangeMode mode) { if ((mode == ChangeMode.ADD || mode == ChangeMode.REMOVE || mode == ChangeMode.SET || mode == ChangeMode.DELETE || mode == ChangeMode.RESET)) @@ -61,7 +62,7 @@ public Class[] acceptChange(ChangeMode mode) { @Override @SuppressWarnings("null") public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { - assert delta != null; + assert mode == ChangeMode.DELETE || mode == ChangeMode.RESET || delta != null; for (final Entity entity : getExpr().getArray(e)) { if (entity == null) return; diff --git a/src/main/java/ch/njol/skript/lang/function/ExprFunctionCall.java b/src/main/java/ch/njol/skript/lang/function/ExprFunctionCall.java index a5135282277..e585c3521e3 100644 --- a/src/main/java/ch/njol/skript/lang/function/ExprFunctionCall.java +++ b/src/main/java/ch/njol/skript/lang/function/ExprFunctionCall.java @@ -24,6 +24,7 @@ import ch.njol.skript.registrations.Converters; import ch.njol.skript.util.Utils; import ch.njol.util.Kleenean; +import ch.njol.util.coll.CollectionUtils; import org.bukkit.event.Event; import org.eclipse.jdt.annotation.Nullable; @@ -33,11 +34,24 @@ public class ExprFunctionCall extends SimpleExpression { private final Class[] returnTypes; private final Class returnType; - @SuppressWarnings("unchecked") public ExprFunctionCall(FunctionReference function) { + this(function, function.returnTypes); + } + + @SuppressWarnings("unchecked") + public ExprFunctionCall(FunctionReference function, Class[] expectedReturnTypes) { this.function = function; - this.returnTypes = function.returnTypes; - this.returnType = (Class) Utils.getSuperType(returnTypes); + Class functionReturnType = function.getReturnType(); + assert functionReturnType != null; + if (CollectionUtils.containsSuperclass(expectedReturnTypes, functionReturnType)) { + // Function returns expected type already + this.returnTypes = new Class[] {functionReturnType}; + this.returnType = (Class) functionReturnType; + } else { + // Return value needs to be converted + this.returnTypes = expectedReturnTypes; + this.returnType = (Class) Utils.getSuperType(expectedReturnTypes); + } } @Override @@ -48,6 +62,19 @@ protected T[] get(Event e) { return Converters.convertArray(returnValue, returnTypes, returnType); } + @Override + @Nullable + @SuppressWarnings("unchecked") + public Expression getConvertedExpression(Class... to) { + if (CollectionUtils.containsSuperclass(to, getReturnType())) + return (Expression) this; + assert function.getReturnType() != null; + if (Converters.converterExists(function.getReturnType(), to)) { + return new ExprFunctionCall<>(function, to); + } + return null; + } + @Override public boolean isSingle() { return function.isSingle(); diff --git a/src/test/skript/tests/regressions/4524-function call conversion.sk b/src/test/skript/tests/regressions/4524-function call conversion.sk index f9b812147ce..95e1f8649cb 100644 --- a/src/test/skript/tests/regressions/4524-function call conversion.sk +++ b/src/test/skript/tests/regressions/4524-function call conversion.sk @@ -3,3 +3,5 @@ function functionCallConversion() :: item: test "function call conversion": assert name of functionCallConversion() is "abc" with "Name of item from function call failed" + + set {_p}'s velocity to vector({_x}, {_y}, {_z})