mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2024-11-22 10:05:12 +01:00
Add PacketWrapper#setCancelled(boolean)
This commit is contained in:
parent
8f97f43f12
commit
7161377794
@ -300,11 +300,6 @@ public interface PacketWrapper {
|
||||
*/
|
||||
PacketWrapper apply(Direction direction, State state, int index, List<Protocol> 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
|
||||
*
|
||||
|
@ -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<PacketValue> readableObjects = new ArrayDeque<>();
|
||||
private final List<PacketValue> 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<Pair<Type, Object>> readableObjects = new ArrayDeque<>();
|
||||
private final List<Pair<Type, Object>> 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> T get(Type<T> type, int index) throws Exception {
|
||||
int currentIndex = 0;
|
||||
for (Pair<Type, Object> 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<Type, Object> 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<Type, Object> 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 <T> void set(Type<T> type, int index, T value) throws Exception {
|
||||
int currentIndex = 0;
|
||||
for (Pair<Type, Object> 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<Type, Object> 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 <T> void write(Type<T> 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<Type, Object> 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<? extends Protocol> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user