@@ -23,6 +23,7 @@ public class BukkitPluginMessageSender extends AbstractBukkitPluginMessageSender
2323 private Constructor <?> packetPlayOutCustomPayloadConstructor ;
2424 private Constructor <?> packetPlayOutMinecraftKeyConstructor ;
2525 private Constructor <?> discardedPayloadConstructor ;
26+ private Method resourceLocationParseMethod ;
2627 private Class <?> minecraftKeyClass ;
2728 private Class <?> customPacketPayloadClass ;
2829 private boolean useMinecraftKey ;
@@ -48,6 +49,7 @@ public BukkitPluginMessageSender(AbstractBukkitBadlionPlugin apiBukkit) {
4849 String suffix = parts [parts .length - 1 ];
4950 if (!suffix .startsWith ("v" )) {
5051 // 1.20.5+ support
52+ // TODO: In 1.20.5+, the private method `CraftPlayer.sendCustomPayload(ResourceLocation, byte[])` should do the trick and handle all future versions
5153 if ("craftbukkit" .equals (suffix )) {
5254 suffix = "" ;
5355 } else {
@@ -142,6 +144,11 @@ public BukkitPluginMessageSender(AbstractBukkitBadlionPlugin apiBukkit) {
142144 this .useDiscardedPayload = true ;
143145 }
144146 }
147+
148+ // 1.21+
149+ if (this .packetPlayOutMinecraftKeyConstructor == null ) {
150+ this .resourceLocationParseMethod = this .getMethod (this .minecraftKeyClass , "parse" , String .class );
151+ }
145152 }
146153
147154 if (this .packetPlayOutCustomPayloadConstructor == null ) {
@@ -217,11 +224,19 @@ public void sendPluginMessagePacket(Player player, String channel, Object data)
217224 Object payload ;
218225
219226 if (this .useDiscardedPayload ) {
220- // 1.20.5+
221- payload = this .discardedPayloadConstructor .newInstance (
222- this .packetPlayOutMinecraftKeyConstructor .newInstance (channel ),
223- this .wrappedBufferMethod .invoke (null , data )
224- );
227+ if (this .packetPlayOutMinecraftKeyConstructor == null ) {
228+ // 1.21+
229+ payload = this .discardedPayloadConstructor .newInstance (
230+ this .resourceLocationParseMethod .invoke (null , channel ),
231+ this .wrappedBufferMethod .invoke (null , data )
232+ );
233+ } else {
234+ // 1.20.5+
235+ payload = this .discardedPayloadConstructor .newInstance (
236+ this .packetPlayOutMinecraftKeyConstructor .newInstance (channel ),
237+ this .wrappedBufferMethod .invoke (null , data )
238+ );
239+ }
225240 } else {
226241 // 1.20.2 - 1.20.4
227242 payload = Proxy .newProxyInstance (this .getClass ().getClassLoader (), new Class []{this .customPacketPayloadClass }, (proxy , method , args ) -> {
0 commit comments