Checking if a player is holding an item?

Discussion in 'Plugin Development' started by SeamusMcFreak, Feb 6, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    SeamusMcFreak

    Hello, I am trying to make a pickaxe which gives you speed without using an attribute.
    Code:
    @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent e){
            Player p = e.getPlayer();
            if(p.getItemInHand().getItemMeta().getLore().contains("Speed I")){
                PotionEffect speedI = new PotionEffect(PotionEffectType.SPEED, 100, 1, false);
                p.addPotionEffect(speedI);
            }
        }
    Problem: When the player holds the item, they have to either right click or left click to run the event. How can I check if they're holding it?
     
  2. Offline

    Xerox262

    Use ItemStack != null to ensure there's an item in hand, then use ItemStack#getType() == Material to see if it's the correct item.
     
  3. Offline

    SeamusMcFreak

    I don't want to check if its a certain item. I just want to check the lore.
     
  4. Offline

    teej107

    PlayerItemHeldEvent (something like that)
     
  5. Offline

    [ HashMap ]

    if(player.getItemInHand != null)
    {

    }

    or:

    if(player.getItemInHand.getType != Material.AIR)
    {

    }
     
  6. Offline

    q8minecraft

    @SeamusMcFreak You can simply get the item's lore and compare it to the lore you want.
     
  7. Offline

    Xerox262

    @SeamusMcFreak
    ItemStack != null // Make sure they have an item or it will throw null pointer trying everything else
    ItemStack#hasItemMeta(); // Check that it has either name or lore, no point getting it if it doesn't
    ItemStack#getItemMeta(); // Get the item meta to get the lore
    ItemMeta#getLore() != null // Make sure it has lore
    List<String>#contains(); // Check if the lore contains what you want
     
  8. Offline

    SeamusMcFreak

    Ok, but what event do I use? Ive tried PlayerItemHeldEvent and PlayerInteractEvent.
     
  9. Offline

    Xerox262

    @SeamusMcFreak It depends what you're using it for, for the speed you could use PlayerItemHeldEvent, then it works when they change from a different item to that item.
     
  10. Offline

    SeamusMcFreak

    I would like the if statement to run when a player is holding the item. But I have no clue which event I use for it.
     
  11. Offline

    Xerox262

    @SeamusMcFreak That is PlayerItemHeldEvent. When you change from say slot 1 to slot 2 it runs it.
     
  12. Offline

    teej107

    Use the PlayerItemHeldEvent.

    EDIT: Ninja'd
     
  13. Offline

    SeamusMcFreak

    IK. I want the if to run when the player is HOLDING the item. Not changes slots from it.
     
  14. Offline

    Xerox262

    @SeamusMcFreak There's no event for that, use a bukkit runnable in your on enable, loop through all players online, check the item in their hand and then run your code.
     
  15. Offline

    SeamusMcFreak

    So I use this?
    Code:
    @Override
    public void run(){
    // if stuff
    }
    And will it increase server lag if I use this method?
     
  16. Offline

    teej107

    When the player logs on, check the item in their hand. Then use the PlayerItemHeldEvent because that event gets triggered when a player changes items being held. (You may have to listen to an InventoryClickEvent too)
     
  17. Offline

    Xerox262

  18. Offline

    SeamusMcFreak

    For some reason it doesn't do anything? Do you know what the problem is?
    Code:
    Code:
    public void run(){
            Player p = (Player) Bukkit.getOnlinePlayers();
            ItemStack item = p.getItemInHand();
            ItemMeta im = p.getItemInHand().getItemMeta();
            List<String> lore = im.getLore();
            if(item != null){
                if(item.hasItemMeta()){
                    if(im.getLore() != null){
                        if(lore.contains("Speed I")){
                            PotionEffect speed = new PotionEffect(PotionEffectType.SPEED, 160, 1, false);
                            p.addPotionEffect(speed);
                        }
                    }
                }
            }
     
  19. Offline

    Xerox262

    Player p = (Player) Bukkit.getOnlinePlayers();
    That should be a for loop, you're casing a List of players to a single player, that's not what you want.

    Also post more of your onEnable, you might have forgot to schedule it.
     
  20. Offline

    SeamusMcFreak

    onEnable:
    Code:
    public void onEnable(){
            registerEvents();
            registerCommands();
        }
     
  21. Offline

    Xerox262

    @SeamusMcFreak You want a bukkit runnable, not just a run method. Something like so

    Code:
    public void onEnable() {
           // What you currently have
           new BukkitRunnable() {
                  @Override
                   public void run() {
                         // Stuff in here.
                   }
            }
    }.runTaskTimer(this, 0, 100);
    You might want to change around those numbers, the first number is how long before it starts running it, only affects it once, the second number is the buffer between each run, remember that if you use a high number then some players will have to wait to get their affects, for example.

    tick 0 It starts, next loop at tick 100
    someone equips the items right after tick 0, tick 1, they have to wait all the way to tick 100 just to get their affects.
     
  22. Offline

    SeamusMcFreak

    You where saying something about using a for loop instead of a variable? If so, how would I do that?
     
  23. Offline

    Xerox262

  24. Offline

    mcdorli

    1.: Item can be null, if the player doesn't hold one. Also 2.: itemmeta and lore both can be null too.

    You need to check for itemHeldEvent, if the player chooses your pickaxe, put him in an arraylist, if he doesn't remove him.


    @Xerox262 @q8minecraft @[ HashMap ] Guys, at least read what he says before posting.
     
  25. Offline

    q8minecraft

    @mcdorli
    As mentioned, he's asking about lores, and my message contained a solution.
     
  26. Offline

    mcdorli

    @q8minecraft
     
  27. Offline

    Xerox262

    We've already established what he wants, he doesn't care about checking when they equip it, he just wants it so that as long as they have the item in their hand it will run the code. Hence BukkitRunnable, then check if the item is null, if it has lore, then checking said lore.
     
Thread Status:
Not open for further replies.

Share This Page