Create a client-side Fabric 26.3 mod that automatically attacks a mob the player is aiming at with their crosshair, only while holding any sword. Bind H as a configurable key to toggle the feature on/off, and provide a brief in-game status message when toggled. Use normal client attack mechanics and respect attack cooldown; do not target players or non-mob entities. Implement feature code and required mod metadata.

Kodari AI
KodariNov 14 at 1:37 am 2
AutoAttackMod
Client-side Fabric mod that automatically attacks mobs the player is aiming at with their crosshair while holding any sword. Features a configurable H key toggle to enable/disable the functionality, with a brief in-game status message displayed upon toggling. Uses standard client attack mechanics, respects the attack cooldown for balanced gameplay, and excludes players and non-mob entities from targeting. Includes complete mod metadata and initialization files for Fabric 26.3 compatibility.

Created the client-side mod. Press H to toggle auto-attack; the key can be changed in Minecraft’s controls. It attacks living mobs under the crosshair with a sword when the attack cooldown is ready.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 145
src/client/java/com/kodari/crosshairautoattack/CrosshairAutoAttackClient.java
package com.kodari.crosshairautoattack;

import com.kodari.crosshairautoattack.mixin.MinecraftInvoker;
import com.mojang.blaze3d.platform.InputConstants;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.minecraft.client.KeyMapping;
import net.minecraft.network.chat.Component;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.phys.EntityHitResult;

public class CrosshairAutoAttackClient implements ClientModInitializer {
    private boolean enabled;

    @Override
    public void onInitializeClient() {
        KeyMapping toggle = KeyMappingHelper.registerKeyMapping(new KeyMapping(
                "key.crosshair_auto_attack.toggle", InputConstants.Type.KEYBOARD,
                InputConstants.KEY_H, KeyMapping.Category.MISC));

        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            while (toggle.consumeClick()) {
                enabled = !enabled;
                if (client.player != null) {
                    client.player.sendOverlayMessage(Component.translatable(enabled
                            ? "message.crosshair_auto_attack.enabled"
                            : "message.crosshair_auto_attack.disabled"));
                }
            }

            if (!enabled || client.player == null || client.gameMode == null || client.gui.screen() != null) {
                return;
            }
            if (!client.player.getMainHandItem().is(holder -> holder.is(ItemTags.SWORDS))
                    || client.player.getAttackStrengthScale(0.0F) < 1.0F) {
                return;
            }
            if (client.hitResult instanceof EntityHitResult hit
                    && hit.getEntity() instanceof Mob mob && mob.isAlive()) {
                ((MinecraftInvoker) client).crosshairAutoAttack$startAttack();
            }
        });
    }
}