How can i send packages on a Minecraft Client (With Fabricmc) and receive it on a Bukkit Server?

1.6k Views Asked by At

I'm a Chinese secondary school student,so my written English may not very well. How can i send packages (like some words) on a Minecraft Client (Use a Fabricmc Mod to send) and receive it on the bukkit server on MC Multiplayer?

1

There are 1 best solutions below

0
Lucan On

This is done using what's known as the 'plugin messaging channel'. Take a look at this Fabricmc wiki to read about client networking (messaging). See this Spigot wiki on the plugin server-side messaging channel; ignore that this wiki talks a lot about bungee, it's just because that's a common use case. You can make your own channel.

The code below is copied from said wikis and is very much pseudo-code:

Client
Sending from the client

PacketByteBuf buf = PacketByteBufs.create();
buf.writeBlockPos(target);
ServerPlayNetworking.send((ServerPlayerEntity) user, TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, buf);

Receiving on the client

ClientPlayNetworking.registerGlobalReceiver(TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, (client, handler, buf, responseSender) -> {
    client.execute(() -> {
        // Everything in this lambda is run on the render thread
        ClientBlockHighlighting.highlightBlock(client, target);
    });
});

Where you see TutorialNetworkingConstants.HIGHL..., that's the Identifier for the channel.

Server (Spigot/Bukkit)
Sending from the server

player.sendPluginMessage(this, "YourChannelName", out.toByteArray());

Receiving on the server

@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
    if (!channel.equals("YourChannelName")) {
      return;
    }
    ByteArrayDataInput in = ByteStreams.newDataInput(message);
    String data = in.readUTF();
    ...

Take a thorough read of those tutorials, they should cover all you need to know. Just be sure to unregister your channels on both the client and server.