When Player::openInventory(inventory) can return Optional.empty()?

In the docs I see this in the description to the Player::openInventory(inventory) method:
The opened Container if the inventory was opened, otherwise Optional.empty()
Can somebody clarify this to me, what is possible reasons that inventory will not be opened?
I am sometimes getting Optional.empty() in my plugin, and can’t understand why…

Only inventories with an associated Container can be opened.
You query for all slots with diamonds in a player inventory.
Inventory inv = player.getInventory().query(QueryOperationsTypes.ITEM_TYPE.of(ItemTypes.DIAMOND))
You can still manipulate this inventory using the API in Sponge. (set/offer/peek/poll)
But how to open/display that inventory is not defined.

Isn’t it also true that in some rare cases a player with an inventory already open can’t be made to open another one without closing the current one first?

You cannot open an inventory during an inventory event.

I am not sure what does with an associated Container mean, but I am almost sure that my inventory is associated with an Container, because usually it opens successfully, but sometimes (rare) I encounter java.util.NoSuchElementException: No value present.
Is there any other reasons why it can return Optional.empty() (not always, really rare, but sometimes it happens)?
P.S. Here is how I create inventory:

Inventory.Builder builder = Inventory.builder(). of(InventoryArchetypes.CHEST); Inventory inventory = builder.build(...);

I think I figured it out.
Most likely it happens when another plugin (and more specifically - GriefPrevention) cancels InteractInventoryEvent.Open
And I have no idea how to bypass it :frowning:

I know a bypass that worked on Bukkit that you could do. Not sure if it works on sponge, haven’t looked into how sponge handles a cancelled event.

Basically the idea is that you uncancel the event. You can do this by putting the event priority to LAST, and then say event.setCancelled(false).

I think it will work, but I hope there is better solution, because its seems to be bad way :frowning:

I hope there is better solution

Just don’t cancel the event in the first place…

Sorry, maybe I did not understand you, but I do not cancel anything, it is another plugin (GriefPrevention) which cancels InteractInventoryEvent.Open, and because of that (most likely) Im getting Optional.empty() in Player::openInventory(inventory).

I think what he is saying is.

If another plugin cancels the event, let it and let your plugin handle it correctly.