Updating Shulker Box Inventory

Discussion in 'Plugin Development' started by ExtremeOrphan, Sep 20, 2019.

  1. Offline

    ExtremeOrphan

    Hello! I am trying to make a plugin where you can right click with a shulker box in your hand and it will open the shulker's inventory in a new inventory that mirrors the shulker's contents. However I want it so they can edit the contents and when the close the inventory the shulker's contents will be updated with the current (mirror inventory) contents.

    The event to open the shulker box and put its contents into a new inventory works, however the event (InventoryCloseEvent) to update the inventory isn't being completed, it wont get past the 4th debug message ( p.sendMessage("3"); )

    Here's my relevant code, any and all assistance is appreciated!

    Code:
    package com.jeremyrpeters.briefcases;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.ShulkerBox;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BlockStateMeta;
    import org.bukkit.inventory.meta.ItemMeta;
    
    
    public class EventListener implements Listener {
    
        Main plugin;
      
        public EventListener (Main instance) {
         
            plugin = instance;
    
        }
    
        @EventHandler
        public void openBriefcase(PlayerInteractEvent event) {
         
            Player p = event.getPlayer();
            ItemStack briefcase = event.getItem(); // Player that is right clicked
                 
            ItemMeta meta = briefcase.getItemMeta();
            String name = meta.getDisplayName();
                 
            if (name.equalsIgnoreCase(ChatColor.RED + "Briefcase")) {
             
                if(briefcase.getItemMeta() instanceof BlockStateMeta) {
                 
                    BlockStateMeta im = (BlockStateMeta) briefcase.getItemMeta();
                 
                    if(im.getBlockState() instanceof ShulkerBox) {
                     
                        event.setCancelled(true);
                     
                        ShulkerBox shulker = (ShulkerBox) im.getBlockState();
                     
                        Inventory briefcase_inv = Bukkit.createInventory(null, 27, ChatColor.RED + "Briefcase");
                     
                        briefcase_inv.setContents(shulker.getInventory().getContents());
                     
                        p.openInventory(briefcase_inv);
                     
                 
                    }
                }
            }
        }
    
        @EventHandler
        public void closeBriefcase(InventoryCloseEvent event) {
         
            Player p = (Player) event.getPlayer();
            ItemStack briefcase = p.getItemInHand();
                 
            p.sendMessage("0");
         
            if (briefcase.getType().equals(Material.BLACK_SHULKER_BOX)) {
             
                p.sendMessage("1");
             
                if (briefcase.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "Briefcase")) {
             
                    p.sendMessage("2");
                 
                    if(briefcase.getItemMeta() instanceof BlockStateMeta) {
    
                        BlockStateMeta briefcase_meta = (BlockStateMeta) briefcase.getItemMeta();
                     
                        p.sendMessage("3");
    
                        if(briefcase_meta.getBlockState() instanceof ShulkerBox) {
                 
                            ShulkerBox shulker = (ShulkerBox) briefcase_meta.getBlockState();
    
                            Inventory currentinv = p.getInventory();
                     
                            shulker.getInventory().setContents(currentinv.getContents());
                         
                            briefcase_meta.setBlockState(shulker);
                         
                            briefcase.setItemMeta(briefcase_meta);
                         
                            p.sendMessage("4");
                        }
                    }
                }
            }
        }
    }
    
     
  2. Offline

    DerDonut

    The problem is that the player inventory has 36 slots (maybe even more not sure about the armor slots) but the shulker box only has 27 slots so you can't duplicate the player inventory into the shulker since there is not enough space. Also: in the interactEvent, check if the item has an itemMeta otherwise you'll get tons of errors which also should indicate your main issue. Look into the console and read the stacktrace!!
     

Share This Page