Solved Having trouble with PlayerItemHeldEvent

Discussion in 'Plugin Development' started by treestompz, Feb 16, 2013.

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

    treestompz

    Hello Everyone,

    I am trying to make a plugin that will modify player's velocity based on whether a rose or dandelion is being held in the player's hotbar.

    Code:
    @EventHandler
            public void dodge(PlayerItemHeldEvent event)
            {
                Player p = event.getPlayer();
           
                if(p.getItemInHand().getTypeId() == 37)
                {
                    Vector v = p.getLocation().getDirection().multiply(0).setX(-3);
                    p.setVelocity(v);
                   
                }
               
                if(p.getItemInHand().getTypeId() == 38)
                {
                    Vector v = p.getLocation().getDirection().multiply(0).setX(3);
                    p.setVelocity(v);
                }
            }
    The code above doesn't modify the velocity when the rose/dandelion is selected, but rather when it is unselected. Does anyone know why and how I can resolve this?

    Thanks in advance!
     
  2. Offline

    Digi

    Because the event is called before player.getItemInHand() is updated, you need to get the item from the event.

    Also, if you want to change player's walking speed there's setWalkSpeed(), just saying.
     
  3. Offline

    treestompz

    So should I use event.getNewSlot()?
     
  4. Offline

    Digi

    Yes, then get the player's item on that slot... but mind that it may be null, so don't directly use getTypeId() on it, first check against null.
     
  5. Offline

    treestompz

    Erm but you can't get the item ID from event.getNewSlot()?
     
  6. Offline

    Digi

    treestompz
    Code:
    ItemStack item = event.getPlayer().getInventory().getItem(event.getNewSlot());
    
    if(item != null)
    {
        switch(item.getType())
        {
             case RED_ROSE:
             {
                  // ... 
                  break;
             }
    
             case YELLOW_FLOWER:
             {
                  // ... 
                  break;
             }
        }
    }
     
  7. Offline

    treestompz

Thread Status:
Not open for further replies.

Share This Page