Inventory #setCancelled() problem...

Discussion in 'Spigot Help' started by MakotoMiyamoto, Feb 23, 2020.

  1. My UI is programmed to only allow the player to put/take items from slot 10 of it's inventory. Theoretically, this works. But, sometimes (and at complete random), the item will decide it wants to drop in another part of the UI (again, at random). I'm really stuck here; any feedback is greatly appreciated!

    Edit: This problem doesn't occur when trying to put an item in a slot that already has an item in it. (i.e. the black stained glass panes in the video [seen below]).

    Edit 2: Obligatory "I know I could do it this way", but I have thought of just putting a different color of glass panes to occupy the empty slots, but that is not the kind of solution I want to resort to. I want to be able to safely use a GUI with empty slots without the risk of the aforementioned issue.

    Edit 3: I have become aware of another issue where shift-clicking an item will also put it in a prohibited slot. My fourth edit will patch that issue.

    Edit 4: Shift-clicking patched, original bug still remains.

    Code (Java):
    @EventHandler
    public void onClick(InventoryClickEvent event) {
        Inventory windowClicked = event.getClickedInventory();
        InventoryView view = event.getView();
        if (windowClicked == null || !view.getTitle().equals(Data.NAME)) {
            return;
        }
        if (event.getAction().equals(InventoryAction.MOVE_TO_OTHER_INVENTORY)) {
            ItemStack itemInSlot = view.getTopInventory().getItem(10);
            if (itemInSlot != null) {
                if (event.getSlot() != 10 && windowClicked.equals(view.getTopInventory())) {
                    event.setCancelled(true);
                    return;
                }
                if (windowClicked.equals(view.getBottomInventory())) {
                    event.setCancelled(true);
                }
                return;
            }
            if (windowClicked.equals(view.getTopInventory())) {
                event.setCancelled(true);
                return;
            }
        }
        if (windowClicked.equals(view.getTopInventory()) && event.getSlot() != 10) {
            event.setCancelled(true);
        }
    }
    Video:
     
    #1 MakotoMiyamoto, Feb 23, 2020
    Last edited: Feb 23, 2020