Mr Poo

make me a 26.2 plugin that adds a special barrel to the game. you put in your totem in the barrel and it copy's it and it will make the game think the barrel is still full so comparators think that there is still something in the barrel but the second your totem pops the totem in the barrel will disappear and the comparator will update call it the totem barrel

Kodari AI
KodariOct 30 at 10:33 pm 1
TotemBarrel
A Minecraft plugin that adds a special craftable barrel item. When a player places a totem inside the Totem Barrel, the plugin stores a copy of the totem and makes the barrel's comparator output think it is still full. The moment the player's totem pops (activates), the stored totem in the barrel disappears and the comparator updates accordingly. The plugin includes crafting recipes, GUI interaction, comparator signal management, and totem activation detection to sync the barrel state with the player's totem usage.

Plan:

  • Add the Paper 26.2 plugin scaffold and a craftable Totem Barrel.

  • Keep its stored totem locked in the barrel as comparator-visible inventory while giving the player a linked copy.

  • On that copy’s resurrection, remove the stored totem and update the barrel; prevent breaking it while armed.

Created the Totem Barrel plugin for Paper 26.2. Craft one from a barrel and a totem, then place it and insert a totem. The barrel keeps a comparator-visible totem while giving you a linked copy; when that copy saves you, the stored totem is removed and the comparator updates. The barrel can’t be broken while a totem is linked.

Mr Poo

make it so you cant craft it and can only get it with commands

Kodari AI
KodariOct 30 at 10:34 pm 1
  • Remove the crafting recipe and clear its old recipe key on startup.

  • Add /totembarrel [player] to give the special barrel; restrict it to operators by default.

Removed the crafting recipe and added /totembarrel [player] to give the special barrel. The command is operator-only by default, and startup removes the old recipe registration.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
3 354
src/main/java/com/kodari/totembarrel/TotemBarrelPlugin.java
package com.kodari.totembarrel;

import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.Barrel;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.entity.EntityResurrectEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;

import java.util.UUID;

public final class TotemBarrelPlugin extends org.bukkit.plugin.java.JavaPlugin implements Listener {
    private NamespacedKey barrelKey;
    private NamespacedKey tokenKey;
    private NamespacedKey worldKey;
    private NamespacedKey xKey;
    private NamespacedKey yKey;
    private NamespacedKey zKey;

    @Override
    public void onEnable() {
        barrelKey = new NamespacedKey(this, "totem_barrel");
        tokenKey = new NamespacedKey(this, "linked_totem");
        worldKey = new NamespacedKey(this, "linked_world");
        xKey = new NamespacedKey(this, "linked_x");
        yKey = new NamespacedKey(this, "linked_y");
        zKey = new NamespacedKey(this, "linked_z");
        Bukkit.getPluginManager().registerEvents(this, this);
        Bukkit.removeRecipe(new NamespacedKey(this, "totem_barrel"));
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!command.getName().equalsIgnoreCase("totembarrel")) {
            return false;
        }
        if (args.length > 1) {
            sender.sendMessage(ChatColor.RED + "Usage: /totembarrel [player]");
            return true;
        }

        Player target;
        if (args.length == 1) {
            target = Bukkit.getPlayerExact(args[0]);
            if (target == null) {
                sender.sendMessage(ChatColor.RED + "That player is not online.");
                return true;
            }
        } else if (sender instanceof Player player) {
            target = player;
        } else {
            sender.sendMessage(ChatColor.RED + "Usage: /totembarrel <player>");
            return true;
        }

