Make OutputConverter.convert return Value

This is a breaking change, but there's only one use of the function on all github

The refactor also makes `register` take a function to `Value`, no one uses the one to `LazyValue` anyway. `registerToValue` is now deprecated
This commit is contained in:
altrisi committed 2023-03-08 14:07:50 +01:00
1 parent 93bec37dac
commit 883d887e11
2 files changed
+36 -44

No files matched your search

@@ -260,7 +260,8 @@ public final class AnnotationParser
Object[] params = getMethodParams(lv, context, t); Object[] params = getMethodParams(lv, context, t);
try try
{ {
return outputConverter.convert(handle.invokeExact(params)); Value result = outputConverter.convert(handle.invokeExact(params));
return (cc, tt) -> result;
} }
catch (Throwable e) catch (Throwable e)
{ {
@@ -14,7 +14,7 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.ClassUtils;
import carpet.script.LazyValue; import carpet.script.value.BooleanValue;
import carpet.script.value.EntityValue; import carpet.script.value.EntityValue;
import carpet.script.value.FormattedTextValue; import carpet.script.value.FormattedTextValue;
import carpet.script.value.NBTSerializableValue; import carpet.script.value.NBTSerializableValue;
@@ -26,40 +26,39 @@ import carpet.script.value.ValueConversions;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/** /**
* <p>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 * <p>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.</p> * values.</p>
* *
* @see #register(Class, Function) * @see #register(Class, Function)
* @see #registerToValue(Class, Function) * @param <T> The type to convert from into a {@link Value}
* @param <T> The type to convert from into a {@link LazyValue}
*/ */
public final class OutputConverter<T> public final class OutputConverter<T>
{ {
private static final Map<Class<?>, OutputConverter<?>> byResult = new HashMap<>(); private static final Map<Class<?>, OutputConverter<?>> byResult = new HashMap<>();
private static final OutputConverter<Value> VALUE = new OutputConverter<>(v -> (c, t) -> v); private static final OutputConverter<Value> VALUE = new OutputConverter<>(Function.identity());
static static
{ {
register(LazyValue.class, Function.identity()); // Primitives are handled. Things are boxed in the process anyway, therefore // Primitives are handled. Things are boxed in the process anyway, therefore would recommend boxed outputs, so you can use null
register(Boolean.class, v -> (v ? LazyValue.TRUE : LazyValue.FALSE)); // would recommend boxed outputs, so you can use null register(Void.TYPE, v -> Value.NULL);
register(Void.TYPE, v -> LazyValue.NULL); register(Boolean.class, BooleanValue::of);
registerToValue(Integer.class, NumericValue::of); register(Integer.class, NumericValue::new);
registerToValue(Double.class, NumericValue::of); register(Double.class, NumericValue::of);
registerToValue(Float.class, NumericValue::of); register(Float.class, NumericValue::of);
registerToValue(Long.class, NumericValue::of); register(Long.class, NumericValue::new);
registerToValue(String.class, StringValue::new); register(String.class, StringValue::new);
registerToValue(Entity.class, EntityValue::new); register(Entity.class, EntityValue::new);
registerToValue(Component.class, FormattedTextValue::new); register(Component.class, FormattedTextValue::new);
registerToValue(Tag.class, NBTSerializableValue::new); register(Tag.class, NBTSerializableValue::new);
registerToValue(BlockPos.class, ValueConversions::of); register(BlockPos.class, ValueConversions::of);
registerToValue(Vec3.class, ValueConversions::of); register(Vec3.class, ValueConversions::of);
registerToValue(ItemStack.class, ValueConversions::of); register(ItemStack.class, ValueConversions::of);
registerToValue(ResourceLocation.class, ValueConversions::of); register(ResourceLocation.class, ValueConversions::of);
registerToValue(GlobalPos.class, ValueConversions::of); register(GlobalPos.class, ValueConversions::of);
} }
private final Function<T, LazyValue> converter; private final Function<T, Value> converter;
private OutputConverter(Function<T, LazyValue> converter) private OutputConverter(Function<T, Value> converter)
{ {
this.converter = converter; this.converter = converter;
} }
@@ -85,27 +84,27 @@ public final class OutputConverter<T>
} }
/** /**
* <p>Converts the given input object into a {@link LazyValue}, to be used in return values of Scarpet functions</p> * <p>Converts the given input object into a {@link Value}, to be used in return values of Scarpet functions</p>
* *
* <p>Returns {@link LazyValue#NULL} if passed a {@code null} input</p> * <p>Returns {@link Value#NULL} if passed a {@code null} input</p>
* *
* @param input The value to convert * @param input The value to convert
* @return The converted value * @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);
} }
/** /**
* <p>Registers a new type to be able to be used as the return value of methods, converting from inputType to a {@link LazyValue}.</p> * <p>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.</p>
* *
* @see #registerToValue(Class, Function)
* @param <T> The type of the input type * @param <T> The type of the input type
* @param inputType The class of T * @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 <T> void register(Class<T> inputType, Function<T, LazyValue> converter) public static <T> void register(Class<T> inputType, Function<T, Value> converter)
{ {
OutputConverter<T> instance = new OutputConverter<>(converter); OutputConverter<T> instance = new OutputConverter<>(converter);
if (byResult.containsKey(inputType)) if (byResult.containsKey(inputType))
@@ -116,21 +115,13 @@ public final class OutputConverter<T>
} }
/** /**
* <p>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.</p>
*
* @see #register(Class, Function) * @see #register(Class, Function)
* @param <T> The type of the input type *
* @param inputType The class of T * @deprecated Just use {@link #register(Class, Function)}, it now does the same as this
* @param converter The function that converts an instance of T to a {@link Value}
*/ */
@Deprecated
public static <T> void registerToValue(Class<T> inputType, Function<T, Value> converter) public static <T> void registerToValue(Class<T> inputType, Function<T, Value> converter)
{ {
OutputConverter<T> instance = new OutputConverter<>(converter.andThen(v -> (c, t) -> v)); register(inputType, converter);
if (byResult.containsKey(inputType))
{
throw new IllegalArgumentException(inputType + " already has a registered OutputConverter");
}
byResult.put(inputType, instance);
} }
} }