Only map between different primitive types when explicitly requested

This would otherwise only happen with unsafe type usage
This commit is contained in:
Nassim Jahnke 2024-08-24 18:00:06 +02:00
parent a9c947517c
commit a993a08b8d
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
3 changed files with 27 additions and 6 deletions

View File

@ -145,6 +145,18 @@ public interface PacketWrapper {
*/
<T> T passthrough(Type<T> 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 <T> 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> T passthroughAndMap(Type<?> type, Type<T> mappedType) throws InformativeException;
/**
* Take all the inputs and write them to the output.
*

View File

@ -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));
}
/**

View File

@ -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 <T> void write(Type<T> 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 <T> @Nullable T attemptTransform(Type<T> expectedType, @Nullable T value) {
private <T> @Nullable T attemptTransform(Type<T> 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> T passthroughAndMap(Type<?> type, Type<T> 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