Files
src
build.gradle
manifest.kod
README.md
Code
15 1,583
src/main/java/com/rebornnpc/RebornNPC.java
package com.rebornnpc;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public final class RebornNPC extends JavaPlugin {
    private NpcRepository repository;
    private NPCManager npcManager;
    private LuckPermsPrefixResolver prefixResolver;
    private PacketNpcService packetService;
    private ChatSimulator chatSimulator;
    private SimulationEngine simulationEngine;

    @Override
    public void onEnable() {
        saveDefaultConfig();
        validateConfiguration();
        repository = new NpcRepository(this);
        npcManager = new NPCManager(this, repository);
        npcManager.load();
        prefixResolver = new LuckPermsPrefixResolver(this);
        packetService = new PacketNpcService(this, npcManager, prefixResolver);
        chatSimulator = new ChatSimulator(this, prefixResolver);
        simulationEngine = new SimulationEngine(this, npcManager, packetService, chatSimulator);
        Bukkit.getPluginManager().registerEvents(packetService, this);
        FakePlayerCommand commandHandler = new FakePlayerCommand(this, npcManager, packetService, chatSimulator, simulationEngine);
        if (getCommand("fakeplayer") == null) {
            throw new IllegalStateException("fakeplayer command is missing from plugin.yml");
        }
        getCommand("fakeplayer").setExecutor(commandHandler);
        getCommand("fakeplayer").setTabCompleter(commandHandler);
        simulationEngine.start();
        Bukkit.getScheduler().runTask(this, packetService::refreshAll);
        getLogger().info("Enabled packet-simulated NPCs. Fake NPCs are not online Bukkit players.");
    }

    @Override
    public void onDisable() {
        if (simulationEngine != null) {
            simulationEngine.stop();
        }
        if (packetService != null) {
            packetService.shutdown();
        }
        if (npcManager != null) {
            npcManager.saveAll();
            if (chatSimulator != null) {
                chatSimulator.clearNpcState();
            }
        }
    }

    public int getNpcLimit() {
        return Math.max(1, Math.min(500, getConfig().getInt("maximum-npcs", 100)));
    }

    public NPCManager getNpcManager() {
        return npcManager;
    }

    public void reloadPlugin() {
        if (packetService != null) {
            packetService.hideAll();
        }
        if (npcManager != null) {
            npcManager.saveAll();
            if (chatSimulator != null) {
                chatSimulator.clearNpcState();
            }
        }
        reloadConfig();
        validateConfiguration();
        npcManager.load();
        simulationEngine.start();
        packetService.refreshAll();
    }

    private void validateConfiguration() {
        int limit = clamp(getConfig().getInt("maximum-npcs", 100), 1, 500, 100);
        getConfig().set("maximum-npcs", limit);
        getConfig().set("visibility.distance-blocks", clamp(getConfig().getInt("visibility.distance-blocks", 128), 16, 512, 128));
        getConfig().set("visibility.refresh-interval-ticks", clamp(getConfig().getInt("visibility.refresh-interval-ticks", 40), 20, 1200, 40));
        getConfig().set("movement.destination-refresh-ticks", clamp(getConfig().getInt("movement.destination-refresh-ticks", 120), 20, 1200, 120));
        getConfig().set("movement.destination-attempts", clamp(getConfig().getInt("movement.destination-attempts", 8), 1, 20, 8));
        getConfig().set("movement.blocks-per-update", clamp(getConfig().getDouble("movement.blocks-per-update", 0.35), 0.05, 1.0, 0.35));
        getConfig().set("movement.default-radius", clamp(getConfig().getDouble("movement.default-radius", 8.0), 1.0, 32.0, 8.0));
        getConfig().set("simulation.interval-ticks", clamp(getConfig().getInt("simulation.interval-ticks", 20), 10, 100, 20));
        getConfig().set("simulation.look-chance-per-update", clamp(getConfig().getDouble("simulation.look-chance-per-update", 0.04), 0.0, 0.25, 0.04));
        getConfig().set("simulation.jump-chance-per-update", clamp(getConfig().getDouble("simulation.jump-chance-per-update", 0.003), 0.0, 0.05, 0.003));
        getConfig().set("chat.global-cooldown-seconds", clamp(getConfig().getInt("chat.global-cooldown-seconds", 45), 30, 3600, 45));
        getConfig().set("chat.per-npc-cooldown-seconds", clamp(getConfig().getInt("chat.per-npc-cooldown-seconds", 120), 60, 7200, 120));
        getConfig().set("chat.max-messages-per-minute", clamp(getConfig().getInt("chat.max-messages-per-minute", 6), 1, 10, 6));
        getConfig().set("chat.range-blocks", clamp(getConfig().getInt("chat.range-blocks", 64), 8, 128, 64));
        getConfig().set("chat.max-message-length", clamp(getConfig().getInt("chat.max-message-length", 200), 20, 256, 200));
        saveConfig();
    }

    private static int clamp(int value, int min, int max, int fallback) {
        return value < min || value > max ? fallback : value;
    }

    private static double clamp(double value, double min, double max, double fallback) {
        return !Double.isFinite(value) || value < min || value > max ? fallback : value;
    }
}