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:
2 files changed
+36
-44
No files matched your search
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
* <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>
|
||||
*
|
||||
* @see #register(Class, Function)
|
||||
* @see #registerToValue(Class, Function)
|
||||
* @param <T> The type to convert from into a {@link LazyValue}
|
||||
* @param <T> The type to convert from into a {@link Value}
|
||||
*/
|
||||
public final class OutputConverter<T>
|
||||
{
|
||||
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
|
||||
{
|
||||
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<T, LazyValue> converter;
|
||||
private final Function<T, Value> converter;
|
||||
|
||||
private OutputConverter(Function<T, LazyValue> converter)
|
||||
private OutputConverter(Function<T, Value> 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
|
||||
* @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 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);
|
||||
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)
|
||||
* @param <T> 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 <T> void registerToValue(Class<T> inputType, Function<T, Value> converter)
|
||||
{
|
||||
OutputConverter<T> 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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user