Button instance

Discussion in 'Spigot Plugin Development' started by Hex_27, May 24, 2017.

  1. I've made my button itemstack in this GUI
    Code (Text):
    public void openCraftingWindow(Player p){
            Inventory inv = Bukkit.createInventory(null, 9, "Crafting Table");
         
            Button b1 = new Button(Material.WORKBENCH, 1, (byte) 0,
                    ChatColor.AQUA + "Open crafting grid",
                    new Runnable(){
                        public void run(){
                            openNormalCraftingWindow(p);
                        }
                    },
                    ChatColor.YELLOW + "Opens normal crafting",
                    ChatColor.YELLOW + "GUI");
         
            Button b2 = new Button(Material.IRON_CHESTPLATE, 1, (byte) 0,
                    ChatColor.AQUA + "Open Equipment Crafting",
                    new Runnable(){
                        public void run(){
                            openArmoury(p);
                        }
                    },
                    ChatColor.YELLOW + "Craft armour and weapons");
         
            inv.addItem(b1);
            inv.addItem(b2);
            p.openInventory(inv);
         
        }
    But the debug logging in this event below only shows "1" when I click my custom itemstack
    Code (Text):
    @EventHandler
        public void onInteract(InventoryClickEvent event){
            if(event.getCurrentItem() != null){
                Bukkit.getLogger().info("1");
                if(event.getCurrentItem() instanceof Button){
                    Bukkit.getLogger().info("2");
                    event.setCancelled(true);
                    ((Button) event.getCurrentItem()).run();
                }
            }
        }
    Why?
     
  2. you should add some lambda to that ;)
    also, you can just add metadata to the items and use that, but i dont believe the items store whether or not they're instances of some item.
     
  3. That not gonna work, use a Custom InventoryHolder.
     
  4. I believe your 'Button' class doesn't extend the ItemStack class, meaning your Button class isn't an instanceof the ItemStack class.
    Code (Text):
    if(event.getCurrentItem() instanceof Button)
    Assuming your button class is encapsulated you could match ItemStacks or ItemMeta information.
    Obviously you need some place to store the instance of that specific Button.
     
  5. It is an extends of Itemstack, but according to the rest on this topic, it seems like that won't work. I guess I'll just put use hashmaps...


    Lambda?
     
  6. What's an InventoryHolder? I don't quite get what it does by looking at the api
     
  7. Basically when you create an Inventory the first parameter Bukkit ask you is the InventoryHolder, most of the times you simply let it null, but you can actually make good use of it because you can use your own InventoryHolder, this means in that class you can store anything and access it on the InventoryClickEvent. I actually use that on my API, here is the Source.
     
  8. An InventoryHolder is any entity (Both tile or living) that have an inventory. Player, dropper, hopper, chests and chested horses are inventory holders. Can be useful when you don't need a specific container, so you can generalize casting the block or the entity to InventoryHolder.
     
  9. You could store metadata to the buttons and check for the metadata to see if it's your buttons or not. Then you won't need to store their instances anywhere.