Миша Ломов

Project: A client-side Fabric mod for Minecraft 1.21.4 (Java 21).

Core feature: When the player types /autosell, the mod starts an automatic loop that sells one potion at a time from the player's inventory on the auction house, every 20 seconds, until the player runs out of potions or types /autosell again to stop.

How it must work (exact behavior):

Player types /autosell → mod sends a chat confirmation ("Auto-sell started") and begins the loop.

Every 20 seconds, the mod:

Scans the entire player inventory (36 slots: 27 main + 9 hotbar) for any potion item (e.g., minecraft:potion, minecraft:splash_potion, minecraft:lingering_potion).

If a potion is found in a stack of 64 (or any stack size > 1), it must split exactly one potion from that stack into the player's cursor or a free slot, then send /ah sell 499999 to list that single potion.

If only single potions remain, it sells one single potion directly.

If no potions are found, it sends "Auto-sell finished: no potions left" and stops.

Typing /autosell again while the loop is running stops it immediately and sends "Auto-sell stopped".

Technical requirements:

Client-side only ("environment": "client" in fabric.mod.json).

Use ClientSendMessageEvents.ALLOW_COMMAND to intercept /autosell (the string arrives without the leading /). Cancel the original send (return false) so the command never reaches the server, and handle the start/stop logic locally.

Use ClientTickEvents.END_CLIENT_TICK to drive the 20-second timer (count ticks: 20 seconds = 400 ticks at 20 TPS). Do not use Thread.sleep or a separate thread — it will freeze the game.

To find and split potions, use MinecraftClient.getInstance().player.getInventory(). The main inventory has 36 slots (indices 0–35; 0–8 are hotbar, 9–35 are main storage). Iterate all 36 slots, check stack.getItem() for potion types, and use PlayerInventory methods (e.g., removeStack, setStack, or splitStack) to separate one potion from a stack before sending the sell command.

To send the sell command, use client.player.networkHandler.sendCommand("ah sell 499999"). This bypasses the ALLOW_COMMAND event, so there is no recursion.

The mod must compile into a standard Fabric .jar for Minecraft 1.21.4.

Important safety note for the AI: The 20-second delay is intentionally slow, but automated auction-house selling may still violate some server rules. The mod should be presented as a client-side convenience tool with a clear warning that the user is responsible for checking server rules before use.

Kodari AI
KodariMon at 8:05 pm 2
AutoSellLoop
Client-side Fabric mod for Minecraft 1.21.4 that automates auction house potion selling. Typing /autosell starts a loop that scans the player's 36-slot inventory every 20 seconds, splits single potions from stacks, and sends /ah sell 499999 via networkHandler.sendCommand. The loop continues until no potions remain or /autosell is typed again to stop. Uses ClientSendMessageEvents.ALLOW_COMMAND to intercept the command locally and ClientTickEvents.END_CLIENT_TICK for tick-based timing (400 ticks per cycle). Includes chat confirmations for start, stop, and completion. User responsible for checking server rules before use.
  • Intercept /autosell locally and drive the loop with client ticks.

  • Use server-synchronized inventory clicks to put one potion in the selected hotbar slot before listing it.

  • Stop safely if a potion cannot be separated or the inventory changes unexpectedly.

Created the client-side auto-sell mod. Use /autosell to start or stop it.

It assumes /ah sell 499999 lists the held item. A free inventory slot is needed to split a stack, and server-side inventory or auction rules may prevent a listing.

Check your server’s rules before using automation.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 214
src/main/java/com/kodari/autosell/AutoSellClient.java
package com.kodari.autosell;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.message.v1.ClientSendMessageEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.network.packet.c2s.play.UpdateSelectedSlotC2SPacket;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.Text;

public final class AutoSellClient implements ClientModInitializer {
    private boolean running;
    private int ticks;
    private int settleTicks;
    private int preparedSlot = -1;

