[Solved] Healing using an item

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

  1. Offline

    hammy78

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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.

    This post has been edited 1 time. It was last edited by hammy78 Jul 28, 2012.
  2. Offline

    8thDimension

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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

    This post has been edited 2 times. It was last edited by 8thDimension Jul 28, 2012.
  3. Offline

    hammy78

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I tried the code, it compiled fine but when I tried to use it, nothing happened.
  6. Offline

    hammy78

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    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");
                    }
            }
        }
    
  7. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Did you register the event?
  8. Offline

    hammy78

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Probably not, erm which bit do I use register it?

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

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    What Goes Here = your listener class
  10. Offline

    hammy78

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Yep, that was the problem, Thanks.
  11. Offline

    hammy78

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I forgot 1 other thing, what do I add so after the user used the item, it is removed from there inventory.
  12. Offline

    8thDimension

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
    p.getInventory().remove(p.getItemInHand());
    Try adding that somewhere, haven't tested it out
  13. Offline

    hammy78

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Thanks that worked.

Share This Page