Jannyboy11 / InvSee-plus-plus

A bukkit plugin for manipulating player inventories
Other
84 stars 12 forks source link

API request: View inventory event #94

Closed SnickoDeadMC closed 5 months ago

SnickoDeadMC commented 5 months ago

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.

Jannyboy11 commented 5 months ago

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?

Jannyboy11 commented 5 months ago

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

SnickoDeadMC commented 5 months ago

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

Jannyboy11 commented 5 months ago

Okay, closing this issue as solved.