Player drinks potion and bottle disappears.

Discussion in 'Plugin Development' started by mrdude123, Mar 24, 2015.

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

    mrdude123

    So, when someone drinks a POTIONBOTTLE I would like it to disappear. Any suggestions on how to make it work? I haven't had any luck in my tries so far.
     
  2. Offline

    Skionz

    ItemConsumeEvent?
     
  3. Offline

    mrdude123

    @Skionz And how would I limit the itemconsumeevent strictly to potion bottles? I've been having all my trouble due to that.
     
  4. Offline

    Aephout14

    Show your code....
     
  5. Offline

    mrdude123

    @Aephout14 I'm trying to make it right now. I haven't been able to advance due to my flustering.
     
  6. Offline

    Konato_K

  7. Offline

    mrdude123

    @Konato_K Ah, I didn't think of that. Thank you so much.

    I'm really stuck. Can someone give me a small example?
     
    Last edited by a moderator: Mar 24, 2015
  8. Offline

    Aephout14

    How are you stuck?
     
  9. Offline

    mrdude123

  10. Offline

    Aephout14

    I am probably wrong but.

    Get the Item, Check If its a Water Bottle, If It Is, Remove It from the players Inventory.
     
  11. Offline

    nverdier

    @mrdude123 Did you mark this thread as Solved on purpose?
     
  12. Offline

    mrdude123

    @Aephout14 No, I want it to be so that if someone drinks the potion, the bottle then disappears from their inventory.

    @nverdier No, sorry about that.
     
    Last edited by a moderator: Mar 24, 2015
  13. Offline

    Aephout14

    So like a Player drinks the water bottle, Then Check If the Item In there Inventory Is Bottle (Not Water Bottle) then remove it, Or you mean like Speed and Health potions etc?
     
  14. Offline

    mrdude123

    @Aephout14 Yes.
    Code:
    package me.thecerealkill3r.bottledispose;
    
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class Main extends JavaPlugin {
    
      @EventHandler
      public void onPlayerConsume(PlayerItemConsumeEvent event)
      {
        final Player player = event.getPlayer();
        Material mat = event.getItem().getType();
    
        if (player.getGameMode() == GameMode.CREATIVE) return;
        if (mat != Material.POTION) return;
        if (event.getItem().getAmount() > 1) return;
    
        new BukkitRunnable()
        {
          public void run() {
            player.setItemInHand(null);
          }
        }
        .runTaskLater(this, 1L);
     
    
    
            return;
    }
    }
    
    This is what I came up with so far. If you can see the Material.POTION, how can I change that to a specific potion? That's all I need.
     
  15. Offline

    Aephout14

    I think you need to use short values, Like doing

    Code:
    Material.POTION, (short) 8197
    
    I am not sure but doing this would be a Healing Potion.
     
  16. Offline

    mrdude123

    Code:
    if (mat != Material.8197 ) return;
    That doesn't work.
    Neither does
    Code:
     if (mat !=Material.POTION.8197) return; 
     
  17. Offline

    Aephout14

    Thats not what I meant, Its Material.POTION (short) 8197
     
  18. Offline

    mrdude123

    @Aephout14 It's telling me this - The operator / is undefined for the argument type(s) Material, short
     
  19. Offline

    Aephout14

    I don't know then...
     
  20. Offline

    nverdier

    @mrdude123 What are you doing?!? Please post all of your code.
     
  21. Offline

    mrdude123

    Code:
    package me.thecerealkill3r.bottledispose;
    
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class Main extends JavaPlugin {
    
      @EventHandler
      public void onPlayerConsume(PlayerItemConsumeEvent event)
      {
        final Player player = event.getPlayer();
        Material mat = event.getItem().getType();
    
        if (player.getGameMode() == GameMode.CREATIVE) return;
        if (mat != Material.POTION(short)8197 ) return;
        if (event.getItem().getAmount() > 1) return;
    
        new BukkitRunnable()
        {
          public void run() {
            player.setItemInHand(null);
          }
        }
        .runTaskLater(this, 1L);
     
    
    
            return;
    }
    }
     
  22. Offline

    nverdier

    @mrdude123 You will have to compare ItemStacks instead of Materials if you want to check if it's a specific potion. Although it seems like you don't need to check for specific potions, so you would just check if the Material is POTION.
     
  23. Offline

    mrdude123

    I'm not sure what you mean. Can you give an example?
     
  24. Offline

    Konato_K

    @mrdude123 If you need specific values for the potion you can use ItemStack#getDurability
     
  25. Offline

    Aephout14

  26. Offline

    sgavster

    Konato_K likes this.
  27. Offline

    mrdude123

    Ok. this is what I got. I made it work, but now if I drink a water bottle it's bottle disappears. Can someone show me how to exempt the waterbottle?

    Plugin is supposed to get rid of bottles from potions like healing or speed, not water bottles.

    Code:
    package npb;
    
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin
      implements Listener
      {
    
      @EventHandler
      public void consume(final PlayerItemConsumeEvent e) {
        ItemStack item = e.getItem();
        if (item.getType().equals(Material.POTION))
          getServer().getScheduler().runTaskLater(this, new Runnable() {
            public void run() {
                if (e.getPlayer().getItemInHand().getType().equals(Material.POTION))
              if ((!e.getPlayer().getGameMode().equals(GameMode.CREATIVE)) && (
                (e.getPlayer().getItemInHand().getType().equals(Material.GLASS_BOTTLE)) || (e.getPlayer().getItemInHand().getType().equals(Material.POTION))))
                e.getPlayer().getInventory().setItemInHand(null);
            }
          }
          , 2L);
      }
    }
    @nverdier

    Do you know?
     
    Last edited by a moderator: Mar 24, 2015
  28. Offline

    nverdier

    @mrdude123 Just check if the item being consumed (PlayerItemConsumeEvent#getItem()) has a type of potion. If it does, then remove a glass bottle.
     
  29. Offline

    mrdude123

    @nverdier A type of a potion? How would I do that?

    I mean, what would I say? POTION. ?
     
  30. Offline

    nverdier

Thread Status:
Not open for further replies.

Share This Page