Mohammed Tamer

i want a auto mace mod that attacks players withtin reach and lock on so it doesnt hit any other player and an auto totem so when one pops it automatically puts another one like a player doing it

Kodari AI
KodariToday at 8:55 pm 2
AutoMaceCombat
An automatic combat enhancement mod for Minecraft Fabric 1.21.11. Features an auto-mace system that continuously attacks the nearest player within reach while maintaining a lock-on target, preventing accidental hits on other players. Includes an auto-totem mechanism that instantly replaces a consumed totem of undying from inventory, mimicking manual player actions. Designed for PvP scenarios, the mod streamlines combat by automating repetitive actions while preserving strategic control over target selection and totem management.

Created the client-side mod:

  • M toggles Auto Mace. It locks onto the nearest visible player within 3 blocks and attacks with a mace when the attack cooldown is ready.

  • N toggles Auto Totem. It moves a totem from your inventory into an empty offhand after one pops.

Both start disabled. Server rules and anti-cheat may restrict automated actions.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 177
src/client/java/com/kodari/macelock/MaceLockClient.java
package com.kodari.macelock;

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.network.AbstractClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.item.Items;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;

public class MaceLockClient implements ClientModInitializer {
    private static final KeyBinding.Category CATEGORY = KeyBinding.Category.create(Identifier.of("macelock", "controls"));
    private KeyBinding maceKey;
    private KeyBinding totemKey;
    private boolean autoMace;
    private boolean autoTotem;
    private AbstractClientPlayerEntity target;

    @Override
    public void onInitializeClient() {
        maceKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.macelock.auto_mace", GLFW.GLFW_KEY_M, CATEGORY));
        totemKey = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.macelock.auto_totem", GLFW.GLFW_KEY_N, CATEGORY));
        ClientTickEvents.END_CLIENT_TICK.register(this::tick);
    }

    private void tick(MinecraftClient client) {
        while (maceKey.wasPressed()) {
            autoMace = !autoMace;
            target = null;
            if (client.player != null) client.player.sendMessage(Text.of("Auto Mace: " + (autoMace ? "ON" : "OFF")), true);
        }
        while (totemKey.wasPressed()) {
            autoTotem = !autoTotem;
            if (client.player != null) client.player.sendMessage(Text.of("Auto Totem: " + (autoTotem ? "ON" : "OFF")), true);
        }
        if (client.player == null || client.world == null || client.interactionManager == null || !client.player.isAlive()) {
            target = null;
            return;
        }
        if (client.currentScreen != null) return;
        if (autoTotem) refillTotem(client);
        if (!autoMace || !client.player.getMainHandStack().isOf(Items.MACE) || client.player.isSpectator()) {
            target = null;
            return;
        }
        if (target == null || !client.world.getPlayers().contains(target) || !eligible(client, target)) {
            target = null;
            double nearest = Double.MAX_VALUE;
            for (AbstractClientPlayerEntity candidate : client.world.getPlayers()) {
                if (eligible(client, candidate)) {
                    double distance = client.player.squaredDistanceTo(candidate);
                    if (distance < nearest) {
                        nearest = distance;
                        target = candidate;
                    }
                }
            }
        }
        if (target != null && client.player.getAttackCooldownProgress(0.5F) >= 1.0F) {
            client.interactionManager.attackEntity(client.player, target);
            client.player.swingHand(Hand.MAIN_HAND);
        }
    }

    private boolean eligible(MinecraftClient client, AbstractClientPlayerEntity candidate) {
        return candidate != client.player && candidate.isAlive() && !candidate.isSpectator()
                && candidate.isAttackable() && client.player.squaredDistanceTo(candidate) <= 9.0
                && client.player.canSee(candidate);
    }

    private void refillTotem(MinecraftClient client) {
        PlayerScreenHandler handler = client.player.playerScreenHandler;
        if (client.player.currentScreenHandler != handler || !handler.getCursorStack().isEmpty()
                || !handler.getSlot(PlayerScreenHandler.OFFHAND_ID).getStack().isEmpty()) return;
        for (int slot = PlayerScreenHandler.INVENTORY_START; slot < PlayerScreenHandler.HOTBAR_END; slot++) {
            if (handler.getSlot(slot).getStack().isOf(Items.TOTEM_OF_UNDYING)) {
                client.interactionManager.clickSlot(handler.syncId, slot, 0, SlotActionType.PICKUP, client.player);
                client.interactionManager.clickSlot(handler.syncId, PlayerScreenHandler.OFFHAND_ID, 0, SlotActionType.PICKUP, client.player);
                return;
            }
        }
    }
}