Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
22 4,990
src/main/java/com/kodari/aicivilization/CivMod.java
package com.kodari.aicivilization;

import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.minecraft.command.DefaultPermissions;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.server.world.ServerWorld;

public class CivMod implements ModInitializer {
    public static final String MOD_ID = "aicivilization";

    @Override
    public void onInitialize() {
        PayloadTypeRegistry.playS2C().register(ManagementScreenPayload.ID, ManagementScreenPayload.CODEC);
        CivilianEntities.initialize();
        SettlementManager.initialize();
        ControlRoomManager.initialize();
        CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> dispatcher.register(
                CommandManager.literal("civ")
                        .then(CommandManager.literal("start")
                                .then(CommandManager.argument("population", IntegerArgumentType.integer(100, 10_000))
                                        .then(CommandManager.argument("kingdoms", IntegerArgumentType.integer(2, 12))
                                                .executes(context -> startCivilization(
                                                        context.getSource(),
                                                        IntegerArgumentType.getInteger(context, "population"),
                                                        IntegerArgumentType.getInteger(context, "kingdoms"), false))
                                                .then(CommandManager.literal("own").executes(context -> startCivilization(
                                                        context.getSource(),
                                                        IntegerArgumentType.getInteger(context, "population"),
                                                        IntegerArgumentType.getInteger(context, "kingdoms"), true))))))
                        .then(CommandManager.literal("activation-limit")
                                .then(CommandManager.argument("limit", IntegerArgumentType.integer(1, 512))
                                        .executes(context -> {
                                            ServerCommandSource source = context.getSource();
                                            CivilizationState state = CivilizationState.get(source.getServer().getOverworld());
                                            if (!state.isStarted()) {
                                                source.sendError(Text.literal("No civilization has been started in this world."));
                                                return 0;
                                            }
                                            int limit = IntegerArgumentType.getInteger(context, "limit");
                                            state.setNpcActivationLimit(limit);
                                            source.sendFeedback(() -> Text.literal("NPC activation limit set to " + limit + "; effective limit for this population: " + state.getNpcActivationLimit() + "."), false);
                                            return 1;
                                        })))
                        .then(CommandManager.literal("status").executes(context -> {
                            ServerCommandSource source = context.getSource();
                            ServerWorld world = source.getServer().getOverworld();
                            CivilizationState state = CivilizationState.get(world);
                            if (!state.isStarted()) {
                                source.sendError(Text.literal("No civilization has been started in this world."));
                                return 0;
                            }

                            var loadedCitizenIds = CivilianEntities.getLoadedCitizenIds(world);
                            int activeEntities = state.getActiveNpcCount(loadedCitizenIds);
                            int backgroundRecords = state.getLivingPopulation() - activeEntities;
                            int backedCitizens = state.getNpcBackedPopulation();
                            int neutralPopulation = state.getNeutralPopulation();
                            int activeNeutral = state.getActiveNeutralNpcCount(loadedCitizenIds);
                            source.sendFeedback(() -> Text.literal("Living population: " + state.getLivingPopulation() + "/" + state.getMaximumPopulation() + " | available capacity: " + (state.getMaximumPopulation() - state.getLivingPopulation())), false);
                            source.sendFeedback(() -> Text.literal("Kingdoms: " + state.getKingdoms().size() + " | saved settlements: " + state.getSettlements().size() + " | active NPC entities: " + activeEntities + " | background records: " + backgroundRecords), false);
                            source.sendFeedback(() -> Text.literal("Saved NPC-backed citizens: " + backedCitizens + " | awaiting activation: " + state.getAwaitingActivationCount() + " | effective activation limit: " + state.getNpcActivationLimit()), false);
                            source.sendFeedback(() -> Text.literal("Neutral population: " + neutralPopulation + " | active: " + activeNeutral + " | background records: " + (neutralPopulation - activeNeutral) + " | saved NPC-backed: " + state.getActiveNeutralNpcCount()), false);
                            for (int index = 0; index < state.getKingdoms().size(); index++) {
                                int kingdomIndex = index;
                                CivilizationState.Kingdom kingdom = state.getKingdoms().get(kingdomIndex);
                                int population = state.getKingdomPopulation(kingdomIndex);
                                int active = state.getActiveNpcCountForKingdom(kingdomIndex, loadedCitizenIds);
                                int savedBacked = state.getActiveNpcCountForKingdom(kingdomIndex);
                                source.sendFeedback(() -> Text.literal(kingdom.name() + " (" + kingdom.color() + "): population " + population + " | active: " + active + " | background records: " + (population - active) + " | saved NPC-backed: " + savedBacked), false);
                            }
                            for (CivilizationState.Settlement settlement : state.getSettlements()) {
                                source.sendFeedback(() -> Text.literal(settlement.name() + " [" + settlement.id() + "] | population: " + state.getSettlementPopulation(settlement) + " | leader ID: " + settlement.leaderCitizenId() + " | development: " + settlement.developmentStage() + " (" + settlement.constructionState() + ")"), false);
                            }
                            return 1;
                        }))
                        .then(CommandManager.literal("screen").executes(context -> sendScreenSnapshot(context.getSource())))
                        .then(CommandManager.literal("room")
                                .requires(source -> source.getPermissions().hasPermission(DefaultPermissions.GAMEMASTERS))
                                .executes(context -> {
                                    ServerCommandSource source = context.getSource();
                                    ServerPlayerEntity player = source.getPlayer();
                                    if (player == null) {
                                        source.sendError(Text.literal("Only an operator player can build the control room."));
                                        return 0;
                                    }
                                    if (source.getWorld() != source.getServer().getOverworld()) {
                                        source.sendError(Text.literal("Build the control room in the Overworld."));
                                        return 0;
                                    }
                                    if (!ControlRoomManager.build(source.getWorld(), player.getBlockPos(), player.getUuid().toString())) {
                                        source.sendError(Text.literal("The control room already exists, or its interior is obstructed."));
                                        return 0;
                                    }
                                    source.sendFeedback(() -> Text.literal("Physical control room configured. Setup and START are owner-only; contract and starting-equipment controls are per-player."), false);
                                    return 1;
                                }))
                        .then(CommandManager.literal("kingdom")
                                .then(CommandManager.literal("claim")
                                        .then(CommandManager.argument("index", IntegerArgumentType.integer(1, 12))
                                                .executes(context -> {
                                                    ServerCommandSource source = context.getSource();
                                                    ServerPlayerEntity player = source.getPlayer();
                                                    if (player == null) {
                                                        source.sendError(Text.literal("Only a player can claim a kingdom."));
                                                        return 0;
                                                    }
                                                    CivilizationState state = CivilizationState.get(source.getServer().getOverworld());
                                                    int kingdomIndex = IntegerArgumentType.getInteger(context, "index") - 1;
                                                    if (!state.claimKingdom(kingdomIndex, player.getUuid().toString())) {
                                                        String message = "That kingdom is already claimed, or you already rule a kingdom.";
                                                        source.sendError(Text.literal(message));
                                                        sendScreenMessage(source, message);
                                                        return 0;
                                                    }
                                                    String message = "You are now the ruler of " + state.getKingdoms().get(kingdomIndex).name() + ".";
                                                    source.sendFeedback(() -> Text.literal(message), false);
                                                    sendScreenMessage(source, message);
                                                    return 1;
                                                }))))
                        .then(CommandManager.literal("citizens")
                                .executes(context -> listCitizens(context.getSource(), 1, false))
                                .then(CommandManager.argument("page", IntegerArgumentType.integer(1, 5000))
                                        .executes(context -> listCitizens(context.getSource(), IntegerArgumentType.getInteger(context, "page"), false))))
                        .then(CommandManager.literal("neutrals")
                                .executes(context -> listCitizens(context.getSource(), 1, true))
                                .then(CommandManager.argument("page", IntegerArgumentType.integer(1, 5000))
                                        .executes(context -> listCitizens(context.getSource(), IntegerArgumentType.getInteger(context, "page"), true))))
                        .then(CommandManager.literal("job")
                                .then(CommandManager.argument("citizen", StringArgumentType.word())
                                        .then(CommandManager.argument("job", StringArgumentType.word())
                                                .executes(context -> {
                                                    ServerCommandSource source = context.getSource();
                                                    CivilizationState state = CivilizationState.get(source.getServer().getOverworld());
                                                    int kingdom = requireKingdomLeader(source, state);
                                                    if (kingdom < 0) {
                                                        return 0;
                                                    }
                                                    String job = StringArgumentType.getString(context, "job").toLowerCase(java.util.Locale.ROOT);
                                                    if (!state.setCitizenJob(StringArgumentType.getString(context, "citizen"), kingdom, job)) {
                                                        String message = "Unknown job, citizen not found in your kingdom, or the citizen cannot be reassigned.";
                                                        source.sendError(Text.literal(message));
                                                        sendScreenMessage(source, message);
                                                        return 0;
                                                    }
                                                    String message = "Citizen job set to " + job + ".";
                                                    source.sendFeedback(() -> Text.literal(message), false);
                                                    sendScreenMessage(source, message);
                                                    return 1;
                                                }))))
                        .then(CommandManager.literal("task")
                                .then(CommandManager.argument("citizen", StringArgumentType.word())
                                        .then(CommandManager.argument("order", StringArgumentType.word())
                                                .executes(context -> {
                                                    ServerCommandSource source = context.getSource();
                                                    CivilizationState state = CivilizationState.get(source.getServer().getOverworld());
                                                    int kingdom = requireKingdomLeader(source, state);
                                                    if (kingdom < 0) {
                                                        return 0;
                                                    }
                                                    String order = StringArgumentType.getString(context, "order").toLowerCase(java.util.Locale.ROOT);
                                                    if (!state.setCitizenOrder(StringArgumentType.getString(context, "citizen"), kingdom, order)) {
                                                        String message = "Unknown or incompatible task, or citizen not found in your kingdom.";
                                                        source.sendError(Text.literal(message));
                                                        sendScreenMessage(source, message);
                                                        return 0;
                                                    }
                                                    String message = "Citizen order set to " + order + ".";
                                                    source.sendFeedback(() -> Text.literal(message), false);
                                                    sendScreenMessage(source, message);
                                                    return 1;
                                                }))))
                        .then(CommandManager.literal("recruit")
                                .then(CommandManager.argument("citizen", StringArgumentType.word())
                                        .executes(context -> {
                                            ServerCommandSource source = context.getSource();
                                            CivilizationState state = CivilizationState.get(source.getServer().getOverworld());
                                            int kingdom = requireKingdomLeader(source, state);
                                            if (kingdom < 0) {
                                                return 0;
                                            }
                                            if (!state.recruitNeutralCitizen(StringArgumentType.getString(context, "citizen"), kingdom)) {
                                                String message = "That citizen is not an eligible neutral citizen.";
                                                source.sendError(Text.literal(message));
                                                sendScreenMessage(source, message);
                                                return 0;
                                            }
                                            String message = "Neutral citizen recruited; total population is unchanged.";
                                            source.sendFeedback(() -> Text.literal(message), false);
                                            sendScreenMessage(source, message);
                                            return 1;
                                        })))
                        .then(CommandManager.literal("war")
                                .then(CommandManager.literal("declare")