Skip to content

Commit b4a7e66

Browse files
committed
Update packet registration to fix issues occurring since bungeecord-protocol commit 9133a6f511b4cfaca5e6a6671b58a6c3b10821ab version is now 1.2.0
1 parent b634545 commit b4a7e66

File tree

14 files changed

+78
-80
lines changed

14 files changed

+78
-80
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,25 @@ This is the full portfolio of protocolize dependencies:
2020
<dependency>
2121
<groupId>de.exceptionflug</groupId>
2222
<artifactId>protocolize-api</artifactId>
23-
<version>1.1.1-SNAPSHOT</version>
23+
<version>1.2.0-SNAPSHOT</version>
2424
<scope>provided</scope>
2525
</dependency>
2626
<dependency>
2727
<groupId>de.exceptionflug</groupId>
2828
<artifactId>protocolize-items</artifactId>
29-
<version>1.1.1-SNAPSHOT</version>
29+
<version>1.2.0-SNAPSHOT</version>
3030
<scope>provided</scope>
3131
</dependency>
3232
<dependency>
3333
<groupId>de.exceptionflug</groupId>
3434
<artifactId>protocolize-inventory</artifactId>
35-
<version>1.1.1-SNAPSHOT</version>
35+
<version>1.2.0-SNAPSHOT</version>
3636
<scope>provided</scope>
3737
</dependency>
3838
<dependency>
3939
<groupId>de.exceptionflug</groupId>
4040
<artifactId>protocolize-world</artifactId>
41-
<version>1.1.1-SNAPSHOT</version>
41+
<version>1.2.0-SNAPSHOT</version>
4242
<scope>provided</scope>
4343
</dependency>
4444
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>de.exceptionflug</groupId>
88
<artifactId>protocolize</artifactId>
9-
<version>1.1.1-SNAPSHOT</version>
9+
<version>1.2.0-SNAPSHOT</version>
1010
<packaging>pom</packaging>
1111

1212
<modules>

protocolize-api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<parent>
4747
<groupId>de.exceptionflug</groupId>
4848
<artifactId>protocolize</artifactId>
49-
<version>1.1.1-SNAPSHOT</version>
49+
<version>1.2.0-SNAPSHOT</version>
5050
</parent>
5151

5252
<groupId>de.exceptionflug</groupId>

protocolize-api/src/main/java/de/exceptionflug/protocolize/api/protocol/PacketRegistration.java

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import net.md_5.bungee.api.ProxyServer;
55
import net.md_5.bungee.protocol.DefinedPacket;
66
import net.md_5.bungee.protocol.Protocol;
7-
import net.md_5.bungee.protocol.Protocol.DirectionData;
87
import net.md_5.bungee.protocol.ProtocolConstants.Direction;
98

109
import 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
}

protocolize-examples/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>de.exceptionflug</groupId>
99
<artifactId>protocolize</artifactId>
10-
<version>1.1.1-SNAPSHOT</version>
10+
<version>1.2.0-SNAPSHOT</version>
1111
</parent>
1212

1313
<groupId>de.exceptionflug</groupId>
@@ -18,19 +18,19 @@
1818
<dependency>
1919
<groupId>de.exceptionflug</groupId>
2020
<artifactId>protocolize-api</artifactId>
21-
<version>1.1.1-SNAPSHOT</version>
21+
<version>1.2.0-SNAPSHOT</version>
2222
<scope>provided</scope>
2323
</dependency>
2424
<dependency>
2525
<groupId>de.exceptionflug</groupId>
2626
<artifactId>protocolize-items</artifactId>
27-
<version>1.1.1-SNAPSHOT</version>
27+
<version>1.2.0-SNAPSHOT</version>
2828
<scope>provided</scope>
2929
</dependency>
3030
<dependency>
3131
<groupId>de.exceptionflug</groupId>
3232
<artifactId>protocolize-inventory</artifactId>
33-
<version>1.1.1-SNAPSHOT</version>
33+
<version>1.2.0-SNAPSHOT</version>
3434
<scope>provided</scope>
3535
</dependency>
3636
</dependencies>

protocolize-inventory/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<parent>
2020
<groupId>de.exceptionflug</groupId>
2121
<artifactId>protocolize</artifactId>
22-
<version>1.1.1-SNAPSHOT</version>
22+
<version>1.2.0-SNAPSHOT</version>
2323
</parent>
2424

2525
<groupId>de.exceptionflug</groupId>

protocolize-inventory/src/main/java/de/exceptionflug/protocolize/inventory/InventoryModule.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import net.md_5.bungee.api.ProxyServer;
1616
import net.md_5.bungee.api.connection.ProxiedPlayer;
1717
import net.md_5.bungee.protocol.Protocol;
18+
import net.md_5.bungee.protocol.ProtocolConstants;
1819

1920
import java.util.Collections;
2021
import java.util.List;
@@ -37,15 +38,15 @@ private InventoryModule() {
3738

3839
public static void initModule() {
3940
// TO_CLIENT
40-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_CLIENT, OpenWindow.class, OpenWindow.MAPPING);
41-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_CLIENT, CloseWindow.class, CloseWindow.MAPPING_CLIENTBOUND);
42-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_CLIENT, ConfirmTransaction.class, ConfirmTransaction.MAPPING_CLIENTBOUND);
43-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_CLIENT, WindowProperty.class, WindowProperty.MAPPING);
41+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_CLIENT, OpenWindow.class, OpenWindow.MAPPING);
42+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_CLIENT, CloseWindow.class, CloseWindow.MAPPING_CLIENTBOUND);
43+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_CLIENT, ConfirmTransaction.class, ConfirmTransaction.MAPPING_CLIENTBOUND);
44+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_CLIENT, WindowProperty.class, WindowProperty.MAPPING);
4445