    @Override
    public void onInitializeClient() {
        ClientSendMessageEvents.ALLOW_COMMAND.register(command -> {
            if (!command.equalsIgnoreCase("autosell")) {
                return true;
            }
            MinecraftClient client = MinecraftClient.getInstance();
            if (running) {
                stop(client, "Auto-sell stopped");
            } else if (client.player != null) {
                running = true;
                ticks = 0;
                preparedSlot = -1;
                client.player.sendMessage(Text.literal("Auto-sell started"), false);
                client.player.sendMessage(Text.literal("Check your server rules before using auto-sell."), false);
            }
            return false;
        });
        ClientTickEvents.END_CLIENT_TICK.register(this::tick);
    }

    private void tick(MinecraftClient client) {
        if (!running) {
            return;
        }
        if (client.player == null || client.interactionManager == null) {
            running = false;
            preparedSlot = -1;
            return;
        }
        if (preparedSlot >= 0) {
            if (--settleTicks > 0) {
                return;
            }
            PlayerInventory inventory = client.player.getInventory();
            ItemStack selected = inventory.getStack(preparedSlot);
            if (client.currentScreen != null || client.player.currentScreenHandler != client.player.playerScreenHandler
                    || !client.player.playerScreenHandler.getCursorStack().isEmpty()
                    || inventory.selectedSlot != preparedSlot || !isPotion(selected) || selected.getCount() != 1) {
                stop(client, "Auto-sell stopped: inventory changed before listing");
                return;
            }
            client.player.networkHandler.sendPacket(new UpdateSelectedSlotC2SPacket(preparedSlot));
            if (!client.player.networkHandler.sendCommand("ah sell 499999")) {
                stop(client, "Auto-sell stopped: could not send the sell command");
                return;
            }
            preparedSlot = -1;
            ticks = 0;
            return;
        }
        if (++ticks < 400) {
            return;
        }
        if (client.currentScreen != null || client.player.currentScreenHandler != client.player.playerScreenHandler
                || !client.player.playerScreenHandler.getCursorStack().isEmpty()) {
            return;
        }

        PlayerInventory inventory = client.player.getInventory();
        int potionSlot = -1;
        for (int i = 0; i < 36; i++) {
            if (isPotion(inventory.getStack(i))) {
                potionSlot = i;
                break;
            }
        }
        if (potionSlot < 0) {
            stop(client, "Auto-sell finished: no potions left");
            return;
        }

        int selectedSlot = inventory.selectedSlot;
        if (inventory.getStack(potionSlot).getCount() > 1) {
            int emptySlot = -1;
            for (int i = 0; i < 36; i++) {
                if (inventory.getStack(i).isEmpty()) {
                    emptySlot = i;
                    break;
                }
            }
            if (emptySlot < 0) {
                stop(client, "Auto-sell stopped: a free inventory slot is needed to split a potion");
                return;
            }
            click(client, potionSlot, 0, SlotActionType.PICKUP);
            click(client, emptySlot, 1, SlotActionType.PICKUP);
            click(client, potionSlot, 0, SlotActionType.PICKUP);
            potionSlot = emptySlot;
        }
        if (potionSlot != selectedSlot) {
            click(client, potionSlot, selectedSlot, SlotActionType.SWAP);
        }
        ItemStack selected = inventory.getStack(selectedSlot);
        if (!client.player.playerScreenHandler.getCursorStack().isEmpty()
                || !isPotion(selected) || selected.getCount() != 1) {
            stop(client, "Auto-sell stopped: could not prepare a single potion");
            return;
        }
        preparedSlot = selectedSlot;
        settleTicks = 5;
    }

    private static void click(MinecraftClient client, int inventorySlot, int button, SlotActionType action) {
        int handlerSlot = inventorySlot < 9 ? 36 + inventorySlot : inventorySlot;
        client.interactionManager.clickSlot(client.player.playerScreenHandler.syncId, handlerSlot, button, action, client.player);
    }

    private static boolean isPotion(ItemStack stack) {
        return stack.getItem() == Items.POTION || stack.getItem() == Items.SPLASH_POTION
                || stack.getItem() == Items.LINGERING_POTION;
    }

    private void stop(MinecraftClient client, String message) {
        running = false;
        preparedSlot = -1;
        ticks = 0;
        if (client.player != null) {
            client.player.sendMessage(Text.literal(message), false);
        }
    }
}