Deleting food in hand + healing the player [SOLVED]

Discussion in 'Plugin Development' started by Limeth, Aug 5, 2012.

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

    Limeth

    Hai, at first let me show you my code, so you know what is going on.
    Code:java
    1.  
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    4. if(event.getPlayer().getInventory().getItemInHand().getType() == Material.COOKED_BEEF){
    5. if(event.getPlayer().getInventory().getItemInHand().getAmount() > 1) {
    6. event.getPlayer().getInventory().getItemInHand().setAmount(event.getPlayer().getInventory().getItemInHand().getAmount() - 1);
    7. } else {
    8. event.getPlayer().getInventory().setItemInHand(new ItemStack(Material.AIR, 0));
    9. }
    10. if(event.getPlayer().getHealth() >= 12) {
    11. event.getPlayer().setHealth(20);
    12. } else {
    13. event.getPlayer().setHealth(event.getPlayer().getHealth() + 8);
    14. }
    15. }
    16. }
    17. }
    18.  

    So the problem I'm having is, that the player can eat for example 4 steak until he gets to the last one, which he just won't eat. I'm asking you for help.

    -----------------------------------------------------------------------------------------------
    OLD POST:

    Hello there, It seems like this code won't get rid of the item in hand that the player is holding. Also I'm kinda math-blind at the moment so if you could tell me how to get it so the food does not heal the player more than the max health is. (20)


    Code:
    if(Action.RIGHT_CLICK_AIR != null || Action.RIGHT_CLICK_BLOCK != null){
    if(event.getPlayer().getInventory().getItemInHand().getType() == Material.COOKED_BEEF){
    event.getPlayer().getInventory().getItemInHand().setAmount(0);
    if(event.getPlayer().getHealth()>=12) {
    event.getPlayer().setHealth(20);
    } else {
    event.getPlayer().setHealth(event.getPlayer().getHealth() + 8);
    }
    }
    }
    + If you could tell me how to code that the "hunger meter" stays at 15 for all the time.

    Thank you <3

    Deleting food: Solved!
    Solving the problem with max food: Solved!
    Hunger meter stays at 15: Solved!
     
  2. Offline

    marwzoor

    Limeth
    I think this should work:
    Code:
    player.getInventory().getItemInHand().setAmount(0);
    
     
  3. Offline

    Limeth

    marwzoor
    Thank you. Now I only need the proper healing - You cant set more health than the max health is.
    When you eat the steak, you get 8 more half-hearts, but it throws an error, because the health was for example 18.
    18 + 8 > 20

    + If you could tell me how to code that the "hunger meter" stays at 15 for all the time.
     
  4. Offline

    marwzoor

    Limeth
    Just do it like this:
    Code:
    if(event.getPlayer().getHealth()>=12)
    {
    event.getPlayer().setHealth(20);
    }
    else
    {
    event.getPlayer().setHealth(event.getPlayer().getHealth() + 8)
    }
    
    And for the hunger thingy, I think there is a hungerchangeevent, just do cancelevent(true)
     
  5. Offline

    Limeth

    Thanks! Seems like Im still having problem with deleting the food. It still doesnt work. I updated the code in the first post.
     
  6. Offline

    bob9

    you might have to set the item in hand to "air" if the amount is zero.
     
  7. Offline

    Limeth

    This is what I did: event.getPlayer().getInventory().setItemInHand(new ItemStack(Material.AIR, 0));
    and it doesn't work.
     
  8. Offline

    ienze

    I using this: player.getInventory().removeItem(player.getInventory().getItemInHand());
     
  9. Offline

    skore87

    Thats because an empty slot isn't Air, it's null. using ienze's solution should work, and setting the amount to 0 should also work if I'm not mistaken.
     
  10. Offline

    Limeth

    ienze
    Thanks for the reply.
    The problem is: Imagine 4 Steak in one slot. When the player tries to eat it (right click) then the whole stack disappears. I need to decrease the amount.

    My bad, I didn't say that there is more steak ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  11. Offline

    skore87

    There is a getAmount method that you can use.
     
  12. Offline

    XbannisherX

    you need to think like the computer,
    Code:
    Player p = event.getPlayer();
    int amount = event.getPlayer().getItemInHand().getAmount();
    p.getItemInHand().setAmount(amount)-1);

    something like this idk if im typing good, on my cellphone right now...
     
  13. Offline

    Limeth

    Seems like
    Code:
    event.getPlayer().getInventory().getItemInHand().setAmount(event.getPlayer().getInventory().getItemInHand().getAmount() - 1);
    Works fine for decreasing the amount from 4 to 1 by clicking 3 times, the last one won't get away.
    I'll tell you when I find something or when I fail.

    This is my current setup:
    Code:
    if(Action.RIGHT_CLICK_AIR != null || Action.RIGHT_CLICK_BLOCK != null){
    if(event.getPlayer().getInventory().getItemInHand().getType() == Material.COOKED_BEEF){
    if(event.getPlayer().getInventory().getItemInHand().getAmount() > 1) {
    event.getPlayer().getInventory().getItemInHand().setAmount(event.getPlayer().getInventory().getItemInHand().getAmount() - 1);
    } else {
    event.getPlayer().getInventory().setItemInHand(new ItemStack(Material.AIR, 0));
    }
    if(event.getPlayer().getHealth()>=12) {
    event.getPlayer().setHealth(20);
    } else {
    event.getPlayer().setHealth(event.getPlayer().getHealth() + 8);
    }
    }
    }
    And I've found out something weird. When I try to eat 4 steak with rightclick, I get stuck at the last one.
    When I try to eat 4 steak with left click (which I dont know why works) I eat it all.
    I still need help with the right click tho.

    Thanks, Limeth

    EDIT: I got an idea. How to cancel the eating animation event? I bet thats the problem.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  14. Offline

    TheSmallBones

    Limeth use playerInteract event instead?
     
  15. Offline

    Limeth

    The problem is, I do use PlayerInteractEvent. :c
     
  16. Offline

    davejavu

    Limeth
    I would remove the steak like this;
    Code:java
    1.  
    2. int amt = player.getItemInHand().getAmount();
    3. if (amt > 1) {
    4. player.getInventory().setItemInHand(new ItemStack(player.getItemInHand(), amt - 1));
    5. } else {
    6. player.getInventory().setItemInHand(new ItemStack(Material.getMaterial(0)));
    7. }
     
  17. Offline

    marwzoor

    Limeth
    Why don't you just use this?
    if(event.getPlayer().getInventory().getItemInHand().getType() == Material.COOKED_BEEF){
    event.getPlayer().getInventory().getItemInHand().setAmount(event.getPlayer().getInventory().getItemInHand().getAmount() - 1);
    }

    You don't need the else :) I think this should work ;)
     
  18. Offline

    Phinary

    As for the hunger thing, you can simply do

    Code:
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent e) {
        e.getPlayer().setFoodLevel(20); //Change to whatever you like
    }
     
  19. Offline

    davejavu

    Shaun Bennett
    That will probably lag servers a TON, being called EVERY TIME a player moves..
    Why not just do this;
    Code:java
    1. @EventHandler
    2. public void onFoodLevelChange(FoodLevelChangeEvent evt) {
    3. evt.setFoodLevel(20)
    4. }

    Efficient coding is efficient.
     
  20. Offline

    Limeth

    Current setting:
    Code:java
    1. public void onPlayerInteract(PlayerInteractEvent event) {
    2. if(Action.RIGHT_CLICK_AIR != null || Action.RIGHT_CLICK_BLOCK != null){
    3. if(event.getPlayer().getInventory().getItemInHand().getType() == Material.COOKED_BEEF){
    4. int amt = event.getPlayer().getItemInHand().getAmount();
    5. if (amt > 1) {
    6. event.getPlayer().getInventory().setItemInHand(new ItemStack(event.getPlayer().getItemInHand().getType(), amt - 1));
    7. } else {
    8. event.getPlayer().getInventory().setItemInHand(new ItemStack(Material.getMaterial(0)));
    9. }
    10. if(event.getPlayer().getHealth()>=12) {
    11. event.getPlayer().setHealth(20);
    12. } else {
    13. event.getPlayer().setHealth(event.getPlayer().getHealth() + 8);
    14. }
    15. }
    16. }


    Now I can't use right mouse button at all (still heals), left mouse button works perfectly.

    I used this the time I posted the first post. It works, but I get stuck at the last one.

    @Shaun Bennett
    @davejavu
    That thing is already solved. Thank you for your reply anyway.
    If you would like, you can look at the "steak eating", which is making me trouble.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  21. Offline

    davejavu

    Limeth
    Code:java
    1. if (Action.RIGHT_CLICK_AIR != null || Action.RIGHT_CLICK_BLOCK != null)

    Personally there I would use:
    Code:java
    1. if (evt.getAction() == Action.RIGHT_CLICK_AIR || evt.getAction() == Action.RIGHT_CLICK_BLOCK)

    But same effect overall. Also, you say LEFT CLICK is working, but NOT right, yet you haven't specified left click in the if statement, so wait wat?
     
  22. Offline

    Limeth

    davejavu
    You can't imagine how a new user of java like me could get confused at coding instant eating of steak.
    ...
    ...
    ...
    steak.

    And don't even tell me about that the left clicking works too. Oh god. Let me just try your code.

    davejavu
    I used you code, it works and it does not allow left clicking.

    I'm still having trouble with teh food deleting. :c

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  23. Offline

    davejavu

    Limeth You said that was solved?
     
  24. Offline

    Phinary

    Because I am lazy and didn't realize that existed. There are plugins that do much worse then my coding, it really doesn't make that big of a difference. Nevertheless, your way is defiantly better.
     
  25. Offline

    Limeth

    davejavu
    The constant hunger bar is solved, but the steak eating is not. It won't let me eat the last one.
     
  26. Offline

    azyhd

    I dont know if you still have the problem with decrasin the amount but us:
    player.getItemInHand().setAmount(-1);

    I dont know if it will work because im on ipod right now
     
  27. Offline

    llamasaylol

    Just seen the kinda request/help and I've had code that has done what you need for ages, so I thought I'd share it (as a plugin though, the code is my secret). http://dev.bukkit.org/server-mods/foodhealz/ (It is currently waiting approval)
     
  28. Offline

    Limeth

    llamasaylol
    Well that won't help me at all, but thanks for sharing anyways.

    azyhd
    That is already solved, but I'm getting stuck at eating the last one.
     
  29. Offline

    azyhd

    I dont really understand what youre trying to do but, if its insta-eat try using:

    if(player.getItemInHand()== Material.APPLE){
    player.setHealth(+1);
    player.getItemInHand().setAmount(-1);

    and that for every food sort. Hope it helps;)
     
  30. Offline

    Limeth

    Wow... Really? I just don't get, how could I miss that.
    Well anyways thank you so much for letting me know it.

    I'm marking this post as solved. Thank you everybody :)
     
    azyhd likes this.
Thread Status:
Not open for further replies.

Share This Page