kaka753_

Hello, please make me a Minecraft mod that is supposed to be for Minecraft Java 1.21.11. With J, you should be able to open a menu where you can choose whether you want to do auto totem or auto totem offhand. And the mod should only work in multiplayer.

Kodari AI
KodariNov 3 at 7:21 pm 2
TotemToggleMod
A Fabric mod for Minecraft 1.21.11 that adds a keybind (J) to open a selection menu in multiplayer, allowing players to toggle automatic totem usage in their main hand or offhand. The mod enhances survival gameplay by automating totem activation, with the menu providing clear options for auto-totem on/off and hand preference. It only functions in multiplayer servers, ensuring fair play in single-player worlds.
  1. Add a multiplayer-only J menu with the two totem modes.

  2. Register J and implement safe inventory-to-offhand swapping for the selected mode.

  3. Add the Fabric project metadata and owo-lib dependency.

Added a client-side Fabric 1.21.11 mod with a menu on J. It only opens or equips totems in multiplayer.

  • Auto Totem: equips a totem in the offhand when health drops to 5 hearts or less.

  • Auto Totem Offhand: keeps a totem in the offhand whenever one is in the inventory.

  • Off: disables the automation.

Any item displaced from the offhand is swapped back into the inventory. The selected mode resets to Off when the game restarts.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
8 253
src/client/java/com/kodari/autototem/AutoTotemClient.java
package com.kodari.autototem;

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.option.KeyBinding;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Items;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;

public class AutoTotemClient implements ClientModInitializer {
    private static KeyBinding menuKey;
    private static Mode mode = Mode.OFF;

    @Override
    public void onInitializeClient() {
        KeyBinding.Category category = KeyBinding.Category.create(Identifier.of("autototem", "general"));
        menuKey = KeyBindingHelper.registerKeyBinding(
                new KeyBinding("key.autototem.menu", GLFW.GLFW_KEY_J, category)
        );
        ClientTickEvents.END_CLIENT_TICK.register(AutoTotemClient::onEndTick);
    }

    public static Mode mode() {
        return mode;
    }

    public static void setMode(Mode selectedMode) {
        mode = selectedMode;
    }

    private static void onEndTick(MinecraftClient client) {
        while (menuKey.wasPressed()) {
            if (isMultiplayer(client)) {
                client.setScreen(new AutoTotemScreen());
            }
        }

        if (!isMultiplayer(client) || mode == Mode.OFF || client.interactionManager == null) {
            return;
        }

        PlayerEntity player = client.player;
        if (player.currentScreenHandler != player.playerScreenHandler) {
            return;
        }

        if (mode == Mode.AUTO_TOTEM && player.getHealth() > 10.0F) {
            return;
        }

        PlayerInventory inventory = player.getInventory();
        if (inventory.getStack(PlayerInventory.OFF_HAND_SLOT).isOf(Items.TOTEM_OF_UNDYING)) {
            return;
        }

        for (int inventorySlot = 0; inventorySlot < 36; inventorySlot++) {
            if (!inventory.getStack(inventorySlot).isOf(Items.TOTEM_OF_UNDYING)) {
                continue;
            }

            int handlerSlot = inventorySlot < 9 ? inventorySlot + 36 : inventorySlot;
            client.interactionManager.clickSlot(
                    player.playerScreenHandler.syncId,
                    handlerSlot,
                    PlayerInventory.OFF_HAND_SLOT,
                    SlotActionType.SWAP,
                    player
            );
            return;
        }
    }

    private static boolean isMultiplayer(MinecraftClient client) {
        return client.world != null && client.player != null && !client.isInSingleplayer();
    }

    public enum Mode {
        OFF,
        AUTO_TOTEM,
        AUTO_TOTEM_OFFHAND
    }
}