Granny
📎 1 file 2,285 chars
📄
pasted-code-1794329750550.txt
txt 60 lines
▶
Create a fully functional Minecraft Fabric client-side mod called **Auto Helmet Combiner**.

Minecraft Version: 1.21.11
Mod Loader: Fabric
Java Version: 21

### MAIN FUNCTION

The mod should automatically detect two identical enchanted helmets in the player's inventory and combine them using an Anvil.

### REQUIREMENTS

1. Detect Protection II helmets in the player's inventory.
2. When two Protection II helmets are found, identify them automatically.
3. Locate a nearby Anvil.
4. If the Anvil is within interaction range, automatically open its GUI.
5. Place the first Protection II helmet in the first Anvil slot.
6. Place the second Protection II helmet in the second Anvil slot.
7. Take the resulting Protection III helmet.
8. Automatically locate a nearby chest.
9. Open the chest and store the Protection III helmet inside.
10. Never drop or delete any items.
11. Never combine unrelated helmets or other items.
12. If no Anvil or chest is available, safely wait.
13. If the Anvil operation requires XP levels, check available XP before proceeding.
14. Prevent repeated clicks and duplicate operations.

### AUTOMATION

- Add a configurable ON/OFF keybind.
- Display current status on screen.
- Add an option to select helmet types.
- Add an option to select enchantment levels.
- Add an option to enable or disable automatic chest storage.
- Add an option to configure interaction range.
- Show current operation status.

### IMPORTANT SAFETY FEATURES

- Never overwrite existing chest items.
- Never discard items if the chest is full.
- Never take items from containers other than the selected output chest.
- Handle lag, inventory synchronization and server delays.
- Do not bypass server permissions or anti-cheat.
- Only use normal Minecraft client interactions.

### TECHNICAL REQUIREMENTS

- Fabric client-side mod.
- Minecraft 1.21.11 compatible.
- Java 21.
- Proper Fabric API and Yarn mappings.
- Use a reliable state machine for automation.
- Keep the mod lightweight.
- Include complete source code.
- Include fabric.mod.json and build.gradle.
- Provide a ready-to-use compiled JAR.
- Include installation instructions.

IMPORTANT: Do not provide pseudocode or incomplete methods. Generate a complete compilable project and verify all Minecraft 1.21.11 API calls.
Kodari AI
KodariNov 10 at 5:10 pm 3
AutoHelmetCombiner
A Fabric client-side mod for Minecraft 1.21.11 that automatically detects two identical enchanted helmets in the player's inventory, combines them via a nearby Anvil to upgrade enchantment levels, and optionally stores the result in a chest. Features a configurable keybind toggle, on-screen status display, helmet type and enchantment level selection, interaction range configuration, and a robust state machine that handles server delays, XP requirements, and prevents duplicate operations. All actions use normal client interactions without bypassing server permissions or anti-cheat systems.

Plan:

  1. Build a client-only Fabric 1.21.11 project with an editable config file.

  2. Implement a guarded state machine for finding helmets, using a nearby anvil, and optionally storing output in a chest.

  3. Add toggle/config keybinds and a compact on-screen status display.

Created the Fabric 1.21.11 client mod project with a guarded anvil workflow, editable settings, configurable keybinds, and an on-screen status display. It only selects matching helmets of the chosen type and Protection level, checks XP before taking the result, and uses normal client interactions. Chest storage targets nearby single chests; if no chest is available or it is full, the result stays in your inventory.

Default controls: H toggle, K helmet type, L Protection level, J chest storage, R search range. Settings are saved to config/auto_helmet_combiner.properties.

To get the JAR:

  1. Click Compile in the top bar, or on phone tap ⋮ → Compile Project.

  2. Click Compile in the window.

  3. When it succeeds, click Download on the build.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
README.md
settings.gradle
Code
7 934
src/client/java/com/kodari/autohelmetcombiner/AutoHelmetCombinerClient.java
package com.kodari.autohelmetcombiner;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import java.util.function.Predicate;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.ChestBlock;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.enums.ChestType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.network.ClientPlayerInteractionManager;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.screen.AnvilScreenHandler;
import net.minecraft.screen.GenericContainerScreenHandler;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.util.Identifier;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.inventory.Inventory;

import org.lwjgl.glfw.GLFW;

public final class AutoHelmetCombinerClient implements ClientModInitializer {
    private static final String MOD_ID = "auto_helmet_combiner";
    private static final String[] HELMET_NAMES = {
            "leather", "chainmail", "iron", "golden", "diamond", "netherite", "turtle", "copper"
    };
    private static final Item[] HELMETS = {
            Items.LEATHER_HELMET,
            Items.CHAINMAIL_HELMET,
            Items.IRON_HELMET,
            Items.GOLDEN_HELMET,
            Items.DIAMOND_HELMET,
            Items.NETHERITE_HELMET,
            Items.TURTLE_HELMET,
            Items.COPPER_HELMET
    };
    private static final double[] RANGE_OPTIONS = {2.5, 3.0, 3.5, 4.0, 4.5};
    private static final Settings SETTINGS = new Settings();

    private static KeyBinding toggleBinding;
    private static KeyBinding helmetBinding;
    private static KeyBinding levelBinding;
    private static KeyBinding storageBinding;
    private static KeyBinding rangeBinding;

