Fabric Plugin Messaging
A library for exchanging messages between plugins in Fabric! This server-side mod registers a special data transfer packet (PluginMessagePacket.class) and a corresponding event (PluginMessageEvent#EVENT) that developers can use in their projects.
The main purpose of this library is to prevent conflicts that occur when multiple mods attempt to register their own data transfer packets. By using unified tools from this mod, developers avoid such problems.
Information for Developers
Add the mod as a dependency in your Gradle build script. This will include the API in your project's final build:
dependencies {
modImplementation(include("com.pokeskies:fabricpluginmessage:1.0.0"))
}
Sending Messages
To send plugin messages, create an instance of the PluginMessagePacket class and send it. You can create the packet by passing either a byte array or FriendlyByteBuf:
ByteArrayDataOutput outputStream = ByteStreams.newDataOutput();
outputStream.writeUTF("Connect");
outputStream.writeUTF("cobblemon");
ServerPlayNetworking.send(player, new PluginMessagePacket(outputStream.toByteArray()));
Receiving Messages
If you need to receive plugin messages from the proxy, you can subscribe to the PluginMessageEvent:
PluginMessageEvent.EVENT.register((payload, context) -> {
ByteArrayDataInput inputStream = ByteStreams.newDataInput(payload.getData());
String channel = inputStream.readUTF();
if (channel.equals("GetServers")) {
String serversList = inputStream.readUTF();
System.out.println("Proxy Servers: " + serversList);
}
});