-
Notifications
You must be signed in to change notification settings - Fork 1
Fabric Integration
RazorPlay01 edited this page May 14, 2025
·
2 revisions
This guide explains how to integrate the Minecraft Networking API with Fabric clients.
Ensure the API is added as a dependency (see Setup). In your build.gradle, include:
dependencies {
modImplementation 'net.fabricmc:fabric-api:0.92.0+1.20.5'
implementation 'com.github.RazorPlay01:PacketHandler:1.1.0'
include 'com.github.RazorPlay01:PacketHandler:1.1.0'
}Use a CustomPayload implementation to bridge the API with Fabric’s networking.
Example:
import com.github.razorplay.packet_handler.network.IPacket;
import com.github.razorplay.packet_handler.network.PacketTCP;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
public record FabricCustomPayload(IPacket packet) implements CustomPayload {
public static final Id<FabricCustomPayload> CUSTOM_PAYLOAD_ID = new Id<>(Identifier.of("my_mod", "packets_channel"));
public static final PacketCodec<RegistryByteBuf, FabricCustomPayload> CODEC = PacketCodec.tuple(
new PacketCodec<ByteBuf, IPacket>() {
@Override
public IPacket decode(ByteBuf byteBuf) {
try {
byte[] data = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(data);
ByteArrayDataInput in = ByteStreams.newDataInput(data);
return PacketTCP.read(in);
} catch (Exception e) {
System.err.println("Error decoding packet: " + e.getMessage());
return null;
}
}
@Override
public void encode(ByteBuf byteBuf, IPacket packet) {
try {
byteBuf.writeBytes(PacketTCP.write(packet));
} catch (Exception e) {
System.err.println("Error encoding packet: " + e.getMessage());
}
}
},
FabricCustomPayload::packet,
FabricCustomPayload::new
);
@Override
public Id<? extends CustomPayload> getId() {
return CUSTOM_PAYLOAD_ID;
}
}In your mod’s initializer, register packets and the payload type:
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
public class MyMod implements ClientModInitializer {
@Override
public void onInitializeClient() {
PacketTCP.registerPackets(MyPacket.class);
PayloadTypeRegistry.playS2C().register(FabricCustomPayload.CUSTOM_PAYLOAD_ID, FabricCustomPayload.CODEC);
PayloadTypeRegistry.playC2S().register(FabricCustomPayload.CUSTOM_PAYLOAD_ID, FabricCustomPayload.CODEC);
ClientPlayNetworking.registerGlobalReceiver(FabricCustomPayload.CUSTOM_PAYLOAD_ID, (payload, context) -> {
IPacket packet = payload.packet();
if (packet instanceof MyPacket myPacket) {
context.client().execute(() -> {
System.out.println("Received: " + myPacket.getMessage());
});
}
});
}
}Send packets from the client to the server:
ClientPlayNetworking.send(new FabricCustomPayload(new MyPacket("Hello from client!")));