When a player right clicks a item keep subtracting hunger

Discussion in 'Plugin Development' started by Mrcool234, May 4, 2014.

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

    Mrcool234

    Im making a plugin where people use their Hunger bar as mana and I need to make it so if a player has right clicked an Item it subtracts his hunger bars but when he right clicks again it keeps going down from there until it reaches 0 i've tried many ways and all I know is to use player.setFoodlevel() can someone help me? P.S: I don't want to use FoodChangeEvent please preferable have me keep using PlayerInteractEvent.

    ~ Joe
     
  2. Offline

    Opacification

    Just check when the player clicks the block, check it 10 times, and set his food level -1 every time.

    That's one way you can do it but there's many others.
     
  3. Offline

    Mrcool234

    Opacification I'm sorry I'm very fluent in java but can you please take the time to give me a line of code?
     
  4. Offline

    Birdgeek3



    Code:
        @EventHandler
        public void onRightClick (PlayerInteractEvent ev) {
            if (ev.getAction().equals(Action.RIGHT_CLICK_AIR) || ev.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {     
                if (ev.getItem().getType().equals(Material.ANVIL)) {
                    ev.getPlayer().setFoodLevel(ev.getPlayer().getFoodLevel() - 1);
                }
            }
        }
    I think this should work if a player right clicks with an anvil in hand on the air or a block of any type. I'm not taking the time to check it though
     
  5. Offline

    Quackster


    ev.getItem() returns the item in the players hand.

    ev.getClickedBlock() is what you're looking for.
     
  6. Offline

    Opacification

    What I said is more than enough to figure it out, spoon feeding you code isn't going to help you learn Bukkit API.
     
    AoH_Ruthless likes this.
  7. Offline

    AoH_Ruthless

    Quackster
    Really? I thought he was asking when the player fight clicks an item in hand, but maybe I interpreted it wrong.
     
  8. Offline

    Quackster

    Oh, now that I read the main post it could be interpreted as either one. Yours is probably more right though. :p
     
  9. Offline

    BeefyPlays

    Correct me if I'm wrong but I'm pretty sure its something like this.
    Code:java
    1. Player player = (Player) event.getPlayer();
    2. player.setFoodLevel(player.getFoodLevel() - 5);
     
  10. Offline

    Opacification


    You're subtracting 5 hunger slots not 1, plus you didn't get the event or the action when right clicking a block.
     
  11. Offline

    BeefyPlays

    That was just an example. I'm pretty sure he can easily implement this into a playerinteractevent.
     
  12. Offline

    Mrcool234

    BeefyPlays Thank you very much that solved my problem :D
     
  13. Offline

    Opacification

    Note the: 'correct me if i'm wrong' part.
     
Thread Status:
Not open for further replies.

Share This Page