3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-28 09:00:09 +01:00

Give PacketValue a generic type

Dieser Commit ist enthalten in:
Nassim Jahnke 2023-10-20 12:31:16 +10:00
Ursprung 8ebc8c34f3
Commit 84a054aac1
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F

Datei anzeigen

@ -46,8 +46,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public class PacketWrapperImpl implements PacketWrapper { public class PacketWrapperImpl implements PacketWrapper {
private static final Protocol[] PROTOCOL_ARRAY = new Protocol[0]; private static final Protocol[] PROTOCOL_ARRAY = new Protocol[0];
private final Deque<PacketValue> readableObjects = new ArrayDeque<>(); private final Deque<PacketValue<?>> readableObjects = new ArrayDeque<>();
private final List<PacketValue> packetValues = new ArrayList<>(); private final List<PacketValue<?>> packetValues = new ArrayList<>();
private final ByteBuf inputBuffer; private final ByteBuf inputBuffer;
private final UserConnection userConnection; private final UserConnection userConnection;
private boolean send = true; private boolean send = true;
@ -73,7 +73,7 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override @Override
public <T> T get(Type<T> type, int index) throws Exception { public <T> T get(Type<T> type, int index) throws Exception {
int currentIndex = 0; int currentIndex = 0;
for (PacketValue packetValue : packetValues) { for (PacketValue<?> packetValue : packetValues) {
if (packetValue.type() != type) { if (packetValue.type() != type) {
continue; continue;
} }
@ -89,7 +89,7 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override @Override
public boolean is(Type type, int index) { public boolean is(Type type, int index) {
int currentIndex = 0; int currentIndex = 0;
for (PacketValue packetValue : packetValues) { for (PacketValue<?> packetValue : packetValues) {
if (packetValue.type() != type) { if (packetValue.type() != type) {
continue; continue;
} }
@ -104,7 +104,7 @@ public class PacketWrapperImpl implements PacketWrapper {
@Override @Override
public boolean isReadable(Type type, int index) { public boolean isReadable(Type type, int index) {
int currentIndex = 0; int currentIndex = 0;
for (PacketValue packetValue : readableObjects) { for (PacketValue<?> packetValue : readableObjects) {
if (packetValue.type().getBaseClass() != type.getBaseClass()) { if (packetValue.type().getBaseClass() != type.getBaseClass()) {
continue; continue;
} }
@ -146,7 +146,7 @@ public class PacketWrapperImpl implements PacketWrapper {
} }
PacketValue readValue = readableObjects.poll(); PacketValue readValue = readableObjects.poll();
Type readType = readValue.type(); Type<?> readType = readValue.type();
if (readType == type if (readType == type
|| (type.getBaseClass() == readType.getBaseClass() || (type.getBaseClass() == readType.getBaseClass()
&& type.getOutputClass() == readType.getOutputClass())) { && type.getOutputClass() == readType.getOutputClass())) {
@ -159,7 +159,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, attemptTransform(type, value)));
} }
/** /**
@ -169,11 +169,12 @@ 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 @Nullable Object attemptTransform(Type<?> expectedType, @Nullable Object value) { private <T> @Nullable T attemptTransform(Type<T> expectedType, @Nullable T 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<?>) {
return ((TypeConverter<?>) expectedType).from(value); //noinspection unchecked
return ((TypeConverter<T>) expectedType).from(value);
} }
Via.getPlatform().getLogger().warning("Possible type mismatch: " + value.getClass().getName() + " -> " + expectedType.getOutputClass()); Via.getPlatform().getLogger().warning("Possible type mismatch: " + value.getClass().getName() + " -> " + expectedType.getOutputClass());
@ -210,10 +211,10 @@ public class PacketWrapperImpl implements PacketWrapper {
} }
int index = 0; int index = 0;
for (PacketValue packetValue : packetValues) { for (final PacketValue<?> packetValue : packetValues) {
try { try {
packetValue.type().write(buffer, packetValue.value()); packetValue.write(buffer);
} catch (Exception e) { } catch (final Exception e) {
throw createInformativeException(e, packetValue.type(), index); throw createInformativeException(e, packetValue.type(), index);
} }
index++; index++;
@ -563,16 +564,16 @@ public class PacketWrapperImpl implements PacketWrapper {
'}'; '}';
} }
public static final class PacketValue { public static final class PacketValue<T> {
private final Type type; private final Type<T> type;
private Object value; private T value;
private PacketValue(Type type, @Nullable Object value) { private PacketValue(final Type<T> type, @Nullable final T value) {
this.type = type; this.type = type;
this.value = value; this.value = value;
} }
public Type type() { public Type<T> type() {
return type; return type;
} }
@ -580,7 +581,11 @@ public class PacketWrapperImpl implements PacketWrapper {
return value; return value;
} }
public void setValue(@Nullable Object value) { public void write(final ByteBuf buffer) throws Exception {
type.write(buffer, value);
}
public void setValue(@Nullable final T value) {
this.value = value; this.value = value;
} }
@ -588,7 +593,7 @@ public class PacketWrapperImpl implements PacketWrapper {
public boolean equals(final Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
final PacketValue that = (PacketValue) o; final PacketValue<?> that = (PacketValue<?>) o;
if (!type.equals(that.type)) return false; if (!type.equals(that.type)) return false;
return Objects.equals(value, that.value); return Objects.equals(value, that.value);
} }