Problems with InventoryMoveItemEvent

Discussion in 'Spigot Plugin Development' started by xerminator, Apr 16, 2019.

  1. I have a problem with the InventoryMoveitemEvent.

    Don't look into my ugly code to much, I will optimize and clear things up when I have it working the way I want it to.

    Problem: When removing items using destination.removeitem(item);
    It keeps 2 in the destination inventory (hopper or chest).

    Code (Text):
    package testing.enderchest;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Server;
    //import org.bukkit.block.Block;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryMoveItemEvent;
    import org.bukkit.event.inventory.InventoryType;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;

    import iofkrautils.main.Main;

    public class EnderChestTest implements Listener {
        ConsoleCommandSender console = Bukkit.getServer().getConsoleSender();
        public void EnderChestEvent(InventoryMoveItemEvent e) {
            Main main = Main.getInst();
            Server server = main.getServer();
            Inventory source = e.getSource();
            //Location sloc = source.getLocation();
            //Block sBlock = sloc.getBlock();
            Inventory destination = e.getDestination();
            Location dloc = destination.getLocation();
            //Block dBlock = dloc.getBlock();
            List<ItemStack> temp = new ArrayList<ItemStack>();
            if(destination.getType() == InventoryType.HOPPER) {
                server.broadcastMessage("Hopper Found!");
                boolean hasEnderchestBeneath = dloc.subtract(0,1,0).getBlock().getType() == Material.ENDER_CHEST;
                if(hasEnderchestBeneath) {
                    server.broadcastMessage("Enderchest found!");
                    server.broadcastMessage("Loading Event...");
                    ItemStack[] items = source.getContents();
                    for(ItemStack item : items) {
                        if(!(item == null)) {
                            if(!(item.getType() == Material.AIR && item.getAmount() > 0)) {
                            console.sendMessage("Item: " + item);
                            server.broadcastMessage("Removing... " + item);
                            else {
                                console.sendMessage(item + " is air!");
    Does not matter which amount of items I do it with, it will keep holding 2 in the inventory and it will also not remove without a amount of atleast 3. I don't know why it does this and I can't seem to find anything in the docs either.
  2. md_5

    Administrator Developer

    Please cancel the event rather than try to manipulate the inventories
  3. Do you know how operators work?

    Code (Text):
    if(!(item == null))
    can be easily written as
    Code (Text):
    if(item != null)
    Same with when checking if the item is air
  4. As @md_5 said, just cancel the event instead of changing the inventories.
    • Funny Funny x 1
  5. Yh I know, it is just something I am goofing around with, finding my way of how to write stuff. The issue is that the code is working but it only works when there is atleast 3 items in the stack amount to be removed and then keeps removing it but always keeps 2.
  6. The thing is that I don't want to cancel the event cause I want people able to Put items in a chest with a hopper beneath into a ender chest. So I need to change inventories based on the certain conditions. If I cancel the event the items will never get out of the chest.

Share This Page