Dean Kone

make a plugin that makes an ender chest double the size

Kodari AI
KodariOct 30 at 6:56 pm 1
EnderExpansion
A Minecraft plugin that doubles the storage capacity of ender chests, transforming the standard 27-slot inventory into a 54-slot expanded storage system. The plugin intercepts ender chest interactions and replaces the default ender chest interface with a custom, larger inventory view. Players access their expanded ender chest storage through normal ender chest interaction, with the plugin seamlessly managing the additional 27 slots in the background. The plugin maintains per-player persistent storage, ensuring that items stored in the expanded ender chest remain safe across server restarts and player sessions. Compatible with Bukkit, Spigot, and Paper servers, this plugin provides an intuitive enhancement to the classic ender chest mechanic without requiring any client-side modifications or additional player setup.
  • Create a Paper plugin that opens a 54-slot ender-chest inventory when a player right-clicks an ender chest.

  • Keep slots 1–27 synced with the vanilla ender chest and persist slots 28–54 per player.

  • Add the plugin descriptor and minimal Gradle build files.

Created the DoubleEnderChest plugin. Right-clicking an ender chest opens a 54-slot inventory: the first 27 slots sync with the vanilla ender chest, and the extra 27 persist per player in ender-chests.yml.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
3 194
src/main/java/com/kodari/doubleenderchest/DoubleEnderChestPlugin.java
package com.kodari.doubleenderchest;

import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
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.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class DoubleEnderChestPlugin extends JavaPlugin implements Listener {
    private final Map<UUID, Inventory> activeInventories = new HashMap<>();
    private File storageFile;
    private YamlConfiguration storage;

    @Override
    public void onEnable() {
        storageFile = new File(getDataFolder(), "ender-chests.yml");
        storage = YamlConfiguration.loadConfiguration(storageFile);
        getServer().getPluginManager().registerEvents(this, this);
    }

    @Override
    public void onDisable() {
        for (Map.Entry<UUID, Inventory> entry : activeInventories.entrySet()) {
            saveInventory(entry.getKey(), entry.getValue());
        }
        activeInventories.clear();
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onEnderChestInteract(PlayerInteractEvent event) {
        if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getClickedBlock() == null
                || !event.getClickedBlock().getType().name().equals("ENDER_CHEST")) {
            return;
        }

        event.setCancelled(true);
        Player player = event.getPlayer();
        player.closeInventory();
        Bukkit.getScheduler().runTask(this, () -> {
            if (player.isOnline()) {
                openEnderChest(player);
            }
        });
    }

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        Inventory top = event.getView().getTopInventory();
        if (top.getHolder() instanceof EnderChestHolder holder) {
            scheduleSave(holder.getOwnerId(), top);
        }
    }

    @EventHandler
    public void onInventoryDrag(InventoryDragEvent event) {
        Inventory top = event.getView().getTopInventory();
        if (top.getHolder() instanceof EnderChestHolder holder) {
            scheduleSave(holder.getOwnerId(), top);
        }
    }

    @EventHandler
    public void onInventoryClose(InventoryCloseEvent event) {
        Inventory inventory = event.getInventory();
        if (!(inventory.getHolder() instanceof EnderChestHolder holder)) {
            return;
        }

        UUID ownerId = holder.getOwnerId();
        saveInventory(ownerId, inventory);
        activeInventories.remove(ownerId, inventory);
    }

    private void openEnderChest(Player player) {
        UUID ownerId = player.getUniqueId();
        EnderChestHolder holder = new EnderChestHolder(ownerId);
        Inventory inventory = Bukkit.createInventory(holder, 54, "Ender Chest");
        holder.setInventory(inventory);

        ItemStack[] contents = new ItemStack[54];
        ItemStack[] enderChestContents = player.getEnderChest().getContents();
        System.arraycopy(enderChestContents, 0, contents, 0, Math.min(27, enderChestContents.length));

        ConfigurationSection extra = storage.getConfigurationSection("players." + ownerId + ".extra");
        if (extra != null) {
            for (int slot = 27; slot < 54; slot++) {
                contents[slot] = extra.getItemStack(Integer.toString(slot));
            }
        }

        inventory.setContents(contents);
        activeInventories.put(ownerId, inventory);
        player.openInventory(inventory);
    }

    private void scheduleSave(UUID ownerId, Inventory inventory) {
        Bukkit.getScheduler().runTask(this, () -> {
            if (activeInventories.get(ownerId) == inventory) {
                saveInventory(ownerId, inventory);
            }
        });
    }

    private void saveInventory(UUID ownerId, Inventory inventory) {
        Player player = Bukkit.getPlayer(ownerId);
        if (player != null) {
            for (int slot = 0; slot < 27; slot++) {
                player.getEnderChest().setItem(slot, inventory.getItem(slot));
            }
        }

        String extraPath = "players." + ownerId + ".extra";
        storage.set(extraPath, null);
        for (int slot = 27; slot < 54; slot++) {
            ItemStack item = inventory.getItem(slot);
            if (item != null) {
                storage.set(extraPath + "." + slot, item);
            }
        }

        try {
            storage.save(storageFile);
        } catch (IOException exception) {
            getLogger().severe("Could not save ender chest data: " + exception.getMessage());
        }
    }

    private static final class EnderChestHolder implements InventoryHolder {
        private final UUID ownerId;
        private Inventory inventory;

        private EnderChestHolder(UUID ownerId) {
            this.ownerId = ownerId;
        }

        private UUID getOwnerId() {
            return ownerId;
        }

        private void setInventory(Inventory inventory) {
            this.inventory = inventory;
        }

        @Override
        public Inventory getInventory() {
            return inventory;
        }
    }
}