Solved Help with OnEat Cookie event

Discussion in 'Plugin Development' started by niko454, Sep 3, 2014.

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

    niko454

    Hello! I need help developing my muffin plugin. Basically what I want it to do is when you eat a certain type of muffin you get a certain type of power up. All the muffins are added and crafting recipes are too.
    I have tried eating a muffin aka cookie but it does not seem to work. Please help :D
    Code:
    Code:java
    1. public class MainMuffins extends JavaPlugin implements Listener {
    2. @EventHandler(ignoreCancelled = true)
    3. public void onPlayerInteractEvent(PlayerInteractEvent e){
    4. //get all the relative values for comparation
    5. final int l = e.getPlayer().getFoodLevel();
    6. final Player p = e.getPlayer();
    7. final int a = p.getItemInHand().getAmount();
    8. ItemStack i = e.getPlayer().getItemInHand();
    9. if(i.getType().equals(Material.COOKIE)){
    10. Plugin plugin = null;
    11. //schedule a task to see if they have eaten the cookie(maybe the time could be a little faster idk)
    12. plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable(){
    13. public void run(){
    14. if(p.getFoodLevel() > l && p.getItemInHand().getAmount() < a){
    15. LivingEntity player = null;
    16. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 12000, 3));
    17. }
    18. }
    19. }
    20. ,100L);
    21.  
    22. }
    23. }
     
  2. Offline

    FabeGabeMC

    niko454
    Check for PlayerItemConsumeEvent instead of PlayerInteractEvent.
    also, setting a plugin to null will give you a NullPointerException.
     
  3. Offline

    NonameSL

    Are you new to java?
    You are creating a null plugin, creating a task with it, then creating a null player and refilling his health.

    Also, wrong event.
     
    Zupsub and Reddeh like this.
  4. Offline

    Reddeh

    This.
    Make sure in your onEnable thing you put "getServer().getPluginManager().registerEvents(this, this);".

    Also may I ask why you're making a brand new, nonexistent player to refill it's health? That's silly, just use p and refill the health of p, with a cast if necessary.

    Also try using the PlayerConsumeEvent c:
     
  5. Offline

    niko454

    I am quite new to Java.
    The regeneration is just an example i'm planning to add effects like strength instead.
    Thanks for helping il tell you what i get.

    New code:
    Code:java
    1. public class MainMuffins extends JavaPlugin implements Listener {
    2. @EventHandler(ignoreCancelled = true)
    3. public void onPlayerInteractEvent(PlayerItemConsumeEvent e){
    4. //get all the relative values for comparation
    5. final int l = e.getPlayer().getFoodLevel();
    6. final Player p = e.getPlayer();
    7. final int a = p.getItemInHand().getAmount();
    8. ItemStack i = e.getPlayer().getItemInHand();
    9. if(i.getType().equals(Material.COOKIE)){
    10. //schedule a task to see if they have eaten the cookie(maybe the time could be a little faster idk)
    11. getServer().getScheduler().scheduleAsyncDelayedTask(Plugin, new Runnable(){
    12. public void run(){
    13. if(p.getFoodLevel() > l && p.getItemInHand().getAmount() < a){
    14. player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 12000, 3));
    15. }
    16. }
    17. }
    18. ,100L);
    19.  
    20. }
    21. }

    In player.addPotionEffect
    Player is red
    In getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable(){
    plugin is red.
    What do i need to do ? o.o

    Nevermind I changed the code to this:
    Code:java
    1. @EventHandler(ignoreCancelled = true)
    2. public void onPlayerClick(PlayerInteractEvent event)
    3. {
    4. Player player = event.getPlayer();
    5. if((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_AIR))
    6. {
    7. if(player.getItemInHand().getType() == Material.COOKIE)
    8. {
    9. player.sendMessage(ChatColor.DARK_RED+ "Powerup added!");
    10. }
    11. }
    12. player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 12000, 3));
    13. }



    Still doesn't seem to work?

    [edit by JaguarJo: merged posts.]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  6. Offline

    Reddeh

    You forgot to change it to PlayerConsumeEvent
     
  7. Offline

    niko454

    Reddeh
    But how does that even work? Without the interact event the whole code is broken There a PlayerItemConsumeEvent but not a PlayerConsumeEvent. PlayerInteract event is required due to that it detects right click when you have a cookie in your hand. My question is what is wrong with the script?
     
  8. Offline

    Gater12

    niko454
    Use PlayerItemConsumeEvent. It fires when the player actually eats the item.
     
  9. Offline

    niko454

    Gater12
    But how? The script turns red if I change it.
     
  10. Offline

    Gater12

    niko454
    Did you import it? What errors are your IDE throwing?
     
  11. Offline

    niko454

    Gater12
    Because it needs to get action without playerinteract it wont be able to. So what do I do? Can you fix up my code?

    Gater12
    Checking sec

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  12. Offline

    Gater12

    niko454
    Why does it need to get action? There's no need. Just remove that if condition check.
     
  13. Offline

    niko454

    Gater12
    Code:java
    1. package com.nikolay;
    2. import java.util.ArrayList;
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.block.Action;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.event.player.PlayerItemConsumeEvent;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18. import org.bukkit.potion.PotionEffect;
    19. import org.bukkit.potion.PotionEffectType;
    20. import org.bukkit.inventory.ShapedRecipe;
    21. import org.bukkit.inventory.meta.ItemMeta;
    22. public class MainMuffins extends JavaPlugin implements Listener {
    23. @EventHandler(ignoreCancelled = true)
    24. public void onPlayerClick(PlayerItemConsumeEvent event)
    25. {
    26. Player player = event.getPlayer();
    27. if((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_AIR))
    28. {
    29. if(player.getItemInHand().getType() == Material.COOKIE)
    30. player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 12000, 3));
    31. player.sendMessage(ChatColor.DARK_RED+ "Powerup added!");
    32. }
    33. }


    Thats my code
    What is red:
    In line

    if((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_AIR))
    getaction is red

    Gater12
    After I remove it is there anything else I need to change?

    Gater12
    New code:
    Code:java
    1. public class MainMuffins extends JavaPlugin implements Listener {
    2. @EventHandler(ignoreCancelled = true)
    3. public void onPlayerClick(PlayerItemConsumeEvent event)
    4. {
    5. Player player = event.getPlayer();
    6. {
    7. if(player.getItemInHand().getType() == Material.COOKIE)
    8. player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 12000, 3));
    9. player.sendMessage(ChatColor.DARK_RED+ "Powerup added!");
    10. }
    11. }


    Gater12
    No errors so far. One second let me test it on the server.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  14. Offline

    Gater12

    niko454
    Don't think so.

    Also, you can choose to Tahg or quote me since they both alert me (Tahging and quoting me results in 2 alerts)
     
  15. Offline

    niko454

    Gater12
    O lol. Sorry about that

    :O it works! :D thanks Il contact you if i have any other issues.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  16. Offline

    Reddeh

    So it worked alright?
    Well, good luck with future plugins I guess.
    Speaking of fixing errors, I need to work on my config file.
     
    TheMintyMate likes this.
  17. Offline

    TheMintyMate

    Yeah, I've got got similar issues :p
    - Minty
     
Thread Status:
Not open for further replies.

Share This Page