eating glistering melons

Discussion in 'Plugin Development' started by monkeymanboy, Jul 21, 2013.

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

    monkeymanboy

    I got it all set up how I want it so that it will give you some hearts and none of your hunger bar but my one problem is that it does not consume the glistering melon heres the code
    Code:java
    1. @EventHandler
    2. public void OnPlayerSoup(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. if(player.getHealth() == 20){
    5. }else{
    6. int smelon = +4;
    7. if((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() ==Action.RIGHT_CLICK_BLOCK) && player.getItemInHand().getType() == Material.SPECKLED_MELON){
    8. player.setHealth(player.getHealth() + smelon > player.getMaxHealth() ? player.getMaxHealth() : player.getHealth() + smelon);
    9. event.getPlayer().getItemInHand().setType(Material.AIR);
     
  2. Offline

    ERROR372

    monkeymanboy

    Does anything happen at all? Do you get the hearts?
     
  3. Offline

    monkeymanboy

    yes
     
  4. Offline

    ERROR372

    monkeymanboy

    what if you do:

    player.setItemInHand(null);

    Does that remove the melon from your hand?
     
  5. Offline

    savagesun

    Replace
    Code:
    event.getPlayer().getItemInHand().setType(Material.AIR);
    with
    Code:
    ItemStack melon = event.getPlayer().getItemInHand();
            melon.setAmount(1);
            event.getPlayer().getInventory().removeItem(melon);
            event.getPlayer().updateInventory();
     
  6. Offline

    monkeymanboy

    yes but if it is a stack at all then it won't get rid of it or even restore the hearts
     
  7. Offline

    ERROR372

    monkeymanboy

    you should replace player.getItemInHand().setType(Material.AIR) with player.setItemInHand(null)

    That will get rid of it, and it's after you've given the player the hearts.

    And later, when you want to handle stacked glistering melons, you decrement the itemstack amount, and if the number prior to the decrement is 1, then do player.setItemInHand(null)
     
  8. Offline

    monkeymanboy

    ok its all working but one more problem it will consume all stacked in my hand
     
  9. Offline

    ERROR372

    monkeymanboy

    Using that code:

    ItemStack melon = event.getPlayer().getItemInHand();
    if(melon.getAmount() > 1)
    {
    melon.setAmount(melon.getAmount()--);​
    event.getPlayer().updateInventory();​
    }
    else
    {
    event.getPlayer().getInventory().removeItem(melon);​
    event.getPlayer().updateInventory();​
    }

    I still think making an ItemStack is unnecessary. Could potentially cause problems later on...
     
  10. Offline

    monkeymanboy

    ok its fine now just one small thing and if theres no easy solution i don't mind much if its a stack of 1 and you have another stack more than one it will consume one from the multiple stack one
     
  11. Offline

    savagesun

    Code:
    ItemStack melon = event.getPlayer().getItemInHand().clone();
            melon.setAmount(1);
            event.getPlayer().getInventory().removeItem(melon);
            event.getPlayer().updateInventory();
    That should work fine.
     
Thread Status:
Not open for further replies.

Share This Page