Skip to content

Commit df3b68d

Browse files
committed
Some cleanup
Fixes super critical issue #2255
1 parent aebefde commit df3b68d

File tree

9 files changed

+54
-58
lines changed

9 files changed

+54
-58
lines changed

src/main/java/com/comphenix/protocol/PacketType.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ public class PacketType implements Serializable, Cloneable, Comparable<PacketTyp
4141
* @author Kristian
4242
*/
4343
public static class Handshake {
44-
private static final Protocol PROTOCOL = Protocol.HANDSHAKING;
44+
static final Protocol PROTOCOL = Protocol.HANDSHAKING;
4545

4646
/**
4747
* Incoming packets.
4848
* @author Kristian
4949
*/
5050
public static class Client extends PacketTypeEnum {
51-
private final static Sender SENDER = Sender.CLIENT;
51+
private static final Sender SENDER = Sender.CLIENT;
5252

5353
@ForceAsync
5454
public static final PacketType SET_PROTOCOL = new PacketType(PROTOCOL, SENDER, 0x00, "SetProtocol", "C00Handshake");
5555

56-
private final static Client INSTANCE = new Client();
56+
private static final Client INSTANCE = new Client();
5757

5858
// Prevent accidental construction
5959
private Client() { super(); }
@@ -71,8 +71,8 @@ public static Sender getSender() {
7171
* @author Kristian
7272
*/
7373
public static class Server extends PacketTypeEnum {
74-
private final static Sender SENDER = Sender.CLIENT;
75-
private final static Server INSTANCE = new Server();
74+
private static final Sender SENDER = Sender.CLIENT;
75+
private static final Server INSTANCE = new Server();
7676
private Server() { super(); }
7777

7878
public static Server getInstance() {
@@ -93,16 +93,16 @@ public static Protocol getProtocol() {
9393
* @author Kristian
9494
*/
9595
public static class Play {
96-
private static final Protocol PROTOCOL = Protocol.PLAY;
96+
static final Protocol PROTOCOL = Protocol.PLAY;
9797

9898
/**
9999
* Outgoing packets.
100100
* @author Kristian
101101
*/
102102
public static class Server extends PacketTypeEnum {
103-
private final static Sender SENDER = Sender.SERVER;
103+
private static final Sender SENDER = Sender.SERVER;
104104

105-
public static final PacketType DELIMITER = new PacketType(PROTOCOL, SENDER, 0x00, "Delimiter", "BundleDelimiterPacket");
105+
public static final PacketType BUNDLE = new PacketType(PROTOCOL, SENDER, 0x00, "Delimiter", "BundleDelimiterPacket");
106106
public static final PacketType SPAWN_ENTITY = new PacketType(PROTOCOL, SENDER, 0x01, "SpawnEntity", "SPacketSpawnObject");
107107
public static final PacketType SPAWN_ENTITY_EXPERIENCE_ORB = new PacketType(PROTOCOL, SENDER, 0x02, "SpawnEntityExperienceOrb", "SPacketSpawnExperienceOrb");
108108
public static final PacketType NAMED_ENTITY_SPAWN = new PacketType(PROTOCOL, SENDER, 0x03, "NamedEntitySpawn", "SPacketSpawnPlayer");
@@ -367,7 +367,7 @@ public static class Server extends PacketTypeEnum {
367367
@Deprecated
368368
public static final PacketType CUSTOM_SOUND_EFFECT = new PacketType(PROTOCOL, SENDER, 0x16, "CustomSoundEffect", "SPacketCustomSound");
369369

370-
private final static Server INSTANCE = new Server();
370+
private static final Server INSTANCE = new Server();
371371

372372
// Prevent accidental construction
373373
private Server() { super(); }
@@ -385,7 +385,7 @@ public static Server getInstance() {
385385
* @author Kristian
386386
*/
387387
public static class Client extends PacketTypeEnum {
388-
private final static Sender SENDER = Sender.CLIENT;
388+
private static final Sender SENDER = Sender.CLIENT;
389389

390390
public static final PacketType TELEPORT_ACCEPT = new PacketType(PROTOCOL, SENDER, 0x00, "TeleportAccept", "CPacketConfirmTeleport");
391391
public static final PacketType TILE_NBT_QUERY = new PacketType(PROTOCOL, SENDER, 0x01, "TileNBTQuery");
@@ -410,7 +410,7 @@ public static class Client extends PacketTypeEnum {
410410
public static final PacketType POSITION = new PacketType(PROTOCOL, SENDER, 0x14, "Flying$Position", "Flying$PacketPlayInPosition", "CPacketPlayer$Position");
411411
public static final PacketType POSITION_LOOK = new PacketType(PROTOCOL, SENDER, 0x15, "Flying$PositionLook", "Flying$PacketPlayInPositionLook", "CPacketPlayer$PositionRotation");
412412
public static final PacketType LOOK = new PacketType(PROTOCOL, SENDER, 0x16, "Flying$Look", "Flying$PacketPlayInLook", "CPacketPlayer$Rotation");
413-
public static final PacketType GROUND = new PacketType(PROTOCOL, SENDER, 0x17, "Flying$", "Flying$d");
413+
public static final PacketType GROUND = new PacketType(PROTOCOL, SENDER, 0x17, "Flying$d");
414414
public static final PacketType VEHICLE_MOVE = new PacketType(PROTOCOL, SENDER, 0x18, "VehicleMove", "CPacketVehicleMove");
415415
public static final PacketType BOAT_MOVE = new PacketType(PROTOCOL, SENDER, 0x19, "BoatMove", "CPacketSteerBoat");
416416
public static final PacketType PICK_ITEM = new PacketType(PROTOCOL, SENDER, 0x1A, "PickItem");
@@ -457,7 +457,7 @@ public static class Client extends PacketTypeEnum {
457457
@Deprecated
458458
public static final PacketType CHAT_PREVIEW = new PacketType(PROTOCOL, SENDER, 0x06, "ChatPreview");
459459

460-
private final static Client INSTANCE = new Client();
460+
private static final Client INSTANCE = new Client();
461461

462462
// Prevent accidental construction
463463
private Client() { super(); }
@@ -480,14 +480,14 @@ public static Protocol getProtocol() {
480480
* @author Kristian
481481
*/
482482
public static class Status {
483-
private static final Protocol PROTOCOL = Protocol.STATUS;
483+
static final Protocol PROTOCOL = Protocol.STATUS;
484484

485485
/**
486486
* Outgoing packets.
487487
* @author Kristian
488488
*/
489489
public static class Server extends PacketTypeEnum {
490-
private final static Sender SENDER = Sender.SERVER;
490+
private static final Sender SENDER = Sender.SERVER;
491491

492492
@ForceAsync
493493
public static final PacketType SERVER_INFO = new PacketType(PROTOCOL, SENDER, 0x00, "ServerInfo", "SPacketServerInfo");
@@ -501,7 +501,7 @@ public static class Server extends PacketTypeEnum {
501501
@ForceAsync
502502
public static final PacketType OUT_SERVER_INFO = SERVER_INFO.clone();
503503

504-
private final static Server INSTANCE = new Server();
504+
private static final Server INSTANCE = new Server();
505505

506506
// Prevent accidental construction
507507
private Server() { super(); }
@@ -519,13 +519,13 @@ public static Server getInstance() {
519519
* @author Kristian
520520
*/
521521
public static class Client extends PacketTypeEnum {
522-
private final static Sender SENDER = Sender.CLIENT;
522+
private static final Sender SENDER = Sender.CLIENT;
523523

524524
public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, "Start", "CPacketServerQuery");
525525
@ForceAsync
526526
public static final PacketType PING = new PacketType(PROTOCOL, SENDER, 0x01, "Ping", "CPacketPing");
527527

528-
private final static Client INSTANCE = new Client();
528+
private static final Client INSTANCE = new Client();
529529

530530
// Prevent accidental construction
531531
private Client() { super(); }
@@ -548,14 +548,14 @@ public static Protocol getProtocol() {
548548
* @author Kristian
549549
*/
550550
public static class Login {
551-
private static final Protocol PROTOCOL = Protocol.LOGIN;
551+
static final Protocol PROTOCOL = Protocol.LOGIN;
552552

553553
/**
554554
* Outgoing packets.
555555
* @author Kristian
556556
*/
557557
public static class Server extends PacketTypeEnum {
558-
private final static Sender SENDER = Sender.SERVER;
558+
private static final Sender SENDER = Sender.SERVER;
559559

560560
@ForceAsync
561561
public static final PacketType DISCONNECT = new PacketType(PROTOCOL, SENDER, 0x00, "Disconnect", "SPacketDisconnect");
@@ -564,7 +564,7 @@ public static class Server extends PacketTypeEnum {
564564
public static final PacketType SET_COMPRESSION = new PacketType(PROTOCOL, SENDER, 0x03, "SetCompression", "SPacketEnableCompression");
565565
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x04, "CustomPayload", "SPacketCustomPayload");
566566

567-
private final static Server INSTANCE = new Server();
567+
private static final Server INSTANCE = new Server();
568568

569569
// Prevent accidental construction
570570
private Server() { super(); }
@@ -582,13 +582,13 @@ public static Server getInstance() {
582582
* @author Kristian
583583
*/
584584
public static class Client extends PacketTypeEnum {
585-
private final static Sender SENDER = Sender.CLIENT;
585+
private static final Sender SENDER = Sender.CLIENT;
586586

587587
public static final PacketType START = new PacketType(PROTOCOL, SENDER, 0x00, "Start", "CPacketLoginStart");
588588
public static final PacketType ENCRYPTION_BEGIN = new PacketType(PROTOCOL, SENDER, 0x01, "EncryptionBegin", "CPacketEncryptionResponse");
589589
public static final PacketType CUSTOM_PAYLOAD = new PacketType(PROTOCOL, SENDER, 0x02, "CustomPayload", "CPacketCustomPayload");
590590

591-
private final static Client INSTANCE = new Client();
591+
private static final Client INSTANCE = new Client();
592592

593593
// Prevent accidental construction
594594
private Client() { super(); }

src/main/java/com/comphenix/protocol/injector/SortedPacketListenerList.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ private void invokeReceivingListener(ErrorReporter reporter, PacketEvent event,
121121
event.setReadOnly(element.getPriority() == ListenerPriority.MONITOR);
122122
element.getListener().onPacketReceiving(event);
123123

124-
} catch (OutOfMemoryError e) {
125-
throw e;
126-
} catch (ThreadDeath e) {
124+
} catch (OutOfMemoryError | ThreadDeath e) {
127125
throw e;
128126
} catch (Throwable e) {
129127
// Minecraft doesn't want your Exception.
@@ -149,22 +147,25 @@ public void invokePacketSending(ErrorReporter reporter, PacketEvent event) {
149147
*/
150148
public void invokePacketSending(ErrorReporter reporter, PacketEvent event, @Nullable ListenerPriority priorityFilter) {
151149
invokeUnpackedPacketSending(reporter, event, priorityFilter);
152-
if(event.getPacketType() == PacketType.Play.Server.DELIMITER && !event.isCancelled()) {
150+
if (event.getPacketType() == PacketType.Play.Server.BUNDLE && !event.isCancelled()) {
153151
// unpack the bundle and invoke for each packet in the bundle
154152
Iterable<PacketContainer> packets = event.getPacket().getPacketBundles().read(0);
155153
List<PacketContainer> outPackets = new ArrayList<>();
156-
for(PacketContainer subPacket : packets) {
154+
for (PacketContainer subPacket : packets) {
157155
PacketEvent subPacketEvent = PacketEvent.fromServer(this, subPacket, event.getNetworkMarker(), event.getPlayer());
158156
invokeUnpackedPacketSending(reporter, subPacketEvent, priorityFilter);
159157

160-
if(!subPacketEvent.isCancelled()) {
161-
outPackets.add(subPacketEvent.getPacket()); // if the packet event has been cancelled, the packet will be removed from the bundle.
158+
if (!subPacketEvent.isCancelled()) {
159+
// if the packet event has been cancelled, the packet will be removed from the bundle
160+
outPackets.add(subPacketEvent.getPacket());
162161
}
163162
}
164-
if(packets.iterator().hasNext()) { // are there still packets in this bundle?
163+
164+
if (packets.iterator().hasNext()) {
165165
event.getPacket().getPacketBundles().write(0, outPackets);
166166
} else {
167-
event.setCancelled(true); // cancel packet if each individual packet has been canceled
167+
// cancel entire packet if each individual packet has been cancelled
168+
event.setCancelled(true);
168169
}
169170
}
170171
}
@@ -201,14 +202,12 @@ private void invokeUnpackedPacketSending(ErrorReporter reporter, PacketEvent eve
201202
* @param event - the related packet event.
202203
* @param element - the listener to invoke.
203204
*/
204-
private final void invokeSendingListener(ErrorReporter reporter, PacketEvent event, PrioritizedListener<PacketListener> element) {
205+
private void invokeSendingListener(ErrorReporter reporter, PacketEvent event, PrioritizedListener<PacketListener> element) {
205206
try {
206207
event.setReadOnly(element.getPriority() == ListenerPriority.MONITOR);
207208
element.getListener().onPacketSending(event);
208209

209-
} catch (OutOfMemoryError e) {
210-
throw e;
211-
} catch (ThreadDeath e) {
210+
} catch (OutOfMemoryError | ThreadDeath e) {
212211
throw e;
213212
} catch (Throwable e) {
214213
// Minecraft doesn't want your Exception.

src/main/java/com/comphenix/protocol/injector/StructureCache.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ public static StructureModifier<Object> getStructure(final PacketType packetType
137137
Class<?> packetClass = PacketRegistry.getPacketClassFromType(type);
138138

139139
// We need to map the Bundle Delimiter to the synthetic bundle packet which contains a list of all packets in a bundle
140-
if(MinecraftVersion.atOrAbove(MinecraftVersion.FEATURE_PREVIEW_2) && packetClass.equals(MinecraftReflection.getBundleDelimiterClass())) {
140+
if (MinecraftVersion.atOrAbove(MinecraftVersion.FEATURE_PREVIEW_2) && packetClass.equals(MinecraftReflection.getBundleDelimiterClass())) {
141141
packetClass = MinecraftReflection.getPackedBundlePacketClass();
142142
}
143+
143144
return new StructureModifier<>(packetClass, MinecraftReflection.getPacketClass(), true);
144145
});
145146
}

src/main/java/com/comphenix/protocol/injector/netty/channel/NettyChannelInjector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ <T> T processOutbound(T action) {
553553
}
554554

555555
// no listener and no marker - no magic :)
556-
if (!this.channelListener.hasListener(packet.getClass()) && marker == null && !Util.isBundlePacket(packet.getClass())) {
556+
if (!this.channelListener.hasListener(packet.getClass()) && marker == null && !MinecraftReflection.isBundlePacket(packet.getClass())) {
557557
return action;
558558
}
559559

src/main/java/com/comphenix/protocol/injector/netty/manager/NetworkManagerInjector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public NetworkManagerInjector(Plugin plugin, Server server, ListenerInvoker list
9191
public PacketEvent onPacketSending(Injector injector, Object packet, NetworkMarker marker) {
9292
// check if we need to intercept the packet
9393
Class<?> packetClass = packet.getClass();
94-
if (this.outboundListeners.contains(packetClass) || marker != null || Util.isBundlePacket(packetClass)) {
94+
if (marker != null || MinecraftReflection.isBundlePacket(packetClass) || outboundListeners.contains(packetClass)) {
9595
// wrap packet and construct the event
9696
PacketContainer container = new PacketContainer(PacketRegistry.getPacketType(packetClass), packet);
9797
PacketEvent packetEvent = PacketEvent.fromServer(this, container, marker, injector.getPlayer());
@@ -109,7 +109,7 @@ public PacketEvent onPacketSending(Injector injector, Object packet, NetworkMark
109109
public PacketEvent onPacketReceiving(Injector injector, Object packet, NetworkMarker marker) {
110110
// check if we need to intercept the packet
111111
Class<?> packetClass = packet.getClass();
112-
if (this.inboundListeners.contains(packetClass) || marker != null) {
112+
if (marker != null || inboundListeners.contains(packetClass)) {
113113
// wrap the packet and construct the event
114114
PacketContainer container = new PacketContainer(PacketRegistry.getPacketType(packetClass), packet);
115115
PacketEvent packetEvent = PacketEvent.fromClient(this, container, marker, injector.getPlayer());

src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ public class PacketRegistry {
4646
*/
4747
protected static class Register {
4848
// The main lookup table
49-
private final Map<PacketType, Optional<Class<?>>> typeToClass = new ConcurrentHashMap<>();
50-
private final Map<Class<?>, PacketType> classToType = new ConcurrentHashMap<>();
49+
final Map<PacketType, Optional<Class<?>>> typeToClass = new ConcurrentHashMap<>();
50+
final Map<Class<?>, PacketType> classToType = new ConcurrentHashMap<>();
5151

52-
private volatile Set<PacketType> serverPackets = new HashSet<>();
53-
private volatile Set<PacketType> clientPackets = new HashSet<>();
54-
private final List<MapContainer> containers = new ArrayList<>();
52+
volatile Set<PacketType> serverPackets = new HashSet<>();
53+
volatile Set<PacketType> clientPackets = new HashSet<>();
54+
final List<MapContainer> containers = new ArrayList<>();
5555

5656
public Register() {}
5757

@@ -383,10 +383,12 @@ public static Class<?> getPacketClassFromType(PacketType type) {
383383
* @return The packet type, or NULL if not found.
384384
*/
385385
public static PacketType getPacketType(Class<?> packet) {
386-
if(Util.isBundlePacket(packet)) {
387-
return PacketType.Play.Server.DELIMITER;
388-
}
389386
initialize();
387+
388+
if (MinecraftReflection.isBundlePacket(packet)) {
389+
return PacketType.Play.Server.BUNDLE;
390+
}
391+
390392
return REGISTER.classToType.get(packet);
391393
}
392394

src/main/java/com/comphenix/protocol/utility/MinecraftReflection.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@ public static Class<?> getPackedBundlePacketClass() {
632632
return getMinecraftClass("network.protocol.game.ClientboundBundlePacket", "ClientboundBundlePacket");
633633
}
634634

635+
public static boolean isBundlePacket(Class<?> packetClass) {
636+
return MinecraftVersion.FEATURE_PREVIEW_2.atOrAbove() && packetClass.equals(getPackedBundlePacketClass());
637+
}
638+
635639
public static Class<?> getBundleDelimiterClass() {
636640
return getMinecraftClass("network.protocol.BundleDelimiterPacket","BundleDelimiterPacket");
637641
}

src/main/java/com/comphenix/protocol/utility/Util.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,4 @@ public static boolean isCurrentlyReloading() {
6060
}
6161
return false;
6262
}
63-
64-
public static boolean isBundlePacket(Class<?> packetClass) {
65-
if(!MinecraftVersion.atOrAbove(MinecraftVersion.FEATURE_PREVIEW_2)) {
66-
return false;
67-
}
68-
if(cachedBundleClass == null) {
69-
cachedBundleClass = MinecraftReflection.getPackedBundlePacketClass();
70-
}
71-
return packetClass.equals(cachedBundleClass);
72-
}
7363
}

src/test/java/com/comphenix/protocol/events/PacketContainerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ public void testCloning() {
828828
for (PacketType type : PacketType.values()) {
829829
// TODO: try to support chat - for now chat contains to many sub classes to properly clone it
830830
if (type.isDeprecated() || !type.isSupported() || type.name().contains("CUSTOM_PAYLOAD") || type.name().contains("CHAT")
831-
|| type.name().contains("DELIMITER")) {
831+
|| type.name().contains("BUNDLE")) {
832832
continue;
833833
}
834834

0 commit comments

Comments
 (0)