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

    @nverdier I'm really sorry, but could you give an example? It would help a lot.
     
  2. Offline

    nverdier

    @mrdude123 Sorry, I don't spoon feed. Just check if PlayerItemConsumeEvent#getItem()#getType returns Material.POTION.
     
    Tyler Christensen likes this.
  3. Offline

    Konato_K

    @nverdier As far I know the "water bottle" it's a Material.POTION aswell.

    @mrdude123 To my knowledge a water bottle always has a data value of 0, you can compare this with #getDurability or #getData, anyway, I also believe plugins have the power to apply potion effects without modifying the actual data value, this is something with the ItemMeta.
     
  4. Offline

    nverdier

    @Konato_K Hmmm... perhaps. Probably, actually.
     
  5. Offline

    mrdude123

    @Konato_K So do you know how I would change it, then?
    Writing the code isn't the problem, it's just where do I put it.
     
  6. Offline

    nverdier

    @mrdude123 Writing the code should include where you put it, yes? So writing the code is in fact the problem?
     
  7. Offline

    Konato_K

    @mrdude123 Well, after you run the logic of removing the item it's too late don't you think?
     
  8. Offline

    mrdude123

    @nverdier I know what to write, just not where to put it. So sure, I guess.
     
  9. You would use the "instanceof" in an if statement. then compare the item in their hand and if it is instanceof potion, whether you do that with itemstacks, or materials.
     
  10. Offline

    dlange

    @mrdude123 You still haven't registered the events...
     
    Tyler Christensen likes this.
  11. Offline

    Konato_K

  12. Well i ment if the itemstack instanceof a itemstack with material potion .-.
     
  13. Offline

    Konato_K

    @Tyler Christensen I don't know what you mean, but an ItemStack will always be an instance of an ItemStack, so an instanceof check won't do anything.
     
  14. Offline

    mrdude123

    @Konato_K
    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;
    import org.bukkit.potion.PotionType;
    
    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() {
                Potion potion = new Potion.fromItemStack(event.getCurrentItem());
                if (potion == PotionType.SPEED && potion == PotionType.INSTANT_HEAL)
            }
          }
          , 2L);
      }
    }
    How's this? wait forgot something
     
  15. Offline

    Konato_K

    @mrdude123 A PotionType will never be equal to a Potion. Also
     
  16. Offline

    mrdude123

    @Konato_K I've got only one bug before my plugin is done.
    Syntax error, insert "AssignmentOperator Expression" to complete Expression

    What can I put in to fix?
    Code:
    (e.getPlayer().getInventory().setItemInHand(Material.AIR)//Error is right here.;
     
  17. Offline

    Konato_K

  18. You do the itemstack instanceof k where k = new ItemStack(Material.Potion) or whatever .-.
     
    • You need to register the events in onEnable.
    • Check if the material equals Material.POTION and the data value is not 0
     
  19. Offline

    Protophite

    @mrdude123

    • Don't name your main class "Main."
    • You need to register your events preferably when the plugin enables.
     
  20. Offline

    Konato_K

    @Tyler Christensen That doesn't work at all, and I don't even think it's valid syntax.
     
  21. Offline

    lilian58660

    Code:
      @EventHandler
      public void onPlayerItemConsume(final PlayerItemConsumeEvent e)
      {
        if (e.getItem().getType().equals(Material.POTION))
          getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
          {
            public void run() {
              if (e.getPlayer().getItemInHand().getType().equals(Material.GLASS_BOTTLE))
                e.getPlayer().getInventory().setItemInHand(null);
            }
          }
          , 0L);
      }
     
  22. Offline

    WeeSkilz

    Code:
    @EventHandler
    public void onPlayerItemConsume(final PlayerItemConsumeEvent {
      if (e.getItem().getType().equals(Material.POTION) && e.getItem().getItemMeta().getDurability() != 0)
        getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
        {
          public void run() {
            if (e.getPlayer().getItemInHand().getType().equals(Material.GLASS_BOTTLE))
              e.getPlayer().getInventory().setItemInHand(null);
          }
        }
        , 0L);
    }
    That exempts water bottles by checking if the durability is 0.
     
Thread Status:
Not open for further replies.

Share This Page