notvisiongg_.

Create a Minecraft (Paper/Spigot) plugin that ensures villagers never despawn or get removed under any circumstances. The plugin must prevent villagers from being affected by natural despawning, mob limits, chunk unloads, or external systems like lag-clearing plugins (e.g., ClearLag). It should listen to relevant events such as entity removal, death, and chunk unload, and cancel any action if the entity is a villager. All villagers should be made persistent by default, and any attempt by another plugin or system to kill or remove a villager must be blocked unless explicitly allowed. Include a configurable config.yml file with options to enable or disable villager protection and allow removal only by admins with a specific permission (villagerprotect.admin). Add a command like /villagerprotect reload to reload the configuration, and log messages in the console whenever a villager removal is prevented for debugging purposes. The plugin should be lightweight, optimized, and compatible with Minecraft version 1.21+.

Kodari AI
KodariApr 15 at 10:30 am 16,889 (v2)

I’ll create a lightweight villager protection plugin that prevents despawning, removal, and death.

VillagerProtect
A lightweight protection system that prevents villagers from despawning, being removed by lag-clearing plugins, or dying under any circumstances. Features persistent villager tracking, event-based protection against removal attempts, configurable admin bypass with permissions, and detailed console logging for debugging. Compatible with Minecraft 1.21+ and designed to work seamlessly with chunk unloading and external plugin interference.

Plugin created successfully! Here’s what was implemented:

Core Protection Features:

  • Persistent villagers on spawn (setRemoveWhenFarAway + setPersistent)

  • Damage prevention for all damage types (environmental, combat, etc.)

  • Admin bypass system for controlled removal when enabled

  • Chunk unload protection ensures persistence even during world management

  • Removal detection logs attempts by external plugins or commands

Configuration Options (config.yml):

  • enabled - Toggle protection on/off

  • admin-can-remove - Allow players with villagerprotect.admin to damage villagers

  • debug-logging - Console logging for all protection events

Commands:

  • /villagerprotect reload - Reload configuration (requires villagerprotect.reload permission)

The plugin intercepts all damage events with HIGH priority and cancels them unless an admin with the proper permission is attacking. All villagers are made persistent immediately on spawn and reinforced during chunk unloads to prevent any removal by lag-clearing plugins or natural despawning.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
7 274
src/main/java/com/villagerprotect/VillagerProtect.java
package com.villagerprotect;

import com.villagerprotect.command.VillagerCommand;
import com.villagerprotect.listener.VillagerListener;
import com.villagerprotect.manager.ConfigManager;
import lombok.Getter;
import org.bukkit.plugin.java.JavaPlugin;

@Getter
public class VillagerProtect extends JavaPlugin {
    private ConfigManager configManager;
    
    @Override
    public void onEnable() {
        this.configManager = new ConfigManager(this);
        
        getServer().getPluginManager().registerEvents(new VillagerListener(this), this);
        
        VillagerCommand commandHandler = new VillagerCommand(this);
        getCommand("villagerprotect").setExecutor(commandHandler);
        getCommand("villagerprotect").setTabCompleter(commandHandler);
        
        getLogger().info("VillagerProtect v" + getDescription().getVersion() + " has been enabled!");
        getLogger().info("Villagers are now protected from despawning and removal.");
    }
    
    @Override
    public void onDisable() {
        getLogger().info("VillagerProtect has been disabled!");
    }
    
    public void reload() {
        this.configManager.reload();
        getLogger().info("Configuration reloaded successfully!");
    }
}