DR Gamaing

Update the PritiveVillagerControl project to target:

  • Minecraft Paper 1.21.11
  • Java 25
  • Maven
  • Paper API 1.21.11

Set the Maven compiler release to 21 if the plugin should retain Java 21 compatibility, or 25 if it specifically requires Java 25. Prefer Java 21-compatible bytecode unless a Java 25 feature is necessary.

Compile and test the plugin, then provide the final JAR file.

📎 1 file 2,191 chars
📄
pasted-code-1794981334938.txt
txt 58 lines
▶
Build a complete Minecraft Paper plugin.

**Plugin name:** PritiveVillagerControl  
**Minecraft version:** Paper 1.21.11  
**Java version:** Java 21  
**Build system:** Maven

### Main objective
Prevent naturally spawned villagers from existing anywhere in the server, including villagers stored in previously unloaded chunks.

### Requirements

1. Listen for villager CreatureSpawnEvent.
2. Block villagers spawned with NATURAL, CHUNK_GEN, BREEDING, and other non-approved spawn reasons.
3. Allow villagers spawned using SPAWNER_EGG and COMMAND.
4. Add a persistent allowed-villager marker using PersistentDataContainer.
5. When a chunk loads, inspect every villager in that chunk.
6. Remove villagers without the approved persistent marker.
7. Preserve approved villagers when chunks unload and reload.
8. Never affect other entity types.
9. Support all worlds, including the Nether and End.
10. Add configurable allowed spawn reasons in config.yml.
11. Add an administrator command:
    /pvc reload
12. Add permission:
    pritivevillagercontrol.admin
13. Log how many villagers were removed from each loaded chunk.
14. Make the plugin efficient and avoid unnecessary chunk loading.
15. Never force-load every chunk in the world.

### Important compatibility requirements

- Use the Paper API for Minecraft 1.21.11.
- Use Java 21.
- Use Maven.
- Use proper Bukkit/Paper event priorities.
- Handle spawn reasons carefully.
- Use PersistentDataContainer to save the approved marker permanently.
- Do not use NMS or reflection unless absolutely necessary.

### Existing villagers

Create a safe migration system for villagers that existed before the plugin was installed.

Do not pretend their original spawn reason can always be recovered. Provide a configurable migration mode:
- STRICT: remove unmarked villagers when their chunks load.
- PRESERVE: keep existing unmarked villagers until an administrator decides what to do.

### Deliverables

- Complete Maven project.
- plugin.yml
- config.yml
- All Java source files.
- README with installation instructions.
- Compile the plugin into a working JAR.
- Run tests and fix compilation errors.
- Explain how to install it on Paper 1.21.11.
Kodari AI
KodariWed at 6:02 am 2
PritiveVillagerControl
PritiveVillagerControl is a Paper plugin that prevents naturally spawned villagers from existing on the server. It blocks villager spawns from NATURAL, CHUNK_GEN, BREEDING, and other non-approved reasons while allowing SPAWNER_EGG and COMMAND spawns. The plugin uses PersistentDataContainer to mark approved villagers and scans loaded chunks to remove unmarked ones. It features configurable spawn reasons, a migration system with STRICT and PRESERVE modes for pre-existing villagers, an admin /pvc reload command with pritivevillagercontrol.admin permission, and logs removed villagers per chunk. Built for Paper 1.21.11 with Java 21 compatibility, it efficiently handles all worlds without forcing chunk loads.
  • Block unapproved villager spawns and persist markers on approved ones.

  • Inspect loaded chunks without loading new ones, using the configured migration mode.

  • Add reload administration, Maven configuration, and installation instructions.

Created the Maven plugin targeting Paper 1.21.11 with Java 21-compatible bytecode. It blocks unapproved villager spawns, marks approved villagers persistently, and supports STRICT and PRESERVE migration modes.

The JAR has not been compiled or tested in this session. To build and download it in Kodari:

  1. Click Compile in the top bar, or on phone tap ⋮ → Compile Project.

  2. Click Compile in the window, then Download when it succeeds.

  3. Place the JAR in your Paper server’s plugins folder and start the server.

