3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-27 16:40:10 +01:00

Only map between different primitive types when explicitly requested

This would otherwise only happen with unsafe type usage
Dieser Commit ist enthalten in:
Nassim Jahnke 2024-08-24 18:00:06 +02:00
Ursprung a9c947517c
Commit a993a08b8d
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
3 geänderte Dateien mit 27 neuen und 6 gelöschten Zeilen

Datei anzeigen

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

Datei anzeigen

@ -63,8 +63,8 @@ public abstract class PacketHandlers implements PacketHandler {
* @param oldType old type * @param oldType old type
* @param newType new type * @param newType new type
*/ */
public void map(Type oldType, Type newType) { public void map(Type<?> oldType, Type<?> newType) {
handler(wrapper -> wrapper.write(newType, wrapper.read(oldType))); handler(wrapper -> wrapper.passthroughAndMap(oldType, newType));
} }
/** /**

Datei anzeigen

@ -123,7 +123,7 @@ public class PacketWrapperImpl implements PacketWrapper {
continue; continue;
} }
if (currentIndex == index) { if (currentIndex == index) {
packetValue.setValue(attemptTransform(type, value)); packetValue.setValue(value);
return; return;
} }
currentIndex++; currentIndex++;
@ -160,7 +160,7 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override @Override
public <T> void write(Type<T> type, T value) { 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 * @param value value
* @return value if already matching, else the converted value or possibly unmatched 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())) { if (value != null && !expectedType.getOutputClass().isAssignableFrom(value.getClass())) {
// Attempt conversion // Attempt conversion
if (expectedType instanceof TypeConverter<?>) { 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()); Via.getPlatform().getLogger().warning("Possible type mismatch: " + value.getClass().getName() + " -> " + expectedType.getOutputClass());
} }
return value; //noinspection unchecked
return (T) value;
} }
@Override @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 @Override
public void passthroughAll() throws InformativeException { public void passthroughAll() throws InformativeException {
// Copy previous objects // Copy previous objects