#[SOLVED] [1.21] Packets Registering

31 messages · Page 1 of 1 (latest)

lusty patrol
#

So, shortly, I have two packets:

  1. Client To Server
  2. Server To Client

I understand how packets looks like in 1.21, but their registering is very weird thing

For example I have this packets

public class TestS2CPacket implements CustomPayload {

    public static final Identifier PACKET_ID = IdentifierUtils.id("test_packet_s2c");
    
    public static final Id<TestS2CPacket> TYPE = new Id<>(PACKET_ID);
    public static final PacketCodec<RegistryByteBuf, TestS2CPacket> CODEC = CustomPayload.codecOf(TestS2CPacket::write, TestS2CPacket::new);

    private final String message;

    public TestS2CPacket(String message) {
        this.message = message;
    }

    public TestS2CPacket(RegistryByteBuf buf) {
        this.message = buf.readString();
    }
    
    public void write(RegistryByteBuf buf) {
        buf.writeString(this.message);
    }
    
    @Override
    public Id<? extends CustomPayload> getId() {
        return TYPE;
    }
}
public class TestC2SPacket implements CustomPayload {

    public static final Identifier PACKET_ID = IdentifierUtils.id("test_packet_c2s");
    
    public static final Id<TestC2SPacket> TYPE = new Id<>(PACKET_ID);
    public static final PacketCodec<RegistryByteBuf, TestC2SPacket> CODEC = CustomPayload.codecOf(TestC2SPacket::write, TestC2SPacket::new);

    private final String message;

    public TestC2SPacket(String message) {
        this.message = message;
    }

    public TestC2SPacket(RegistryByteBuf buf) {
        this.message = buf.readString();
    }
    
    public void write(RegistryByteBuf buf) {
        buf.writeString(this.message);
    }
    
    @Override
    public Id<? extends CustomPayload> getId() {
        return TYPE;
    }
}

Read messages below

#

And I have two manager classes for their registering at client and server side

public class ClientPacketManager {
    // This method invokes only at client initializing (ClientModInitializer)
    public static void register() {
        // Firstly, I need to register TestS2CPacket here, but I really don't know how to
        // do that right

        ClientPlayNetworking.registerGlobalReceiver(TestS2CPacket.TYPE, ((payload, context) -> {
            // handling here
        }));
    }
}
public class ServerPacketManager {
    // This method invokes only at common initializing (ModInitializer)
    public static void register() {
        // Firstly, I need to register TestC2SPacket here, but I really don't know how to
        // do that right

        ServerPlayNetworking.registerGlobalReceiver(TestC2SPacket.TYPE, ((payload, context) -> {
            // handling here
        }));
    }
}
#

My mod can be installed on the server, and that means that server will only invoke ServerPacketManager.register();, but the client will invoke and ServerPacketManager.register();, and ClientPacketManager.register();

west musk
#

What exactly is the issue?

lusty patrol
#

And I'm not sure that's right, because when I do that, client-side throws when trying to decode TestS2CPacket 🤔

west musk
#

Also kind of weird to have packets with buffers when you should be doing packet codecs on pojos

#

Have you read the 1.20.5 blog's networking changes?

lusty patrol
#

Yes, I read, but I still don’t understand how I should register packets and on which sides via PayloadTypeRegistry

west musk
#

You first register the payload on both sides

#

and only after that you register the handlers

west musk
lusty patrol
#

Should I do something like that?

public class ServerPacketManager {
    // This method invokes only at common initializing (ModInitializer)
    public static void register() {
        // Firstly, I need to register TestS2CPacket here, but I really don't know how to
        // do that right
        PayloadTypeRegistry.playS2C().register(TestS2CPacket.TYPE, TestS2CPacket.CODEC);
        PayloadTypeRegistry.playC2S().register(TestC2SPacket.TYPE, TestC2SPacket.CODEC);

        ServerPlayNetworking.registerGlobalReceiver(TestC2SPacket.TYPE, ((payload, context) -> {
            // handling here
        }));
    }
}
west musk
#

What you did there was register TestS2CPacket and TestC2SPacket payloads (like two separate packet payloads and then registered the server side handler for TestC2SPacket

#

Yes that looks correct to me

lusty patrol
#

It means that registering TestS2CPacket at ClientPacketManager.register(); will be wrong?

west musk
#

Why would it be wrong?

#

You are registering the receiver on the side that's bound to receive the packet

#

If the packet is S2C, it means it's clientbound

#

Meaning the client is the one handling it coming in

lusty patrol
#

Hmm, then it's strange because I gets this error

#

Sorry for so many stonecutter comments

regal bluff
lusty patrol
#

Okay, looks like I got it

#

Let me check

#

Yeah, now it works

lusty patrol
#

A lot of thanks @west musk ❤️ and @regal bluff ❤️

#

I think this post will be useful to others in the future