So H

Fabric 1.21.11 where if you get a hit with the axe on a player and the hit registers, it switches to a customisable slot with a customisable delay

Kodari AI
KodariOct 21 at 1:05 am 2
AxeSwapCombat
A Fabric 1.21.11 mod that automatically switches the player's held item to a customizable hotbar slot after landing a successful axe hit on another player. The mod detects melee axe attacks, triggers a configurable delay, then swaps to the designated slot. Includes configurable slot selection, delay duration, and toggle option. Works in both single-player and multiplayer, respects cooldowns, and integrates with Fabric API events for reliable hit registration.
  1. Detect confirmed axe damage to another player on the server.

  2. Send a client trigger, then switch to a configured hotbar slot after a configured delay.

  3. Create the Fabric 1.21.11 project files and an editable config.

Created the Fabric 1.21.11 mod. It switches to the configured hotbar slot after a confirmed direct axe hit that damages another player, including a lethal hit.

The client config is created at config/axe_swap.properties:

  • slot=1 — hotbar slot, numbered 1–9

  • delay_ticks=2 — delay in ticks; 20 ticks is about one second

Install the mod on both the server and the attacking player’s client for multiplayer use.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 233
src/client/java/com/kodari/axeswap/client/AxeSwapClient.java
package com.kodari.axeswap.client;

import com.kodari.axeswap.network.AxeHitPayload;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.MinecraftClient;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

public class AxeSwapClient implements ClientModInitializer {
    private static final Path CONFIG_PATH = Path.of("config", "axe_swap.properties");
    private static final AtomicInteger PENDING_HITS = new AtomicInteger();
    private static int targetSlot = 1;
    private static int delayTicks = 2;
    private static int remainingTicks = -1;

    @Override
    public void onInitializeClient() {
        loadConfig();

        ClientPlayNetworking.registerGlobalReceiver(AxeHitPayload.ID, (payload, context) -> PENDING_HITS.incrementAndGet());

        ClientTickEvents.END_CLIENT_TICK.register(AxeSwapClient::onClientTick);
    }

    private static void onClientTick(MinecraftClient client) {
        if (PENDING_HITS.getAndSet(0) > 0) {
            remainingTicks = delayTicks;
        } else if (remainingTicks > 0) {
            remainingTicks--;
        }

        if (remainingTicks < 0) {
            return;
        }

        if (remainingTicks == 0) {
            if (client.player != null) {
                client.player.getInventory().setSelectedSlot(targetSlot - 1);
            }
            remainingTicks = -1;
        }
    }

    private static void loadConfig() {
        Properties properties = new Properties();
        properties.setProperty("slot", "1");
        properties.setProperty("delay_ticks", "2");

        try {
            Files.createDirectories(CONFIG_PATH.getParent());
            if (Files.exists(CONFIG_PATH)) {
                try (Reader reader = Files.newBufferedReader(CONFIG_PATH)) {
                    properties.load(reader);
                }
            } else {
                try (Writer writer = Files.newBufferedWriter(CONFIG_PATH)) {
                    properties.store(writer, "Axe Swap settings");
                }
            }
        } catch (IOException ignored) {
            return;
        }

        targetSlot = parseSetting(properties.getProperty("slot"), 1, 1, 9);
        delayTicks = parseSetting(properties.getProperty("delay_ticks"), 2, 0, 72000);
    }

    private static int parseSetting(String value, int fallback, int minimum, int maximum) {
        try {
            return Math.max(minimum, Math.min(maximum, Integer.parseInt(value)));
        } catch (NumberFormatException exception) {
            return fallback;
        }
    }
}