mastar_probg

make me the auto vilager trading mod for 1.21.11 versiion select a trade and automatical filing trade

Kodari AI
KodariMay 6 at 9:05 am 76,212 (v2)
AutoVillagerTrading
A Fabric 1.21.4 mod that adds automatic villager trading. Open a villager's trade screen, select a trade, and the mod will automatically and repeatedly execute that trade using items from your inventory. Features a toggle keybind to start/stop auto-trading, visual feedback showing the selected trade, and smart inventory management to fill trade slots and collect output items automatically.

Let me read the necessary docs before writing any code.

AutoVillagerTrading
A Fabric 1.21.4 client-side mod that automates villager trading. Open a villager's trade screen, left-click any trade to select it, then press R to start auto-trading. The mod repeatedly sends the trade to the server and collects the result into your inventory. Press R again to stop, or close the screen. A HUD overlay shows the selected trade name and whether auto-trading is active.

Plan:

  • Register keybind ® + client tick event + HUD overlay in AutoVillagerTradingClient

  • AutoTradeManager singleton holds trade state and executes trades via SelectMerchantTradeC2SPacket + clickSlot

  • MerchantScreenHandlerMixin injects into setOfferIndex to capture selected trade index (best-effort, require=0)

  • MerchantScreenMixin injects into mouseClicked (positional fallback) and keyPressed (toggle keybind)

Plan:

  • No mixins needed — auto-trading works by clicking slot 2 (output) repeatedly; vanilla handler auto-refills inputs from inventory

  • AutoTradeManager singleton holds state and executes trades by clicking the output slot

  • AutoVillagerTradingClient registers keybind ®, client tick, and HUD overlay

  • HUD shows trading status and current output item when a merchant screen is open

The mod is complete with three files:

  • AutoTradeManager — singleton that tracks active state and clicks the merchant output slot (slot 2) every 5 ticks. Vanilla automatically re-fills the input slots from inventory, so no SelectMerchantTradeC2SPacket is needed. Stops automatically after 3 seconds of empty output (ingredients exhausted).

  • AutoVillagerTradingClient — registers the R keybind, wires up client tick, and draws a HUD overlay showing trading status + selected item whenever a merchant screen is open.

  • No mixins needed — the entire mechanic works through vanilla slot click behavior.

Usage: Open a villager, click the trade you want, then press R to start/stop. The mod will collect output items repeatedly until your ingredients run out.

mastar_probg

version 1.21.11

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 247
src/client/java/com/autovillagertrading/AutoVillagerTradingClient.java
package com.autovillagertrading;

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.HudRenderCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.MerchantScreen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.MerchantScreenHandler;
import org.lwjgl.glfw.GLFW;

public class AutoVillagerTradingClient implements ClientModInitializer {

    private KeyBinding toggleKey;

    @Override
    public void onInitializeClient() {
        toggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding(
            "key.autovillagertrading.toggle",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_R,
            "category.autovillagertrading"
        ));

        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            while (toggleKey.wasPressed()) {
                AutoTradeManager.getInstance().toggle();
            }
            AutoTradeManager.getInstance().tick();
        });

        HudRenderCallback.EVENT.register((context, tickCounter) -> {
            MinecraftClient client = MinecraftClient.getInstance();
            if (!(client.currentScreen instanceof MerchantScreen screen)) return;

            AutoTradeManager manager = AutoTradeManager.getInstance();
            boolean active = manager.isActive();

            MerchantScreenHandler handler = screen.getScreenHandler();
            ItemStack output = handler.getSlot(2).getStack();

            int x = 5;
            int y = 5;
            int panelWidth = 186;
            int panelHeight = 27;

            context.fill(x - 3, y - 3, x + panelWidth, y + panelHeight, 0x90000000);

            int statusColor = active ? 0x55FF55 : 0xAAAAAA;
            String statusText = active ? "● Auto-Trading: ACTIVE" : "○ Auto-Trading: INACTIVE";
            context.drawText(client.textRenderer, statusText, x, y, statusColor, true);

            if (!output.isEmpty()) {
                int infoColor = active ? 0x55FFFF : 0xFFFFAA;
                String infoText = "Item: " + output.getName().getString() + "  [R to " + (active ? "stop" : "start") + "]";
                context.drawText(client.textRenderer, infoText, x, y + 13, infoColor, true);
            } else {
                context.drawText(client.textRenderer, "Click a trade to select it, then press R", x, y + 13, 0xFF5555, true);
            }
        });
    }
}