Buckets:
| package com.example.examplemod; | |
| import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; | |
| import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; | |
| import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; | |
| import net.minecraft.network.RegistryByteBuf; | |
| import net.minecraft.network.codec.PacketCodec; | |
| import net.minecraft.network.packet.CustomPayload; | |
| import net.minecraft.util.Identifier; | |
| /** | |
| * Networking switched to the CustomPayload record system in 1.20.5+ and is | |
| * unchanged through 1.21.11. Define payload, register codec + handler. | |
| */ | |
| public class ExampleNetworking { | |
| public static final Identifier PAYLOAD_ID = Identifier.of(ExampleMod.MOD_ID, "example_payload"); | |
| // The payload itself - a simple record holding the data you want to send. | |
| public record ExamplePayload(String message, int amount) implements CustomPayload { | |
| public static final CustomPayload.Id<ExamplePayload> ID = | |
| new CustomPayload.Id<>(PAYLOAD_ID); | |
| public static final PacketCodec<RegistryByteBuf, ExamplePayload> CODEC = | |
| PacketCodec.tuple( | |
| PacketCodec.STRING_UTF8, ExamplePayload::message, | |
| PacketCodec.VAR_INT, ExamplePayload::amount, | |
| ExamplePayload::new | |
| ); | |
| public Id<? extends CustomPayload> getId() { | |
| return ID; | |
| } | |
| } | |
| // Call once from ModInitializer#onInitialize() (common to client+server) | |
| public static void registerCommon() { | |
| PayloadTypeRegistry.playC2S().register(ExamplePayload.ID, ExamplePayload.CODEC); | |
| PayloadTypeRegistry.playS2C().register(ExamplePayload.ID, ExamplePayload.CODEC); | |
| // Server-side receiver | |
| ServerPlayNetworking.registerGlobalReceiver(ExamplePayload.ID, (payload, context) -> { | |
| context.player().sendMessage( | |
| net.minecraft.text.Text.literal("Server got: " + payload.message() + " x" + payload.amount()), | |
| false | |
| ); | |
| }); | |
| } | |
| // Call from ClientModInitializer#onInitializeClient() | |
| public static void registerClient() { | |
| ClientPlayNetworking.registerGlobalReceiver(ExamplePayload.ID, (payload, context) -> { | |
| // handle a packet sent from the server to this client | |
| }); | |
| } | |
| // Example: sending a packet from client to server | |
| public static void sendExamplePacket(String message, int amount) { | |
| ClientPlayNetworking.send(new ExamplePayload(message, amount)); | |
| } | |
| } | |
Xet Storage Details
- Size:
- 2.57 kB
- Xet hash:
- df599113363f01b1dc6389f001290e950bd4d3f10a8130c5af8286722a204b3f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.