Set health on Food...

Discussion in 'Plugin Development' started by MrGermanrain, Jul 21, 2013.

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

    MrGermanrain

    I am trying to make it so that when a player right clicks a cookie, the cookie will turn to air and the players health will be 100%.

    Code:java
    1. package me.mrgermanrain.scheal;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.player.PlayerInteractEvent;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12.  
    13. public class scheal extends JavaPlugin{
    14. public final Logger logger = Logger.getLogger("Minecraft");
    15. public static scheal plugin;
    16.  
    17. @Override
    18. public void onDisable() {
    19. PluginDescriptionFile pdfFile = this.getDescription();
    20. this.logger.info(pdfFile.getName() + " has been Disabled!");
    21. }
    22.  
    23. @Override
    24. public void onEnable() {
    25. PluginDescriptionFile pdfFile = this.getDescription();
    26. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been Enabled!");
    27. }
    28.  
    29. public boolean onPlayerRightClick(Player p, PlayerInteractEvent event){
    30. Player player = (Player) p;
    31. if(player.getItemInHand().getType() != Material.COOKIE){
    32. player.setItemInHand(new ItemStack(Material.AIR));
    33. player.setHealth(20);
    34. }
    35. return false;
    36.  
    37. }
    38. }
     
  2. Offline

    Compressions

    MrGermanrain NONONONONONO. Use a PlayerInteractEvent as a void and check if the action is right clicking air or right clicking a block, check if them item in hand is a cookie, decrease the amount of the cookies by 1, and set the player's health to 20.
     
  3. Offline

    Bart

    Code:java
    1.  
    2. package me.mrgermanrain.scheal;
    3.  
    4. import java.util.logging.Logger;
    5.  
    6. import org.bukkit.Material;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.player.PlayerInteractEvent;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.plugin.PluginDescriptionFile;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12. import org.bukkit.event.EventHandler;
    13.  
    14.  
    15. public class scheal extends JavaPlugin implements Listener{
    16. public final Logger logger = Logger.getLogger("Minecraft");
    17. public static scheal plugin;
    18.  
    19. @Override
    20. public void onDisable() {
    21. PluginDescriptionFile pdfFile = this.getDescription();
    22. this.logger.info(pdfFile.getName() + " has been Disabled!");
    23. }
    24.  
    25. @Override
    26. public void onEnable() {
    27. PluginDescriptionFile pdfFile = this.getDescription();
    28. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " has been Enabled!");
    29. getServer().getPluginManager().registerEvents(this, this);
    30. }
    31.  
    32. @EventHandler
    33. public void onFoodConsume(PlayerItemConsumeEvent event){
    34. Player player = event.getPlayer();
    35. if(event.getItem().getType() == Material.COOKIE){
    36. player.setHealth(20);
    37. }
    38. return false;
    39.  
    40. }
    41. }
    42.  

    Fixed code ^

    There were a few things wrong with it.

    -> You used PlayerInteractEvent, that would do it instantly and when you left or right click.. I changed it to the consume event so that it will only do it when you eat it (Might not be what you want)
    -> You need to register events in onEnable()
    -> You need to add @EventHandler to the events (nothing else)
    -> Events need to be void, not boolean and they only have one parameter
    -> If the class contains listeners it needs to implement Listener.

    All code is untested, I'm about to go so I can't help you any more.
     
  4. Offline

    MrGermanrain

    thanks ill test it :D
     
Thread Status:
Not open for further replies.

Share This Page