Shahrzad R

ceate a minecraft mod that makes a criminal escaping cops

Kodari AI
KodariOct 23 at 2:08 pm 1
CriminalEscapeMod
A Minecraft Fabric mod that transforms the gameplay into a thrilling criminal versus police chase experience. Players can choose to play as a criminal attempting to evade capture or as a police officer tasked with apprehending the fugitive. The mod introduces wanted levels that escalate as the criminal commits crimes, triggering increased police presence and pursuit intensity. Features include police AI that intelligently tracks and corners the player, getaway vehicle mechanics for high-speed escapes, and a bounty system rewarding successful captures. The mod adds new items like handcuffs, police radios, and smoke bombs to enhance the chase dynamic.
  • Add a server-side wanted system triggered when a player attacks a villager or iron golem.

  • Spawn iron-golem cops to pursue wanted players, and clear wanted status after they escape long enough.

  • Add the Fabric entrypoint and project metadata.

Created the Fabric 26.3 mod. In survival, attacking a villager or iron golem raises your wanted level and spawns pursuing iron-golem cops. Each 60 seconds without another crime lowers your wanted level; no new items are required.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 153
src/main/java/com/example/wanted/WantedMod.java
package com.example.wanted;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

public class WantedMod implements ModInitializer {
    private static final int MAX_WANTED_LEVEL = 3;
    private static final int TICKS_TO_ESCAPE = 1200;
    private static final Map<UUID, WantedStatus> WANTED_PLAYERS = new HashMap<>();

    @Override
    public void onInitialize() {
        AttackEntityCallback.EVENT.register(WantedMod::onAttackEntity);
        ServerTickEvents.END_SERVER_TICK.register(WantedMod::tickServer);
    }

    private static InteractionResult onAttackEntity(Player player, Level level,
                                                     net.minecraft.world.InteractionHand hand,
                                                     net.minecraft.world.entity.Entity target,
                                                     net.minecraft.world.phys.EntityHitResult hitResult) {
        if (!(level instanceof ServerLevel) || (target.getType() != EntityTypes.VILLAGER
                && target.getType() != EntityTypes.IRON_GOLEM)) {
            return InteractionResult.PASS;
        }

        WantedStatus status = WANTED_PLAYERS.computeIfAbsent(player.getUUID(), ignored -> new WantedStatus());
        status.level = Math.min(MAX_WANTED_LEVEL, status.level + 1);
        status.ticksSinceCrime = 0;
        return InteractionResult.PASS;
    }

    private static void tickServer(MinecraftServer server) {
        Iterator<Map.Entry<UUID, WantedStatus>> iterator = WANTED_PLAYERS.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<UUID, WantedStatus> entry = iterator.next();
            var player = server.getPlayerList().getPlayer(entry.getKey());
            if (player == null) {
                iterator.remove();
                continue;
            }

            WantedStatus status = entry.getValue();
            if (status.copsSpawned < status.level && player.level() instanceof ServerLevel serverLevel) {
                var cop = EntityTypes.IRON_GOLEM.create(serverLevel, EntitySpawnReason.COMMAND);
                if (cop != null) {
                    cop.setPos(player.getX() + 8.0, player.getY(), player.getZ() + 8.0);
                    cop.setTarget(player);
                    if (serverLevel.addFreshEntity(cop)) {
                        status.copsSpawned++;
                    }
                }
            }

            status.ticksSinceCrime++;
            if (status.ticksSinceCrime >= TICKS_TO_ESCAPE) {
                status.level--;
                status.ticksSinceCrime = 0;
                status.copsSpawned = Math.min(status.copsSpawned, status.level);
                if (status.level == 0) {
                    iterator.remove();
                }
            }
        }
    }

    private static final class WantedStatus {
        private int level;
        private int ticksSinceCrime;
        private int copsSpawned;
    }
}