18
18
*/
19
19
package com .comphenix .packetwrapper ;
20
20
21
+ import com .comphenix .packetwrapper .util .VersionUtil ;
21
22
import com .comphenix .protocol .PacketType ;
22
23
import com .comphenix .protocol .ProtocolLibrary ;
23
24
import com .comphenix .protocol .ProtocolManager ;
24
25
import com .comphenix .protocol .events .PacketContainer ;
25
- import com .comphenix .protocol .utility .MinecraftVersion ;
26
26
import com .google .common .base .Objects ;
27
27
import org .bukkit .entity .Player ;
28
28
31
31
public abstract class AbstractPacket {
32
32
// The packet we will be modifying
33
33
protected final PacketContainer handle ;
34
-
35
- protected static final ProtocolManager PROTOCOL_MANAGER = ProtocolLibrary .getProtocolManager ();
36
- protected static final MinecraftVersion VERSION = PROTOCOL_MANAGER .getMinecraftVersion ();
37
34
/*
38
35
* Note that the following primitive fields are all `static final`
39
36
* so that JIT can inline them and even perform static-folding
40
37
*/
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 ();
44
41
45
42
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" );
47
44
}
48
45
49
46
/**
@@ -72,6 +69,10 @@ public PacketContainer getHandle() {
72
69
return handle ;
73
70
}
74
71
72
+ protected static ProtocolManager protocolManager () {
73
+ return ProtocolManagerHolder .PROTOCOL_MANAGER ;
74
+ }
75
+
75
76
/**
76
77
* Send the current packet to the given receiver.
77
78
*
@@ -80,8 +81,7 @@ public PacketContainer getHandle() {
80
81
*/
81
82
public void sendPacket (Player receiver ) {
82
83
try {
83
- PROTOCOL_MANAGER .sendServerPacket (receiver ,
84
- getHandle ());
84
+ protocolManager ().sendServerPacket (receiver , getHandle ());
85
85
} catch (InvocationTargetException e ) {
86
86
throw new RuntimeException ("Cannot send packet." , e );
87
87
}
@@ -91,7 +91,7 @@ public void sendPacket(Player receiver) {
91
91
* Send the current packet to all online players.
92
92
*/
93
93
public void broadcastPacket () {
94
- PROTOCOL_MANAGER .broadcastServerPacket (getHandle ());
94
+ protocolManager () .broadcastServerPacket (getHandle ());
95
95
}
96
96
97
97
/**
@@ -115,10 +115,13 @@ public void recievePacket(Player sender) {
115
115
*/
116
116
public void receivePacket (Player sender ) {
117
117
try {
118
- PROTOCOL_MANAGER .recieveClientPacket (sender ,
119
- getHandle ());
118
+ protocolManager ().recieveClientPacket (sender , getHandle ());
120
119
} catch (Exception e ) {
121
120
throw new RuntimeException ("Cannot receive packet." , e );
122
121
}
123
122
}
123
+
124
+ private static final class ProtocolManagerHolder {
125
+ private static final ProtocolManager PROTOCOL_MANAGER = ProtocolLibrary .getProtocolManager ();
126
+ }
124
127
}
0 commit comments