mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2024-11-22 01:55:47 +01:00
Only map between different primitive types when explicitly requested
This would otherwise only happen with unsafe type usage
This commit is contained in:
parent
a9c947517c
commit
a993a08b8d
@ -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.
|
||||
*
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user