Kaboom
📎 1 file 2,869 chars
📄
pasted-code-1794677641349.txt
txt 36 lines
▶
Build a client-side Fabric mod for Minecraft 1.21.11 (Java 21, Fabric Loader + Fabric API, Gradle project with the current fabric-loom). Name: "Packet Guard". Give me the complete project: build.gradle, gradle.properties, fabric.mod.json, the mixins JSON, all source files, and short build instructions.

GOAL
Prevent me from being kicked/"chunk banned" because of items with oversized data (bloated components/NBT, deeply nested containers like shulkers inside barrels). The mod protects both outgoing and incoming traffic.

FEATURES
1. Outgoing protection (creative inventory):
   - Mixin into the code that sends CreativeInventoryActionC2SPacket.
   - Before sending, estimate the serialized size of the ItemStack (encode it with the ItemStack codec / RegistryFriendlyByteBuf and measure the bytes).
   - If the size exceeds a configurable threshold (default 64 KB), cancel the packet and show a red warning in chat with the slot and the size.
   - Do the same for Pick Block (Ctrl + Middle Click) so oversized items never enter my inventory.

2. Incoming protection (inventory sync):
   - Harden the handling of InventoryS2CPacket, ScreenHandlerSlotUpdateS2CPacket and SetCursorItem/SetPlayerInventory packets.
   - Catch exceptions and oversized data during ItemStack decoding via mixin. Replace the affected stack with ItemStack.EMPTY instead of letting the connection drop.
   - Log which slot was replaced and show a chat warning.
   - Also guard against excessive nesting depth of container components (e.g. max depth 3, configurable).

3. Config:
   - JSON config file in the Fabric config directory (max stack size in bytes, max nesting depth, enable/disable outgoing and incoming protection, chat warnings on/off).
   - Optional: ModMenu integration if easy, otherwise just the file.

4. Commands (client-side, Fabric client command API):
   - /packetguard status: show settings and the number of blocked stacks this session.
   - /packetguard threshold : change the size limit.
   - /packetguard toggle: enable/disable the mod.

REQUIREMENTS
- Client-side only ("environment": "client"), no server component.
- Use Mixins with proper @Inject targets for 1.21.11. Use the correct mappings (Yarn or Mojang, tell me which one you chose) and name every class and method you target.
- Use the data component system (not legacy NBT) since this is 1.21.x.
- Clean, commented code, no deprecated APIs, null-safe, no crashes if a mixin target fails.
- If any class name, method or packet signature for 1.21.11 is uncertain, say so explicitly and tell me what to check, instead of guessing.

OUTPUT FORMAT
Show the file tree first, then each file in its own code block with its path as heading. End with build steps (./gradlew build) and a short test plan (how to test with an oversized shulker-in-shulker item in creative).
Kodari AI
KodariNov 14 at 5:48 pm 4
PacketGuard
A client-side Fabric mod for Minecraft 1.21.11 that prevents disconnection from oversized item data. It intercepts outgoing CreativeInventoryActionC2SPacket and Pick Block operations, measuring serialized ItemStack size via RegistryFriendlyByteBuf encoding, blocking packets exceeding a configurable byte threshold (default 64KB) with chat warnings. Incoming protection hardens InventoryS2CPacket, ScreenHandlerSlotUpdateS2CPacket, and cursor packets by catching decoding exceptions and replacing problematic stacks with ItemStack.EMPTY, plus guards against excessive container component nesting depth. Features JSON configuration in Fabric config directory, client commands for status/threshold/toggle, and session block counters.
  • Verify the 1.21.11 Yarn packet, stack-serialization, and client-command APIs before writing code.

  • Build the client-only Fabric project with configurable guards, client commands, and targeted mixins.

  • Include the project metadata and concise build/test steps.

Plain Text
packet-guard/
├── build.gradle
├── gradle.properties
├── settings.gradle
└── src/
    └── client/
        ├── java/com/kodari/packetguard/
        │   ├── PacketGuard.java
        │   ├── PacketGuardClient.java
        │   ├── PacketGuardCommands.java
        │   ├── PacketGuardConfig.java
        │   └── mixin/
        │       ├── ClientConnectionMixin.java
        │       ├── ClientPlayerInteractionManagerMixin.java
        │       └── ClientPlayNetworkHandlerMixin.java
        └── resources/
            ├── fabric.mod.json
            └── packetguard.mixins.json

The client-only Fabric 1.21.11 project uses Yarn mappings. It checks outgoing creative inventory and pick-block stacks, sanitizes oversized or over-nested stacks in decoded inventory updates, writes settings to config/packetguard.json, and registers /packetguard status, /packetguard threshold <bytes>, and /packetguard toggle.

Important limitation: incoming stacks are checked after successful packet decoding. A malformed stack that throws during decoding cannot safely be replaced with ItemStack.EMPTY: the decoder may have consumed only part of its payload, so the packet’s remaining bytes cannot be reliably realigned. Such a decode failure may still disconnect the client.

Build: On desktop, click Compile in the top bar; on phone, tap ⋮ → Compile Project. Click Compile in the window, then Download when it succeeds.

Test plan: In a creative test world, try sending an oversized stack through the creative inventory and Ctrl+middle-clicking a block with an oversized pick stack; verify the red warnings and blocked count. Test incoming sanitization by having a test server send an oversized or over-depth stack in an inventory update, then confirm it is replaced with an empty stack.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
12 476
src/client/java/com/kodari/packetguard/PacketGuardClient.java
package com.kodari.packetguard;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;

public final class PacketGuardClient implements ClientModInitializer {
    @Override
    public void onInitializeClient() {
        PacketGuard.initialize(PacketGuardConfig.load());
        ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) ->
                PacketGuardCommands.register(dispatcher));
    }
}