44import net .md_5 .bungee .api .ProxyServer ;
55import net .md_5 .bungee .protocol .DefinedPacket ;
66import net .md_5 .bungee .protocol .Protocol ;
7- import net .md_5 .bungee .protocol .Protocol .DirectionData ;
87import net .md_5 .bungee .protocol .ProtocolConstants .Direction ;
98
109import java .lang .reflect .*;
@@ -18,17 +17,24 @@ public final class PacketRegistration {
1817
1918 private Constructor protocolMappingConstructor ;
2019 private Method registerMethod , getIdMethod ;
21- private Class <?> mappingClass ;
20+ private Class <?> mappingClass , directionData ;
21+ private Field toServerField , toClientField ;
2222
2323 {
2424 try {
2525 mappingClass = Class .forName ("net.md_5.bungee.protocol.Protocol$ProtocolMapping" );
26+ directionData = Class .forName ("net.md_5.bungee.protocol.Protocol$DirectionData" );
2627 protocolMappingConstructor = mappingClass .getConstructor (int .class , int .class );
2728 protocolMappingConstructor .setAccessible (true );
28- registerMethod = DirectionData . class .getDeclaredMethod ("registerPacket" , Class .class , Array .newInstance (mappingClass , 0 ).getClass ());
29+ registerMethod = directionData .getDeclaredMethod ("registerPacket" , Class .class , Array .newInstance (mappingClass , 0 ).getClass ());
2930 registerMethod .setAccessible (true );
30- getIdMethod = DirectionData . class .getDeclaredMethod ("getId" , Class .class , int .class );
31+ getIdMethod = directionData .getDeclaredMethod ("getId" , Class .class , int .class );
3132 getIdMethod .setAccessible (true );
33+
34+ toServerField = Protocol .class .getDeclaredField ("TO_SERVER" );
35+ toServerField .setAccessible (true );
36+ toClientField = Protocol .class .getDeclaredField ("TO_CLIENT" );
37+ toClientField .setAccessible (true );
3238 } catch (final Exception e ) {
3339 ProxyServer .getInstance ().getLogger ().log (Level .SEVERE ,"Exception occurred while initializing PacketRegistration: " , e );
3440 }
@@ -37,34 +43,36 @@ public final class PacketRegistration {
3743 /**
3844 * This method registers a {@link AbstractPacket} for the PLAY / GAME protocol in direction to the client. This method is equal to
3945 * {@code registerPacket(Protocol.GAME.TO_CLIENT, clazz, protocolIdMapping);}
40- * @see PacketRegistration#registerPacket(DirectionData , Class, Map)
46+ * @see PacketRegistration#registerPacket(Protocol, Direction , Class, Map)
4147 * @param clazz the class of the packet
4248 * @param protocolIdMapping a map containing the protocol versions and their corresponding packet id.
4349 */
4450 public void registerPlayClientPacket (final Class <? extends AbstractPacket > clazz , final Map <Integer , Integer > protocolIdMapping ) {
45- registerPacket (Protocol .GAME .TO_CLIENT , clazz , protocolIdMapping );
51+ registerPacket (Protocol .GAME , Direction .TO_CLIENT , clazz , protocolIdMapping );
4652 }
4753
4854 /**
4955 * This method registers a {@link AbstractPacket} for the PLAY / GAME protocol in direction to the client. This method is equal to
5056 * {@code registerPacket(Protocol.GAME.TO_SERVER, clazz, protocolIdMapping);}
51- * @see PacketRegistration#registerPacket(DirectionData , Class, Map)
57+ * @see PacketRegistration#registerPacket(Protocol, Direction , Class, Map)
5258 * @param clazz the class of the packet
5359 * @param protocolIdMapping a map containing the protocol versions and their corresponding packet id.
5460 */
5561 public void registerPlayServerPacket (final Class <? extends AbstractPacket > clazz , final Map <Integer , Integer > protocolIdMapping ) {
56- registerPacket (Protocol .GAME .TO_SERVER , clazz , protocolIdMapping );
62+ registerPacket (Protocol .GAME , Direction .TO_SERVER , clazz , protocolIdMapping );
5763 }
5864
5965 /**
6066 * This method registers a {@link AbstractPacket}.
61- * @param data the protocol and direction. May be {@code Protocol.GAME.TO_SERVER} or {@code Protocol.GAME.TO_CLIENT}.
67+ * @param protocol the protocol.
68+ * @param direction the protocol direction
6269 * @param clazz the class of the packet
6370 * @param protocolIdMapping a map containing the protocol versions and their corresponding packet id.
6471 */
65- public void registerPacket (final DirectionData data , final Class <? extends AbstractPacket > clazz , final Map <Integer , Integer > protocolIdMapping ) {
72+ public void registerPacket (final Protocol protocol , final Direction direction , final Class <? extends AbstractPacket > clazz , final Map <Integer , Integer > protocolIdMapping ) {
6673 Preconditions .checkNotNull (clazz , "The clazz cannot be null!" );
67- Preconditions .checkNotNull (data , "The data cannot be null!" );
74+ Preconditions .checkNotNull (protocol , "The protocol cannot be null!" );
75+ Preconditions .checkNotNull (direction , "The direction cannot be null!" );
6876 Preconditions .checkNotNull (protocolIdMapping , "The protocolIdMapping cannot be null!" );
6977 try {
7078 final Object mapArray = Array .newInstance (mappingClass , protocolIdMapping .size ());
@@ -74,7 +82,7 @@ public void registerPacket(final DirectionData data, final Class<? extends Abstr
7482 Array .set (mapArray , index , map );
7583 index ++;
7684 }
77- registerMethod .invoke (data , clazz , mapArray );
85+ registerMethod .invoke (getDirectionData ( protocol , direction ) , clazz , mapArray );
7886 ProxyServer .getInstance ().getLogger ().info ("[Protocolize] Injected custom packet: " +clazz .getName ());
7987 } catch (final Exception e ) {
8088 ProxyServer .getInstance ().getLogger ().log (Level .SEVERE , "Exception occurred while registering packet: " +clazz .getName (), e );
@@ -84,22 +92,23 @@ public void registerPacket(final DirectionData data, final Class<? extends Abstr
8492 /**
8593 * Returns a packet id for the given protocol and it's version.
8694 * @see de.exceptionflug.protocolize.api.util.ProtocolVersions
87- * @param data the protocol and direction. May be {@code Protocol.GAME.TO_SERVER} or {@code Protocol.GAME.TO_CLIENT}.
95+ * @param protocol the protocol.
96+ * @param direction the protocol direction
8897 * @param protocolVersion the protocol version
8998 * @param clazz the class of the packet
9099 * @return the packet id or -1 if the packet is not registered in that specific direction
91100 */
92- public int getPacketID (final DirectionData data , final int protocolVersion , final Class <? extends DefinedPacket > clazz ) {
101+ public int getPacketID (final Protocol protocol , final Direction direction , final int protocolVersion , final Class <? extends DefinedPacket > clazz ) {
102+ Preconditions .checkNotNull (clazz , "The clazz cannot be null!" );
103+ Preconditions .checkNotNull (protocol , "The protocol cannot be null!" );
104+ Preconditions .checkNotNull (direction , "The direction cannot be null!" );
105+ final Object data = getDirectionData (protocol , direction );
93106 try {
94107 return (int ) getIdMethod .invoke (data , clazz , protocolVersion );
95108 } catch (final IllegalAccessException | InvocationTargetException e ) {
96109 if (e .getCause () != null && e .getCause () instanceof IllegalArgumentException ) {
97110 try {
98- if (data .getDirection () == Direction .TO_CLIENT ) {
99- return (int ) getIdMethod .invoke (Protocol .GAME .TO_CLIENT , clazz , protocolVersion );
100- } else {
101- return (int ) getIdMethod .invoke (Protocol .GAME .TO_SERVER , clazz , protocolVersion );
102- }
111+ return (int ) getIdMethod .invoke (data , clazz , protocolVersion );
103112 } catch (final IllegalAccessException | InvocationTargetException e1 ) {
104113 e1 .printStackTrace ();
105114 }
@@ -109,4 +118,16 @@ public int getPacketID(final DirectionData data, final int protocolVersion, fina
109118 return -1 ;
110119 }
111120
121+ private Object getDirectionData (final Protocol protocol , final Direction direction ) {
122+ try {
123+ if (direction == Direction .TO_SERVER )
124+ return toServerField .get (protocol );
125+ else
126+ return toClientField .get (protocol );
127+ } catch (IllegalAccessException e ) {
128+ e .printStackTrace ();
129+ }
130+ return null ;
131+ }
132+
112133}
0 commit comments