rijuunite

Create a Minecraft Client-Side Mod called "RijuuKit" for Fabric (MC Version: 1.20.1).

Concept:

RijuuKit is a purely client-side mod that allows players to quickly save and restore inventory/kit layouts (Armor, Offhand, Hotbar, Main Inventory) using custom commands.

Command Syntax:

  1. /rk save <kit_name>

- Reads the client player's current inventory (Armor, Offhand, and Main Inventory). - Saves item IDs, stack counts, NBT data (enchantments, durability, lore, custom names), and slot positions into a JSON file located in .minecraft/config/rijuukit/<kit_name>.json. - Displays a client-side chat message: "§a[RijuuKit] Kit '<kit_name>' saved successfully!"

  1. /rk load <kit_name>

- Checks if .minecraft/config/rijuukit/<kit_name>.json exists. - If not found, displays error: "§c[RijuuKit] Kit '<kit_name>' does not exist!" - If found: - For Singleplayer / Creative / OP: Clears the player's inventory and loads the exact saved NBT items into their respective slots. - For Client-Side execution: Uses ClientPlayerEntity / ClientPlayNetworkHandler to sync slot changes or send necessary packets to apply the saved kit layout. - Displays chat message: "§a[RijuuKit] Kit '<kit_name>' loaded!"

  1. /rk list

- Scans .minecraft/config/rijuukit/ directory and lists all saved kit names in chat.

Technical Requirements:

  • Register commands using ClientCommandRegistrationCallback (Fabric Client Command API v2).
  • Save and parse item data using Gson and Minecraft's NBT Compound API (NbtCompound / ItemStack.writeNbt).
  • Ensure smooth error handling (null checks for empty slots, handling corrupted JSON files gracefully).

Please generate the complete Java code structure with:

  1. RijuuKitClient.java (Client Mod Initializer & Command Registration)
  2. KitManager.java (Handling JSON File I/O, NBT Serialization/Deserialization)
Kodari AI
KodariJul 22 at 7:49 am 11
RijuuKit
RijuuKit is a purely client-side Fabric mod for Minecraft 1.20.1 that saves and restores named inventory kits through the /rk command family. It captures hotbar, main inventory, armor, and offhand slots, including item counts and complete NBT such as enchantments, durability, lore, and custom names. Kits are stored as readable JSON under config/rijuukit, with list support, name validation, missing-file handling, and graceful recovery from corrupted kit data.
Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 570
src/main/java/com/rijuukit/RijuuKitClient.java
package com.rijuukit;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

public final class RijuuKitClient implements ClientModInitializer {
    private static final int INVENTORY_SIZE = 41;

    private KitManager kitManager;

