Solved Item in hand not working!

Discussion in 'Plugin Development' started by superchris05326, Oct 18, 2014.

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

    superchris05326

    I want a player to right click a sign and it will take the potato out of their inventory. This works but the only problem is that if the potato is in their hand when they right click, it doesn't get removed. To prevent such an error, I want to prevent right clicking the sign with a potato in their hand. But for some reason my code doesn't work. Is their a way I could allow it to be removed while its in their hand, or do you know of a fix for the item in hand problem. BTW Everything is working fine its just not detecting that I have a potato in my hand.
    Code:java
    1. ItemStack po = new ItemStack(Material.POTATO_ITEM);
    2. if(hasGItem(p)){
    3. //has item
    4. if(!(p.getInventory().getItemInHand().equals(po))){
    5. //potato is not in hand
    6. removeGItem(p);
    7. QuestKillPlayer.toBeKilled.remove(p.getName());
    8. p.sendMessage(ChatColor.GREEN + "You have been forgiven!");
    9. }else{
    10. //potato is in hand
    11. p.sendMessage(ChatColor.RED + "You cannot have the Potato in your hand!");
    12. }
    13.  
    14. }else{
    15. //doesnt have item
    16. p.sendMessage(ChatColor.RED + "You must have a Potato to recieve Forgivness!");
    17. }


    And here is the hasGItem and removeGItem methods:
    Code:java
    1. public boolean hasGItem(Player p){
    2. return p.getInventory().contains(Material.POTATO_ITEM);
    3. }
    4.  
    5. public void removeGItem(Player p){
    6. Inventory inv = p.getInventory();
    7. inv.removeItem(new ItemStack(Material.POTATO_ITEM, 1));
    8. p.updateInventory();
    9. }
     
Thread Status:
Not open for further replies.

Share This Page