How to open an inventory through another inventory

Discussion in 'Plugin Development' started by Random_Tomato, Jul 14, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Random_Tomato

    Hello, As I am fairly new to plugin development please excuse me if the problem is really obvious. But I am trying to open a inventory through another inventory and I am having trouble on trying to figure out how to do so.

    [​IMG]

    Basically by clicking the arrow I would like the inventory to change to the second one.

    [​IMG]

    Here is my plugin code, Hopefully someone can help!

    Inventory Click Class:
    Inventory Click (open)

    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            Inventory inv = event.getInventory();
            if(!(event.getWhoClicked() instanceof Player))
            return;
          
            if(!inv.getTitle().equals(ChatColor.RED + "" + ChatColor.BOLD + "Hats"))
                return;
    
            Player player = (Player) event.getWhoClicked();
            PlayerInventory inventory = player.getInventory();
            ItemStack item = event.getCurrentItem();
          
        // Blocks which will be used as hats
          
        //    ItemStack redstoneTorch = new ItemStack(Material.REDSTONE_TORCH_ON, 1);
        //    ItemStack leaves = new ItemStack(Material.LEAVES, 1);
            ItemStack purpurPillar = new ItemStack(Material.PURPUR_PILLAR, 1);
            ItemStack bone = new ItemStack(Material.BONE_BLOCK, 1);
            ItemStack magma = new ItemStack(Material.MAGMA, 1);
            ItemStack beacon = new ItemStack(Material.BEACON, 1);
            ItemStack netherwart = new ItemStack(Material.NETHER_WART_BLOCK, 1);
            ItemStack redNetherbrick = new ItemStack(Material.RED_NETHER_BRICK, 1);
            ItemStack purpur = new ItemStack(Material.PURPUR_BLOCK, 1);
            ItemStack prismarine = new ItemStack(Material.PRISMARINE, 1);
            ItemStack seaLanturn = new ItemStack(Material.SEA_LANTERN, 1);
            ItemStack ice = new ItemStack(Material.ICE, 1);
            ItemStack darkPrismarine = new ItemStack(Material.PRISMARINE, 1);
            ItemStack packedIce = new ItemStack(Material.PACKED_ICE, 1);
            ItemStack clear = new ItemStack(Material.AIR, 1);
        //    ItemStack next = new ItemStack(Material.ARROW, 1);
            ItemStack sapling = new ItemStack(Material.SAPLING, 1);
          
        // What happens when the items are clicked in the inventory
        // Use - player.openInventory(?); - to open sub menus
          
        // MENU 1
          
            if(item.getType() == Material.PURPUR_PILLAR) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(purpurPillar);
            }
          
            if(item.getType() == Material.BONE_BLOCK) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(bone);
            }
          
            if(item.getType() == Material.MAGMA) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(magma);
            }
          
            if(item.getType() == Material.BEACON) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(beacon);
            }
          
            if(item.getType() == Material.NETHER_WART_BLOCK) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(netherwart);
            }
          
            if(item.getType() == Material.RED_NETHER_BRICK) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(redNetherbrick);
            }
          
            if(item.getType() == Material.PURPUR_BLOCK) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(purpur);
            }
          
            if(item.getType() == Material.PRISMARINE) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(prismarine);
            }
          
            if(item.getType() == Material.SEA_LANTERN) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(seaLanturn);
            }
          
            if(item.getType() == Material.ICE) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(ice);
            }
          
            if(item.getType() == Material.PRISMARINE) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(darkPrismarine);
            }
          
            if(item.getType() == Material.PACKED_ICE) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(packedIce);
            }
          
            if(item.getType() == Material.BARRIER) {
                player.sendMessage(ChatColor.RED + "You cleared you hat");
                inventory.setHelmet(clear);
            }
          
            // if(item.getType() == Material.ARROW) {
            //    player.openInventory(inv2);
            // }
    
            // MENU 2
          
            if(item.getType() == Material.SAPLING) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(sapling);
            }
    
            //    if(item.getType() == Material.REDSTONE_TORCH_ON) {
            //        player.sendMessage(ChatColor.RED + "You equipped the hat");
            //        inventory.setHelmet(redstoneTorch);  
            //    }
              
            //    if(item.getType() == Material.LEAVES) {
            //        player.sendMessage(ChatColor.RED + "You equipped the hat");
            //        inventory.setHelmet(leaves);
            //    }
      
            event.setCancelled(true);
            player.closeInventory();
              
            }
        


    Hats Class:
    Hats Class (open)

    Code:
    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String [] args) {
            if(!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You must be a player to execute this command!");
                return false;
            }
          
            Player player = (Player) sender;
          
            // INVENTORY 1
          
            Inventory inv = Bukkit.createInventory(null, 45, ChatColor.RED + "" + ChatColor.BOLD + "Hats");
          
            // Line 1
          
            ItemStack Purpur = nameItem(Material.PURPUR_PILLAR, ChatColor.RED + "" + ChatColor.BOLD + "Purpur Pillar Hat");
            inv.setItem(10, Purpur);
          
            ItemStack BoneBlock = nameItem(Material.BONE_BLOCK, ChatColor.RED + "" + ChatColor.BOLD + "Bone Hat");
            inv.setItem(11, BoneBlock);
          
            ItemStack Magma = nameItem(Material.MAGMA, ChatColor.RED + "" + ChatColor.BOLD + "Magma Hat");
            inv.setItem(12, Magma);
          
            ItemStack Beacon = nameItem(Material.BEACON, ChatColor.RED + "" + ChatColor.BOLD + "Beacon Hat");
            inv.setItem(13, Beacon);
          
            ItemStack Netherwart = nameItem(Material.NETHER_WART_BLOCK, ChatColor.RED + "" + ChatColor.BOLD + "Netherwart Hat");
            inv.setItem(14, Netherwart);
          
            ItemStack redNetherBrick = nameItem(Material.RED_NETHER_BRICK, ChatColor.RED + "" + ChatColor.BOLD + "Red Netherbrick Hat");
            inv.setItem(15, redNetherBrick);
          
            ItemStack PurpurBlock = nameItem(Material.PURPUR_BLOCK, ChatColor.RED + "" + ChatColor.BOLD + "Purpur Hat");
            inv.setItem(16, PurpurBlock);
          
            // Line 2
          
            ItemStack Prismarine = nameItem(Material.PRISMARINE, ChatColor.RED + "" + ChatColor.BOLD + "Prismarine Bricks Hat");
            inv.setItem(20, Prismarine);
          
            ItemStack seaLanturn = nameItem(Material.SEA_LANTERN, ChatColor.RED + "" + ChatColor.BOLD + "Sea Lanturn Hat");
            inv.setItem(21, seaLanturn);
          
            ItemStack Ice = nameItem(Material.ICE, ChatColor.RED + "" + ChatColor.BOLD + "Ice Hat");
            inv.setItem(22, Ice);
          
            ItemStack darkPrismarine = nameItem(Material.PRISMARINE, ChatColor.RED + "" + ChatColor.BOLD + "Dark Prismarine Hat");
            inv.setItem(23, darkPrismarine);
          
            ItemStack packedIce = nameItem(Material.PACKED_ICE, ChatColor.RED + "" + ChatColor.BOLD + "Packed Ice Hat");
            inv.setItem(24, packedIce);
          
            // Line 5
          
            ItemStack barrier = nameItem(Material.BARRIER, ChatColor.RED + "" + ChatColor.BOLD + "Clear Hat");
            inv.setItem(40, barrier);
          
            ItemStack next = nameItem(Material.ARROW, ChatColor.RED + "" + ChatColor.BOLD + "Next Page");
            inv.setItem(44, next);
          
            // OPEN THE INV
          
            player.openInventory(inv);
          
            return true;
        }
      
        private ItemStack nameItem(ItemStack item, String name) {
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(name);
          
            item.setItemMeta(meta);
            return item;
        }
      
        private ItemStack nameItem(Material item, String name) {
            return nameItem(new ItemStack(item), name);
        }
    
    


    Hats2 Class:
    Hats2 (open)

    Code:
    public class Hats2 implements CommandExecutor {
      
            public boolean onCommand(CommandSender sender, Command command, String commandLabel, String [] args) {
                if(!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.RED + "You must be a player to execute this command!");
                    return false;
                }
              
                Player player = (Player) sender;
              
                // INVENTORY 1
              
                Inventory inv2 = Bukkit.createInventory(null, 45, ChatColor.RED + "" + ChatColor.BOLD + "Hats");
              
                // Line 1
              
                ItemStack sapling = nameItem(Material.SAPLING, ChatColor.RED + "" + ChatColor.BOLD + "Sapling Hat");
                inv2.setItem(10, sapling);
              
                // Line 2
              
                ItemStack Prismarine = nameItem(Material.PRISMARINE, ChatColor.RED + "" + ChatColor.BOLD + "Prismarine Bricks Hat");
                inv2.setItem(20, Prismarine);
              
                // Line 5
              
                ItemStack previous = nameItem(Material.ARROW, ChatColor.RED + "" + ChatColor.BOLD + "Previous Page");
                inv2.setItem(36, previous);
              
                ItemStack barrier = nameItem(Material.BARRIER, ChatColor.RED + "" + ChatColor.BOLD + "Clear Hat");
                inv2.setItem(40, barrier);
              
                ItemStack next = nameItem(Material.ARROW, ChatColor.RED + "" + ChatColor.BOLD + "Next Page");
                inv2.setItem(44, next);
              
                // OPEN THE INV
              
                player.openInventory(inv2);
              
                return true;
            }
          
            private ItemStack nameItem(ItemStack item, String name) {
                ItemMeta meta = item.getItemMeta();
                meta.setDisplayName(name);
              
                item.setItemMeta(meta);
                return item;
            }
          
            private ItemStack nameItem(Material item, String name) {
                return nameItem(new ItemStack(item), name);
            }
    
        }
    


    Thanks for taking your time to read through and any help would be much appreciated, I am just very confused and I really want to get this to work!

    -Random
     
  2. Offline

    Tecno_Wizard

    @Random_Tomato, this is something you have to be careful about.

    You do use player#openInventory again inside of the click listener, but that can't be done inside the inventory itself because that would be modifying the inventory in a way that it is not designed to handle. To do this properly, you must create a scheduled task to open the inventory on the next tick. https://hub.spigotmc.org/javadocs/spigot/org/bukkit/scheduler/BukkitScheduler.html

    You can access the scheduler by calling JavaPlugin#getServer()#getScheduler()
     
  3. Offline

    Lordloss

    @Tecno_Wizard I dont know why you think this makes problems, i did it dozens of times in different plugins, and it worked pretty every time. No need for a delay here.

    The issue lays here:

    Code:
            // if(item.getType() == Material.ARROW) {
            //    player.openInventory(inv2);
            // }
            // MENU 2
        
            if(item.getType() == Material.SAPLING) {
                player.sendMessage(ChatColor.RED + "You equipped the hat");
                inventory.setHelmet(sapling);
            }
            //    if(item.getType() == Material.REDSTONE_TORCH_ON) {
            //        player.sendMessage(ChatColor.RED + "You equipped the hat");
            //        inventory.setHelmet(redstoneTorch); 
            //    }
            
            //    if(item.getType() == Material.LEAVES) {
            //        player.sendMessage(ChatColor.RED + "You equipped the hat");
            //        inventory.setHelmet(leaves);
            //    }
            event.setCancelled(true);
            player.closeInventory();
    You close the inventory right after opening it.
     
  4. Offline

    Tecno_Wizard

    @Lordloss, straight from the javadocs.

    Javadoc entry (open)

    Because InventoryClickEvent occurs within a modification of the Inventory, not all Inventory related methods are safe to use.

    The following should never be invoked by an EventHandler for InventoryClickEvent using the HumanEntity or InventoryView associated with this event:

    To invoke one of these methods, schedule a task using BukkitScheduler.runTask(Plugin, Runnable), which will run the task on the next tick. Also be aware that this is not an exhaustive list, and other methods could potentially create issues as well.
     
    I Al Istannen likes this.
Thread Status:
Not open for further replies.

Share This Page