Remove an item from a chest

Discussion in 'Spigot Plugin Development' started by Blooker, Apr 28, 2017.

  1. Hello,
    I was trying to remove an item from a chest.
    Code (Text):
    e.getDestination().remove(e.getItem());
     
    It worked fine when there were no items of that type in the chest. But as soon as i put an item in the chest it didn't work anymore... Is that a bug? Am I doing sth wrong?
    Help is appreciated :D
    Thanks
     
  2. WAS

    WAS

    If I am not mistaken, that code is trying to open the inventory, and remove the item which is not yet in the inventory at that very second?

    #getItem() is the item in the act of moving. #getDestination() is the inventory the item will be put into.

    I may be wrong but it appears to be what the docs indicate. So you would be trying to remove an item that doesn't exist in that inventory yet.

    Solution(?): You could run a task to remove the item after a tick, allowing the event to finish.
     
  3. I did that. The code is only a code snippet. It is called 1 tick after the event. The problem is that it doesn't remove an item if there already are items of that type in the inventory
     
  4. Have you tried getting the slot of the item then redusing the items amount by one and setting the slot to the new itemstack
     
  5. I couldn't figure out how to get the slot of the item... How do you do that?
     
    #5 Blooker, Apr 28, 2017
    Last edited: Apr 28, 2017
  6. WAS

    WAS

    Clearly it's not (or you don't understand) as those methods are from the event, which shouldn't be accessed from a task after the event has finished. For one, it would throw a NPE I'm sure on "e".

    You need to get that specific inventory (or pass it to the task) and also pass the item to the task, and than remove the item from that inventory.
     
    #6 WAS, Apr 28, 2017
    Last edited: Apr 28, 2017
  7. Do you mean that I should change
    Code (Text):
        @EventHandler
        public void onChestContentChange(InventoryMoveItemEvent e){
            if(e.getDestination().getType() == InventoryType.CHEST){
                getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                      public void run() {
                            e.getDestination().remove(e.getItem());
                      }
                }, 1L);
            }
        }
     
    into
    Code (Text):
        @EventHandler
        public void onChestContentChange(InventoryMoveItemEvent e){
            if(e.getDestination().getType() == InventoryType.CHEST){
                Inventory in = e.getDestination();
                ItemStack it = e.getItem();
                getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                      public void run() {
                            in.remove(it);
                      }
                }, 1L);
            }
        }
    ?
     
  8. Anyone here to help me?
     
  9. What about testing before bumping the thread? have you tried that what @WAS mentioned? Did it work?
     
    • Creative Creative x 1
  10. I already tried. And I bumped the thread because I really need help
     
  11. Chest#getInventory#removeItem
     
  12. WAS

    WAS

    Something like (untested). You may need to adjust the tasks time.

    Code (Java):
        @EventHandler
        public void onChestContentChange(InventoryMoveItemEvent e){
            if(e.getDestination().getType() == InventoryType.CHEST){
                final Inventory in = e.getDestination();
                final ItemStack it = e.getItem();
                new BukkitRunnable() {
                   @Override
                   public void run() {
                      if  ( in != null && it != null ) {
                         in.remove(it);
                      }
                   }
                }.runTaskLater(plugin, 1);
            }
        }
    Or if not

    Code (Java):
        private static Inventory in;
        private static ItemStack it

        @EventHandler
        public void onChestContentChange(InventoryMoveItemEvent e){
            if(e.getDestination().getType() == InventoryType.CHEST){
                in = e.getDestination();
                it = e.getItem();
                new BukkitRunnable() {
                   @Override
                   public void run() {
                      if  ( in != null && it != null ) {
                         in.remove(it);
                         in = null;
                         it = null;
                      }
                   }
                }.runTaskLater(plugin, 1);
            }
        }