diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketWrapper.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketWrapper.java index 4ca573a0c..8cfdf07fb 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketWrapper.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/packet/PacketWrapper.java @@ -145,6 +145,18 @@ public interface PacketWrapper { */ T passthrough(Type type) throws InformativeException; + /** + * Take a value from the input and write to the output, mapping the output type. + * This only works for types implementing {@link com.viaversion.viaversion.api.type.TypeConverter}, which is generally only the primitive wrapper types. + * + * @param type The type to read. + * @param mappedType The type to write. + * @param The return type of the type you wish to pass through. + * @return The type which was read/written. + * @throws InformativeException If it failed to read or write + */ + T passthroughAndMap(Type type, Type mappedType) throws InformativeException; + /** * Take all the inputs and write them to the output. * diff --git a/api/src/main/java/com/viaversion/viaversion/api/protocol/remapper/PacketHandlers.java b/api/src/main/java/com/viaversion/viaversion/api/protocol/remapper/PacketHandlers.java index 73b21c222..8b9d16cea 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/protocol/remapper/PacketHandlers.java +++ b/api/src/main/java/com/viaversion/viaversion/api/protocol/remapper/PacketHandlers.java @@ -63,8 +63,8 @@ public abstract class PacketHandlers implements PacketHandler { * @param oldType old type * @param newType new type */ - public void map(Type oldType, Type newType) { - handler(wrapper -> wrapper.write(newType, wrapper.read(oldType))); + public void map(Type oldType, Type newType) { + handler(wrapper -> wrapper.passthroughAndMap(oldType, newType)); } /** diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java index 92ad9561a..269d21fa0 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java @@ -123,7 +123,7 @@ public class PacketWrapperImpl implements PacketWrapper { continue; } if (currentIndex == index) { - packetValue.setValue(attemptTransform(type, value)); + packetValue.setValue(value); return; } currentIndex++; @@ -160,7 +160,7 @@ public class PacketWrapperImpl implements PacketWrapper { @Override public void write(Type type, T value) { - packetValues.add(new PacketValue<>(type, attemptTransform(type, value))); + packetValues.add(new PacketValue<>(type, value)); } /** @@ -170,7 +170,7 @@ public class PacketWrapperImpl implements PacketWrapper { * @param value value * @return value if already matching, else the converted value or possibly unmatched value */ - private @Nullable T attemptTransform(Type expectedType, @Nullable T value) { + private @Nullable T attemptTransform(Type expectedType, @Nullable Object value) { if (value != null && !expectedType.getOutputClass().isAssignableFrom(value.getClass())) { // Attempt conversion if (expectedType instanceof TypeConverter) { @@ -180,7 +180,8 @@ public class PacketWrapperImpl implements PacketWrapper { Via.getPlatform().getLogger().warning("Possible type mismatch: " + value.getClass().getName() + " -> " + expectedType.getOutputClass()); } - return value; + //noinspection unchecked + return (T) value; } @Override @@ -196,6 +197,14 @@ public class PacketWrapperImpl implements PacketWrapper { } } + @Override + public T passthroughAndMap(Type type, Type mappedType) throws InformativeException { + final Object value = read(type); + final T mappedValue = attemptTransform(mappedType, value); + write(mappedType, mappedValue); + return mappedValue; + } + @Override public void passthroughAll() throws InformativeException { // Copy previous objects