Before starting, back up your world: the default STRICT mode removes unmarked villagers, including existing ones. Edit config.yml and use /pvc reload to change settings.

Kodari can make mistakes. Review important code before using.
Files
src
pom.xml
README.md
Code
5 198
src/main/java/com/kodari/pritivevillagercontrol/PritiveVillagerControl.java
package com.kodari.pritivevillagercontrol;

import java.util.EnumSet;
import java.util.Locale;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.EntitiesLoadEvent;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;

public final class PritiveVillagerControl extends JavaPlugin implements Listener {
    private NamespacedKey approvedKey;
    private Set<SpawnReason> allowedReasons = EnumSet.noneOf(SpawnReason.class);
    private boolean preserveUnmarked;

    @Override
    public void onEnable() {
        saveDefaultConfig();
        approvedKey = new NamespacedKey(this, "approved_villager");
        loadSettings();
        getServer().getPluginManager().registerEvents(this, this);
        Bukkit.getScheduler().runTask(this, () -> {
            for (World world : Bukkit.getWorlds()) {
                for (Chunk chunk : world.getLoadedChunks()) {
                    inspectChunk(chunk);
                }
            }
        });
    }

    private void loadSettings() {
        Set<SpawnReason> reasons = EnumSet.noneOf(SpawnReason.class);
        for (String configured : getConfig().getStringList("allowed-spawn-reasons")) {
            try {
                reasons.add(SpawnReason.valueOf(configured.trim().toUpperCase(Locale.ROOT)));
            } catch (IllegalArgumentException exception) {
                getLogger().warning("Ignoring unknown spawn reason: " + configured);
            }
        }
        allowedReasons = reasons;
        String mode = getConfig().getString("migration-mode", "STRICT").trim().toUpperCase(Locale.ROOT);
        if (!mode.equals("STRICT") && !mode.equals("PRESERVE")) {
            getLogger().warning("Unknown migration mode '" + mode + "'; using STRICT.");
            mode = "STRICT";
        }
        preserveUnmarked = mode.equals("PRESERVE");
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onVillagerSpawn(CreatureSpawnEvent event) {
        if (!(event.getEntity() instanceof Villager villager)) {
            return;
        }
        if (!allowedReasons.contains(event.getSpawnReason())) {
            event.setCancelled(true);
            return;
        }
        villager.getPersistentDataContainer().set(approvedKey, PersistentDataType.BYTE, (byte) 1);
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onChunkLoad(ChunkLoadEvent event) {
        inspectChunk(event.getChunk());
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onEntitiesLoad(EntitiesLoadEvent event) {
        inspectEntities(event.getChunk(), event.getEntities());
    }

    private void inspectChunk(Chunk chunk) {
        inspectEntities(chunk, java.util.Arrays.asList(chunk.getEntities()));
    }

    private void inspectEntities(Chunk chunk, Iterable<Entity> entities) {
        int removed = 0;
        if (!preserveUnmarked) {
            for (Entity entity : entities) {
                if (entity instanceof Villager villager) {
                    Byte marker = villager.getPersistentDataContainer().get(approvedKey, PersistentDataType.BYTE);
                    if (marker == null || marker != (byte) 1) {
                        villager.remove();
                        removed++;
                    }
                }
            }
        }
        getLogger().info("Chunk " + chunk.getWorld().getName() + " [" + chunk.getX() + ", "
                + chunk.getZ() + "]: removed " + removed + " unapproved villager(s).");
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!sender.hasPermission("pritivevillagercontrol.admin")) {
            sender.sendMessage("\u00a7cYou do not have permission to use this command.");
            return true;
        }
        if (args.length != 1 || !args[0].equalsIgnoreCase("reload")) {
            sender.sendMessage("\u00a7cUsage: /" + label + " reload");
            return true;
        }
        reloadConfig();
        loadSettings();
        sender.sendMessage("\u00a7aPritiveVillagerControl configuration reloaded.");
        return true;
    }
}