0
\$\begingroup\$

How can I check if a players held item is of a specific item type - for example an axe. (can be iron axe, gold axe, wooden axe, etc.)?

Or do I have to do multiple if statements to have this done?

@EventHandler(priority= EventPriority.HIGH)
    public void onInteract(PlayerInteractEvent event){
        Player p = event.getPlayer();
        ItemStack heldItem = p.getInventory().getItemInMainHand();

        if(heldItem.getType() == Material.DIAMOND_AXE){ //I dont want to do multiple if statements for multiple type of Axes
            //...
        } //other if statement for Material.WOODEN_AXE etc.
    }
\$\endgroup\$
2
  • \$\begingroup\$ Assuming there isn't a Material.IsAxe(m) or something, you can avoid multiple if statements by using the "or" operator ( || ) and comparing between them. The if statement will base so long as ONE of the cases is true. So for example: Material m = heldItem.getType(); if(m == Material.DIAMOND_AXE || m == Material.WOODEN_AXE || m == Material.GOLD_AXE || ...){ /* It is one of the axe types */ } I however don't know enough about Minecraft programming to know whether this is the best solution. It is merely ~a~ solution. \$\endgroup\$
    – Liam Lime
    Apr 23, 2016 at 18:09
  • \$\begingroup\$ Thanks Liam. I checked, theres a method called "isSimilar". I look into it, how it works. The Method with the OR statement I knew, but thanks you anyways for posting. :) \$\endgroup\$
    – paskl
    Apr 23, 2016 at 18:22

3 Answers 3

1
\$\begingroup\$

The cleanest method would be to check if m.toString() contains the "_AXE" string in it. Same with swords ("_SWORD"), and every tool.

\$\endgroup\$
0
0
\$\begingroup\$

I ended up using this

if (
            //Axe
                m == Material.DIAMOND_AXE
                        || m == Material.GOLD_AXE
                        || m == Material.IRON_AXE
                        || m == Material.STONE_AXE
                        || m == Material.WOOD_AXE

                        //Pickaxe
                        || m == Material.DIAMOND_PICKAXE
                        || m == Material.GOLD_PICKAXE
                        || m == Material.IRON_PICKAXE
                        || m == Material.STONE_PICKAXE
                        || m == Material.WOOD_PICKAXE

                        //Hoe
                        || m == Material.DIAMOND_HOE
                        || m == Material.GOLD_HOE
                        || m == Material.IRON_HOE
                        || m == Material.STONE_HOE
                        || m == Material.WOOD_HOE

                        //Spade
                        || m == Material.DIAMOND_SPADE
                        || m == Material.GOLD_SPADE
                        || m == Material.IRON_SPADE
                        || m == Material.STONE_SPADE
                        || m == Material.WOOD_SPADE

                        //Sword
                        || m == Material.DIAMOND_SWORD
                        || m == Material.GOLD_SWORD
                        || m == Material.IRON_SWORD
                        || m == Material.STONE_SWORD
                        || m == Material.WOOD_SWORD
                ) {


            //...
        }

But I guess theres cleaner code somewhere out there..

\$\endgroup\$
1
  • \$\begingroup\$ I saw a question on the bukkit.org forums, wich was very close to this. I post the same here whaf I posted there \$\endgroup\$
    – Bálint
    Apr 23, 2016 at 21:04
-1
\$\begingroup\$

I think you can use this:

if(heldItem.getItem() instanceof ItemAxe){
//do something
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .