Healing problems

Discussion in 'Plugin Development' started by jackson30007, Oct 12, 2013.

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

    jackson30007

    I've been trying to make a plugin where you gain health by right clicking an apple. Here is the code I've done but it doesn't seem to work, can anyone help?
    Code:java
    1. package me.jackson30007.foodhealer;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Damageable;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.Action;
    8. import org.bukkit.event.player.PlayerInteractEvent;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Foodhealer extends JavaPlugin implements Listener {
    13. public static Foodhealer instance;
    14. public static Foodhealer getinstance() {
    15. return instance;
    16. }
    17.  
    18. public void onEnable() {
    19. instance = this;
    20. Foodhealer foodheal = new Foodhealer();
    21. getServer().getPluginManager().registerEvents(foodheal, this);
    22. }
    23.  
    24. @EventHandler
    25.  
    26. public void onAppleUse(PlayerInteractEvent e) {
    27.  
    28. if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    29.  
    30. if (e.getPlayer().getItemInHand() == new ItemStack(Material.APPLE)) {
    31.  
    32. e.getPlayer().getInventory().removeItem(new ItemStack(Material.APPLE, 1));
    33. }
    34. if(((Damageable) e).getHealth() <= 16) {
    35. ((Damageable) e).setHealth(((Damageable) e).getHealth() + 4);
    36. }else{
    37. ((Damageable) e).setHealth(20);
    38. }
    39. }
    40. }
    41. }
     
  2. Offline

    SkillSam

    jackson30007 Perhaps you could change the way you register the events.

    Code:java
    1. Bukkit.getPluginManager().registerEvents(this, this);


    And it seems you have closed the bracket too early in your PlayerInteractEvent, as well as you are not defining the player health.

    Code:java
    1. @EventHandler
    2. public void onAppleUse (PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. if (p.getItemInHand().getType() == Material.APPLE) {
    6. if (p.getHealth() == p.getMaxHealth()) return;
    7. if (p.getHealth() + 4 > p.getMaxHealth()) {
    8. p.setHealth(p.getMaxHealth());
    9. } else {
    10. p.setHealth(p.getHealth() + 4);
    11. }
    12. p.getInventory().removeItem(new ItemStack[] { new ItemStack(Material.APPLE, 1) });
    13. }
    14. }
    15. }
     
  3. Offline

    jackson30007

    Thats how I first coded it but I kept getting the "The method getHealth() is ambiguous for the type Player" Error, so I changed the p.getHealth() to e.getHealth() and added the cast (damageable) to e.getHealth() but nothing works.
     
  4. Offline

    SkillSam

    jackson30007 Oh, well have you seen this article? I think you would probably have to place the Bukkit API jar first in your referenced libraries, instead of using the craftbukkit.jar.
     
  5. Offline

    deathknife

    jackson30007 Instead of p.getHealth use:
    Damageable damag = Player;
    damag.getHealth();
     
Thread Status:
Not open for further replies.

Share This Page