ItemStack check

Discussion in 'Spigot Plugin Development' started by EscargotRouge, Feb 14, 2018 at 6:12 PM.

  1. Hey, I have done this code, but it doesn't works, when I right clic a block, even with my hand, it send me "yop" and not "nop":
    Code (Text):
    package me.salut;

    import org.bukkit.Material;
    import org.bukkit.block.BlockFace;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;

    public class PluginListener implements Listener {

        @EventHandler
        public void onBreakBlock(PlayerInteractEvent e){
            ItemStack houe = new ItemStack(Material.DIAMOND_HOE);
            Player p = e.getPlayer();
            if(e.getBlockFace()==BlockFace.UP){
                if(e.getAction()==Action.RIGHT_CLICK_BLOCK){
                    if(p.getItemInHand()==houe){
                        p.sendMessage("yop");
                    }
                    else{
                        p.sendMessage("nop");
                    }
                }
            }
            }
        }
     
    Thank you very much
    Nathan
     
  2. It will not work. You can use:
    Code (Java):
    if(p.getItemInHand().getType() == Material.DIAMOND_HOE){
    EDIT: After 1.8, use p.getInventory().getItemInMainHand()
     
    #2 Feeps, Feb 14, 2018 at 6:16 PM
    Last edited: Feb 14, 2018 at 6:27 PM
    • Like Like x 1
  3. Don't compare objects with "==". Also, why is your method called "onBlockBreak" but is it actually a PlayerInteractListener? Kind of confused me for a second.

    EDIT: What version of Spigot are you working with? .getItemInHand() is deprecated since 1.9.
     
    • Like Like x 1
  4. Oh thank you very much :D


    Because I have tried multiples events...
    Thank you :D
     
  5. That's always not working...

    Code (Text):
    package me.salut;

    import org.bukkit.Material;
    import org.bukkit.block.BlockFace;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;

    public class PluginListener implements Listener {

        @EventHandler
        public void onBreakBlock(PlayerInteractEvent e){
            Player p = e.getPlayer();
            if(e.getBlockFace()==BlockFace.UP){
                if(e.getAction()==Action.RIGHT_CLICK_BLOCK){
                    if(p.getItemInHand().getType() == Material.DIAMOND_HOE){
                        p.sendMessage("yop");
                    }
                    else{
                        p.sendMessage("nop");
                    }
                }
            }
            }
        }
     
     
  6. Once again, what version of Spigot are you working with currently?
     
    • Like Like x 1
  7. I'm using 1.12, but I don't know how to use the getItemInMainHand()
    Sorry if i'm very bad in Java, I'm just trying to make a very simple plugin for my server -_-
    Thank you very much
    Nathan
     
  8. You'll want to get the Player's inventory first, then access the getItemInMainHand() method.
    HumanEntity (= Player) method documentation: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/HumanEntity.html#getInventory--
    PlayerInventory interface: https://hub.spigotmc.org/javadocs/s...tory/PlayerInventory.html#getItemInMainHand--
     
  9. Code (Text):
    package me.salut;

    import org.bukkit.Material;
    import org.bukkit.block.BlockFace;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;

    public class PluginListener implements Listener {

        @EventHandler
        public void onBreakBlock(PlayerInteractEvent e){
            Player p = e.getPlayer();
            if(e.getBlockFace()==BlockFace.UP){
                if(e.getAction()==Action.RIGHT_CLICK_BLOCK){
                    if(p.getInventory().getItemInMainHand().getType() == Material.DIAMOND_HOE){
                        p.sendMessage("yop");
                    }
                    else{
                        p.sendMessage("nop");
                    }
                }
            }
            }
        }
     

    Thank you very much, but this is not working...
    Nathan :)
     
  10. Try adding some debug print messages, see which of the if-statements results to false or true where you don't expect it. Also print some of the contents of the item in the main hand and its material and such.
     
  11. OMG... I'm so stupid... I just relaunched my Eclipse 30 mins ago and it changed the default plugin folder, so it was not the good one..
    And so that's working now...
    I'm so sorry
     
  12. But thank you very very very much for your time ;)
     
  13. Not contributing to discussion, but

    houe
     
    • Funny Funny x 1
  14. Use this to detect the item and target item of your selected item!
    PD (if you add more items you do not know else if it generates an error)

    Code (Text):
    if(currentItem.getType() == Material.TRAPPED_CHEST && currentItem.getItemMeta().getDisplayName().equals("┬žePlayer Alive")){
                        e.setCancelled(true);
                        none.menuAlive(p);
                    }
     
  15. Also wouldn't hurt to null-check the item with which you click.
     

Share This Page