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.
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.
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.
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.
No Errors. Yes Im registering events. The explosion spell I made works but the heal spell doesn't. Both are in the same class.
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.
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.
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; } } }
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 ^^
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.
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.