Solved InventoryCloseEvent firing when opening inventory after interacting with block

Discussion in 'Spigot Plugin Development' started by CablePlays, Jul 11, 2021.

Thread Status:
Not open for further replies.
  1. I have a listener for inventories closing:

    Code (Java):
    public class InventoryClose implements Listener {

        @EventHandler
        private void event(InventoryCloseEvent e) {
            Bukkit.broadcastMessage("INVENTORY TITLE: " + e.getView().getTitle());
        }
    }

    I have a listener for players interacting:

    Code (Java):
    public class PlayerInteract implements Listener {

        @EventHandler
        private void event(PlayerInteractEvent e) {
            Player p = e.getPlayer();

            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                p.openInventory(Bukkit.createInventory(p, 9));
            }
        }
    }

    When a player right-clicks a block, it opens an inventory. This inventory's name is broadcasted because of the InventoryCloseEvent listener. Why is this happening if the inventory is being opened?

    When I created a command that opened an inventory, the InventoryCloseEvent listener did not pick it up. Any explanations would be appreciated!
     
  2. What plugins are you using on your test server, and what fork (spigot, paper, purpur, etc.)? Also, I believe this was a reported bug on 1.16, but it got solved.
     
  3. Hi! When I was testing I removed all my plugins and had only those two listeners running. I'm running paper 1.17.1, and I don't know what solved means because it isn't fixed in my current version.
     
  4. If this is a bug then I'll just wait until it's fixed. Can somebody confirm that it is indeed a bug or hit me up with an explanation to why this happens?
     
  5. Haha thanks but no thanks. I'm just curious about my issue, but it's not preventing me from what I plan to do.
     
  6. Try with
    Code (Java):
    Bukkit.createInventory(null, 9)
     
  7. When you right click the block, the player interaction event is fired twice, once for both hands. The inventory opens twice, and because it opens twice, the first one closes, firing the closing event.

    The inventory doesn't close, it's only opened
     
  8. Thanks! XD
    I don't know why I didn't realize this, I've had this problem before. :)
     
Thread Status:
Not open for further replies.