    private static boolean enabled;
    private static String status = "Off";
    private static Stage stage = Stage.IDLE;
    private static long tick;
    private static int waitTicks;
    private static int completionTicks;

    private static Item runHelmet;
    private static int runProtectionLevel;
    private static boolean runAutoStore;
    private static int firstInventoryIndex = -1;
    private static int secondInventoryIndex = -1;
    private static ItemStack firstInput = ItemStack.EMPTY;
    private static ItemStack secondInput = ItemStack.EMPTY;
    private static ItemStack resultTemplate = ItemStack.EMPTY;
    private static int playerResultCountBefore;
    private static int chestResultCountBefore;
    private static int playerChestCountBefore;
    private static BlockPos anvilPos;
    private static BlockPos chestPos;

    @Override
    public void onInitializeClient() {
        MinecraftClient client = MinecraftClient.getInstance();
        SETTINGS.load(client.runDirectory.toPath());

        KeyBinding.Category category = KeyBinding.Category.create(Identifier.of(MOD_ID, "general"));
        toggleBinding = KeyBindingHelper.registerKeyBinding(
                new KeyBinding("key.auto_helmet_combiner.toggle", GLFW.GLFW_KEY_H, category));
        helmetBinding = KeyBindingHelper.registerKeyBinding(
                new KeyBinding("key.auto_helmet_combiner.helmet", GLFW.GLFW_KEY_K, category));
        levelBinding = KeyBindingHelper.registerKeyBinding(
                new KeyBinding("key.auto_helmet_combiner.level", GLFW.GLFW_KEY_L, category));
        storageBinding = KeyBindingHelper.registerKeyBinding(
                new KeyBinding("key.auto_helmet_combiner.storage", GLFW.GLFW_KEY_J, category));
        rangeBinding = KeyBindingHelper.registerKeyBinding(
                new KeyBinding("key.auto_helmet_combiner.range", GLFW.GLFW_KEY_R, category));

        ClientTickEvents.END_CLIENT_TICK.register(AutoHelmetCombinerClient::tick);
        HudElementRegistry.addLast(Identifier.of(MOD_ID, "status"), AutoHelmetCombinerClient::renderHud);
    }

    private static void tick(MinecraftClient client) {
        tick++;
        handleKeybinds();

        if (client.player == null || client.world == null || client.interactionManager == null) {
            if (enabled) {
                status = "Waiting for a world";
            }
            return;
        }
        if (!enabled) {
            status = "Paused";
            return;
        }

        switch (stage) {
            case IDLE -> {
                if (tick % 20 == 0) {
                    startIfPossible(client);
                }
            }
            case OPENING_ANVIL -> waitForAnvil(client);
            case PICKUP_FIRST -> pickupFirst(client);
            case WAIT_FIRST_CURSOR -> placeFirst(client);
            case WAIT_FIRST_PLACED -> waitForFirst(client);
            case PICKUP_SECOND -> pickupSecond(client);
            case WAIT_SECOND_CURSOR -> placeSecond(client);
            case WAIT_SECOND_PLACED -> waitForSecond(client);
            case WAIT_RESULT -> waitForResult(client);
            case WAIT_OUTPUT_TRANSFER -> waitForOutputTransfer(client);
            case WAIT_CHEST -> findChest(client);
            case OPENING_CHEST -> waitForChest(client);
            case STORE_RESULT -> storeResult(client);
            case WAIT_CHEST_TRANSFER -> waitForChestTransfer(client);
            case COMPLETE -> {
                if (++completionTicks >= 40) {
                    resetOperation();
                    status = "Ready";
                }
            }
        }
    }

    private static void handleKeybinds() {
        while (toggleBinding.wasPressed()) {
            enabled = !enabled;
            status = enabled ? "Enabled" : "Paused";
        }
        while (helmetBinding.wasPressed()) {
            SETTINGS.helmetIndex = (SETTINGS.helmetIndex + 1) % HELMETS.length;
            SETTINGS.save();
            status = "Helmet: " + displayName(SETTINGS.helmetIndex);
        }
        while (levelBinding.wasPressed()) {
            SETTINGS.protectionLevel = SETTINGS.protectionLevel % 4 + 1;
            SETTINGS.save();
            status = "Input enchantment: Protection " + SETTINGS.protectionLevel;
        }
        while (storageBinding.wasPressed()) {
            SETTINGS.autoStore = !SETTINGS.autoStore;
            SETTINGS.save();
            status = "Chest storage: " + (SETTINGS.autoStore ? "On" : "Off");
        }
        while (rangeBinding.wasPressed()) {
            SETTINGS.rangeIndex = (SETTINGS.rangeIndex + 1) % RANGE_OPTIONS.length;
            SETTINGS.save();
            status = "Search range: " + range() + " blocks";
        }
    }

    private static void startIfPossible(MinecraftClient client) {
        if (client.currentScreen != null) {
            status = "Close the open screen to start";
            return;
        }

        PlayerInventory inventory = client.player.getInventory();
        int[] pair = findHelmetPair(client, inventory);
        if (pair == null) {
            status = "Waiting for matching Protection " + SETTINGS.protectionLevel + " helmets";
            return;
        }

        BlockPos foundAnvil = findNearest(client, state -> state.isOf(Blocks.ANVIL)
                || state.isOf(Blocks.CHIPPED_ANVIL)
                || state.isOf(Blocks.DAMAGED_ANVIL));
        if (foundAnvil == null) {