Solved Inventory changes between opening and closing

Discussion in 'Spigot Plugin Development' started by Big_Bad_E, Feb 2, 2020.

  1. Basically, I'm trying to keep an inventory open unless the player closes it by clicking an item. My current system:
    Code (Text):
        public void onInventoryClose(InventoryCloseEvent event) {
            EnchantmentPlayer enchantPlayer = handler.getPlayer((Player) event.getPlayer());
            if (enchantPlayer.getCurrentGUI() != null && enchantPlayer.getCurrentGUI().getInventory() != null && enchantPlayer.getCurrentGUI().getInventory().equals(event.getInventory())) {
                EnchantmentGUI gui = enchantPlayer.getCurrentGUI();
    Basically, I get/store an object which tracks the current open inventory with an object (EnchantmentGUI).
    The thing is, between the openInventory call on the same inventory, and the player closing the inventory again, the inventory instance changes. Basically:

    Open Inv A -> Close Inv A -> Reopen Inv A -> ? -> Close Inv B -> Inv closes when it shouldn't

    The inventory isn't modified by the player, nothing happens to the player or the inventory between Inv A being reopened and Inv B being closed instead of Inv A.

    What could change the Inventory object to be a different object with the same contents? Should I store something else instead of the Inventory object to be able to differentiate between multiple maybe identical inventories?
  2. Sometimes, only a modified inventory is given. Therefore, you should use a task that is delayed by one tick while opening / closing an inventory.
  3. That was it! Thanks!