Atte Tuominen
📎 1 file 1,789 chars
📄
pasted-code-1790946521365.txt
txt 35 lines
▶
Act as an expert Minecraft Mod Developer. I want you to write the complete source code for a client-side Minecraft mod that automates a specific command and keypress sequence. 

Please provide the complete file structure, build.gradle config, and the main Java class file.

Here are the requirements for the mod:
1. Trigger: The macro sequence should toggle ON and OFF when pressing the "Alt Gr" (Right Alt) key.
2. The Macro Loop: When turned ON, it must execute the following sequence continuously in a loop until toggled OFF:
   - Type and send the command: /afk
   - Wait 5 seconds.
   - Type and send the command: /afksetting
   - Wait 1.5 seconds (to allow the GUI to open).
   - Simulate a mouse click exactly in the center of the screen (to click a button in the GUI).
   - Wait 1 second.
   - Press the "E" key to close the GUI.
   - Wait 1 second.
   - Type and send the command: /spawn 2
   - Wait 5 seconds.
   - Type and send the command: /afksetting
   - Wait 1.5 seconds.
   - Type and send the command: /afk
   - Wait 10 seconds.
   - Type and send the command: /afksetting
   - Wait 1.5 seconds.
   - Simulate a mouse click exactly in the center of the screen.
   - Wait 1 second.
   - Press the "E" key to close the GUI.
   - Wait 1 second.
   - Type and send the command: /spawn 2
   - Wait 5 seconds.
   - Repeat the loop from the beginning.

Safety requirement: The macro execution must run on a separate thread or use client ticks safely so it doesn't freeze the game during the "wait/sleep" periods. It must also check if the toggle is turned OFF during the sleeps to stop immediately.

Before you write the code, please ask me which Minecraft version (e.g., 1.20.4, 1.21) and which Mod Loader (Fabric or Forge) I am using, so you can provide the correct templates.
Kodari AI
KodariOct 2 at 1:13 pm 1
AfkMacroAutomation
A client-side Minecraft Fabric mod for version 1.21.4 that automates a complex AFK command and keypress macro sequence. Toggle the automation on and off by pressing the Right Alt key. The mod executes a continuous loop of sending commands, waiting, simulating mouse clicks at screen center, and pressing the E key to close GUI interfaces. The macro runs on a separate thread with safe client tick integration, ensuring the game remains responsive during wait periods. It checks the toggle state during sleeps to stop immediately when deactivated, providing seamless AFK automation without freezing the game.
  1. Build a client-only Fabric 1.21.4 project with Loom configuration and mod metadata.

  2. Implement the Right Alt toggle and the full sequence as a non-blocking client-tick state machine.

  3. Verify each Minecraft/Fabric API signature against the mounted 1.21.4 source before creating files.

Created a client-only Fabric 1.21.4 project. Right Alt toggles the macro; its sequence runs on client ticks, so waits don’t block the game and toggling off stops it on the next tick.

Includes the Loom build configuration, Fabric metadata, English keybinding name, and AfkMacroClient implementation. Note: the waits use game ticks (20 ticks per second), so they can stretch if the game’s tick rate slows.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 183
src/main/java/com/kodari/afkmacro/AfkMacroClient.java
package com.kodari.afkmacro;

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.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;

public class AfkMacroClient implements ClientModInitializer {
    private static final KeyBinding TOGGLE_KEY = KeyBindingHelper.registerKeyBinding(
            new KeyBinding(
                    "key.afkmacro.toggle",
                    InputUtil.Type.KEYSYM,
                    InputUtil.GLFW_KEY_RIGHT_ALT,
                    KeyBinding.MISC_CATEGORY
            )
    );

    private static final Step[] SEQUENCE = {
            new Step(Action.AFK, 100),
            new Step(Action.AFK_SETTING, 30),
            new Step(Action.CLICK_CENTER, 20),
            new Step(Action.PRESS_E, 20),
            new Step(Action.SPAWN, 100),
            new Step(Action.AFK_SETTING, 30),
            new Step(Action.AFK, 200),
            new Step(Action.AFK_SETTING, 30),
            new Step(Action.CLICK_CENTER, 20),
            new Step(Action.PRESS_E, 20),
            new Step(Action.SPAWN, 100)
    };

    private static boolean enabled;
    private static int stepIndex;
    private static int waitTicks;

    @Override
    public void onInitializeClient() {
        ClientTickEvents.END_CLIENT_TICK.register(AfkMacroClient::onEndClientTick);
    }

    private static void onEndClientTick(MinecraftClient client) {
        while (TOGGLE_KEY.wasPressed()) {
            enabled = !enabled;
            stepIndex = 0;
            waitTicks = 0;
        }

        if (!enabled || client.player == null) {
            return;
        }

        if (waitTicks > 0) {
            waitTicks--;
            if (waitTicks > 0) {
                return;
            }
        }

        Step step = SEQUENCE[stepIndex];
        stepIndex = (stepIndex + 1) % SEQUENCE.length;
        perform(client, step.action());
        waitTicks = step.delayTicks();
    }

    private static void perform(MinecraftClient client, Action action) {
        switch (action) {
            case AFK -> client.player.networkHandler.sendChatCommand("afk");
            case AFK_SETTING -> client.player.networkHandler.sendChatCommand("afksetting");
            case SPAWN -> client.player.networkHandler.sendChatCommand("spawn 2");
            case CLICK_CENTER -> {
                Screen screen = client.currentScreen;
                if (screen != null) {
                    double centerX = client.getWindow().getScaledWidth() / 2.0;
                    double centerY = client.getWindow().getScaledHeight() / 2.0;
                    screen.mouseClicked(centerX, centerY, 0);
                    screen.mouseReleased(centerX, centerY, 0);
                }
            }
            case PRESS_E -> {
                if (client.currentScreen != null) {
                    client.currentScreen.keyPressed(InputUtil.GLFW_KEY_E, 0, 0);
                }
            }
        }
    }

    private enum Action {
        AFK,
        AFK_SETTING,
        SPAWN,
        CLICK_CENTER,
        PRESS_E
    }

    private record Step(Action action, int delayTicks) {
    }
}