From b359c6c76c38093c4d44f27a29457d19b035f077 Mon Sep 17 00:00:00 2001 From: altrisi Date: Mon, 31 May 2021 20:51:10 +0200 Subject: [PATCH] Implement `@Locator.Function` and `@Locator.Vec3d` (#891) * Implemente `@Locator.Function` * Implement `@Locator.Vec3d` * Small import cleanup * Javadoc --- .../carpet/script/annotation/Locator.java | 36 +++++++------------ .../script/argument/FunctionArgument.java | 4 +-- .../script/argument/Vector3Argument.java | 26 ++++++++------ 3 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/main/java/carpet/script/annotation/Locator.java b/src/main/java/carpet/script/annotation/Locator.java index 1f0e87e1..d4d90e6a 100644 --- a/src/main/java/carpet/script/annotation/Locator.java +++ b/src/main/java/carpet/script/annotation/Locator.java @@ -6,7 +6,7 @@ import java.lang.annotation.Target; import java.lang.reflect.AnnotatedType; import java.util.Iterator; -import org.apache.commons.lang3.NotImplementedException; +import com.google.common.collect.Lists; import carpet.script.CarpetContext; import carpet.script.Context; @@ -26,9 +26,7 @@ import static java.lang.annotation.ElementType.TYPE_USE; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - *

Class that holds annotation for {@link Argument} locators to be used in Scarpet functions.

- * - *

Note: Only {@link Block} locator is currently implemented.

+ *

Class that holds the annotations for {@link Argument} locators, in order for them to be used in Scarpet functions.

*/ public interface Locator { @@ -64,14 +62,11 @@ public interface Locator /** *

Represents that the annotated argument must be gotten by passing the arguments in there into a {@link Vector3Argument} locator.

* - *

Must be used in either a {@link Vector3Argument} or a {@link Vec3d} parameter, though the latest may not get all the data.

- * - *

Note: This locator has not been implemented yet (TODO)

+ *

Must be used in either a {@link Vector3Argument} or a {@link net.minecraft.util.math.Vec3d Vec3d} parameter.

*/ @Documented @Retention(RUNTIME) @Target({ PARAMETER, TYPE_USE }) - @Deprecated // Not implemented public @interface Vec3d { /** @@ -95,14 +90,11 @@ public interface Locator *

Can be used in both {@link FunctionArgument} and {@link FunctionValue} types, but the last won't have access to arguments provided to the * function, even though they will still be consumed from the arguments the function was called with.

* - *

This will consume any remaining parameters passed to the function, therefore any other parameters after this will throw.

- * - *

Note: This locator has not been implemented yet (TODO)

+ *

This will consume any remaining parameters passed to the function, therefore any other parameter after this will throw.

*/ @Documented @Retention(RUNTIME) @Target({ PARAMETER, TYPE_USE }) - @Deprecated // Not implemented public @interface Function { /** @@ -211,10 +203,10 @@ public interface Locator @Override public R checkAndConvert(Iterator valueIterator, Context context, Context.Type theLazyT) { - Vector3Argument locator = null; - // TODO Make the locator - // return (R) (returnVec3d ? locator.vec : locator); - throw new NotImplementedException("Locator.Vec3d still require adapting Vector3Argument to accept iterators!"); + Vector3Argument locator = Vector3Argument.findIn(valueIterator, 0, optionalDirection, optionalEntity); + @SuppressWarnings("unchecked") + R ret = (R) (returnVec3d ? locator.vec : locator); + return ret; } } @@ -228,8 +220,7 @@ public interface Locator { this.returnFunctionValue = type == FunctionValue.class; if (!returnFunctionValue && type != FunctionArgument.class) - throw new IllegalArgumentException( - "Params annotated with Locator.Function must be of either FunctionArgument or FunctionValue type"); + throw new IllegalArgumentException("Params annotated with Locator.Function must be of either FunctionArgument or FunctionValue type"); this.allowNone = annotation.allowNone(); this.checkArgs = annotation.checkArgs(); if (returnFunctionValue && allowNone) @@ -240,11 +231,10 @@ public interface Locator public R checkAndConvert(Iterator valueIterator, Context context, Context.Type theLazyT) { Module module = context.host.main; - FunctionArgument locator = null; - // TODO Make the locator - // return (R) (returnFunctionValue ? locator.function : locator); - throw new NotImplementedException("Locator.FunctionValue still requires adapting the annotation system to somehow get lv size" - + "or FunctionArgument to not depend on it!"); + FunctionArgument locator = FunctionArgument.findIn(context, module, Lists.newArrayList(valueIterator), 0, allowNone, checkArgs); + @SuppressWarnings("unchecked") + R ret = (R) (returnFunctionValue ? locator.function : locator); + return ret; } @Override diff --git a/src/main/java/carpet/script/argument/FunctionArgument.java b/src/main/java/carpet/script/argument/FunctionArgument.java index 64fc1587..bbcb3422 100644 --- a/src/main/java/carpet/script/argument/FunctionArgument.java +++ b/src/main/java/carpet/script/argument/FunctionArgument.java @@ -1,8 +1,6 @@ package carpet.script.argument; import carpet.script.Context; -import carpet.script.Fluff; -import carpet.script.LazyValue; import carpet.script.ScriptHost; import carpet.script.bundled.Module; import carpet.script.command.CommandArgument; @@ -31,7 +29,7 @@ public class FunctionArgument extends Argument /** * @param c context * @param module module - * @param params list of lazy params + * @param params list of params * @param offset offset where to start looking for functional argument * @param allowNone none indicates no function present, otherwise it will croak * @param checkArgs whether the caller expects trailing parameters to fully resolve function argument list diff --git a/src/main/java/carpet/script/argument/Vector3Argument.java b/src/main/java/carpet/script/argument/Vector3Argument.java index 1dc440f1..212d2df7 100644 --- a/src/main/java/carpet/script/argument/Vector3Argument.java +++ b/src/main/java/carpet/script/argument/Vector3Argument.java @@ -1,7 +1,5 @@ package carpet.script.argument; -import carpet.script.CarpetContext; -import carpet.script.LazyValue; import carpet.script.exception.InternalExpressionException; import carpet.script.value.BlockValue; import carpet.script.value.EntityValue; @@ -11,7 +9,9 @@ import carpet.script.value.Value; import net.minecraft.entity.Entity; import net.minecraft.util.math.Vec3d; +import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; public class Vector3Argument extends Argument { @@ -52,11 +52,15 @@ public class Vector3Argument extends Argument return findIn(params, offset, false, false); } - public static Vector3Argument findIn(List params, int offset, boolean optionalDirection, boolean optionalEntity) + public static Vector3Argument findIn(List params, int offset, boolean optionalDirection, boolean optionalEntity) { + return findIn(params.listIterator(offset), offset, optionalDirection, optionalEntity); + } + + public static Vector3Argument findIn(Iterator params, int offset, boolean optionalDirection, boolean optionalEntity) { try { - Value v1 = params.get(0 + offset); + Value v1 = params.next(); if (v1 instanceof BlockValue) { return (new Vector3Argument(Vec3d.ofCenter(((BlockValue) v1).getPos()), 1+offset)).fromBlock(); @@ -84,21 +88,21 @@ public class Vector3Argument extends Argument } Vec3d pos = new Vec3d( NumericValue.asNumber(v1).getDouble(), - NumericValue.asNumber(params.get(1 + offset)).getDouble(), - NumericValue.asNumber(params.get(2 + offset)).getDouble()); + NumericValue.asNumber(params.next()).getDouble(), + NumericValue.asNumber(params.next()).getDouble()); double yaw = 0.0D; double pitch = 0.0D; int eatenLength = 3; - if (params.size()>3+offset && optionalDirection) + if (params.hasNext() && optionalDirection) { - yaw = NumericValue.asNumber(params.get(3 + offset)).getDouble(); - pitch = NumericValue.asNumber(params.get(4 + offset)).getDouble(); + yaw = NumericValue.asNumber(params.next()).getDouble(); + pitch = NumericValue.asNumber(params.next()).getDouble(); eatenLength = 5; } - return new Vector3Argument(pos,offset+eatenLength, yaw, pitch); + return new Vector3Argument(pos, offset+eatenLength, yaw, pitch); } - catch (IndexOutOfBoundsException e) + catch (IndexOutOfBoundsException | NoSuchElementException e) { throw new InternalExpressionException("Position argument should be defined either by three coordinates (a triple or by three arguments), or a positioned block value"); }