Remove item when only 1 is left

Discussion in 'Plugin Development' started by Zeryther, Dec 14, 2014.

Thread Status:
Not open for further replies.
  1. Code:
    @EventHandler
       public void onEatFood(PlayerInteractEvent e){
         Player p = e.getPlayer();
         Material m = e.getPlayer().getItemInHand().getType();
         double health = ((Damageable)p).getHealth();
         double maxhealth = ((Damageable)p).getMaxHealth();
        
         if(m == Material.BREAD){
           if(maxhealth > health + 5){
             p.setHealth(health + 5);
           } else {
             p.setHealth(maxhealth);
           }
          
           if(p.getItemInHand().getAmount() == 1){
             p.getInventory().remove(Material.BREAD);
           } else {
             p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
             p.updateInventory();
           }
         }
       }
    
    Everything works fine but if 1 piece of bread is left, the user gets healed but the bread isn't removing. Any help?

    - Zeryther
     
  2. Offline

    Skionz

    @Zeryther Try changing it to less then or equal to 1 and remove the item in their hand.
     
  3. @Skionz
    You mean like this? Still doesn't work..
    Code:
    @EventHandler
       public void onEatFood(PlayerInteractEvent e){
         Player p = e.getPlayer();
         Material m = e.getPlayer().getItemInHand().getType();
         double health = ((Damageable)p).getHealth();
         double maxhealth = ((Damageable)p).getMaxHealth();
         
         if(m == Material.BREAD){
           if(maxhealth > health + 5){
             p.setHealth(health + 5);
           } else {
             p.setHealth(maxhealth);
           }
           
           if(p.getItemInHand().getAmount() == 1){
             p.getItemInHand().setAmount(1);
             p.getInventory().remove(Material.BREAD);
           } else {
             p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
             p.updateInventory();
           }
         }
       }
    
     
  4. Offline

    Skionz

    @Zeryther No I mean check if the amount is less then or equal to 0 and then remove the entire ItemStack from their hand.
     
Thread Status:
Not open for further replies.

Share This Page