So I'm trying to make a plugin where you can right click a nether star and it will remove one nether star from the slot in your inventory, and give you 4 hearts. I can't seem to figure out why, whenever I run this plugin, it doesn't do anything. Main Class: Code:java package sppvp; import org.bukkit.plugin.java.JavaPlugin; public class SPPvP extends JavaPlugin { public void onEnable() { getLogger().info("SPPvP Enabing... Hold tight!"); getServer().getPluginManager().registerEvents(new Listeners(), this); getLogger().info("OK we made it."); } public void onDisable() { getLogger().info("SPPvP Disabling."); } } Listeners Class: Code:java package sppvp; import org.bukkit.Material;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.player.PlayerInteractEvent; public class Listeners implements Listener { @EventHandler public void onPlayerInteractEvent(PlayerInteractEvent e) { if (e.getPlayer().getItemInHand().equals(Material.NETHER_STAR)) { e.getPlayer().getItemInHand().setAmount(e.getPlayer().getItemInHand().getAmount() - 1); e.getPlayer().setHealth(e.getPlayer().getHealth() + 8); return; } return; }} Any help is appreciated!
20zinnm You forgot to check if the action was a right click. You forgot to check if they even held an item. An ItemStack will never equal a Material. When the amount is 1, setting the amount to 0 will not remove the ItemStack. Instead, if the amount is 1 and you need to remove something, set the ItemStack in hand to null. Don't forget to check if getHealth() + 8 < getMaxHealth()! The return statements are redundant in your case.
Wow thanks. Should I check for both right click air and block? fireblast709 EDIT by Moderator: merged posts, please use the edit button instead of double posting.
20zinnm assuming you want to allow them to heal themselves even if they don't look directly at a block (for example, if they look up to the sky), yes
fireblast709 Thanks for the help! fireblast709 Here is my revised code: Please advise. It still is doing nothing in-game and I can't figure out why the listener isn't being triggered. Code:java package sppvp; import org.bukkit.Material;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.block.Action;import org.bukkit.event.player.PlayerInteractEvent; public class Listeners implements Listener { @EventHandler public void onPlayerInteractEvent(PlayerInteractEvent e) { if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { if (e.getPlayer().getItemInHand() != null) { if (e.getPlayer().getItemInHand().equals(Material.NETHER_STAR)) { if (e.getPlayer().getItemInHand().getAmount() == 1) { e.getPlayer().getItemInHand().setType(null); } else { e.getPlayer() .getItemInHand() .setAmount( e.getPlayer().getItemInHand() .getAmount() - 1); } if (!((e.getPlayer().getHealth() + 8) <= e.getPlayer() .getMaxHealth())) { e.getPlayer().setHealth(e.getPlayer().getMaxHealth()); } else { e.getPlayer().setHealth(e.getPlayer().getHealth() + 8); } } } } }} My onEnable() method is registering the listener class just as it worked with all my other plugins. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
20zinnm bullet point #3 "an ItemStack will never equal a Material" ;3. Check if the ItemStack's Material == Material.NETHER_STAR.