[Solved] Healing using an item

Discussion in 'Plugin Development' started by hammy78, Jul 28, 2012.

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

    hammy78

    I am currently using this code to heal the player:
    Code:
    if(commandLabel.equalsIgnoreCase("recover")){
    player.setHealth(20);
    player.setFoodLevel(20);
    player.setFireTicks(20);
    player.sendMessage(ChatColor.GREEN + "You have recovered");
    }
    But what if I wanted instead of a command, to heal or recover they had to use an item.
     
  2. Offline

    8thDimension

    What do you mean by use an item?

    Like right clicking?

    If you're checking for right click, for one you could do something like:

    Code:
    @EventHandler
    public void PlayerInteract(PlayerInteractEvent event) {
        if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                Player p = event.getPlayer();
           
                if (p.getItemInHand().getType().equals(Material.DIAMOND)) {
                    // All the healing code here
                }
        }
    }
    I haven't tested it, but what this does is if the player right clicks air or a block, if they're holding diamond it will run the healing code or whatever you want. Of course, you can replace the Material.DIAMOND with whatever item you'd like
     
  3. Offline

    hammy78

    By using an item I mean, say if they had a piece of paper in there hand, and clicked with it, then they would heal.
     
  4. Offline

    8thDimension

    Oh, in that case you'd just do something similar

    Code:
         @EventHandler
         public void PlayerInteract(PlayerInteractEvent event) {
              if (event.getAction().equals(Action.LEFT_CLICK_AIR) || event.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                   Player p = event.getPlayer();
     
                   if (p.getItemInHand().getType().equals(Material.DIAMOND)) {
                        // All the healing code here
                   }
              }
         }
    
    Just replace RIGHT with LEFT

    Of course, if you don't care whether or not it's left or right click, then you can simply just do:

    Code:
     
         @EventHandler
         public void PlayerInteract(PlayerInteractEvent event) {
              Player p = event.getPlayer();
     
              if (p.getItemInHand().getType().equals(Material.DIAMOND)) {
                   // All the healing code here
              }
         }
     
    
     
  5. Offline

    hammy78

    I tried the code, it compiled fine but when I tried to use it, nothing happened.

    I could have done it wrong but:
    Code:
    @EventHandler
        public void PlayerInteract(PlayerInteractEvent event) {
            if (event.getAction().equals(Action.LEFT_CLICK_AIR) || event.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                    Player p = event.getPlayer();
               
                    if (p.getItemInHand().getType().equals(Material.PAPER)) {
                       p.sendMessage("If you see this, it works");
                    }
            }
        }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  6. Offline

    Firefly

    Did you register the event?
     
  7. Offline

    hammy78

    Probably not, erm which bit do I use register it?

    I know "pm.registerEvents(WHAT GOES HERE, this);"
     
  8. Offline

    Firefly

    What Goes Here = your listener class
     
  9. Offline

    hammy78

    Yep, that was the problem, Thanks.

    I forgot 1 other thing, what do I add so after the user used the item, it is removed from there inventory.

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

    8thDimension

    Code:
    p.getInventory().remove(p.getItemInHand());
    Try adding that somewhere, haven't tested it out
     
  11. Offline

    hammy78

    Thanks that worked.
     
Thread Status:
Not open for further replies.

Share This Page