Jon-Paul Carriço Fernandes

A slime stuff that you can craft using string a stick and a slime ball the slime staff can summoned when West slime armor you can summon more and then 4 the slimes can do anything even attack mobs

Kodari AI
KodariOct 19 at 2:48 pm 1
SlimeSummonerStaff
Creates a craftable slime staff from string, stick, and slime ball. When equipped with full slime armor, the staff summons up to four loyal slime minions that follow the player and automatically attack nearby hostile mobs. Minions persist until dismissed or player dies, with a cooldown between summons.
  • Add the Paper plugin setup and register the slime staff and slime armor crafting recipes.

  • Make the staff summon follower slimes that seek and attack nearby mobs.

  • Let a full slime armor set raise the summon limit from two to four.

Created the SlimeCompanions plugin. Craft the staff with a slime ball, stick, and string; right-click to summon up to two slimes. Craft and wear the full slime armor set to raise the limit to four. Companions follow you and attack nearby mobs.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
3 255
src/main/java/com/kodari/slimecompanions/SlimeCompanionsPlugin.java
package com.kodari.slimecompanions;

import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

public final class SlimeCompanionsPlugin extends JavaPlugin implements Listener {
    private final Set<Slime> summonedSlimes = new HashSet<>();
    private NamespacedKey staffKey;
    private NamespacedKey armorKey;
    private NamespacedKey ownerKey;

    @Override
    public void onEnable() {
        staffKey = new NamespacedKey(this, "slime_staff");
        armorKey = new NamespacedKey(this, "slime_armor");
        ownerKey = new NamespacedKey(this, "slime_owner");
        registerRecipes();
        getServer().getPluginManager().registerEvents(this, this);
        getServer().getScheduler().runTaskTimer(this, this::updateSummons, 20L, 10L);
    }

    @Override
    public void onDisable() {
        for (Slime slime : summonedSlimes) {
            if (slime.isValid() && !slime.isDead()) {
                slime.remove();
            }
        }
        summonedSlimes.clear();
    }

    private void registerRecipes() {
        ItemStack staff = createStaff();
        ShapedRecipe staffRecipe = new ShapedRecipe(new NamespacedKey(this, "slime_staff_recipe"), staff);
        staffRecipe.shape(" S ", " T ", " L ");
        staffRecipe.setIngredient('S', material("SLIME_BALL"));
        staffRecipe.setIngredient('T', material("STICK"));
        staffRecipe.setIngredient('L', material("STRING"));
        getServer().addRecipe(staffRecipe);

        registerArmorRecipe("helmet", "LEATHER_HELMET", "SSS", "S S");
        registerArmorRecipe("chestplate", "LEATHER_CHESTPLATE", "S S", "SSS", "SSS");
        registerArmorRecipe("leggings", "LEATHER_LEGGINGS", "SSS", "S S", "S S");
        registerArmorRecipe("boots", "LEATHER_BOOTS", "S S", "S S");
    }

