.spinjitsu

make me an anchor macro, when i hold a respawn anchor in my hand and and place it, make it swap to my glowstone slot, and then to my totem slot, with a right click action after each action

Kodari AI
KodariNov 8 at 1:20 pm 2
AnchorComboSwap
Creates a Fabric mod for Minecraft 1.21.11 that adds a macro system for respawn anchor placement. When holding a respawn anchor and placing it, the mod automatically swaps to the player's glowstone slot, performs a right-click to charge the anchor, then swaps to the totem slot and performs another right-click. Provides seamless inventory management and automated interaction for efficient anchor charging and totem equipping during gameplay.
  1. Verify the Fabric 1.21.11 client-tick, item, and inventory APIs.

  2. Create a client-only macro that places the anchor, right-clicks with glowstone, then right-clicks with the totem.

Created the client-only Fabric 1.21.11 anchor macro. Right-click with a respawn anchor selected to place it; the macro then selects hotbar glowstone and right-clicks, followed by your hotbar totem and another right-click. It finishes with the totem selected.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 166
src/client/java/com/kodari/anchormacro/AnchorMacroClient.java
package com.kodari.anchormacro;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;

public final class AnchorMacroClient implements ClientModInitializer {
    private static final int IDLE = 0;
    private static final int USE_GLOWSTONE = 1;
    private static final int USE_TOTEM = 2;

    private int stage = IDLE;
    private int glowstoneSlot;
    private int totemSlot;
    private boolean wasUsePressed;

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

    private void onEndClientTick(MinecraftClient client) {
        if (client.player == null || client.interactionManager == null) {
            stage = IDLE;
            wasUsePressed = false;
            return;
        }

        boolean usePressed = client.options.useKey.isPressed();
        boolean justPressed = usePressed && !wasUsePressed;
        wasUsePressed = usePressed;

        if (stage != IDLE) {
            if (!(client.crosshairTarget instanceof BlockHitResult hit)) {
                return;
            }

            PlayerInventory inventory = client.player.getInventory();
            if (stage == USE_GLOWSTONE) {
                inventory.setSelectedSlot(glowstoneSlot);
                client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, hit);
                stage = USE_TOTEM;
            } else {
                inventory.setSelectedSlot(totemSlot);
                client.interactionManager.interactBlock(client.player, Hand.MAIN_HAND, hit);
                stage = IDLE;
            }
            return;
        }

        if (!justPressed
                || !(client.crosshairTarget instanceof BlockHitResult)
                || !client.player.getInventory().getMainStacks()
                        .get(client.player.getInventory().getSelectedSlot())
                        .isOf(Items.RESPAWN_ANCHOR)) {
            return;
        }

        PlayerInventory inventory = client.player.getInventory();
        int foundGlowstoneSlot = findHotbarSlot(inventory, Items.GLOWSTONE);
        int foundTotemSlot = findHotbarSlot(inventory, Items.TOTEM_OF_UNDYING);
        if (foundGlowstoneSlot < 0 || foundTotemSlot < 0) {
            return;
        }

        glowstoneSlot = foundGlowstoneSlot;
        totemSlot = foundTotemSlot;
        stage = USE_GLOWSTONE;
    }

    private int findHotbarSlot(PlayerInventory inventory, Item item) {
        for (int slot = 0; slot < 9; slot++) {
            if (inventory.getMainStacks().get(slot).isOf(item)) {
                return slot;
            }
        }
        return -1;
    }
}