Item to Health

Discussion in 'Plugin Development' started by 20zinnm, Oct 15, 2014.

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

    20zinnm

    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
    1. package sppvp;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class SPPvP extends JavaPlugin {
    6.  
    7. public void onEnable() {
    8. getLogger().info("SPPvP Enabing... Hold tight!");
    9. getServer().getPluginManager().registerEvents(new Listeners(), this);
    10. getLogger().info("OK we made it.");
    11. }
    12.  
    13. public void onDisable() {
    14. getLogger().info("SPPvP Disabling.");
    15. }
    16.  
    17.  
    18. }
    19.  

    Listeners Class:
    Code:java
    1. package sppvp;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.player.PlayerInteractEvent;
    7.  
    8. public class Listeners implements Listener {
    9.  
    10. @EventHandler
    11. public void onPlayerInteractEvent(PlayerInteractEvent e) {
    12. if (e.getPlayer().getItemInHand().equals(Material.NETHER_STAR)) {
    13. e.getPlayer().getItemInHand().setAmount(e.getPlayer().getItemInHand().getAmount() - 1);
    14. e.getPlayer().setHealth(e.getPlayer().getHealth() + 8);
    15. return;
    16. }
    17. return;
    18. }
    19. }
    20.  

    Any help is appreciated!
     
  2. Offline

    fireblast709

    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.
     
  3. Offline

    20zinnm

    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.
     
    Last edited by a moderator: Jun 14, 2016
  4. Offline

    fireblast709

    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 :p
     
  5. Offline

    20zinnm

    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
    1. package sppvp;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.block.Action;
    7. import org.bukkit.event.player.PlayerInteractEvent;
    8.  
    9. public class Listeners implements Listener {
    10.  
    11. @EventHandler
    12. public void onPlayerInteractEvent(PlayerInteractEvent e) {
    13. if (e.getAction().equals(Action.RIGHT_CLICK_AIR)
    14. || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    15. if (e.getPlayer().getItemInHand() != null) {
    16. if (e.getPlayer().getItemInHand().equals(Material.NETHER_STAR)) {
    17. if (e.getPlayer().getItemInHand().getAmount() == 1) {
    18. e.getPlayer().getItemInHand().setType(null);
    19. } else {
    20. e.getPlayer()
    21. .getItemInHand()
    22. .setAmount(
    23. e.getPlayer().getItemInHand()
    24. .getAmount() - 1);
    25. }
    26. if (!((e.getPlayer().getHealth() + 8) <= e.getPlayer()
    27. .getMaxHealth())) {
    28. e.getPlayer().setHealth(e.getPlayer().getMaxHealth());
    29. } else {
    30. e.getPlayer().setHealth(e.getPlayer().getHealth() + 8);
    31. }
    32. }
    33. }
    34. }
    35. }
    36. }


    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.
     
    Last edited by a moderator: Jun 14, 2016
  6. Offline

    fireblast709

    20zinnm bullet point #3 "an ItemStack will never equal a Material" ;3. Check if the ItemStack's Material == Material.NETHER_STAR.
     
Thread Status:
Not open for further replies.

Share This Page