    private void registerArmorRecipe(String slot, String materialName, String... shape) {
        ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "slime_" + slot + "_recipe"), createArmor(slot, materialName));
        recipe.shape(shape);
        recipe.setIngredient('S', material("SLIME_BALL"));
        getServer().addRecipe(recipe);
    }

    private ItemStack createStaff() {
        ItemStack item = new ItemStack(material("STICK"));
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(ChatColor.GREEN.toString() + ChatColor.BOLD + "Slime Staff");
        meta.setLore(List.of(ChatColor.GRAY + "Right-click to summon a slime companion."));
        meta.getPersistentDataContainer().set(staffKey, PersistentDataType.BYTE, (byte) 1);
        item.setItemMeta(meta);
        return item;
    }

    private ItemStack createArmor(String slot, String materialName) {
        ItemStack item = new ItemStack(material(materialName));
        LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
        meta.setColor(Color.LIME);
        meta.setDisplayName(ChatColor.GREEN.toString() + ChatColor.BOLD + "Slime " + titleCase(slot));
        meta.setLore(List.of(ChatColor.GRAY + "Part of a full set: summon up to four slimes."));
        meta.getPersistentDataContainer().set(armorKey, PersistentDataType.STRING, slot);
        item.setItemMeta(meta);
        return item;
    }

    private String titleCase(String value) {
        return Character.toUpperCase(value.charAt(0)) + value.substring(1);
    }

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

    @EventHandler
    public void onStaffUse(PlayerInteractEvent event) {
        if (event.getHand() != EquipmentSlot.HAND || !isStaff(event.getItem())) {
            return;
        }
        switch (event.getAction()) {
            case RIGHT_CLICK_AIR, RIGHT_CLICK_BLOCK -> {
                event.setCancelled(true);
                summon(event.getPlayer());
            }
            default -> {
            }
        }
    }

    private boolean isStaff(ItemStack item) {
        return item != null && item.hasItemMeta()
                && item.getItemMeta().getPersistentDataContainer().has(staffKey, PersistentDataType.BYTE);
    }

    private void summon(Player player) {
        int limit = isWearingFullSlimeArmor(player) ? 4 : 2;
        if (countSummons(player.getUniqueId()) >= limit) {
            player.sendMessage(ChatColor.GREEN + "Your slime companions are already at their limit.");
            return;
        }

        Slime slime = player.getWorld().spawn(player.getLocation().add(1, 0, 0), Slime.class);
        slime.setSize(1);
        slime.setCustomName(ChatColor.GREEN + player.getName() + "'s Slime");
        slime.setRemoveWhenFarAway(false);
        slime.getPersistentDataContainer().set(ownerKey, PersistentDataType.STRING, player.getUniqueId().toString());
        summonedSlimes.add(slime);
        player.sendMessage(ChatColor.GREEN + "A slime companion has joined you.");
    }

    private int countSummons(UUID ownerId) {
        int count = 0;
        for (Slime slime : summonedSlimes) {
            if (slime.isValid() && !slime.isDead()
                    && ownerId.toString().equals(slime.getPersistentDataContainer().get(ownerKey, PersistentDataType.STRING))) {
                count++;
            }
        }
        return count;
    }

    private boolean isWearingFullSlimeArmor(Player player) {
        return hasArmorPiece(player.getInventory().getHelmet(), "helmet")
                && hasArmorPiece(player.getInventory().getChestplate(), "chestplate")
                && hasArmorPiece(player.getInventory().getLeggings(), "leggings")
                && hasArmorPiece(player.getInventory().getBoots(), "boots");
    }

    private boolean hasArmorPiece(ItemStack item, String slot) {
        return item != null && item.hasItemMeta()
                && slot.equals(item.getItemMeta().getPersistentDataContainer().get(armorKey, PersistentDataType.STRING));
    }

    private void updateSummons() {
        for (Slime slime : new ArrayList<>(summonedSlimes)) {
            if (!slime.isValid() || slime.isDead()) {
                summonedSlimes.remove(slime);
                continue;
            }

            String ownerId = slime.getPersistentDataContainer().get(ownerKey, PersistentDataType.STRING);
            Player owner;
            try {
                owner = ownerId == null ? null : Bukkit.getPlayer(UUID.fromString(ownerId));
            } catch (IllegalArgumentException exception) {
                owner = null;
            }
            if (owner == null || !owner.isOnline()) {
                slime.remove();
                summonedSlimes.remove(slime);
                continue;
            }
            if (slime.getWorld() != owner.getWorld()) {
                slime.teleport(owner.getLocation());
            }

            Mob target = findTarget(slime);
            if (target != null) {
                slime.setTarget(target);
            } else {
                slime.setTarget(null);
                if (slime.getLocation().distanceSquared(owner.getLocation()) > 16) {
                    slime.getPathfinder().moveTo(owner.getLocation());
                }
            }