Jesse Youmans

a plugin that adds a new kind of villager that lets you buy every main mob (no bosses or variants) that stay still and turns or makes movement that looks like dancing

Kodari AI
KodariOct 6 at 10:40 pm 1
MobDanceVillager
Adds a new villager type that sells every main mob (excluding bosses and variants). Purchased mobs are spawned with a special AI that keeps them stationary while performing rhythmic, dance-like movement animations. The plugin includes a custom villager NPC with a unique trading interface, configurable pricing per mob, and a spawn system that applies the dance behavior. Players can purchase mobs using coins or items, and the dancing mobs serve as decorative or interactive elements in the world. The plugin supports configurable dance patterns, movement speeds, and animation styles to create lively, entertaining mob displays.
  1. Create the Paper plugin setup and /mobmerchant spawn command.

  2. Add a paginated emerald shop for the main non-boss, non-variant mobs.

  3. Make purchased mobs stationary, invulnerable, and continuously dance by turning.

Created the Dancing Mob Merchant plugin for Paper 1.21.4. Use /mobmerchant spawn, then right-click the villager to buy mobs for 5 emeralds each. Purchased mobs stay in place and dance by turning; boss mobs and common mob variants are excluded.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
start
Code
5 371
src/main/java/com/kodari/mobmerchant/DancingMobMerchantPlugin.java
package com.kodari.mobmerchant;

import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;

public final class DancingMobMerchantPlugin extends JavaPlugin implements Listener, CommandExecutor {
    private static final int OFFER_SLOTS = 45;
    private static final int MOB_PRICE = 5;
    private static final List<String> MOB_TYPES = List.of(
            "ALLAY", "ARMADILLO", "AXOLOTL", "BAT", "BEE", "BLAZE", "BREEZE", "CAMEL", "CAT",
            "CHICKEN", "COD", "COW", "CREEPER", "CREAKING", "DOLPHIN", "ENDERMAN", "ENDERMITE",
            "EVOKER", "FOX", "FROG", "GHAST", "GOAT", "GUARDIAN", "HOGLIN", "HORSE", "IRON_GOLEM",
            "LLAMA", "OCELOT", "PANDA", "PARROT", "PHANTOM", "PIG", "PIGLIN", "PILLAGER",
            "POLAR_BEAR", "PUFFERFISH", "RABBIT", "RAVAGER", "SALMON", "SHEEP", "SHULKER",
            "SILVERFISH", "SKELETON", "SLIME", "SNIFFER", "SNOW_GOLEM", "SPIDER", "SQUID",
            "STRIDER", "TROPICAL_FISH", "TURTLE", "VEX", "VILLAGER", "VINDICATOR", "WANDERING_TRADER",
            "WARDEN", "WITCH", "WOLF", "ZOMBIE"
    );

    private final Map<UUID, DanceMotion> dancers = new HashMap<>();
    private NamespacedKey merchantKey;
    private NamespacedKey dancerKey;
    private List<MobOffer> offers;
    private long danceTick;

    @Override
    public void onEnable() {
        merchantKey = new NamespacedKey(this, "mob_merchant");
        dancerKey = new NamespacedKey(this, "dancing_mob");
        offers = createOffers();

        getServer().getPluginManager().registerEvents(this, this);
        getCommand("mobmerchant").setExecutor(this);
        restoreDancers();
        Bukkit.getScheduler().runTaskTimer(this, this::animateDancers, 2L, 2L);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("Only players can spawn a mob merchant.");
            return true;
        }
        if (!player.hasPermission("dancingmobmerchant.spawn")) {
            player.sendMessage(color("&cYou don't have permission to do that."));
            return true;
        }
        if (args.length != 1 || !args[0].equalsIgnoreCase("spawn")) {
            player.sendMessage(color("&eUsage: /mobmerchant spawn"));
            return true;
        }

