0

I am making a bukkit plugin with a sign shop that opens a static inventory and you click to get kits. I have gotten the inventory set up, it opens when the sign is clicked, and the items I want are in there. However, when I click in the inventory, the first if statement returns false. Here is my code:

@EventHandler
    public void onInventoryClick(InventoryClickEvent e){
        if (e.getInventory() == kitInvent){
            Player player = (Player) e.getWhoClicked();
            player.sendMessage("Kit Shop Opened");

            if (e.getCurrentItem().getItemMeta().getDisplayName() == ChatColor.DARK_PURPLE + "Grenadier Kit"){
                e.setCancelled(true);
                player.getInventory().clear();
                Potion potion = new Potion(PotionType.INSTANT_DAMAGE, 1);
                potion.setSplash(true);
                ItemStack grenadeStack = new ItemStack(Material.POTION, 32);


                potion.apply(grenadeStack);
                player.getInventory().addItem(grenadeStack);
            }
        }
    }

It can't get past the "if (e.getInventory() == kitInvent)" part because when I comment it out, it sends the message "Kit Shop Opened".

Also, am I doing the next if statement correctly, where it checks the item's title? I was having issues there too, but I didn't know if it was simply because of the previous if statement.

Any help is greatly appreciated!

Ty Brantner
  • 85
  • 1
  • 7
  • 1
    We need to know what `kitInvent` is and what you do to it. Please update your code. – bw2801 Aug 15 '13 at 15:01
  • kitInvent is the inventory I created to hold the kit objects, which you click to choose. It's able to be opened just fine when the sign says what it's supposed to, but then I have the problem above. I put a single kit in it, which displays fine. This has to be the problem code. – Ty Brantner Aug 26 '13 at 22:29

3 Answers3

2

The opened inventory will never be truly equal to the inventory "kitInvent" because == checks for other things than the name, items inside. There are many ways you can check if the inventory is the "same" as kitInvent. ("same" as in what you are WANTING to do, check if the player is in the right inventory)

Some ways you can check if the inventory is the one you want is to check if the inventory name is the same. Although that is the general check, you can also check items in the inventory and item names– but sticking with the name is good enough. (Especially if you are using ChatColors, because chests can be renamed!) Also, for extra checks, use equalsIgnoreCase().

if(e.getInventory().getName().equalsIgnoreCase(kitInvent.getName()))

Lastly, a quote taken from this thread about == vs .equals() tells you that equals() is the way to go if you only want to check ONE thing, nothing else.

equals will only compare what it is written to compare, no more, no less.

Wish you luck on getting through your project.

Community
  • 1
  • 1
0xA
  • 132
  • 1
  • 10
  • You realize this question is over 3 years old? The Bukkit API is dying at the same time. The answer is appreciated however. – Xorifelse Nov 17 '16 at 02:44
  • Thanks for letting me notice that; was the first question to pop up in what I should answer. Will check next time. – 0xA Nov 17 '16 at 02:45
0

You should use if (e.getInventory().equals(kitInvent)) instead of ==. .equals looks for identical values rather than the reference.

As for the next if statement, I also recommend doing .equals, since it's considered bad coding practice to use == when comparing strings.

beechboy2000
  • 151
  • 6
0

You can do if(e.getInventory().getName().equalsIgnoreCase("the name of the inventory you want to do stuff with")) this is going to return true if the name of the inventory is equals to the name of the inventory of the event !

ItzAndree
  • 21
  • 4