PlayerItemConsumeEvent not working?

Discussion in 'Plugin Development' started by SiezureSalad, Nov 23, 2017.

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

    SiezureSalad

    Code:
        @EventHandler
        public void onPotionDrink(PlayerItemConsumeEvent event) {
            Player player = event.getPlayer();
            player.sendMessage("Consumed item");
            if (event.getItem().getDurability() == 8257) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 120, 0));
                player.sendMessage("Giving 2:00 of regen");
            }
        }
    Idea of the plugin is to give 2 minutes of regeneration when they drink an extended regeneration potion (normally 1:30). Not sure why but I don't get the "Consumed item" debug line when I drink a potion or when I eat food or anything, am I using the event completely wrong?
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    SiezureSalad

    Code:
    package me.vaape;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class PlayerListener implements Listener{
       
        public PlayerListener(PotionDuration plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onPotionDrink(PlayerItemConsumeEvent event) {
            Player player = event.getPlayer();
            player.sendMessage("Consumed item");
            if (event.getItem().getDurability() == 8257) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 120, 0));
                player.sendMessage("Giving 2:00 of regen");
            }
        }
    }
    
     
  4. Offline

    MightyOne

    did you register the listener?
     
  5. Offline

    SiezureSalad

    Yes. i got it to work I reloaded the plugin and it works now. Now having trouble with the 2nd part, registering the consumed item as an extended regen potion. Does anyone have a good method to do this? I've tried a few different ways now and doesn't seem to want to work for me.

    Not sure how to deal with this or what it means exactly. My guess is that the NullPointerException part means that I need to sympathise for when the item name is null or something similar? But I added

    else if (name.equals(null)) {
    }

    and I still got the same error.

    Thanks in advance.

    EDIT: Okay I fixed it by changing the line
    Code:
            if (name.toUpperCase().contains("POTION OF REGENERATION")) {
    to
    Code:
            if (name == "Potion of Regeneration") {
    and no error! But it won't register the regen potion as a regen potion and still only gives me the default duration, not the 2 minutes like its supposed to.

    UPDATED CODE:
    Code:
    package me.vaape.potionduration;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class PlayerListener implements Listener{
       
        public PlayerListener(PotionDuration plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onPotionDrink(PlayerItemConsumeEvent event) {
            Player player = event.getPlayer();
            player.sendMessage("Consumed item");
            ItemStack item = event.getItem();
            String name = item.getItemMeta().getDisplayName();
            if (name == "Potion of Regeneration") {
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 120, 0));
                player.sendMessage("Giving 2:00 of regen");
            }
        }
    }
    
    
     
    Last edited by a moderator: Nov 24, 2017
  6. If item.getType().equals(Material.Potion) whatever and see if it's regen. Or get the ID of the item and see if it's the same as a regen potion

    1. Why do you need to code potions when it's already in Minecraft? And if you're attempting to set the time you need to set the event to canceled or else Minecraft natural potion drinking and yours will clash.

    And if you're comparing strings, use .equals("") not '=='

    '==' is for primitive data types.

    2. Register events inside your main class, or inside a method inside the main class. If you need to access the main class inside your Event (which isn't the case here) then just add a constructor that takes your main class.
     
  7. Offline

    SiezureSalad

    I'm extending the length of the regen potion to 2 minutes. Also I was having trouble testing whether it was a regen pot, as I tried item.getDurability() == 8257 (which is the extended regen potion I believe) and it didn't register. Any other methods to check it?
     
  8. Get durability returns the durability of the item, not the item ID. Check for the item ID. And set the event to cancelled then do your extension.

    I hate spoonfeeding but: e.getItem().getTypeId().equals(whatever);
     
    Last edited: Nov 24, 2017
  9. Offline

    SiezureSalad

    Code:
            if (item.getType().equals(Material.POTION) && item.getTypeId().equals(8257)) {
    Yeah I already tried that and it doesn't register it either.
    1. The method getTypeID() is deprecated.
    2. You can't use .equals on it.
     
  10. 1. If you're making a one class plugin or even one event plugin it doesn't really matter,

    2. You're allowed to use '==' for item.getType() because it's a primitive data type. I said .equals(), because I'm not on my compiler so I assumed it would take an argument for .equals();.

    3. You don't need to check if the type is a potion and the id because each item has a unique id anyways. It's redundant.

    4.
    1. Potion potion = new Potion(PotionType.WHATEVER);
    2. if(p.getItemInHand().equals(potion)). if (item.equals(potion) OR '==' If you must, whatever works I'm not in my compiler). I found that using a quick google search because I haven't used potions in my plugins before, you could've easily done that before running to the help forum everytime you don't know how to do something.
     
  11. Offline

    SiezureSalad

    I've already tried that. Potion is deprecated so that doesn't work.
     
  12. Use it. Just because something is 'deprecated' doesn't always mean it won't work. It just means it is an outdated way to do something, or less efficient. But in your case, it doesn't matter because your plugin is small.
     
  13. Offline

    SiezureSalad

    Code:
       
        @EventHandler
        public void onPotionDrink(PlayerItemConsumeEvent event) {
            Player player = event.getPlayer();
            player.sendMessage("Consumed item");
            ItemStack item = event.getItem();
            Potion regenPot = new Potion(PotionType.REGEN);
            if (item.getType().equals(Material.POTION) && item.equals(regenPot)) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 120*20, 0));
                player.sendMessage("Giving 2:00 of regen");
            }
        }
    }
    Yeah I ran it but it still didn't seem to work.
     
  14. You don't need to check if the item is a potion and if it equals regen pot. I recommend to learn Java and then the Bukkit API, not the other way around. It helps to see if things are redundant and if your code will 'work' or not.
     
  15. Offline

    SiezureSalad

    Nevertheless, I followed what you suggested and it didn't work. Doesn't matter if it was redundant, your method didn't work either. If you don't want to help that's fine, just don't reply, don't have to be a complete dick about it. I'd rather have someone help who actually knows what they are talking about or at least someone who wants to help instead of some passive aggressive prick like you.
     
  16. It's not being passive-aggressive, it's being blunt because you're wasting time by running to the help forum everytime your plugin doesn't work. You've made 3 threads this week?
    https://bukkit.org/threads/playeritemconsumeevent-not-working.467275/
    https://bukkit.org/threads/cant-register-player-as-entity-in-entitydamagebyentityevent.467281/
    https://bukkit.org/threads/error-when-i-eat-an-item-nullpointerexception.467351/

    Here you go, tested on my test server:
    Code:
        @EventHandler
        public void onPotionDrink(PlayerItemConsumeEvent event) {
            Player player = event.getPlayer();
            player.sendMessage("Consumed item");
            ItemStack item = event.getItem();
            PotionMeta pMeta = (PotionMeta) item.getItemMeta();
    
            if (pMeta.getBasePotionData().getType().equals(PotionType.REGEN)) {
                event.setCancelled(true);
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, DURATION, AMPLIFIER));
                player.sendMessage("Giving 2:00 of regen");
        
                if (!player.getGameMode().equals(GameMode.CREATIVE))
                    player.getInventory().remove(item);
            }
        }
    [​IMG]
    [​IMG]
    [​IMG]
    [​IMG]
    I know what I'm talking about. Everything I said was true in terms of Java. Just because I don't know the Bukkit API 100% after 2 years of not practicing it, doesn't mean I don't know what I'm talking about.
     
    Last edited: Nov 25, 2017
  17. Offline

    timtower Administrator Administrator Moderator

    Merged threads
     
Thread Status:
Not open for further replies.

Share This Page