Skip to content

Commit

Permalink
Fix ExprFunctionCall conversion (#4986)
Browse files Browse the repository at this point in the history
* Change default return type and implement conversion
  • Loading branch information
TPGamesNL authored Aug 15, 2022
1 parent 5c37a30 commit adb687d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/java/ch/njol/skript/expressions/ExprVelocity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/ch/njol/skript/lang/function/ExprFunctionCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,11 +34,24 @@ public class ExprFunctionCall<T> extends SimpleExpression<T> {
private final Class<? extends T>[] returnTypes;
private final Class<T> returnType;

@SuppressWarnings("unchecked")
public ExprFunctionCall(FunctionReference<T> function) {
this(function, function.returnTypes);
}

@SuppressWarnings("unchecked")
public ExprFunctionCall(FunctionReference<?> function, Class<? extends T>[] expectedReturnTypes) {
this.function = function;
this.returnTypes = function.returnTypes;
this.returnType = (Class<T>) 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<T>) functionReturnType;
} else {
// Return value needs to be converted
this.returnTypes = expectedReturnTypes;
this.returnType = (Class<T>) Utils.getSuperType(expectedReturnTypes);
}
}

@Override
Expand All @@ -48,6 +62,19 @@ protected T[] get(Event e) {
return Converters.convertArray(returnValue, returnTypes, returnType);
}

@Override
@Nullable
@SuppressWarnings("unchecked")
public <R> Expression<? extends R> getConvertedExpression(Class<R>... to) {
if (CollectionUtils.containsSuperclass(to, getReturnType()))
return (Expression<? extends R>) 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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})

0 comments on commit adb687d

Please sign in to comment.