11package net .badlion .blccpsapibukkit .listener ;
22
33import net .badlion .blccpsapibukkit .BlcCpsApiBukkit ;
4- import org .bukkit .Bukkit ;
54import org .bukkit .entity .Player ;
65import org .bukkit .event .EventHandler ;
76import org .bukkit .event .Listener ;
@@ -18,33 +17,25 @@ public class PlayerListener implements Listener {
1817 private BlcCpsApiBukkit plugin ;
1918 private String versionSuffix ;
2019
21- // Reflection stuff
22- private Class <?> craftPlayerClass ;
23- private Method getHandleMethod ;
20+ private Method getHandleMethod ;
2421
25- private Class <?> nmsPlayerClass ;
26- private Field playerConnectionField ;
22+ private Field playerConnectionField ;
2723
28- private Class <?> playerConnectionClass ;
29- private Method sendPacketMethod ;
24+ private Method sendPacketMethod ;
3025
31- private Class <?> packetPlayOutCustomPayloadClass ;
32- private Constructor <?> packetPlayOutCustomPayloadConstructor ;
26+ private Constructor <?> packetPlayOutCustomPayloadConstructor ;
3327
3428 // Bukkit 1.8+ support
3529 private Class <?> packetDataSerializerClass ;
3630 private Constructor <?> packetDataSerializerConstructor ;
3731
38- // Netty classes used by newer 1.8 and newer
39- private Class <?> byteBufClass ;
40- private Class <?> unpooledClass ;
41- private Method wrappedBufferMethod ;
32+ private Method wrappedBufferMethod ;
4233
4334 public PlayerListener (BlcCpsApiBukkit plugin ) {
4435 this .plugin = plugin ;
4536
4637 // Get the v1_X_Y from the end of the package name, e.g. v_1_7_R4 or v_1_12_R1
47- String packageName = Bukkit .getServer ().getClass ().getPackage ().getName ();
38+ String packageName = plugin .getServer ().getClass ().getPackage ().getName ();
4839 String [] parts = packageName .split ("\\ ." );
4940
5041 if (parts .length > 0 ) {
@@ -59,72 +50,74 @@ public PlayerListener(BlcCpsApiBukkit plugin) {
5950 }
6051
6152 // We need to use reflection because Bukkit by default handles plugin messages in a really silly way
62- this .craftPlayerClass = this .getClass ("org.bukkit.craftbukkit." + this .versionSuffix + ".entity.CraftPlayer" );
63- if (this .craftPlayerClass == null ) {
53+ // Reflection stuff
54+ Class <?> craftPlayerClass = this .getClass ("org.bukkit.craftbukkit." + this .versionSuffix + ".entity.CraftPlayer" );
55+ if (craftPlayerClass == null ) {
6456 throw new RuntimeException ("Failed to find CraftPlayer class" );
6557 }
6658
67- this . nmsPlayerClass = this .getClass ("net.minecraft.server." + this .versionSuffix + ".EntityPlayer" );
68- if (this . nmsPlayerClass == null ) {
59+ Class <?> nmsPlayerClass = this .getClass ("net.minecraft.server." + this .versionSuffix + ".EntityPlayer" );
60+ if (nmsPlayerClass == null ) {
6961 throw new RuntimeException ("Failed to find EntityPlayer class" );
7062 }
7163
72- this . playerConnectionClass = this .getClass ("net.minecraft.server." + this .versionSuffix + ".PlayerConnection" );
73- if (this . playerConnectionClass == null ) {
64+ Class <?> playerConnectionClass = this .getClass ("net.minecraft.server." + this .versionSuffix + ".PlayerConnection" );
65+ if (playerConnectionClass == null ) {
7466 throw new RuntimeException ("Failed to find PlayerConnection class" );
7567 }
7668
77- this . packetPlayOutCustomPayloadClass = this .getClass ("net.minecraft.server." + this .versionSuffix + ".PacketPlayOutCustomPayload" );
78- if (this . packetPlayOutCustomPayloadClass == null ) {
69+ Class <?> packetPlayOutCustomPayloadClass = this .getClass ("net.minecraft.server." + this .versionSuffix + ".PacketPlayOutCustomPayload" );
70+ if (packetPlayOutCustomPayloadClass == null ) {
7971 throw new RuntimeException ("Failed to find PacketPlayOutCustomPayload class" );
8072 }
8173
82- this .packetPlayOutCustomPayloadConstructor = this .getConstructor (this . packetPlayOutCustomPayloadClass , String .class , byte [].class );
74+ this .packetPlayOutCustomPayloadConstructor = this .getConstructor (packetPlayOutCustomPayloadClass , String .class , byte [].class );
8375 if (this .packetPlayOutCustomPayloadConstructor == null ) {
8476 // Newer versions of Minecraft use a different custom packet system
8577 this .packetDataSerializerClass = this .getClass ("net.minecraft.server." + this .versionSuffix + ".PacketDataSerializer" );
8678 if (this .packetDataSerializerClass == null ) {
8779 throw new RuntimeException ("Failed to find PacketPlayOutCustomPayload constructor or PacketDataSerializer class" );
8880 }
8981
90- this .byteBufClass = this .getClass ("io.netty.buffer.ByteBuf" );
91- if (this .byteBufClass == null ) {
82+ // Netty classes used by newer 1.8 and newer
83+ Class <?> byteBufClass = this .getClass ("io.netty.buffer.ByteBuf" );
84+ if (byteBufClass == null ) {
9285 throw new RuntimeException ("Failed to find PacketPlayOutCustomPayload constructor or ByteBuf class" );
9386 }
9487
95- this .packetDataSerializerConstructor = this .getConstructor (this .packetDataSerializerClass , this . byteBufClass );
88+ this .packetDataSerializerConstructor = this .getConstructor (this .packetDataSerializerClass , byteBufClass );
9689 if (this .packetDataSerializerConstructor == null ) {
9790 throw new RuntimeException ("Failed to find PacketPlayOutCustomPayload constructor or PacketDataSerializer constructor" );
9891 }
9992
100- this . unpooledClass = this .getClass ("io.netty.buffer.Unpooled" );
101- if (this . unpooledClass == null ) {
93+ Class <?> unpooledClass = this .getClass ("io.netty.buffer.Unpooled" );
94+ if (unpooledClass == null ) {
10295 throw new RuntimeException ("Failed to find PacketPlayOutCustomPayload constructor or Unpooled class" );
10396 }
10497
105- this .wrappedBufferMethod = this .getMethod (this . unpooledClass , "wrappedBuffer" , byte [].class );
98+ this .wrappedBufferMethod = this .getMethod (unpooledClass , "wrappedBuffer" , byte [].class );
10699 if (this .wrappedBufferMethod == null ) {
107100 throw new RuntimeException ("Failed to find PacketPlayOutCustomPayload constructor or wrappedBuffer()" );
108101 }
109102
110103 // If we made it this far in theory we are on at least 1.8
111- this .packetPlayOutCustomPayloadConstructor = this .getConstructor (this . packetPlayOutCustomPayloadClass , String .class , this .packetDataSerializerClass );
104+ this .packetPlayOutCustomPayloadConstructor = this .getConstructor (packetPlayOutCustomPayloadClass , String .class , this .packetDataSerializerClass );
112105 if (this .packetPlayOutCustomPayloadConstructor == null ) {
113106 throw new RuntimeException ("Failed to find PacketPlayOutCustomPayload constructor 2x" );
114107 }
115108 }
116109
117- this .getHandleMethod = this .getMethod (this . craftPlayerClass , "getHandle" );
110+ this .getHandleMethod = this .getMethod (craftPlayerClass , "getHandle" );
118111 if (this .getHandleMethod == null ) {
119112 throw new RuntimeException ("Failed to find CraftPlayer.getHandle()" );
120113 }
121114
122- this .playerConnectionField = this .getField (this . nmsPlayerClass , "playerConnection" );
115+ this .playerConnectionField = this .getField (nmsPlayerClass , "playerConnection" );
123116 if (this .playerConnectionField == null ) {
124117 throw new RuntimeException ("Failed to find EntityPlayer.playerConnection" );
125118 }
126119
127- this .sendPacketMethod = this .getMethod (this . playerConnectionClass , "sendPacket" );
120+ this .sendPacketMethod = this .getMethod (playerConnectionClass , "sendPacket" );
128121 if (this .sendPacketMethod == null ) {
129122 throw new RuntimeException ("Failed to find PlayerConnection.sendPacket()" );
130123 }
@@ -143,7 +136,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {
143136
144137 // Newer MC version, setup ByteBuf object
145138 if (this .packetDataSerializerClass != null ) {
146- Object byteBuf = this .wrappedBufferMethod .invoke (null , message );
139+ Object byteBuf = this .wrappedBufferMethod .invoke (null , ( Object ) message );
147140 Object packetDataSerializer = this .packetDataSerializerConstructor .newInstance (byteBuf );
148141
149142 packet = this .packetPlayOutCustomPayloadConstructor .newInstance (channel , packetDataSerializer );
@@ -168,15 +161,15 @@ public void onPlayerJoin(PlayerJoinEvent event) {
168161 }
169162 }
170163
171- public Class <?> getClass (String className ) {
164+ private Class <?> getClass (String className ) {
172165 try {
173166 return Class .forName (className );
174167 } catch (ClassNotFoundException e ) {
175168 return null ;
176169 }
177170 }
178171
179- public Constructor <?> getConstructor (Class <?> clazz , Class <?> ...params ) {
172+ private Constructor <?> getConstructor (Class <?> clazz , Class <?>... params ) {
180173 for (final Constructor <?> constructor : clazz .getDeclaredConstructors ()) {
181174 if (Arrays .equals (constructor .getParameterTypes (), params )) {
182175 constructor .setAccessible (true );
@@ -187,7 +180,7 @@ public Constructor<?> getConstructor(Class<?> clazz, Class<?> ...params) {
187180 return null ;
188181 }
189182
190- public Method getMethod (Class <?> clazz , String methodName , Class <?>... params ) {
183+ private Method getMethod (Class <?> clazz , String methodName , Class <?>... params ) {
191184 for (final Method method : clazz .getDeclaredMethods ()) {
192185 if (method .getName ().equals (methodName )) {
193186 if (params .length > 0 ) {
@@ -205,7 +198,7 @@ public Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
205198 return null ;
206199 }
207200
208- public Field getField (Class <?> clazz , String fieldName ) {
201+ private Field getField (Class <?> clazz , String fieldName ) {
209202 for (final Field field : clazz .getDeclaredFields ()) {
210203 if (field .getName ().equals (fieldName )) {
211204 field .setAccessible (true );
0 commit comments