Remove potion after consuming code won't work

Discussion in 'Plugin Development' started by bennie3211, Mar 29, 2015.

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

    bennie3211

    Hi all,

    I've a question; I typed a piece of code, that after consuming a potion (that will trigger after each potion) the empty bottle that stays behind should be removed. Now i got this piece of code:

    Code:
    @EventHandler
        public void onConsume(PlayerItemConsumeEvent event) {
           
            ItemStack i = event.getItem();
            final Player pl = event.getPlayer();
            Integer slot = pl.getInventory().getHeldItemSlot();
           
            if (plugin.getArenaManager().getArena(pl) == null)
                return;
           
            if (!plugin.configConfig.getConfigConfig().getBoolean("RemoveBottleAfterConsuming"))
                return;
           
            if (i.getType().equals(Material.POTION)) {
                plugin.getServer().getScheduler().runTaskLater(plugin,
                    new Runnable() {
                        public void run() {
                            if (pl.getInventory().getItem(slot).getType().equals(Material.GLASS_BOTTLE)) {
                                pl.getInventory().getItem(slot).setType(Material.AIR);
                            }
                        }
                }, 1L);
            }
        }
    The code runs, I checked it with debug messages and errors that I got before when I tried to set the inventory spot to 'null'. But now when I changed it to Material.AIR it doesn't give an error in console and nothing happens in game?! Someone who can help me?
     
  2. Offline

    Tecno_Wizard

    @bennie3211 I was pretty sure that slots save as null, so that's surprising. Are you sure that you are setting the item to null and not the material?
     
  3. Offline

    SuchSwegMuchWow

    @bennie3211 Try updating inventory or simply player.removeItem();
     
  4. Offline

    bennie3211

    @Tecno_Wizard The variable 'slot' is a number, it works perfect if I do what @SuchSwegMuchWow said, pl.getInventory().remove(Material.GLASS_BOTTLE); or pl.getInventory().removeItem(slots)but that will remove all glass_bottles, and I only want to remove the one that was a potion before, else players will lose there normal glass_bottle.

    The error that I get when I try to set the empty potion bottle to null is:

    Material cannot be null. (IllegalArgumentException)

    EDIT: Sorryfor double post, I didn't know it was posted alread :s
     
  5. Offline

    SuchSwegMuchWow

    @bennie3211 player.removeItem(Material.GLASS_BOTTLE, 1); will only remove 1
     
Thread Status:
Not open for further replies.

Share This Page