4546
// TO_SERVER
46-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_SERVER, CloseWindow.class, CloseWindow.MAPPING_SERVERBOUND);
47-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_SERVER, ConfirmTransaction.class, ConfirmTransaction.MAPPING_SERVERBOUND);
48-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_SERVER, ClickWindow.class, ClickWindow.MAPPING);
47+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_SERVER, CloseWindow.class, CloseWindow.MAPPING_SERVERBOUND);
48+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_SERVER, ConfirmTransaction.class, ConfirmTransaction.MAPPING_SERVERBOUND);
49+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_SERVER, ClickWindow.class, ClickWindow.MAPPING);
4950

5051
// Adapters
5152
ProtocolAPI.getEventManager().registerListener(new OpenWindowAdapter());

protocolize-items/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<parent>
2020
<groupId>de.exceptionflug</groupId>
2121
<artifactId>protocolize</artifactId>
22-
<version>1.1.1-SNAPSHOT</version>
22+
<version>1.2.0-SNAPSHOT</version>
2323
</parent>
2424

2525
<groupId>de.exceptionflug</groupId>

protocolize-items/src/main/java/de/exceptionflug/protocolize/items/ItemsModule.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import de.exceptionflug.protocolize.items.adapter.*;
66
import de.exceptionflug.protocolize.items.packet.*;
77
import net.md_5.bungee.protocol.Protocol;
8+
import net.md_5.bungee.protocol.ProtocolConstants;
89

910
public class ItemsModule {
1011

@@ -15,14 +16,14 @@ private ItemsModule() {
1516

1617
public static void initModule() {
1718
// TO_CLIENT
18-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_CLIENT, SetSlot.class, SetSlot.MAPPING);
19-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_CLIENT, WindowItems.class, WindowItems.MAPPING);
20-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_CLIENT, HeldItemChange.class, HeldItemChange.MAPPING_CLIENTBOUND);
19+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_CLIENT, SetSlot.class, SetSlot.MAPPING);
20+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_CLIENT, WindowItems.class, WindowItems.MAPPING);
21+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_CLIENT, HeldItemChange.class, HeldItemChange.MAPPING_CLIENTBOUND);
2122

2223
// TO_SERVER
23-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_SERVER, UseItem.class, UseItem.MAPPING);
24-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_SERVER, BlockPlacement.class, BlockPlacement.MAPPING);
25-
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME.TO_SERVER, HeldItemChange.class, HeldItemChange.MAPPING_SERVERBOUND);
24+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_SERVER, UseItem.class, UseItem.MAPPING);
25+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_SERVER, BlockPlacement.class, BlockPlacement.MAPPING);
26+
ProtocolAPI.getPacketRegistration().registerPacket(Protocol.GAME, ProtocolConstants.Direction.TO_SERVER, HeldItemChange.class, HeldItemChange.MAPPING_SERVERBOUND);
2627

2728
// ADAPTERS
2829
ProtocolAPI.getEventManager().registerListener(new WindowItemsAdapter());

protocolize-plugin/pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<parent>
88
<groupId>de.exceptionflug</groupId>
99
<artifactId>protocolize</artifactId>
10-
<version>1.1.1-SNAPSHOT</version>
10+
<version>1.2.0-SNAPSHOT</version>
1111
</parent>
1212

1313
<groupId>de.exceptionflug</groupId>
1414
<artifactId>protocolize-plugin</artifactId>
15-
<version>1.1.1-SNAPSHOT</version>
15+
<version>1.2.0-SNAPSHOT</version>
1616

1717
<properties>
1818
<plugin.main>de.exceptionflug.protocolize.ProtocolizePlugin</plugin.main>
@@ -23,22 +23,22 @@
2323
<dependency>
2424
<groupId>de.exceptionflug</groupId>
2525
<artifactId>protocolize-api</artifactId>
26-
<version>1.1.1-SNAPSHOT</version>
26+
<version>1.2.0-SNAPSHOT</version>
2727
</dependency>
2828
<dependency>
2929
<groupId>de.exceptionflug</groupId>
3030
<artifactId>protocolize-items</artifactId>
31-
<version>1.1.1-SNAPSHOT</version>
31+
<version>1.2.0-SNAPSHOT</version>
3232
</dependency>
3333
<dependency>
3434
<groupId>de.exceptionflug</groupId>
3535
<artifactId>protocolize-world</artifactId>
36-
<version>1.1.1-SNAPSHOT</version>
36+
<version>1.2.0-SNAPSHOT</version>
3737
</dependency>
3838
<dependency>
3939
<groupId>de.exceptionflug</groupId>
4040
<artifactId>protocolize-inventory</artifactId>
41-
<version>1.1.1-SNAPSHOT</version>
41+
<version>1.2.0-SNAPSHOT</version>
4242
</dependency>
4343
</dependencies>
4444

0 commit comments

Comments
 (0)