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:?]