Pritvi Naidu

''Create a Fabric 1.21.11 Manhunt mod with an AI Hunter bot for singleplayer. Add a /manhunt start command that spawns a hostile AI player entity named 'Hunter' equipped with a tracking compass pointing toward the player. Give the AI hunter the ability to sprint, jump over obstacles, follow the player through Nether and End portals, and attack with melee weapons. Display 'Speedrunner Wins!' if the player kills the Ender Dragon, or 'Hunter Wins!' if the player dies."

Kodari AI
KodariNov 13 at 12:39 am 2
AIHunterManhunt
Adds a singleplayer Manhunt mode with an AI-controlled Hunter bot. The /manhunt start command spawns a hostile AI player entity equipped with a tracking compass that points toward the player. The Hunter can sprint, jump over obstacles, pursue the player through Nether and End portals, and attack with melee weapons. Victory conditions display "Speedrunner Wins!" upon Ender Dragon defeat or "Hunter Wins!" if the player dies.

Plan:

  1. Add a Fabric entrypoint registering /manhunt start and server event handlers.

  2. Spawn a persistent hostile Hunter, equip and update its player-tracking compass, and keep it with the player across dimensions.

  3. Announce the win condition when the Ender Dragon dies or the player dies.

  4. Add the required mod metadata and resources.

Created the Fabric 1.21.11 Manhunt mod. In singleplayer, run /manhunt start to summon the persistent Hunter. It’s a hostile Husk-based bot—not a fake ServerPlayer—equipped with an iron sword and a compass that tracks you. Its hostile AI pursues and jumps obstacles; when you change dimensions, it reappears near you in the Nether or End.

The mod announces “Hunter Wins!” when you die and “Speedrunner Wins!” when you kill the Ender Dragon.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 230
src/main/java/com/kodari/manhunt/ManhuntMod.java
package com.kodari.manhunt;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.LodestoneTrackerComponent;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.boss.dragon.EnderDragonEntity;
import net.minecraft.entity.mob.HuskEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.EntityType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
import net.minecraft.util.math.GlobalPos;

import java.util.Optional;
import java.util.UUID;

public class ManhuntMod implements ModInitializer {
    private static UUID activePlayerUuid;
    private static ServerPlayerEntity activePlayer;
    private static HuskEntity activeHunter;

    @Override
    public void onInitialize() {
        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
                dispatcher.register(CommandManager.literal("manhunt")
                        .then(CommandManager.literal("start").executes(context -> {
                            ServerPlayerEntity player = context.getSource().getPlayerOrThrow();
                            if (activeHunter != null && !activeHunter.isRemoved()) {
                                activeHunter.discard();
                            }

                            activePlayerUuid = player.getUuid();
                            activePlayer = player;
                            activeHunter = spawnHunter((ServerWorld) player.getEntityWorld(), player);
                            if (activeHunter == null) {
                                activePlayerUuid = null;
                                activePlayer = null;
                                player.sendMessage(Text.literal("Could not spawn the Hunter."), false);
                                return 0;
                            }

                            player.sendMessage(Text.literal("Manhunt started! The Hunter is after you."), false);
                            return 1;
                        }))));

        ServerTickEvents.END_SERVER_TICK.register(ManhuntMod::tickHunter);
        ServerLivingEntityEvents.AFTER_DEATH.register(ManhuntMod::afterDeath);
    }

    private static HuskEntity spawnHunter(ServerWorld world, ServerPlayerEntity player) {
        HuskEntity hunter = EntityType.HUSK.create(world, SpawnReason.COMMAND);
        if (hunter == null) {
            return null;
        }

        hunter.refreshPositionAndAngles(player.getX() + 5.0, player.getY(), player.getZ() + 5.0, 0.0F, 0.0F);
        hunter.setCustomName(Text.literal("Hunter"));
        hunter.setCustomNameVisible(true);
        hunter.setPersistent();
        hunter.setTarget(player);

        EntityAttributeInstance movementSpeed = hunter.getAttributeInstance(EntityAttributes.MOVEMENT_SPEED);
        if (movementSpeed != null) {
            movementSpeed.setBaseValue(0.32);
        }

        hunter.equipStack(EquipmentSlot.MAINHAND, new ItemStack(Items.IRON_SWORD));
        ItemStack compass = new ItemStack(Items.COMPASS);
        updateCompass(compass, world, player);
        hunter.equipStack(EquipmentSlot.OFFHAND, compass);

        return world.spawnEntity(hunter) ? hunter : null;
    }

    private static void tickHunter(MinecraftServer server) {
        if (activePlayerUuid == null || activeHunter == null || activeHunter.isRemoved()) {
            return;
        }

        ServerPlayerEntity player = server.getPlayerManager().getPlayer(activePlayerUuid);
        if (player == null || !player.isAlive()) {
            return;
        }
        activePlayer = player;

        ServerWorld playerWorld = (ServerWorld) player.getEntityWorld();
        if (activeHunter.getEntityWorld() != playerWorld) {
            activeHunter.discard();
            activeHunter = spawnHunter(playerWorld, player);
            if (activeHunter == null) {
                return;
            }
        }

        activeHunter.setTarget(player);
        activeHunter.setSprinting(true);
        updateCompass(activeHunter.getEquippedStack(EquipmentSlot.OFFHAND), playerWorld, player);
    }

    private static void updateCompass(ItemStack compass, ServerWorld world, ServerPlayerEntity player) {
        compass.set(DataComponentTypes.LODESTONE_TRACKER, new LodestoneTrackerComponent(
                Optional.of(GlobalPos.create(world.getRegistryKey(), player.getBlockPos())), true));
    }

    private static void afterDeath(LivingEntity entity, net.minecraft.entity.damage.DamageSource damageSource) {
        if (entity instanceof ServerPlayerEntity player
                && activePlayerUuid != null
                && activePlayerUuid.equals(player.getUuid())) {
            player.sendMessage(Text.literal("Hunter Wins!"), false);
            endHunt();
        } else if (entity instanceof EnderDragonEntity
                && damageSource.getAttacker() instanceof ServerPlayerEntity killer
                && activePlayerUuid != null
                && activePlayerUuid.equals(killer.getUuid())
                && activePlayer != null
                && activePlayer.isAlive()) {
            activePlayer.sendMessage(Text.literal("Speedrunner Wins!"), false);
            endHunt();
        }
    }

    private static void endHunt() {
        if (activeHunter != null && !activeHunter.isRemoved()) {
            activeHunter.discard();
        }
        activeHunter = null;
        activePlayer = null;
        activePlayerUuid = null;
    }
}