#[1.20.5 - 1.21.4] ClassCastException when encoding CustomPayload in server

8 messages · Page 1 of 1 (latest)

brittle stone
#

I'm sending a custom network packet from the server to client, when the player uses a block. In the block's onUse():

if (!world.isClient() && player instanceof ServerPlayerEntity serverPlayer) {
            ServerPlayNetworking.send(serverPlayer, new LargeIronSignScreenOpenPayload(pos));
            return ActionResult.SUCCESS;
        }

The custom payload:

public record LargeIronSignScreenOpenPayload(BlockPos pos) implements CustomPayload {
    
    @Override
    public Id<? extends CustomPayload> getId() {
        return PACKET_ID;
    }
    
    public static final CustomPayload.Id<LargeIronSignScreenOpenPayload> PACKET_ID = new CustomPayload.Id<>(Identifier.of(LargeIronSign.MOD_ID, LargeIronSignBlock.PATH + "_screen_open"));
    public static final PacketCodec<RegistryByteBuf, LargeIronSignScreenOpenPayload> PACKET_CODEC =
            PacketCodec.tuple(
                    BlockPos.PACKET_CODEC, LargeIronSignScreenOpenPayload::pos,
                    LargeIronSignScreenOpenPayload::new).cast();
}

The custom payload is registered in the mod init (on client + server):

PayloadTypeRegistry.playC2S().register(LargeIronSignScreenOpenPayload.PACKET_ID, LargeIronSignScreenOpenPayload.PACKET_CODEC);

This works fine when playing single player with integrated server. But when playing on a server, using the block throws this exception on the server:

[15:17:28] [Netty Epoll Server IO #1/ERROR]: Error sending packet clientbound/minecraft:custom_payload
io.netty.handler.codec.EncoderException: Failed to encode packet 'clientbound/minecraft:custom_payload'
(...)
Caused by: java.lang.ClassCastException: class com.jordanl2.largeironsign.LargeIronSignScreenOpenPayload cannot be cast to class net.minecraft.class_8711 (com.jordanl2.largeironsign.LargeIronSignScreenOpenPayload and net.minecraft.class_8711 are in unnamed module of loader 'knot' @3e6fa38a)
        at knot/net.minecraft.class_9139$8.encode(class_9139.java:44) ~[server-intermediary.jar:?]
#

[1.21.4] ClassCastException when encoding CustomPayload in server

brittle stone
#

This is also happening with all Minecraft versions from 1.20.5 onwards.

brittle stone
#

[1.20.5 - 1.21.4] ClassCastException when encoding CustomPayload in server

brittle stone
#

I was wondering if class net.minecraft.class_8711 is the CustomPayload interface, but that's actually interface net.minecraft.class_8710 (makes sense it's an interface...) so I'm not sure what class net.minecraft.class_8711 actually is.

#

My yarn mapping is: 1.21.4+build.4

#

I seem to have fixed the issue. Previously, I was registering the serverbound (playC2S) on the server and clientbound (playS2C) on the client only. I'm now registering both directions on just the server, and that seems to have fixed it.