Heal isn't working

Discussion in 'Plugin Development' started by plisov, Jun 13, 2015.

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

    plisov

    I'm trying to make an item heal the player when it is clicked with. It is not working for some reason. Here is my code.
    In Main Class
    Code:
    if (cmd.getName().equalsIgnoreCase("health")) {
                Player player = (Player) sender;
                if (sender instanceof Player) {
                if (sender.hasPermission("ezplexmc.health")) {
                    Material material = Material.FERMENTED_SPIDER_EYE;
                       int ammount = 1;
                       String name = ChatColor.GREEN + "Heal Spell";
                 
                       ItemStack item = new ItemStack(material, ammount);
                       ItemMeta itemMeta = item.getItemMeta();
                       itemMeta.setDisplayName(name);
                 
                       item.setItemMeta(itemMeta);
                       player.getInventory().addItem(item);
                       
                    }
                }
            }
    
    In the listener class
    Code:
    public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
    
    if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Heal Spell")) {
                player.sendMessage("Test");
                player.setHealth(20);
                } else {
                    return;
                }
           
            }
    
    Nothing is red. No errors in console. It just doesn't work.
     
  2. Offline

    87pen

    You cast player to sender before checking if it's a player. And health is saved as an int when it needs to be a double, this is your problem. If console used the command it would throw an error.
     
    Last edited: Jun 13, 2015
  3. Offline

    Drkmaster83

    Additionally, I'm sure that there's a NullPointerException being thrown as a result of your PlayerInteractEvent. Definitely check to see if you're registering the listener.
     
  4. Offline

    plisov

    So your're saying it should look like this?
    Code:
    if (cmd.getName().equalsIgnoreCase("health")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                if (sender.hasPermission("ezplexmc.health")) {
                    Material material = Material.FERMENTED_SPIDER_EYE;
                       double amount = 1;
                       String name = ChatColor.GREEN + "Heal Spell";
                 
                       ItemStack item = new ItemStack(material);
                       ItemMeta itemMeta = item.getItemMeta();
                       itemMeta.setDisplayName(name);
                 
                       item.setItemMeta(itemMeta);
                       player.getInventory().addItem(item);
                       
                    }
                }
            }
    
    doesnt work.
     
  5. Offline

    Konato_K

  6. Offline

    plisov

    hey
    I click with a fermented spider eye and it doesnt heal.
     
  7. Offline

    Konato_K

    @plisov Do you get any errors? Are you registering your events?
     
  8. Offline

    plisov

    No Errors. Yes Im registering events. The explosion spell I made works but the heal spell doesn't. Both are in the same class.
     
  9. Offline

    87pen

    The amount doesn't need to be double, health needs to be a double, while it used to be an int. In your player.setHealth(); Replace the int with a double.
     
  10. Offline

    plisov

    I don't quite get what you are trying to say.
    There is no int in the player.setHealth();
     
  11. Offline

    87pen

    Player.setHealth(20); <----- 20 = int Player.setHealth(20.0); <----- double. It's as simple as adding a .0 to the end of your 20. Please learn Java if you don't know the difference between an int and a double you shouldn't be tackling bukkit.
     
  12. Offline

    plisov

    I've already tried that. It still won't work.
     
  13. Offline

    87pen

    I just tested it and it works perfectly. Post updated code?
     
  14. Offline

    plisov

    Main class
    Code:
    if (cmd.getName().equalsIgnoreCase("health")) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                if (sender.hasPermission("ezplexmc.health")) {
                    Material material = Material.FERMENTED_SPIDER_EYE;
                       int amount = 1;
                       String name = ChatColor.GREEN + "Heal Spell";
                 
                       ItemStack item = new ItemStack(material, amount);
                       ItemMeta itemMeta = item.getItemMeta();
                       itemMeta.setDisplayName(name);
                 
                       item.setItemMeta(itemMeta);
                       player.getInventory().addItem(item);
                       
                    }
                }
            }
    
    Listener Class
    Code:
    public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
    
    if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Heal Spell")) {
                player.sendMessage("Test");
                player.setHealth(20.0);
                } else {
                    return;
                }
           
            }
    }
    
     
  15. Offline

    Konato_K

    @plisov Full code, does that method even have @EventHandler?
     
  16. Offline

    plisov

    Code:
    package me.plisov.enable.Listener;
    
    import me.plisov.Main;
    import me.plisov.Title;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.World;
    import org.bukkit.block.BlockFace;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityBreakDoorEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    
    public class ChatListener implements Listener {
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
    
    if(player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Heal Spell")) {
                player.sendMessage("Test");
                player.setHealth(20.0);
                } else {
                    return;
                }
           
            }
    
    Thats the listener class ^^
     
  17. Offline

    Konato_K

    @plisov I don't see the other event handler you were talking about, so this is not the full code.
     
  18. Offline

    plisov

    Which event specifically. The rest of the code is irrelevant to this. What was the event?
     
  19. Offline

    Msrules123

    Make sure you see if the item *has* itemmeta before trying to access it. That won't fix your problem.

    1. Are you catching the normally ignored events?
    2.
    ^ Useless
    3.
    Your class name is ChatListener, but you are using all these imports. Seperate your events by classes, one per class. You shouldn't cram this much stuff into one class.
     
  20. Clean up :
    Code:
    if (cmd.getName().equalsIgnoreCase("health")) {
        if (sender instanceof Player) {
            Player player = (Player) sender;
            if (sender.hasPermission("ezplexmc.health")) {
                String name = ChatColor.GREEN + "Heal Spell";
                int amount = 1;
                ItemStack item = new ItemStack(Material.FERMENTED_SPIDER_EYE, amount);
                ItemMeta itemMeta = item.getItemMeta();
                itemMeta.setDisplayName(name);
                item.setItemMeta(itemMeta);
                player.getInventory().addItem(item);
    
            }else{
                //no perms
            }
        }else{
            sender.sendMessage(ChatColor.RED + "You have to be a player to use this.")
        }
    }
    Also, the int amount is kinda redundant at this stage.
     
Thread Status:
Not open for further replies.

Share This Page