tinycodinglm / mc /mc-0046.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame Contribute Delete
3 kB
{"type": "session", "harness": "opencode", "id": "mc-0046", "name": "Networking v2"}
{"type": "message", "message": {"role": "system", "content": "You are an expert Fabric mod developer. You write idiomatic Minecraft Fabric mods using Java, Fabric API, and Yarn mappings."}}
{"type": "message", "message": {"role": "user", "content": "How does the newer networking API work with CustomPayload and StreamCodec in Fabric 1.21+?"}}
{"type": "message", "message": {"role": "assistant", "content": "```java\npackage com.example.mymod.networking;\n\nimport net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;\nimport net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;\nimport net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;\nimport net.minecraft.network.PacketByteBuf;\nimport net.minecraft.network.codec.PacketCodec;\nimport net.minecraft.network.packet.CustomPayload;\nimport net.minecraft.util.Identifier;\nimport net.minecraft.util.math.BlockPos;\n\npublic class MyNetworking {\n public record ChatMessagePayload(String message, BlockPos pos) implements CustomPayload {\n public static final Id<ChatMessagePayload> ID = new Id<>(Identifier.of(\"mymod\", \"chat_message\"));\n public static final PacketCodec<PacketByteBuf, ChatMessagePayload> CODEC = PacketCodec.of(\n (buf, payload) -> { buf.writeString(payload.message()); buf.writeBlockPos(payload.pos()); },\n buf -> new ChatMessagePayload(buf.readString(), buf.readBlockPos())\n );\n @Override public Id<? extends CustomPayload> getId() { return ID; }\n }\n\n public record SyncDataPayload(int entityId, float health) implements CustomPayload {\n public static final Id<SyncDataPayload> ID = new Id<>(Identifier.of(\"mymod\", \"sync_data\"));\n public static final PacketCodec<PacketByteBuf, SyncDataPayload> CODEC = PacketCodec.of(\n (buf, payload) -> { buf.writeInt(payload.entityId()); buf.writeFloat(payload.health()); },\n buf -> new SyncDataPayload(buf.readInt(), buf.readFloat())\n );\n @Override public Id<? extends CustomPayload> getId() { return ID; }\n }\n\n public static void register() {\n PayloadTypeRegistry.playC2S().register(ChatMessagePayload.ID, ChatMessagePayload.CODEC);\n PayloadTypeRegistry.playS2C().register(SyncDataPayload.ID, SyncDataPayload.CODEC);\n\n ServerPlayNetworking.registerGlobalReceiver(ChatMessagePayload.ID, (payload, context) -> {\n context.server().execute(() -> {\n context.player().sendMessage(Text.literal(\"Server got: \" + payload.message()));\n });\n });\n\n ClientPlayNetworking.registerGlobalReceiver(SyncDataPayload.ID, (payload, context) -> {\n context.client().execute(() -> {\n var entity = context.client().world.getEntityById(payload.entityId());\n // Apply health sync\n });\n });\n }\n}\n```"}}