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.
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
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.
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 } }
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"); } } }
I forgot 1 other thing, what do I add so after the user used the item, it is removed from there inventory.