InventoryClickEvent doesn't react on other inventories..

Discussion in 'Spigot Plugin Development' started by FroZenKeyboard, May 28, 2017.

  1. This is my clickevent, when i click a block in the inventory name called "§9Blokker" it doesn't react.. What am i doing wrong?

    Code (Text):
        @EventHandler
        public void onInventory(InventoryClickEvent e) {
            Player p = (Player)e.getWhoClicked();
            if(e.getInventory().getName().equalsIgnoreCase("§6§lStartside") && e.getCurrentItem().getType() == Material.SANDSTONE) {
                p.openInventory(ShopEvent.blokker);
                ShopEvent.loadInventoryes();
            }else if(e.getInventory().getName().equalsIgnoreCase("§6§lStartside") && e.getCurrentItem().getType() == Material.GOLD_SWORD) {
                p.openInventory(ShopEvent.våpen);
                ShopEvent.loadInventoryes();
               
                }else if(e.getInventory().getName().equalsIgnoreCase("§9Blokker") && e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§bSandstein")) {
                    p.sendMessage("a"); // Doesn't react here...
                   
                }
            }
     
  2. Don't use the '§' symbol. Use the ChatColor enums. Also, check the names are what you think they are.


    Sent from my iPhone using Tapatalk
     
  3. As already said, try using ChatColor instead of color codes. If that doesn't work, you can add a debug message, that tells you the name of the inventory and the name of the item, before the check. Maybe the inventory name or item name is not how you want it to be.
    Code (Java):
    @EventHandler
    public void onInventory(InventoryClickEvent e) {
        Player p = (Player)e.getWhoClicked();

        // Debug messages
        p.sendMessage("Inventory Name: " + e.getInventory().getName());
        p.sendMessage("Current Item's Name: " + e.getCurrentItem().getItemMeta().getDisplayName());

        ...
    }
     
  4. Debug messages doesn't show at the "§9Blokker" inventory. The inventory name is right, so i don't understand what the fuck is wrong...
     
  5. Maybe the item's display name is wrong, so e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§bSandstein") is false, thus the entire if statement is false?
     
  6. They can also just ChatColor.stripColor(coloredName).equals("Name")
     
  7. Or just straight up compare inventories.


    Sent from my iPhone using Tapatalk
     
  8. I don't think the "§" is the error. Cause it works for the inventory name : "§6§lStartside".
     
  9. So, do as suggested and use the ChatColor enums. You can also compare inventories directly, or do as @ExpDev suggested and use ChatColor#stripColor


    Sent from my iPhone using Tapatalk
     
  10. Still need help with this, bump.
     
  11. Follow what I suggested. Show us what you used if you did.
     
  12. How do i compare inventories?
     
  13. ==
     
  14. never use == for objects. == is checking if the objects are stored at the same memory address while equals() will compare the two objects.
     
  15. So now what i've tried is:

    Code (Text):
        @EventHandler
        public void onInventory(InventoryClickEvent e) {
            Player p = (Player)e.getWhoClicked();
            p.sendMessage("Inventory Name: " + e.getInventory().getName());
            p.sendMessage("Current Item's Name: " + e.getCurrentItem().getItemMeta().getDisplayName());
            if(e.getInventory().getName().equalsIgnoreCase("§6§lStartside") && e.getCurrentItem().getType() == Material.SANDSTONE) {
                p.openInventory(ShopEvent.blokker);
                ShopEvent.loadInventoryes();
            }else if(e.getInventory().getName().equalsIgnoreCase("§6§lStartside") && e.getCurrentItem().getType() == Material.GOLD_SWORD) {
                p.openInventory(ShopEvent.våpen);
                ShopEvent.loadInventoryes();
            }else if(e.getInventory() == ShopEvent.våpen && e.getCurrentItem().getType() == Material.GOLD_SWORD) {
                p.openInventory(ShopEvent.våpen);
                ShopEvent.loadInventoryes();
            }
        }
    }
    What i don't understand is, why is the "Startside" inventory working. But not the others..
     
  16. If you're comparing the objects I'm not sure if I would use the == operator (used to compare enums or memory locations). equals() might favor you more, or use instanceof for the holder.
     
  17. Exactly, and in this case you want to check to see if they are stored in the same location in memory?
     
  18. When you open the GUI, I presume you open a stored Inventory instance. When you compare the two, check if the opened GUI == the instance that you opened.
     
  19. I'm not entirely sure if bukkit passes everything by reference but == can return strange things. Just use .equals() because bukkit will handle the check and you're sure it will give the right response
     
  20.