    @Override
    public void onInitializeClient() {
        this.kitManager = new KitManager();

        ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) ->
            registerCommands(dispatcher)
        );
    }

    private void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher) {
        dispatcher.register(
            ClientCommandManager.literal("rk")
                .then(ClientCommandManager.literal("save")
                    .then(ClientCommandManager.argument("kit_name", StringArgumentType.word())
                        .executes(context ->
                            saveKit(StringArgumentType.getString(context, "kit_name"))
                        )
                    )
                )
                .then(ClientCommandManager.literal("load")
                    .then(ClientCommandManager.argument("kit_name", StringArgumentType.word())
                        .executes(context ->
                            loadKit(StringArgumentType.getString(context, "kit_name"))
                        )
                    )
                )
                .then(ClientCommandManager.literal("list")
                    .executes(context -> listKits())
                )
        );
    }

    private int saveKit(String kitName) {
        MinecraftClient client = MinecraftClient.getInstance();
        ClientPlayerEntity player = client.player;

        if (player == null) {
            return 0;
        }

        KitManager.SaveStatus status = kitManager.saveKit(kitName, player.getInventory());

        if (status == KitManager.SaveStatus.SUCCESS) {
            sendSuccess(player, "[RijuuKit] Kit '" + kitName + "' saved successfully!");
        } else if (status == KitManager.SaveStatus.INVALID_NAME) {
            sendError(player, "[RijuuKit] Invalid kit name. Use only letters, numbers, '_' or '-'.");
        } else {
            sendError(player, "[RijuuKit] The kit could not be saved.");
        }

        return 1;
    }

    private int loadKit(String kitName) {
        MinecraftClient client = MinecraftClient.getInstance();
        ClientPlayerEntity player = client.player;

        if (player == null) {
            return 0;
        }

        KitManager.LoadResult result = kitManager.loadKit(kitName);

        if (result.status() == KitManager.LoadStatus.INVALID_NAME) {
            sendError(player, "[RijuuKit] Invalid kit name.");
            return 1;
        }

        if (result.status() == KitManager.LoadStatus.NOT_FOUND) {
            sendError(player, "[RijuuKit] Kit '" + kitName + "' does not exist!");
            return 1;
        }

        if (result.status() == KitManager.LoadStatus.CORRUPTED) {
            sendError(player, "[RijuuKit] Kit '" + kitName + "' is corrupted and could not be loaded.");
            return 1;
        }

        if (result.status() == KitManager.LoadStatus.IO_ERROR || result.kit() == null) {
            sendError(player, "[RijuuKit] The kit could not be read.");
            return 1;
        }

        if (applyKit(client, player, result.kit())) {
            sendSuccess(player, "[RijuuKit] Kit '" + kitName + "' loaded!");
        }

        return 1;
    }

    private int listKits() {
        MinecraftClient client = MinecraftClient.getInstance();
        ClientPlayerEntity player = client.player;

        if (player == null) {
            return 0;
        }

        if (kitManager.listKits().isEmpty()) {
            sendInfo(player, "[RijuuKit] No saved kits found.");
        } else {
            sendInfo(player, "[RijuuKit] Saved kits: " + String.join(", ", kitManager.listKits()));
        }

        return 1;
    }

    private boolean applyKit(
        MinecraftClient client,
        ClientPlayerEntity player,
        KitManager.Kit kit
    ) {
        if (client.isInSingleplayer()) {
            IntegratedServer server = client.getServer();

            if (server == null) {
                sendError(player, "[RijuuKit] The integrated server is unavailable.");
                return false;
            }

            applyToIntegratedServer(server, player, kit);
            return true;
        }

        if (!player.isCreative()) {
            sendError(
                player,
                "[RijuuKit] Multiplayer kit loading requires Creative mode because the server controls inventory changes."
            );
            return false;
        }

        applyToInventory(player.getInventory(), kit);
        syncCreativeInventory(player);
        return true;
    }

    private void applyToIntegratedServer(
        IntegratedServer server,
        ClientPlayerEntity clientPlayer,
        KitManager.Kit kit
    ) {
        server.execute(() -> {
            ServerPlayerEntity serverPlayer =
                server.getPlayerManager().getPlayer(clientPlayer.getUuid());

            if (serverPlayer == null) {
                return;
            }

            applyToInventory(serverPlayer.getInventory(), kit);
            serverPlayer.playerScreenHandler.sendContentUpdates();
        });
    }

    private void applyToInventory(PlayerInventory inventory, KitManager.Kit kit) {
        for (int slot = 0; slot < INVENTORY_SIZE; slot++) {
            inventory.setStack(slot, ItemStack.EMPTY);
        }

        kit.items().forEach((slot, nbt) ->
            inventory.setStack(slot, ItemStack.fromNbt(nbt))
        );
    }

    private void syncCreativeInventory(ClientPlayerEntity player) {
        ClientPlayNetworkHandler networkHandler = player.networkHandler;
        PlayerInventory inventory = player.getInventory();

        for (int inventorySlot = 0; inventorySlot < INVENTORY_SIZE; inventorySlot++) {
            int screenSlot = toScreenHandlerSlot(inventorySlot);
            networkHandler.sendPacket(
                new CreativeInventoryActionC2SPacket(
                    screenSlot,
                    inventory.getStack(inventorySlot).copy()