Solved Delete an item on the InventoryMoveItemEvent ?

Discussion in 'Spigot Plugin Development' started by JackkGC, May 18, 2016.

  1. Hey guys,

    I'm trying to make chests work like hoppers (getting items from hoppers above them). I check the locations of the chest and the hopper on the InventoryMoveItemEvent and if the hopper is above the chest I want to do sth. with the item. But the item would duplicate if it's not deleted... So how do I do it? I tried:

    Code (Text):
    e.setCancelled(true);
    Code (Text):
    e.setCancelled(true);
    e.getInitiator().remove(e.getItem());
    Code (Text):
    e.setItem(new ItemStack(Material.AIR));
    and
    Code (Text):
    e.setItem(null);
    EDIT:
    and also as suggested by @worthless_hobo:
    Code (Text):
    event.getSource().remove(event.getItem())
    Nothing works the way I need :(
    Thanks already :D
     
    #1 JackkGC, May 18, 2016
    Last edited: May 18, 2016
  2. Try using something like this
    Code (Text):
    event.getSource().remove(event.getItem())
     
  3. For some reason I think that would bring a few bugs with it. Is it tested?
     
  4. No it's just a suggesting off the top of my head.
    What bugs do you think it would cause?
     
  5. If the entire inventory is full with the same item. It would remove the first one in that inventory, not the one you clicked. (I think)
     
  6. Ah ok, that is possible but this is the InventoryMoveItemEvent not a click event. I think that if this acts like a hopper it would always take the first one in the inventory. I might be incorrect but I believe that it should work.
     
  7. Ah, I think you're right. Humanity got wise by testing, so why not try ;)
     
  8. Sorry to write this, but it isn't working the way I'd like it. I should mention that I got 3 hoppers in a row and the middle one of them is over the chest I want to fill. The item isn't deleted. It's detected by the event and I can add it to the chest, but I just can't remove it.. I don't know why. All the methods I tried didn't do what I want!
     
  9. FINALLY I solved it :D
    It seems like that the item shorty disappears and isn't in any of the hoppers. So you can't remove it from the source and also not from the destination! You have to delete the item from the destionation inventory with a one tick delay! So this code is my solution:

    Code (Text):
    this.getServer().getScheduler().runTaskLater(this, new Runnable() {
                                 
                                public void run() {
                                   
                                    e.getDestination().removeItem(e.getItem());

                                }
                                }, 1L);
     
    Hope this helps a lot of people, like me, who are searching for the soulution to this very confusing problem :D