        Location spawnLocation = player.getLocation().clone()
                .add(player.getLocation().getDirection().normalize().multiply(2));
        Villager merchant = spawnLocation.getWorld().spawn(spawnLocation, Villager.class);
        merchant.setProfession(Villager.Profession.FARMER);
        merchant.setCustomName(color("&d&lMob Merchant"));
        merchant.setCustomNameVisible(true);
        merchant.setInvulnerable(true);
        merchant.setAI(false);
        merchant.setGravity(false);
        merchant.setCollidable(false);
        merchant.getPersistentDataContainer().set(merchantKey, PersistentDataType.BYTE, (byte) 1);
        player.sendMessage(color("&aMob Merchant spawned. Right-click it to browse the mobs."));
        return true;
    }

    @EventHandler
    public void onMerchantInteract(PlayerInteractEntityEvent event) {
        Entity clicked = event.getRightClicked();
        if (!clicked.getPersistentDataContainer().has(merchantKey, PersistentDataType.BYTE)) {
            return;
        }
        event.setCancelled(true);
        event.getPlayer().openInventory(createShop(0));
    }

    @EventHandler
    public void onShopClick(InventoryClickEvent event) {
        if (!(event.getView().getTopInventory().getHolder() instanceof MobShopHolder holder)) {
            return;
        }
        event.setCancelled(true);
        if (!(event.getWhoClicked() instanceof Player player) || event.getClickedInventory() == null
                || event.getClickedInventory() != event.getView().getTopInventory()) {
            return;
        }

        int slot = event.getRawSlot();
        int page = holder.page();
        if (slot == 45 && page > 0) {
            player.openInventory(createShop(page - 1));
            return;
        }
        if (slot == 53 && (page + 1) * OFFER_SLOTS < offers.size()) {
            player.openInventory(createShop(page + 1));
            return;
        }
        if (slot < 0 || slot >= OFFER_SLOTS) {
            return;
        }

        int offerIndex = page * OFFER_SLOTS + slot;
        if (offerIndex >= offers.size()) {
            return;
        }
        MobOffer offer = offers.get(offerIndex);
        if (!withdrawEmeralds(player, MOB_PRICE)) {
            player.sendMessage(color("&cYou need " + MOB_PRICE + " emeralds to buy a mob."));
            return;
        }

        Location spawnLocation = player.getLocation().clone()
                .add(player.getLocation().getDirection().normalize().multiply(2));
        Entity spawned = spawnLocation.getWorld().spawnEntity(spawnLocation, offer.type());
        if (spawned instanceof LivingEntity living) {
            living.setGravity(false);
            living.setInvulnerable(true);
            living.setCollidable(false);
            living.setPersistent(true);
            living.setCustomName(color("&d&lDancing " + offer.name()));
            living.setCustomNameVisible(true);
            if (living instanceof Mob mob) {
                mob.setAI(false);
                mob.setRemoveWhenFarAway(false);
            }
            living.getPersistentDataContainer().set(dancerKey, PersistentDataType.BYTE, (byte) 1);
            dancers.put(living.getUniqueId(), new DanceMotion(living.getLocation().getYaw()));
        }
        player.sendMessage(color("&aYou bought a dancing " + offer.name() + " for " + MOB_PRICE + " emeralds."));
        player.openInventory(createShop(page));
    }

    @EventHandler
    public void onDancerDeath(EntityDeathEvent event) {
        dancers.remove(event.getEntity().getUniqueId());
    }

    private List<MobOffer> createOffers() {
        List<MobOffer> available = new ArrayList<>();
        for (String typeName : MOB_TYPES) {
            try {
                EntityType type = EntityType.valueOf(typeName);
                available.add(new MobOffer(type, formatName(typeName)));
            } catch (IllegalArgumentException ignored) {
                // This mob is not available on the server's Minecraft version.
            }
        }
        return List.copyOf(available);
    }

    private Inventory createShop(int page) {
        MobShopHolder holder = new MobShopHolder(page);
        Inventory inventory = Bukkit.createInventory(holder, 54, color("&5&lMob Merchant"));
        holder.setInventory(inventory);

        int firstOffer = page * OFFER_SLOTS;
        int lastOffer = Math.min(firstOffer + OFFER_SLOTS, offers.size());
        for (int i = firstOffer; i < lastOffer; i++) {
            MobOffer offer = offers.get(i);
            ItemStack item = XMaterial.matchXMaterial(offer.type().name() + "_SPAWN_EGG")
                    .map(XMaterial::parseItem)
                    .orElseGet(() -> XMaterial.matchXMaterial("PAPER").map(XMaterial::parseItem).orElse(null));
            if (item == null) {
                continue;
            }
            ItemMeta meta = item.getItemMeta();
            if (meta != null) {