Some cleanup

Fixes super critical issue #2255
This commit is contained in:
Dan Mulloy 2023-03-25 21:45:29 -05:00
parent aebefded86
commit df3b68df4c
No known key found for this signature in database
GPG Key ID: E3B02DE32FB04AC1
9 changed files with 54 additions and 58 deletions

View File

@ -41,19 +41,19 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @author Kristian * @author Kristian
*/ */
public static class Handshake { public static class Handshake {
private static final Protocol PROTOCOL = Protocol.HANDSHAKING; static final Protocol PROTOCOL = Protocol.HANDSHAKING;
/** /**
* Incoming packets. * Incoming packets.
* @author Kristian * @author Kristian
*/ */
public static class Client extends PacketTypeEnum { public static class Client extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT; private static final Sender SENDER = Sender.CLIENT;
@ForceAsync @ForceAsync
public static final PacketType SET_PROTOCOL = new PacketType(PROTOCOL, SENDER, 0x00, "SetProtocol", "C00Handshake"); public static final PacketType SET_PROTOCOL = new PacketType(PROTOCOL, SENDER, 0x00, "SetProtocol", "C00Handshake");
private final static Client INSTANCE = new Client(); private static final Client INSTANCE = new Client();
// Prevent accidental construction // Prevent accidental construction
private Client() { super(); } private Client() { super(); }
@ -71,8 +71,8 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @author Kristian * @author Kristian
*/ */
public static class Server extends PacketTypeEnum { public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT; private static final Sender SENDER = Sender.CLIENT;
private final static Server INSTANCE = new Server(); private static final Server INSTANCE = new Server();
private Server() { super(); } private Server() { super(); }
public static Server getInstance() { public static Server getInstance() {
@ -93,16 +93,16 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @author Kristian * @author Kristian
*/ */
public static class Play { public static class Play {
private static final Protocol PROTOCOL = Protocol.PLAY; static final Protocol PROTOCOL = Protocol.PLAY;
/** /**
* Outgoing packets. * Outgoing packets.
* @author Kristian * @author Kristian
*/ */
public static class Server extends PacketTypeEnum { public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.SERVER; private static final Sender SENDER = Sender.SERVER;
public static final PacketType DELIMITER = new PacketType(PROTOCOL, SENDER, 0x00, "Delimiter", "BundleDelimiterPacket"); public static final PacketType BUNDLE = new PacketType(PROTOCOL, SENDER, 0x00, "Delimiter", "BundleDelimiterPacket");
public static final PacketType SPAWN_ENTITY = new PacketType(PROTOCOL, SENDER, 0x01, "SpawnEntity", "SPacketSpawnObject"); public static final PacketType SPAWN_ENTITY = new PacketType(PROTOCOL, SENDER, 0x01, "SpawnEntity", "SPacketSpawnObject");
public static final PacketType SPAWN_ENTITY_EXPERIENCE_ORB = new PacketType(PROTOCOL, SENDER, 0x02, "SpawnEntityExperienceOrb", "SPacketSpawnExperienceOrb"); public static final PacketType SPAWN_ENTITY_EXPERIENCE_ORB = new PacketType(PROTOCOL, SENDER, 0x02, "SpawnEntityExperienceOrb", "SPacketSpawnExperienceOrb");
public static final PacketType NAMED_ENTITY_SPAWN = new PacketType(PROTOCOL, SENDER, 0x03, "NamedEntitySpawn", "SPacketSpawnPlayer"); public static final PacketType NAMED_ENTITY_SPAWN = new PacketType(PROTOCOL, SENDER, 0x03, "NamedEntitySpawn", "SPacketSpawnPlayer");
@ -367,7 +367,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
@Deprecated @Deprecated
public static final PacketType CUSTOM_SOUND_EFFECT = new PacketType(PROTOCOL, SENDER, 0x16, "CustomSoundEffect", "SPacketCustomSound"); public static final PacketType CUSTOM_SOUND_EFFECT = new PacketType(PROTOCOL, SENDER, 0x16, "CustomSoundEffect", "SPacketCustomSound");
private final static Server INSTANCE = new Server(); private static final Server INSTANCE = new Server();
// Prevent accidental construction // Prevent accidental construction
private Server() { super(); } private Server() { super(); }
@ -385,7 +385,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @author Kristian * @author Kristian
*/ */
public static class Client extends PacketTypeEnum { public static class Client extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT; private static final Sender SENDER = Sender.CLIENT;
public static final PacketType TELEPORT_ACCEPT = new PacketType(PROTOCOL, SENDER, 0x00, "TeleportAccept", "CPacketConfirmTeleport"); public static final PacketType TELEPORT_ACCEPT = new PacketType(PROTOCOL, SENDER, 0x00, "TeleportAccept", "CPacketConfirmTeleport");
public static final PacketType TILE_NBT_QUERY = new PacketType(PROTOCOL, SENDER, 0x01, "TileNBTQuery"); public static final PacketType TILE_NBT_QUERY = new PacketType(PROTOCOL, SENDER, 0x01, "TileNBTQuery");
@ -410,7 +410,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
public static final PacketType POSITION = new PacketType(PROTOCOL, SENDER, 0x14, "Flying$Position", "Flying$PacketPlayInPosition", "CPacketPlayer$Position"); public static final PacketType POSITION = new PacketType(PROTOCOL, SENDER, 0x14, "Flying$Position", "Flying$PacketPlayInPosition", "CPacketPlayer$Position");
public static final PacketType POSITION_LOOK = new PacketType(PROTOCOL, SENDER, 0x15, "Flying$PositionLook", "Flying$PacketPlayInPositionLook", "CPacketPlayer$PositionRotation"); public static final PacketType POSITION_LOOK = new PacketType(PROTOCOL, SENDER, 0x15, "Flying$PositionLook", "Flying$PacketPlayInPositionLook", "CPacketPlayer$PositionRotation");
public static final PacketType LOOK = new PacketType(PROTOCOL, SENDER, 0x16, "Flying$Look", "Flying$PacketPlayInLook", "CPacketPlayer$Rotation"); public static final PacketType LOOK = new PacketType(PROTOCOL, SENDER, 0x16, "Flying$Look", "Flying$PacketPlayInLook", "CPacketPlayer$Rotation");
public static final PacketType GROUND = new PacketType(PROTOCOL, SENDER, 0x17, "Flying$", "Flying$d"); public static final PacketType GROUND = new PacketType(PROTOCOL, SENDER, 0x17, "Flying$d");
public static final PacketType VEHICLE_MOVE = new PacketType(PROTOCOL, SENDER, 0x18, "VehicleMove", "CPacketVehicleMove"); public static final PacketType VEHICLE_MOVE = new PacketType(PROTOCOL, SENDER, 0x18, "VehicleMove", "CPacketVehicleMove");
public static final PacketType BOAT_MOVE = new PacketType(PROTOCOL, SENDER, 0x19, "BoatMove", "CPacketSteerBoat"); public static final PacketType BOAT_MOVE = new PacketType(PROTOCOL, SENDER, 0x19, "BoatMove", "CPacketSteerBoat");
public static final PacketType PICK_ITEM = new PacketType(PROTOCOL, SENDER, 0x1A, "PickItem"); public static final PacketType PICK_ITEM = new PacketType(PROTOCOL, SENDER, 0x1A, "PickItem");
@ -457,7 +457,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
@Deprecated @Deprecated
public static final PacketType CHAT_PREVIEW = new PacketType(PROTOCOL, SENDER, 0x06, "ChatPreview"); public static final PacketType CHAT_PREVIEW = new PacketType(PROTOCOL, SENDER, 0x06, "ChatPreview");
private final static Client INSTANCE = new Client(); private static final Client INSTANCE = new Client();
// Prevent accidental construction // Prevent accidental construction
private Client() { super(); } private Client() { super(); }
@ -480,14 +480,14 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @author Kristian * @author Kristian
*/ */
public static class Status { public static class Status {
private static final Protocol PROTOCOL = Protocol.STATUS; static final Protocol PROTOCOL = Protocol.STATUS;
/** /**
* Outgoing packets. * Outgoing packets.
* @author Kristian * @author Kristian
*/ */
public static class Server extends PacketTypeEnum { public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.SERVER; private static final Sender SENDER = Sender.SERVER;
@ForceAsync @ForceAsync
public static final PacketType SERVER_INFO = new PacketType(PROTOCOL, SENDER, 0x00, "ServerInfo", "SPacketServerInfo"); public static final PacketType SERVER_INFO = new PacketType(PROTOCOL, SENDER, 0x00, "ServerInfo", "SPacketServerInfo");
@ -501,7 +501,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
@ForceAsync @ForceAsync
public static final PacketType OUT_SERVER_INFO = SERVER_INFO.clone(); public static final PacketType OUT_SERVER_INFO = SERVER_INFO.clone();
private final static Server INSTANCE = new Server(); private static final Server INSTANCE = new Server();
// Prevent accidental construction // Prevent accidental construction
private Server() { super(); } private Server() { super(); }
@ -519,13 +519,13 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @author Kristian * @author Kristian
*/ */
public static class Client extends PacketTypeEnum { public static class Client extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT; private static final Sender SENDER = Sender.CLIENT;
public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, "Start", "CPacketServerQuery"); public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, "Start", "CPacketServerQuery");
@ForceAsync @ForceAsync
public static final PacketType PING = new PacketType(PROTOCOL, SENDER, 0x01, "Ping", "CPacketPing"); public static final PacketType PING = new PacketType(PROTOCOL, SENDER, 0x01, "Ping", "CPacketPing");
private final static Client INSTANCE = new Client(); private static final Client INSTANCE = new Client();
// Prevent accidental construction // Prevent accidental construction
private Client() { super(); } private Client() { super(); }
@ -548,14 +548,14 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @author Kristian * @author Kristian
*/ */
public static class Login { public static class Login {
private static final Protocol PROTOCOL = Protocol.LOGIN; static final Protocol PROTOCOL = Protocol.LOGIN;
/** /**
* Outgoing packets. * Outgoing packets.
* @author Kristian * @author Kristian
*/ */
public static class Server extends PacketTypeEnum { public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.SERVER; private static final Sender SENDER = Sender.SERVER;
@ForceAsync @ForceAsync
public static final PacketType DISCONNECT = new PacketType(PROTOCOL, SENDER, 0x00, "Disconnect", "SPacketDisconnect"); public static final PacketType DISCONNECT = new PacketType(PROTOCOL, SENDER, 0x00, "Disconnect", "SPacketDisconnect");
@ -564,7 +564,7 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
public static final PacketType SET_COMPRESSION = new PacketType(PROTOCOL, SENDER, 0x03, "SetCompression", "SPacketEnableCompression"); public static final PacketType SET_COMPRESSION = new PacketType(PROTOCOL, SENDER, 0x03, "SetCompression", "SPacketEnableCompression");
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x04, "CustomPayload", "SPacketCustomPayload"); public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x04, "CustomPayload", "SPacketCustomPayload");
private final static Server INSTANCE = new Server(); private static final Server INSTANCE = new Server();
// Prevent accidental construction // Prevent accidental construction
private Server() { super(); } private Server() { super(); }
@ -582,13 +582,13 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @author Kristian * @author Kristian
*/ */
public static class Client extends PacketTypeEnum { public static class Client extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT; private static final Sender SENDER = Sender.CLIENT;
public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, "Start", "CPacketLoginStart"); public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, "Start", "CPacketLoginStart");
public static final PacketType ENCRYPTION_BEGIN = new PacketType(PROTOCOL, SENDER, 0x01, "EncryptionBegin", "CPacketEncryptionResponse"); public static final PacketType ENCRYPTION_BEGIN = new PacketType(PROTOCOL, SENDER, 0x01, "EncryptionBegin", "CPacketEncryptionResponse");
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x02, "CustomPayload", "CPacketCustomPayload"); public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x02, "CustomPayload", "CPacketCustomPayload");
private final static Client INSTANCE = new Client(); private static final Client INSTANCE = new Client();
// Prevent accidental construction // Prevent accidental construction
private Client() { super(); } private Client() { super(); }

View File

@ -121,9 +121,7 @@ public final class SortedPacketListenerList extends AbstractConcurrentListenerMu
event.setReadOnly(element.getPriority() == ListenerPriority.MONITOR); event.setReadOnly(element.getPriority() == ListenerPriority.MONITOR);
element.getListener().onPacketReceiving(event); element.getListener().onPacketReceiving(event);
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError | ThreadDeath e) {
throw e;
} catch (ThreadDeath e) {
throw e; throw e;
} catch (Throwable e) { } catch (Throwable e) {
// Minecraft doesn't want your Exception. // Minecraft doesn't want your Exception.
@ -149,7 +147,7 @@ public final class SortedPacketListenerList extends AbstractConcurrentListenerMu
*/ */
public void invokePacketSending(ErrorReporter reporter, PacketEvent event, @Nullable ListenerPriority priorityFilter) { public void invokePacketSending(ErrorReporter reporter, PacketEvent event, @Nullable ListenerPriority priorityFilter) {
invokeUnpackedPacketSending(reporter, event, priorityFilter); invokeUnpackedPacketSending(reporter, event, priorityFilter);
if(event.getPacketType() == PacketType.Play.Server.DELIMITER && !event.isCancelled()) { if (event.getPacketType() == PacketType.Play.Server.BUNDLE && !event.isCancelled()) {
// unpack the bundle and invoke for each packet in the bundle // unpack the bundle and invoke for each packet in the bundle
Iterable<PacketContainer> packets = event.getPacket().getPacketBundles().read(0); Iterable<PacketContainer> packets = event.getPacket().getPacketBundles().read(0);
List<PacketContainer> outPackets = new ArrayList<>(); List<PacketContainer> outPackets = new ArrayList<>();
@ -158,13 +156,16 @@ public final class SortedPacketListenerList extends AbstractConcurrentListenerMu
invokeUnpackedPacketSending(reporter, subPacketEvent, priorityFilter); invokeUnpackedPacketSending(reporter, subPacketEvent, priorityFilter);
if (!subPacketEvent.isCancelled()) { if (!subPacketEvent.isCancelled()) {
outPackets.add(subPacketEvent.getPacket()); // if the packet event has been cancelled, the packet will be removed from the bundle. // if the packet event has been cancelled, the packet will be removed from the bundle
outPackets.add(subPacketEvent.getPacket());
} }
} }
if(packets.iterator().hasNext()) { // are there still packets in this bundle?
if (packets.iterator().hasNext()) {
event.getPacket().getPacketBundles().write(0, outPackets); event.getPacket().getPacketBundles().write(0, outPackets);
} else { } else {
event.setCancelled(true); // cancel packet if each individual packet has been canceled // cancel entire packet if each individual packet has been cancelled
event.setCancelled(true);
} }
} }
} }
@ -201,14 +202,12 @@ public final class SortedPacketListenerList extends AbstractConcurrentListenerMu
* @param event - the related packet event. * @param event - the related packet event.
* @param element - the listener to invoke. * @param element - the listener to invoke.
*/ */
private final void invokeSendingListener(ErrorReporter reporter, PacketEvent event, PrioritizedListener<PacketListener> element) { private void invokeSendingListener(ErrorReporter reporter, PacketEvent event, PrioritizedListener<PacketListener> element) {
try { try {
event.setReadOnly(element.getPriority() == ListenerPriority.MONITOR); event.setReadOnly(element.getPriority() == ListenerPriority.MONITOR);
element.getListener().onPacketSending(event); element.getListener().onPacketSending(event);
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError | ThreadDeath e) {
throw e;
} catch (ThreadDeath e) {
throw e; throw e;
} catch (Throwable e) { } catch (Throwable e) {
// Minecraft doesn't want your Exception. // Minecraft doesn't want your Exception.

View File

@ -140,6 +140,7 @@ public class StructureCache {
if (MinecraftVersion.atOrAbove(MinecraftVersion.FEATURE_PREVIEW_2) && packetClass.equals(MinecraftReflection.getBundleDelimiterClass())) { if (MinecraftVersion.atOrAbove(MinecraftVersion.FEATURE_PREVIEW_2) && packetClass.equals(MinecraftReflection.getBundleDelimiterClass())) {
packetClass = MinecraftReflection.getPackedBundlePacketClass(); packetClass = MinecraftReflection.getPackedBundlePacketClass();
} }
return new StructureModifier<>(packetClass, MinecraftReflection.getPacketClass(), true); return new StructureModifier<>(packetClass, MinecraftReflection.getPacketClass(), true);
}); });
} }

View File

@ -553,7 +553,7 @@ public class NettyChannelInjector implements Injector {
} }
// no listener and no marker - no magic :) // no listener and no marker - no magic :)
if (!this.channelListener.hasListener(packet.getClass()) && marker == null && !Util.isBundlePacket(packet.getClass())) { if (!this.channelListener.hasListener(packet.getClass()) && marker == null && !MinecraftReflection.isBundlePacket(packet.getClass())) {
return action; return action;
} }

View File

@ -91,7 +91,7 @@ public class NetworkManagerInjector implements ChannelListener {
public PacketEvent onPacketSending(Injector injector, Object packet, NetworkMarker marker) { public PacketEvent onPacketSending(Injector injector, Object packet, NetworkMarker marker) {
// check if we need to intercept the packet // check if we need to intercept the packet
Class<?> packetClass = packet.getClass(); Class<?> packetClass = packet.getClass();
if (this.outboundListeners.contains(packetClass) || marker != null || Util.isBundlePacket(packetClass)) { if (marker != null || MinecraftReflection.isBundlePacket(packetClass) || outboundListeners.contains(packetClass)) {
// wrap packet and construct the event // wrap packet and construct the event
PacketContainer container = new PacketContainer(PacketRegistry.getPacketType(packetClass), packet); PacketContainer container = new PacketContainer(PacketRegistry.getPacketType(packetClass), packet);
PacketEvent packetEvent = PacketEvent.fromServer(this, container, marker, injector.getPlayer()); PacketEvent packetEvent = PacketEvent.fromServer(this, container, marker, injector.getPlayer());
@ -109,7 +109,7 @@ public class NetworkManagerInjector implements ChannelListener {
public PacketEvent onPacketReceiving(Injector injector, Object packet, NetworkMarker marker) { public PacketEvent onPacketReceiving(Injector injector, Object packet, NetworkMarker marker) {
// check if we need to intercept the packet // check if we need to intercept the packet
Class<?> packetClass = packet.getClass(); Class<?> packetClass = packet.getClass();
if (this.inboundListeners.contains(packetClass) || marker != null) { if (marker != null || inboundListeners.contains(packetClass)) {
// wrap the packet and construct the event // wrap the packet and construct the event
PacketContainer container = new PacketContainer(PacketRegistry.getPacketType(packetClass), packet); PacketContainer container = new PacketContainer(PacketRegistry.getPacketType(packetClass), packet);
PacketEvent packetEvent = PacketEvent.fromClient(this, container, marker, injector.getPlayer()); PacketEvent packetEvent = PacketEvent.fromClient(this, container, marker, injector.getPlayer());

View File

@ -46,12 +46,12 @@ public class PacketRegistry {
*/ */
protected static class Register { protected static class Register {
// The main lookup table // The main lookup table
private final Map<PacketType, Optional<Class<?>>> typeToClass = new ConcurrentHashMap<>(); final Map<PacketType, Optional<Class<?>>> typeToClass = new ConcurrentHashMap<>();
private final Map<Class<?>, PacketType> classToType = new ConcurrentHashMap<>(); final Map<Class<?>, PacketType> classToType = new ConcurrentHashMap<>();
private volatile Set<PacketType> serverPackets = new HashSet<>(); volatile Set<PacketType> serverPackets = new HashSet<>();
private volatile Set<PacketType> clientPackets = new HashSet<>(); volatile Set<PacketType> clientPackets = new HashSet<>();
private final List<MapContainer> containers = new ArrayList<>(); final List<MapContainer> containers = new ArrayList<>();
public Register() {} public Register() {}
@ -383,10 +383,12 @@ public class PacketRegistry {
* @return The packet type, or NULL if not found. * @return The packet type, or NULL if not found.
*/ */
public static PacketType getPacketType(Class<?> packet) { public static PacketType getPacketType(Class<?> packet) {
if(Util.isBundlePacket(packet)) {
return PacketType.Play.Server.DELIMITER;
}
initialize(); initialize();
if (MinecraftReflection.isBundlePacket(packet)) {
return PacketType.Play.Server.BUNDLE;
}
return REGISTER.classToType.get(packet); return REGISTER.classToType.get(packet);
} }

View File

@ -632,6 +632,10 @@ public final class MinecraftReflection {
return getMinecraftClass("network.protocol.game.ClientboundBundlePacket", "ClientboundBundlePacket"); return getMinecraftClass("network.protocol.game.ClientboundBundlePacket", "ClientboundBundlePacket");
} }
public static boolean isBundlePacket(Class<?> packetClass) {
return MinecraftVersion.FEATURE_PREVIEW_2.atOrAbove() && packetClass.equals(getPackedBundlePacketClass());
}
public static Class<?> getBundleDelimiterClass() { public static Class<?> getBundleDelimiterClass() {
return getMinecraftClass("network.protocol.BundleDelimiterPacket","BundleDelimiterPacket"); return getMinecraftClass("network.protocol.BundleDelimiterPacket","BundleDelimiterPacket");
} }

View File

@ -60,14 +60,4 @@ public final class Util {
} }
return false; return false;
} }
public static boolean isBundlePacket(Class<?> packetClass) {
if(!MinecraftVersion.atOrAbove(MinecraftVersion.FEATURE_PREVIEW_2)) {
return false;
}
if(cachedBundleClass == null) {
cachedBundleClass = MinecraftReflection.getPackedBundlePacketClass();
}
return packetClass.equals(cachedBundleClass);
}
} }

View File

@ -828,7 +828,7 @@ public class PacketContainerTest {
for (PacketType type : PacketType.values()) { for (PacketType type : PacketType.values()) {
// TODO: try to support chat - for now chat contains to many sub classes to properly clone it // TODO: try to support chat - for now chat contains to many sub classes to properly clone it
if (type.isDeprecated() || !type.isSupported() || type.name().contains("CUSTOM_PAYLOAD") || type.name().contains("CHAT") if (type.isDeprecated() || !type.isSupported() || type.name().contains("CUSTOM_PAYLOAD") || type.name().contains("CHAT")
|| type.name().contains("DELIMITER")) { || type.name().contains("BUNDLE")) {
continue; continue;
} }