Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
Fixes super critical issue dmulloy2#2255
  • Loading branch information
dmulloy2 committed Mar 26, 2023
1 parent aebefde commit df3b68d
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 58 deletions.
44 changes: 22 additions & 22 deletions src/main/java/com/comphenix/protocol/PacketType.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
* @author Kristian
*/
public static class Handshake {
private static final Protocol PROTOCOL = Protocol.HANDSHAKING;
static final Protocol PROTOCOL = Protocol.HANDSHAKING;

/**
* Incoming packets.
* @author Kristian
*/
public static class Client extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT;
private static final Sender SENDER = Sender.CLIENT;

@ForceAsync
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
private Client() { super(); }
Expand All @@ -71,8 +71,8 @@ public static Sender getSender() {
* @author Kristian
*/
public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.CLIENT;
private final static Server INSTANCE = new Server();
private static final Sender SENDER = Sender.CLIENT;
private static final Server INSTANCE = new Server();
private Server() { super(); }

public static Server getInstance() {
Expand All @@ -93,16 +93,16 @@ public static Protocol getProtocol() {
* @author Kristian
*/
public static class Play {
private static final Protocol PROTOCOL = Protocol.PLAY;
static final Protocol PROTOCOL = Protocol.PLAY;

/**
* Outgoing packets.
* @author Kristian
*/
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_EXPERIENCE_ORB = new PacketType(PROTOCOL, SENDER, 0x02, "SpawnEntityExperienceOrb", "SPacketSpawnExperienceOrb");
public static final PacketType NAMED_ENTITY_SPAWN = new PacketType(PROTOCOL, SENDER, 0x03, "NamedEntitySpawn", "SPacketSpawnPlayer");
Expand Down Expand Up @@ -367,7 +367,7 @@ public static class Server extends PacketTypeEnum {
@Deprecated
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
private Server() { super(); }
Expand All @@ -385,7 +385,7 @@ public static Server getInstance() {
* @author Kristian
*/
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 TILE_NBT_QUERY = new PacketType(PROTOCOL, SENDER, 0x01, "TileNBTQuery");
Expand All @@ -410,7 +410,7 @@ public static class Client extends PacketTypeEnum {
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 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 BOAT_MOVE = new PacketType(PROTOCOL, SENDER, 0x19, "BoatMove", "CPacketSteerBoat");
public static final PacketType PICK_ITEM = new PacketType(PROTOCOL, SENDER, 0x1A, "PickItem");
Expand Down Expand Up @@ -457,7 +457,7 @@ public static class Client extends PacketTypeEnum {
@Deprecated
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
private Client() { super(); }
Expand All @@ -480,14 +480,14 @@ public static Protocol getProtocol() {
* @author Kristian
*/
public static class Status {
private static final Protocol PROTOCOL = Protocol.STATUS;
static final Protocol PROTOCOL = Protocol.STATUS;

/**
* Outgoing packets.
* @author Kristian
*/
public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.SERVER;
private static final Sender SENDER = Sender.SERVER;

@ForceAsync
public static final PacketType SERVER_INFO = new PacketType(PROTOCOL, SENDER, 0x00, "ServerInfo", "SPacketServerInfo");
Expand All @@ -501,7 +501,7 @@ public static class Server extends PacketTypeEnum {
@ForceAsync
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
private Server() { super(); }
Expand All @@ -519,13 +519,13 @@ public static Server getInstance() {
* @author Kristian
*/
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");
@ForceAsync
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
private Client() { super(); }
Expand All @@ -548,14 +548,14 @@ public static Protocol getProtocol() {
* @author Kristian
*/
public static class Login {
private static final Protocol PROTOCOL = Protocol.LOGIN;
static final Protocol PROTOCOL = Protocol.LOGIN;

/**
* Outgoing packets.
* @author Kristian
*/
public static class Server extends PacketTypeEnum {
private final static Sender SENDER = Sender.SERVER;
private static final Sender SENDER = Sender.SERVER;

@ForceAsync
public static final PacketType DISCONNECT = new PacketType(PROTOCOL, SENDER, 0x00, "Disconnect", "SPacketDisconnect");
Expand All @@ -564,7 +564,7 @@ public static class Server extends PacketTypeEnum {
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");

private final static Server INSTANCE = new Server();
private static final Server INSTANCE = new Server();

// Prevent accidental construction
private Server() { super(); }
Expand All @@ -582,13 +582,13 @@ public static Server getInstance() {
* @author Kristian
*/
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 ENCRYPTION_BEGIN = new PacketType(PROTOCOL, SENDER, 0x01, "EncryptionBegin", "CPacketEncryptionResponse");
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
private Client() { super(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ private void invokeReceivingListener(ErrorReporter reporter, PacketEvent event,
event.setReadOnly(element.getPriority() == ListenerPriority.MONITOR);
element.getListener().onPacketReceiving(event);

} catch (OutOfMemoryError e) {
throw e;
} catch (ThreadDeath e) {
} catch (OutOfMemoryError | ThreadDeath e) {
throw e;
} catch (Throwable e) {
// Minecraft doesn't want your Exception.
Expand All @@ -149,22 +147,25 @@ public void invokePacketSending(ErrorReporter reporter, PacketEvent event) {
*/
public void invokePacketSending(ErrorReporter reporter, PacketEvent event, @Nullable ListenerPriority 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
Iterable<PacketContainer> packets = event.getPacket().getPacketBundles().read(0);
List<PacketContainer> outPackets = new ArrayList<>();
for(PacketContainer subPacket : packets) {
for (PacketContainer subPacket : packets) {
PacketEvent subPacketEvent = PacketEvent.fromServer(this, subPacket, event.getNetworkMarker(), event.getPlayer());
invokeUnpackedPacketSending(reporter, subPacketEvent, priorityFilter);

if(!subPacketEvent.isCancelled()) {
outPackets.add(subPacketEvent.getPacket()); // if the packet event has been cancelled, the packet will be removed from the bundle.
if (!subPacketEvent.isCancelled()) {
// 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);
} 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);
}
}
}
Expand Down Expand Up @@ -201,14 +202,12 @@ private void invokeUnpackedPacketSending(ErrorReporter reporter, PacketEvent eve
* @param event - the related packet event.
* @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 {
event.setReadOnly(element.getPriority() == ListenerPriority.MONITOR);
element.getListener().onPacketSending(event);

} catch (OutOfMemoryError e) {
throw e;
} catch (ThreadDeath e) {
} catch (OutOfMemoryError | ThreadDeath e) {
throw e;
} catch (Throwable e) {
// Minecraft doesn't want your Exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ public static StructureModifier<Object> getStructure(final PacketType packetType
Class<?> packetClass = PacketRegistry.getPacketClassFromType(type);

// We need to map the Bundle Delimiter to the synthetic bundle packet which contains a list of all packets in a bundle
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();
}

return new StructureModifier<>(packetClass, MinecraftReflection.getPacketClass(), true);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ <T> T processOutbound(T action) {
}

// 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public NetworkManagerInjector(Plugin plugin, Server server, ListenerInvoker list
public PacketEvent onPacketSending(Injector injector, Object packet, NetworkMarker marker) {
// check if we need to intercept the packet
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
PacketContainer container = new PacketContainer(PacketRegistry.getPacketType(packetClass), packet);
PacketEvent packetEvent = PacketEvent.fromServer(this, container, marker, injector.getPlayer());
Expand All @@ -109,7 +109,7 @@ public PacketEvent onPacketSending(Injector injector, Object packet, NetworkMark
public PacketEvent onPacketReceiving(Injector injector, Object packet, NetworkMarker marker) {
// check if we need to intercept the packet
Class<?> packetClass = packet.getClass();
if (this.inboundListeners.contains(packetClass) || marker != null) {
if (marker != null || inboundListeners.contains(packetClass)) {
// wrap the packet and construct the event
PacketContainer container = new PacketContainer(PacketRegistry.getPacketType(packetClass), packet);
PacketEvent packetEvent = PacketEvent.fromClient(this, container, marker, injector.getPlayer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public class PacketRegistry {
*/
protected static class Register {
// The main lookup table
private final Map<PacketType, Optional<Class<?>>> typeToClass = new ConcurrentHashMap<>();
private final Map<Class<?>, PacketType> classToType = new ConcurrentHashMap<>();
final Map<PacketType, Optional<Class<?>>> typeToClass = new ConcurrentHashMap<>();
final Map<Class<?>, PacketType> classToType = new ConcurrentHashMap<>();

private volatile Set<PacketType> serverPackets = new HashSet<>();
private volatile Set<PacketType> clientPackets = new HashSet<>();
private final List<MapContainer> containers = new ArrayList<>();
volatile Set<PacketType> serverPackets = new HashSet<>();
volatile Set<PacketType> clientPackets = new HashSet<>();
final List<MapContainer> containers = new ArrayList<>();

public Register() {}

Expand Down Expand Up @@ -383,10 +383,12 @@ public static Class<?> getPacketClassFromType(PacketType type) {
* @return The packet type, or NULL if not found.
*/
public static PacketType getPacketType(Class<?> packet) {
if(Util.isBundlePacket(packet)) {
return PacketType.Play.Server.DELIMITER;
}
initialize();

if (MinecraftReflection.isBundlePacket(packet)) {
return PacketType.Play.Server.BUNDLE;
}

return REGISTER.classToType.get(packet);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,10 @@ public static Class<?> getPackedBundlePacketClass() {
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() {
return getMinecraftClass("network.protocol.BundleDelimiterPacket","BundleDelimiterPacket");
}
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/com/comphenix/protocol/utility/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,4 @@ public static boolean isCurrentlyReloading() {
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ public void testCloning() {
for (PacketType type : PacketType.values()) {
// 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")
|| type.name().contains("DELIMITER")) {
|| type.name().contains("BUNDLE")) {
continue;
}

Expand Down

0 comments on commit df3b68d

Please sign in to comment.