diff --git a/src/main/java/carpet/script/annotation/AnnotationParser.java b/src/main/java/carpet/script/annotation/AnnotationParser.java index 2d64a0c0..9c5b6a66 100644 --- a/src/main/java/carpet/script/annotation/AnnotationParser.java +++ b/src/main/java/carpet/script/annotation/AnnotationParser.java @@ -260,7 +260,8 @@ public final class AnnotationParser Object[] params = getMethodParams(lv, context, t); try { - return outputConverter.convert(handle.invokeExact(params)); + Value result = outputConverter.convert(handle.invokeExact(params)); + return (cc, tt) -> result; } catch (Throwable e) { diff --git a/src/main/java/carpet/script/annotation/OutputConverter.java b/src/main/java/carpet/script/annotation/OutputConverter.java index 2c95b87e..d24a3fec 100644 --- a/src/main/java/carpet/script/annotation/OutputConverter.java +++ b/src/main/java/carpet/script/annotation/OutputConverter.java @@ -14,7 +14,7 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.phys.Vec3; import org.apache.commons.lang3.ClassUtils; -import carpet.script.LazyValue; +import carpet.script.value.BooleanValue; import carpet.script.value.EntityValue; import carpet.script.value.FormattedTextValue; import carpet.script.value.NBTSerializableValue; @@ -26,40 +26,39 @@ import carpet.script.value.ValueConversions; import javax.annotation.Nullable; /** - *

A converter from a given {@link Object} of type T into a {@link LazyValue}, used in order to convert the outputs of methods into usable Scarpet + *

A converter from a given {@link Object} of type T into a {@link Value}, used in order to convert the outputs of methods into usable Scarpet * values.

* * @see #register(Class, Function) - * @see #registerToValue(Class, Function) - * @param The type to convert from into a {@link LazyValue} + * @param The type to convert from into a {@link Value} */ public final class OutputConverter { private static final Map, OutputConverter> byResult = new HashMap<>(); - private static final OutputConverter VALUE = new OutputConverter<>(v -> (c, t) -> v); + private static final OutputConverter VALUE = new OutputConverter<>(Function.identity()); static { - register(LazyValue.class, Function.identity()); // Primitives are handled. Things are boxed in the process anyway, therefore - register(Boolean.class, v -> (v ? LazyValue.TRUE : LazyValue.FALSE)); // would recommend boxed outputs, so you can use null - register(Void.TYPE, v -> LazyValue.NULL); - registerToValue(Integer.class, NumericValue::of); - registerToValue(Double.class, NumericValue::of); - registerToValue(Float.class, NumericValue::of); - registerToValue(Long.class, NumericValue::of); - registerToValue(String.class, StringValue::new); - registerToValue(Entity.class, EntityValue::new); - registerToValue(Component.class, FormattedTextValue::new); - registerToValue(Tag.class, NBTSerializableValue::new); - registerToValue(BlockPos.class, ValueConversions::of); - registerToValue(Vec3.class, ValueConversions::of); - registerToValue(ItemStack.class, ValueConversions::of); - registerToValue(ResourceLocation.class, ValueConversions::of); - registerToValue(GlobalPos.class, ValueConversions::of); + // Primitives are handled. Things are boxed in the process anyway, therefore would recommend boxed outputs, so you can use null + register(Void.TYPE, v -> Value.NULL); + register(Boolean.class, BooleanValue::of); + register(Integer.class, NumericValue::new); + register(Double.class, NumericValue::of); + register(Float.class, NumericValue::of); + register(Long.class, NumericValue::new); + register(String.class, StringValue::new); + register(Entity.class, EntityValue::new); + register(Component.class, FormattedTextValue::new); + register(Tag.class, NBTSerializableValue::new); + register(BlockPos.class, ValueConversions::of); + register(Vec3.class, ValueConversions::of); + register(ItemStack.class, ValueConversions::of); + register(ResourceLocation.class, ValueConversions::of); + register(GlobalPos.class, ValueConversions::of); } - private final Function converter; + private final Function converter; - private OutputConverter(Function converter) + private OutputConverter(Function converter) { this.converter = converter; } @@ -85,27 +84,27 @@ public final class OutputConverter } /** - *

Converts the given input object into a {@link LazyValue}, to be used in return values of Scarpet functions

+ *

Converts the given input object into a {@link Value}, to be used in return values of Scarpet functions

* - *

Returns {@link LazyValue#NULL} if passed a {@code null} input

+ *

Returns {@link Value#NULL} if passed a {@code null} input

* * @param input The value to convert * @return The converted value */ - public LazyValue convert(@Nullable T input) + public Value convert(@Nullable T input) { - return input == null ? LazyValue.NULL : converter.apply(input); + return input == null ? Value.NULL : converter.apply(input); } /** - *

Registers a new type to be able to be used as the return value of methods, converting from inputType to a {@link LazyValue}.

+ *

Registers a new type to be able to be used as the return value of methods, converting from inputType to a {@link Value} + * using the given function.

* - * @see #registerToValue(Class, Function) * @param The type of the input type * @param inputType The class of T - * @param converter The function that converts the an instance of T to a {@link LazyValue} + * @param converter The function that converts the instance of T to a {@link Value} */ - public static void register(Class inputType, Function converter) + public static void register(Class inputType, Function converter) { OutputConverter instance = new OutputConverter<>(converter); if (byResult.containsKey(inputType)) @@ -116,21 +115,13 @@ public final class OutputConverter } /** - *

Registers a new type to be able to be used as the return value of methods, converting from inputType to a {@link LazyValue}, with the - * converter returning a {@link Value} type.

- * * @see #register(Class, Function) - * @param The type of the input type - * @param inputType The class of T - * @param converter The function that converts an instance of T to a {@link Value} + * + * @deprecated Just use {@link #register(Class, Function)}, it now does the same as this */ + @Deprecated public static void registerToValue(Class inputType, Function converter) { - OutputConverter instance = new OutputConverter<>(converter.andThen(v -> (c, t) -> v)); - if (byResult.containsKey(inputType)) - { - throw new IllegalArgumentException(inputType + " already has a registered OutputConverter"); - } - byResult.put(inputType, instance); + register(inputType, converter); } }