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 dc22c4278..faaf1f36b 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 @@ -300,11 +300,6 @@ public interface PacketWrapper { */ PacketWrapper apply(Direction direction, State state, int index, List pipeline) throws Exception; - /** - * Cancel this packet from sending - */ - void cancel(); - /** * Check if this packet is cancelled. * @@ -312,6 +307,20 @@ public interface PacketWrapper { */ boolean isCancelled(); + /** + * Cancel this packet from sending. + */ + default void cancel() { + setCancelled(true); + } + + /** + * Sets the cancellation state of the packet. + * + * @param cancel whether the packet should be cancelled + */ + void setCancelled(boolean cancel); + /** * Get the user associated with this Packet * 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 37e44a27e..3acc6906b 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 @@ -30,7 +30,6 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.TypeConverter; import com.viaversion.viaversion.exception.CancelException; import com.viaversion.viaversion.exception.InformativeException; -import com.viaversion.viaversion.util.Pair; import com.viaversion.viaversion.util.PipelineUtil; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFuture; @@ -45,6 +44,8 @@ import org.checkerframework.checker.nullness.qual.Nullable; public class PacketWrapperImpl implements PacketWrapper { private static final Protocol[] PROTOCOL_ARRAY = new Protocol[0]; + private final Deque readableObjects = new ArrayDeque<>(); + private final List packetValues = new ArrayList<>(); private final ByteBuf inputBuffer; private final UserConnection userConnection; private boolean send = true; @@ -53,8 +54,6 @@ public class PacketWrapperImpl implements PacketWrapper { */ private PacketType packetType; private int id; - private final Deque> readableObjects = new ArrayDeque<>(); - private final List> packetValues = new ArrayList<>(); public PacketWrapperImpl(int packetId, @Nullable ByteBuf inputBuffer, UserConnection userConnection) { this.id = packetId; @@ -72,8 +71,10 @@ public class PacketWrapperImpl implements PacketWrapper { @Override public T get(Type type, int index) throws Exception { int currentIndex = 0; - for (Pair packetValue : packetValues) { - if (packetValue.key() != type) continue; + for (PacketValue packetValue : packetValues) { + if (packetValue.type() != type) { + continue; + } if (currentIndex == index) { //noinspection unchecked return (T) packetValue.value(); @@ -86,8 +87,10 @@ public class PacketWrapperImpl implements PacketWrapper { @Override public boolean is(Type type, int index) { int currentIndex = 0; - for (Pair packetValue : packetValues) { - if (packetValue.key() != type) continue; + for (PacketValue packetValue : packetValues) { + if (packetValue.type() != type) { + continue; + } if (currentIndex == index) { return true; } @@ -99,8 +102,10 @@ public class PacketWrapperImpl implements PacketWrapper { @Override public boolean isReadable(Type type, int index) { int currentIndex = 0; - for (Pair packetValue : readableObjects) { - if (packetValue.key().getBaseClass() != type.getBaseClass()) continue; + for (PacketValue packetValue : readableObjects) { + if (packetValue.type().getBaseClass() != type.getBaseClass()) { + continue; + } if (currentIndex == index) { return true; } @@ -113,8 +118,10 @@ public class PacketWrapperImpl implements PacketWrapper { @Override public void set(Type type, int index, T value) throws Exception { int currentIndex = 0; - for (Pair packetValue : packetValues) { - if (packetValue.key() != type) continue; + for (PacketValue packetValue : packetValues) { + if (packetValue.type() != type) { + continue; + } if (currentIndex == index) { packetValue.setValue(attemptTransform(type, value)); return; @@ -140,23 +147,23 @@ public class PacketWrapperImpl implements PacketWrapper { } } - Pair read = readableObjects.poll(); - Type rtype = read.key(); - if (rtype == type - || (type.getBaseClass() == rtype.getBaseClass() - && type.getOutputClass() == rtype.getOutputClass())) { + PacketValue readValue = readableObjects.poll(); + Type readType = readValue.type(); + if (readType == type + || (type.getBaseClass() == readType.getBaseClass() + && type.getOutputClass() == readType.getOutputClass())) { //noinspection unchecked - return (T) read.value(); - } else if (rtype == Type.NOTHING) { + return (T) readValue.value(); + } else if (readType == Type.NOTHING) { return read(type); // retry } else { - throw createInformativeException(new IOException("Unable to read type " + type.getTypeName() + ", found " + read.key().getTypeName()), type, readableObjects.size()); + throw createInformativeException(new IOException("Unable to read type " + type.getTypeName() + ", found " + readValue.type().getTypeName()), type, readableObjects.size()); } } @Override public void write(Type type, T value) { - packetValues.add(new Pair<>(type, attemptTransform(type, value))); + packetValues.add(new PacketValue(type, attemptTransform(type, value))); } /** @@ -207,11 +214,11 @@ public class PacketWrapperImpl implements PacketWrapper { } int index = 0; - for (Pair packetValue : packetValues) { + for (PacketValue packetValue : packetValues) { try { - packetValue.key().write(buffer, packetValue.value()); + packetValue.type().write(buffer, packetValue.value()); } catch (Exception e) { - throw createInformativeException(e, packetValue.key(), index); + throw createInformativeException(e, packetValue.type(), index); } index++; } @@ -392,13 +399,13 @@ public class PacketWrapperImpl implements PacketWrapper { } @Override - public void cancel() { - this.send = false; + public boolean isCancelled() { + return !this.send; } @Override - public boolean isCancelled() { - return !this.send; + public void setCancelled(boolean cancel) { + this.send = !cancel; } @Override @@ -452,7 +459,9 @@ public class PacketWrapperImpl implements PacketWrapper { } private void sendToServer0(Class protocol, boolean skipCurrentPipeline, boolean currentThread) throws Exception { - if (isCancelled()) return; + if (isCancelled()) { + return; + } try { ByteBuf output = constructPacket(protocol, skipCurrentPipeline, Direction.SERVERBOUND); @@ -505,4 +514,26 @@ public class PacketWrapperImpl implements PacketWrapper { ", readableObjects=" + readableObjects + '}'; } + + private static final class PacketValue { + private final Type type; + private Object value; + + private PacketValue(Type type, @Nullable Object value) { + this.type = type; + this.value = value; + } + + public Type type() { + return type; + } + + public @Nullable Object value() { + return value; + } + + public void setValue(@Nullable Object value) { + this.value = value; + } + } }