        ItemStack item = createTotemBarrelItem();
        if (item == null) {
            sender.sendMessage(ChatColor.RED + "Could not create a Totem Barrel on this server version.");
            return true;
        }
        target.getInventory().addItem(item).values().forEach(leftover ->
                target.getWorld().dropItemNaturally(target.getLocation(), leftover));
        sender.sendMessage(ChatColor.GREEN + "Gave a Totem Barrel to " + target.getName() + ".");
        if (!sender.equals(target)) {
            target.sendMessage(ChatColor.GREEN + "You received a Totem Barrel.");
        }
        return true;
    }

    private ItemStack createTotemBarrelItem() {
        ItemStack item = XMaterial.matchXMaterial("BARREL").map(XMaterial::parseItem).orElse(null);
        if (item == null) {
            return null;
        }
        ItemMeta meta = item.getItemMeta();
        meta.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Totem Barrel");
        meta.setLore(java.util.List.of(ChatColor.GRAY + "Insert a totem to link a usable copy."));
        meta.getPersistentDataContainer().set(barrelKey, PersistentDataType.BYTE, (byte) 1);
        item.setItemMeta(meta);
        return item;
    }

    @EventHandler
    public void onBarrelPlace(BlockPlaceEvent event) {
        if (!isTotemBarrelItem(event.getItemInHand())) {
            return;
        }
        BlockState state = event.getBlockPlaced().getState();
        if (!(state instanceof Barrel barrel)) {
            return;
        }
        barrel.getPersistentDataContainer().set(barrelKey, PersistentDataType.BYTE, (byte) 1);
        barrel.update(true, false);
    }

    @EventHandler
    public void onBarrelBreak(BlockBreakEvent event) {
        Block block = event.getBlock();
        BlockState state = block.getState();
        if (!(state instanceof Barrel barrel) || !isTotemBarrel(barrel)) {
            return;
        }

        for (ItemStack item : barrel.getInventory().getContents()) {
            if (getToken(item) != null) {
                event.setCancelled(true);
                event.getPlayer().sendMessage(ChatColor.RED + "The Totem Barrel cannot be broken while its totem is linked.");
                return;
            }
        }

        Material air = XMaterial.matchXMaterial("AIR").map(XMaterial::parseMaterial).orElse(null);
        ItemStack droppedBarrel = createTotemBarrelItem();
        if (air == null || droppedBarrel == null) {
            return;
        }
        event.setCancelled(true);
        event.setDropItems(false);
        block.setType(air);
        block.getWorld().dropItemNaturally(block.getLocation(), droppedBarrel);
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        Inventory top = event.getView().getTopInventory();
        Barrel barrel = getTotemBarrel(top);
        if (barrel == null) {
            return;
        }

        if (event.getClickedInventory() == top) {
            event.setCancelled(true);
            int slot = event.getSlot();
            if (slot < 0 || slot >= top.getSize() || top.getItem(slot) != null || !isTotem(event.getCursor())) {
                return;
            }
            insertTotem(event, barrel, slot, event.getCursor());
            return;
        }

        if (event.getClickedInventory() != event.getView().getBottomInventory() || !event.isShiftClick()) {
            return;
        }
        ItemStack source = event.getCurrentItem();
        if (!isTotem(source)) {
            return;
        }
        int slot = findEmptySlot(top);
        if (slot < 0 || (source.getAmount() > 1 && event.getWhoClicked().getInventory().firstEmpty() < 0)) {
            event.setCancelled(true);
            return;
        }

        event.setCancelled(true);
        insertTotem(event, barrel, slot, source);
        ItemStack remaining = source.clone();
        if (remaining.getAmount() <= 1) {
            event.setCurrentItem(null);
        } else {
            remaining.setAmount(remaining.getAmount() - 1);
            event.setCurrentItem(remaining);
        }
    }

    private void insertTotem(InventoryClickEvent event, Barrel barrel, int slot, ItemStack source) {
        String token = UUID.randomUUID().toString();
        ItemStack stored = source.clone();
        stored.setAmount(1);
        setToken(stored, token);

        ItemStack linkedCopy = stored.clone();
        setLink(linkedCopy, token, barrel.getBlock());
        barrel.getInventory().setItem(slot, stored);

        if (source.getAmount() == 1 && event.getClickedInventory() == event.getView().getTopInventory()) {
            event.setCursor(linkedCopy);
            return;
        }

        if (source.getAmount() > 1 && event.getClickedInventory() == event.getView().getTopInventory()) {
            ItemStack remainder = source.clone();