I created inventory: Inventory inventory = Inventory.builder().of(InventoryArchetypes.CHEST).build(GWMCrates.getInstance()); //There is some manipulations with inventory. player.openInventory(inventory, GWMCrates.getInstance().getDefaultCause()); FIRST_GUI_INVENTORIES.put(inventory, player);
OKay, all good.
After this, I tried to cancel any clicks in this inventory: @Listener(order = Order.LATE) public void cancelClick(ClickInventoryEvent event) { System.out.println(20); for (Inventory inventory : FirstGuiOpenManager.FIRST_GUI_INVENTORIES.keySet()) { System.out.println("inventory from set = " + inventory); } Container container = event.getTargetInventory(); System.out.println("event container = " + container); Optional<Inventory> optional_inventory = event.getCause().first(Inventory.class); if (!optional_inventory.isPresent()) return; Inventory inventory = optional_inventory.get(); System.out.println("event inventory = " + inventory); System.out.println("contains1 = " + FirstGuiOpenManager.FIRST_GUI_INVENTORIES.containsKey(container)); System.out.println("contains2 = " + FirstGuiOpenManager.FIRST_GUI_INVENTORIES.containsKey(inventory)); }
Not helps, I can click/drag/interact with items in this inventory, console output:
OKay, I can not understood, how to compare inventories?
This not works too: Optional<Container> optional_container = player.getOpenInventory(); if (optional_container.isPresent() && inventory.equals(optional_container.get())) {
I want to cancel event only if clicked inventory, is in my Set FIRST_GUI_INVENTORIES.
But as you can see by text in console “contains1” and “contains2”, its never true, ever when I know that I now clicks on inventory, that my plugin created some moment ago, and added it Set FIRST_GUI_INVENTORIES.
In second post, I comparing “player.getOpenInventory() and my inventory”.
I 100% know, that Im now see that inventory, which my plugin created earlier, but got “false” as result of comparing…
Maybe I wrong compare them?
Maybe inv1.equaks(inv2) not suitable here?
Maybe there is another method?
It’s possible that player.getOpenInventory() in fact returns a view, combining the actual open inventory and the player’s own inventory. Try making your own InventoryProperty and applying it to your inventory during building, and then querying for it in your equality test. You could also just use the Identifiable InventoryProperty.
Edit: Also, NEVER use mutable values as HashMap keys. Not ever. Due to hash bucket calculation, if some property of the class ever changes, it’s easy for it to get lost in the map, and it won’t be findable by containsKey or get if this happens. The same will happen for any container implementation relying on hashing, such as HashSet.
Can you please write code example, how to set identifiable property to inventory, and how to get it?
I writed this code: Inventory inventory = Inventory.builder().of(InventoryArchetypes.CHEST). property("identifiable", new Identifiable(UUID.randomUUID())). build(GWMCrates.getInstance()); Collection<Identifiable> properties = inventory.getProperties(Identifiable.class); if (properties.isEmpty()) { System.out.println("Created inventory empty properties!"); } else { properties.forEach(identifiable -> System.out.println("Identifiable from created inventory = " + identifiable)); }
But in console I see “empty properties”…