package us.myles.ViaVersion.api.remapper; import org.jetbrains.annotations.Nullable; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.exception.InformativeException; public abstract class ValueTransformer implements ValueWriter { private final Type inputType; private final Type outputType; public ValueTransformer(@Nullable Type inputType, Type outputType) { this.inputType = inputType; this.outputType = outputType; } public ValueTransformer(Type outputType) { this(null, outputType); } /** * Transform a value from one type to another * * @param wrapper The current packet * @param inputValue The input value * @return The value to write to the wrapper * @throws Exception Throws exception if it fails to transform a value */ public abstract T2 transform(PacketWrapper wrapper, T1 inputValue) throws Exception; @Override public void write(PacketWrapper writer, T1 inputValue) throws Exception { try { writer.write(outputType, transform(writer, inputValue)); } catch (InformativeException e) { e.addSource(this.getClass()); throw e; } } @Nullable public Type getInputType() { return inputType; } public Type getOutputType() { return outputType; } }