Jannyboy11/InvSee-plus-plus

API request: View inventory event

Closed this issue · 4 comments

The API doesn't seem to have a way to get when an inventory is opened. It'd be great to have an event or callback when a player spectates an inventory. There is SpectatorInventoryOfflineCreatedEvent, but it seems to only be for spectating an offline player's inventory.

The intention here is for my plugin to detect when the invseeing begins and when it ends, so that I can do things like cancelling inventory clicks etc.
More specifically, I have a backpack plugin, and right clicking the backpack in the invsee menu let's the player open it and take the items. I am trying to cancel the inventory click event if the player had opened it while invseeing.

The reason such an event is not in the InvSee++ API is because this event is already in the Bukkit API; it's called InventoryOpenEvent. You can check whether the inventory is a SpectatorInventory simply by using:

@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {
    if (event.getInventory() instanceof SpectatorInventory spectatorInventory) {
        //use spectatorInventory here
    }
}

For detecting when spectating ends you can use InventoryCloseEvent similarly.
If you want to obtain the viewing player, use event.getView().getPlayer().

Does this solve your problem, or is there a reason why this is insufficient for you?

Do you think the text on https://github.com/Jannyboy11/InvSee-plus-plus/wiki/Events can be improved in some way?

Yes that is perfect. Thanks very much! What you had in that comment would be great in the docs.

Okay, closing this issue as solved.