Спидран Новичка

doll - controller as item, that can be crafted with 5 iron ingots and 1 redstone dust, summons doll or minecraft mannequin that you can control if you are currently using the item

Kodari AI
KodariOct 24 at 2:59 am 1
DollController
DollController lets players craft a doll item from 5 iron ingots and 1 redstone dust. When held and used, it summons a controllable Minecraft mannequin or doll entity. While actively using the item, the player gains remote control over the summoned doll, directing its movement and actions. The plugin manages doll spawning, ownership, control state, and item crafting recipes, providing a unique puppeteering gameplay mechanic.
  1. Add the plugin entry point and register the controller recipe.

  2. Make the controller summon/despawn a mannequin and steer it by movement and look direction while held.

  3. Add the plugin descriptor.

Created the DollController plugin: craft the controller with 5 iron ingots and 1 redstone dust, right-click to summon or dismiss the mannequin, and hold it while moving and looking to steer the doll.

Kodari can make mistakes. Review important code before using.
Files
src
Code
2 162
src/main/java/com/kodari/dollcontroller/DollControllerPlugin.java
package com.kodari.dollcontroller;

import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public final class DollControllerPlugin extends JavaPlugin implements Listener {
    private final Map<UUID, UUID> dollsByOwner = new HashMap<>();
    private NamespacedKey controllerKey;
    private Material controllerMaterial;

    @Override
    public void onEnable() {
        controllerKey = new NamespacedKey(this, "doll_controller");
        controllerMaterial = material("BLAZE_ROD");
        Bukkit.getPluginManager().registerEvents(this, this);
        registerRecipe();
    }

    @Override
    public void onDisable() {
        for (UUID dollId : dollsByOwner.values()) {
            removeDoll(dollId);
        }
        dollsByOwner.clear();
    }

    private void registerRecipe() {
        ItemStack controller = createController();
        ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "controller_recipe"), controller);
        recipe.shape("III", "IRI");
        recipe.setIngredient('I', material("IRON_INGOT"));
        recipe.setIngredient('R', material("REDSTONE"));
        Bukkit.addRecipe(recipe);
    }

    private ItemStack createController() {
        ItemStack item = new ItemStack(controllerMaterial);
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName("§6Doll Controller");
        meta.setLore(java.util.List.of("§7Right-click to summon or dismiss a doll.", "§7Hold this item to control it."));
        meta.getPersistentDataContainer().set(controllerKey, PersistentDataType.BYTE, (byte) 1);
        item.setItemMeta(meta);
        return item;
    }

    private Material material(String name) {
        return XMaterial.matchXMaterial(name)
                .map(XMaterial::parseMaterial)
                .orElseThrow(() -> new IllegalStateException("Required material is unavailable: " + name));
    }

    private boolean isController(ItemStack item) {
        if (item == null || item.getType() != controllerMaterial || !item.hasItemMeta()) {
            return false;
        }
        Byte marker = item.getItemMeta().getPersistentDataContainer().get(controllerKey, PersistentDataType.BYTE);
        return marker != null && marker == (byte) 1;
    }

    private boolean isHoldingController(Player player) {
        return isController(player.getInventory().getItemInMainHand())
                || isController(player.getInventory().getItemInOffHand());
    }

    @EventHandler
    public void onControllerUse(PlayerInteractEvent event) {
        if (event.getHand() != EquipmentSlot.HAND || !isController(event.getItem())) {
            return;
        }
        Action action = event.getAction();
        if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) {
            return;
        }

        event.setCancelled(true);
        Player player = event.getPlayer();
        UUID existingId = dollsByOwner.remove(player.getUniqueId());
        if (existingId != null) {
            removeDoll(existingId);
            player.sendMessage("§eYour doll has been dismissed.");
            return;
        }

        Location spawnLocation = controlledLocation(player);
        ArmorStand doll = (ArmorStand) player.getWorld().spawnEntity(spawnLocation, EntityType.ARMOR_STAND);
        doll.setArms(true);
        doll.setBasePlate(false);
        doll.setGravity(false);
        doll.setInvulnerable(true);
        doll.setCustomName(player.getName() + "'s Doll");
        doll.setCustomNameVisible(true);
        dollsByOwner.put(player.getUniqueId(), doll.getUniqueId());
        player.sendMessage("§aYour doll has been summoned. Hold the controller and move to control it.");
    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        if (event.getTo() == null || !isHoldingController(event.getPlayer())) {
            return;
        }
        UUID dollId = dollsByOwner.get(event.getPlayer().getUniqueId());
        if (dollId == null) {
            return;
        }
        org.bukkit.entity.Entity entity = Bukkit.getEntity(dollId);
        if (entity instanceof ArmorStand doll && doll.getWorld().equals(event.getTo().getWorld())) {
            doll.teleport(controlledLocation(event.getTo()));
        }
    }

    private Location controlledLocation(Player player) {
        return controlledLocation(player.getLocation());
    }

    private Location controlledLocation(Location playerLocation) {
        Location dollLocation = playerLocation.clone().add(playerLocation.getDirection().multiply(2.0));
        dollLocation.setPitch(0.0f);
        return dollLocation;
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        UUID dollId = dollsByOwner.remove(event.getPlayer().getUniqueId());
        if (dollId != null) {
            removeDoll(dollId);
        }
    }

    private void removeDoll(UUID dollId) {
        org.bukkit.entity.Entity doll = Bukkit.getEntity(dollId);
        if (doll != null) {
            doll.remove();
        }
    }
}