ProtocolLib

3M Downloads

Writing to plugin channel result in a FieldAccessException

PapiCapi opened this issue ยท 0 comments

commented

Make sure you're doing the following

  • You're using the latest build for your server version
  • This isn't an issue caused by another plugin
  • You've checked for duplicate issues
  • You didn't use /reload

Describe the question
I'm trying to send a "minecraft:debug/game_test_add_marker" (https://wiki.vg/Plugin_channels#minecraft:debug.2Fgame_test_add_marker) but in 1.20.4 it doesn't seem to work anymore.
It used to work in 1.20.1.

The error i get is :

[18:35:11 ERROR]: Could not pass event PlayerItemHeldEvent to Action v1.0
com.comphenix.protocol.reflect.FieldAccessException: Field index 0 is out of bounds for length 0
        at com.comphenix.protocol.reflect.FieldAccessException.fromFormat(FieldAccessException.java:49) ~[ProtocolLib.jar:?]
        at com.comphenix.protocol.reflect.StructureModifier.write(StructureModifier.java:315) ~[ProtocolLib.jar:?]

I know that in the common errors wiki (https://github.com/dmulloy2/ProtocolLib/wiki/Common-Errors) there is a section about this error but i don't really understand how to fix it.
My maven project uses paper 1.20.4 as well as my server (build 436).

Code

Here is the main part :

ByteBuf packet = Unpooled.buffer();
packet.writeLong(blockPosToLong(b.getLocation().getX(),b.getLocation().getY(),b.getLocation().getZ()));
				      
Color color = hex2Rgb("#DDDADA",100);
packet.writeInt(color.getRGB());

String text = "";
writeString(packet, text);
				      
packet.writeInt(100000); //time
				      
sendPayload(event.getPlayer(), "debug/game_test_add_marker", packet);

The blockPosToLong method :

public static long blockPosToLong(double x, double y, double z) {
        return ((long) x & 67108863L) << 38 | (long) y & 4095L | ((long) z & 67108863L) << 12;
}

The hex2Rgb method :

private static Color hex2Rgb(String colorStr, int transparency) {
        return new Color(
            Integer.valueOf(colorStr.substring(1, 3), 16),
            Integer.valueOf(colorStr.substring(3, 5), 16),
            Integer.valueOf(colorStr.substring(5, 7), 16), transparency);
}

The sendPayload method :

public static void sendPayload(Player receiver, String channel, ByteBuf bytes) {
    	PacketContainer container = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.CUSTOM_PAYLOAD);
	container.getMinecraftKeys().write(0, new MinecraftKey(channel)); // <---- i get the error here !
		
	Object serializer = MinecraftReflection.getPacketDataSerializer(bytes);
        container.getModifier().withType(ByteBuf.class).write(0, serializer);
        ProtocolLibrary.getProtocolManager().sendServerPacket(receiver, container);
}