Fabric API

Fabric API

111M Downloads

Can not register global receiver in client

byxiaobai opened this issue · 7 comments

commented

Crash report:"Caused by: java.lang.IllegalArgumentException: Cannot register handler as no payload type has been registered with name "catvideo:main" for CLIENTBOUND PLAY
at knot//net.fabricmc.fabric.impl.networking.GlobalReceiverRegistry.assertPayloadType(GlobalReceiverRegistry.java:218)
at knot//net.fabricmc.fabric.impl.networking.GlobalReceiverRegistry.registerGlobalReceiver(GlobalReceiverRegistry.java:80)
at knot//net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking.registerGlobalReceiver(ClientPlayNetworking.java:72"
code.zip

commented

You are registering on C2S, not S2C.

commented

You are registering on C2S, not S2C.

Yes,I only need C2S,this is a client mod
I use bukkit PluginMessaging to send packet to client

commented

CLIENTBOUND PLAY means you are trying to send from the server to the client (S2C)
the client must know that the packet can be received S2C

commented

CLIENTBOUND PLAY 表示您尝试从服务器发送到客户端 (S2C),客户端必须知道数据包可以被 S2C 接收

oh,yes.
now it works well.
but when I send plugin message to client, An exception was thrown

image

this is my plugin code:

private void sendNormalPluginMessage(Player player, String channel, String msg){
        try {
            OutputUtil.remindVerify();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        ByteBuf buf = null;
        switch (VersionData.serverVersion){
            case V1_18:
            case V1_20:
            case V1_21:
                byte[] bytes = (msg).getBytes(StandardCharsets.UTF_8);
                buf = Unpooled.buffer(bytes.length);
                buf.writeBytes(bytes);
                break;
            case V1_16:
            case V1_17:
            case V1_19:

                try {
                    Class<? extends CommandSender> senderClass = player.getClass();
                    Method addChannel = senderClass.getDeclaredMethod("addChannel", String.class);
                    addChannel.setAccessible(true);
                    addChannel.invoke(player, channel);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                bytes = (msg).getBytes(StandardCharsets.UTF_8);
                buf = Unpooled.buffer(bytes.length+300);
                buf.writeByte(IDX);
                buf.writeBytes(bytes);
                break;
            case V1_12:
            case V1_7_10:
                bytes = msg.getBytes(StandardCharsets.UTF_8);
                buf = Unpooled.buffer(bytes.length+50);
                buf.writeBytes(bytes);
                break;
        }
        player.sendPluginMessage(SuperVideo.getPlugin(), channel, buf.array());
    }

it can work on 1.20 server,but has this problem when work on 1.21 server

commented

your packetcodec for the payload doesnt match the packet you are sending

commented

your packetcodec for the payload doesnt match the packet you are sending

Thanks a lot! You really saved my day!

EVERYTHING WORK PERFECT NOW

commented

The new code:

    public void init() {
        PayloadTypeRegistry.playC2S().register(BlockHighlightPayload.ID, BlockHighlightPayload.CODEC);
        PayloadTypeRegistry.playS2C().register(BlockHighlightPayload.ID, BlockHighlightPayload.CODEC);

        ClientPlayNetworking.registerGlobalReceiver(BlockHighlightPayload.ID, new ClientPlayNetworking.PlayPayloadHandler<BlockHighlightPayload>() {
            @Override
            public void receive(BlockHighlightPayload payload, ClientPlayNetworking.Context context) {

            }
        });
    }
package pers.byxiaobai.supervideo.network;

import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.encoding.StringEncoding;

public class MyPacketCodec implements PacketCodec<ByteBuf, String> {
    @Override
    public String decode(ByteBuf buf) {
        String str=StringEncoding.decode(buf,32767);
        if (buf.isReadable()) {
            buf.skipBytes(buf.readableBytes());
        }
        NetworkManager.INSTANCE.onReceive(str);
        return str;
    }

    @Override
    public void encode(ByteBuf buf, String value) {
        StringEncoding.encode(buf, value, 32767);
    }
}