Skip to content

Commit 67b119a

Browse files
committed
chore: initialize PROTOCOL_MANAGER lazily
1 parent a458ee2 commit 67b119a

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

PacketWrapper/src/main/java/com/comphenix/packetwrapper/AbstractPacket.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
*/
1919
package com.comphenix.packetwrapper;
2020

21+
import com.comphenix.packetwrapper.util.VersionUtil;
2122
import com.comphenix.protocol.PacketType;
2223
import com.comphenix.protocol.ProtocolLibrary;
2324
import com.comphenix.protocol.ProtocolManager;
2425
import com.comphenix.protocol.events.PacketContainer;
25-
import com.comphenix.protocol.utility.MinecraftVersion;
2626
import com.google.common.base.Objects;
2727
import org.bukkit.entity.Player;
2828

@@ -31,19 +31,16 @@
3131
public abstract class AbstractPacket {
3232
// The packet we will be modifying
3333
protected final PacketContainer handle;
34-
35-
protected static final ProtocolManager PROTOCOL_MANAGER = ProtocolLibrary.getProtocolManager();
36-
protected static final MinecraftVersion VERSION = PROTOCOL_MANAGER.getMinecraftVersion();
3734
/*
3835
* Note that the following primitive fields are all `static final`
3936
* so that JIT can inline them and even perform static-folding
4037
*/
41-
protected static final int MAJOR_VERSION = VERSION.getMajor();
42-
protected static final int MINOR_VERSION = VERSION.getMinor();
43-
protected static final int VERSION_BUILD = VERSION.getBuild();
38+
protected static final int MAJOR_VERSION = VersionUtil.getMajorVersion();
39+
protected static final int MINOR_VERSION = VersionUtil.getMinorVersion();
40+
protected static final int VERSION_BUILD = VersionUtil.getBuildVersion();
4441

4542
static {
46-
if (MAJOR_VERSION != 1) throw new Error("Major Minecraft version " + VERSION + " is unsupported");
43+
if (MAJOR_VERSION != 1) throw new Error("Major Minecraft version " + MAJOR_VERSION + " is unsupported");
4744
}
4845

4946
/**
@@ -72,6 +69,10 @@ public PacketContainer getHandle() {
7269
return handle;
7370
}
7471

72+
protected static ProtocolManager protocolManager() {
73+
return ProtocolManagerHolder.PROTOCOL_MANAGER;
74+
}
75+
7576
/**
7677
* Send the current packet to the given receiver.
7778
*
@@ -80,8 +81,7 @@ public PacketContainer getHandle() {
8081
*/
8182
public void sendPacket(Player receiver) {
8283
try {
83-
PROTOCOL_MANAGER.sendServerPacket(receiver,
84-
getHandle());
84+
protocolManager().sendServerPacket(receiver, getHandle());
8585
} catch (InvocationTargetException e) {
8686
throw new RuntimeException("Cannot send packet.", e);
8787
}
@@ -91,7 +91,7 @@ public void sendPacket(Player receiver) {
9191
* Send the current packet to all online players.
9292
*/
9393
public void broadcastPacket() {
94-
PROTOCOL_MANAGER.broadcastServerPacket(getHandle());
94+
protocolManager().broadcastServerPacket(getHandle());
9595
}
9696

9797
/**
@@ -115,10 +115,13 @@ public void recievePacket(Player sender) {
115115
*/
116116
public void receivePacket(Player sender) {
117117
try {
118-
PROTOCOL_MANAGER.recieveClientPacket(sender,
119-
getHandle());
118+
protocolManager().recieveClientPacket(sender, getHandle());
120119
} catch (Exception e) {
121120
throw new RuntimeException("Cannot receive packet.", e);
122121
}
123122
}
123+
124+
private static final class ProtocolManagerHolder {
125+
private static final ProtocolManager PROTOCOL_MANAGER = ProtocolLibrary.getProtocolManager();
126+
}
124127
}

PacketWrapper/src/main/java/com/comphenix/packetwrapper/WrapperPlayServerMount.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@
1818
*/
1919
package com.comphenix.packetwrapper;
2020

21-
import java.util.ArrayList;
22-
import java.util.List;
23-
24-
import org.bukkit.World;
25-
import org.bukkit.entity.Entity;
26-
2721
import com.comphenix.protocol.PacketType;
28-
import com.comphenix.protocol.ProtocolLibrary;
29-
import com.comphenix.protocol.ProtocolManager;
3022
import com.comphenix.protocol.events.PacketContainer;
3123
import com.comphenix.protocol.events.PacketEvent;
24+
import org.bukkit.World;
25+
import org.bukkit.entity.Entity;
26+
27+
import java.util.ArrayList;
28+
import java.util.List;
3229

3330
public class WrapperPlayServerMount extends AbstractPacket {
3431

@@ -100,7 +97,7 @@ public List<Entity> getPassengers(World world) {
10097
List<Entity> passengers = new ArrayList<>();
10198

10299
for (int id : ids) {
103-
Entity entity = PROTOCOL_MANAGER.getEntityFromID(world, id);
100+
Entity entity = protocolManager().getEntityFromID(world, id);
104101
if (entity != null) {
105102
passengers.add(entity);
106103
}

PacketWrapper/src/main/java/com/comphenix/packetwrapper/WrapperPlayServerSpawnEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public WrapperPlayServerSpawnEntity(Entity entity, int type, int objectData) {
5151

5252
// Useful constructor
5353
private static PacketContainer fromEntity(Entity entity, int type, int objectData) {
54-
if (entityConstructor == null)
55-
entityConstructor = PROTOCOL_MANAGER.createPacketConstructor(TYPE, entity, type, objectData);
54+
if (entityConstructor == null) entityConstructor = protocolManager()
55+
.createPacketConstructor(TYPE, entity, type, objectData);
5656
return entityConstructor.createPacket(entity, type, objectData);
5757
}
5858

0 commit comments

Comments
 (0)