Jannyboy11 / InvSee-plus-plus

A bukkit plugin for manipulating player inventories
Other
92 stars 18 forks source link

Invsee Inventory Closed Event #81

Closed creatorfromhell closed 1 year ago

creatorfromhell commented 1 year ago

My suggestion is fairly straight forward. I would like to see an event called when another player's inventory has been closed after using Invsee to view/change it. This should allow the listening plugins to know a few things about the inventory such as:

In order to make this event more efficient, this should only be called when an inventory is closed in edit mode. This is mostly because this indicates that nothing has changed if the player who closed the invsee inventory doesn't have edit permissions.

This will allow for plugins to fully be compatible with Invsee when they use player inventories for various things, but follow the idea that the API doesn't naturally provide access to modify offline player inventories.

Jannyboy11 commented 1 year ago

Hi, I am not exactly sure what your use case is, but all these things that you mention are already possible with the current api. The only thing that is not currently possible is to be notified when a SpectatorInventory is saved. But I don't think this is necessary. SpectatorInventories automatically get updated when modified by a player or by a different plugin. They are backed by the actual nms inventory of the target player. Hence watching another admin change the content's of a target player in real-time works so well in InvSee++. See: https://github.com/Jannyboy11/InvSee-plus-plus/wiki/Persistence#what-if-the-target-player-logs-in-while-being-spectated.

Here is an example of how to do the things that you required with the current api:

import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.inventory.ItemStack;
import org.bukkit.entity.HumanEntity;

import com.janboerman.invsee.spigot.api.SpectatorInventoryView;
import com.janboerman.invsee.spigot.api.target.Target;

class MyListener implements Listener {

    @EventHandler
    public void onClose(InventoryCloseEvent event) {
        if (event.getView() instanceof SpectatorInventoryView invseeView) {
            //get the player owning the inventory:
            Target target = invseeView.getTarget();

            //items currently in the inventory:
            ItemStack[] contents = invseeView.getInventory().getContents();

            //checking whether the inventory was modified by this spectator:
            boolean modifiedContents = !invseeView.getTrackedDifference().isEmpty();

            //getting the spectating player:
            HumanEntity spectator = invseeView.getPlayer();
        }
    }

}

Here are some links to some javadocs:

Note that Target is an algebraic data type (sum of products), you must check which implementation it is and then get properties of of those, e.g.:

//requires import com.janboerman.invsee.api.target.*;

Target target = ...
if (target instanceof PlayerTarget playerTarget) {
    org.bukkit.entity.HumanEntity player = playerTarget.getPlayer();
    //can call player.getName() or player.getUniqueId() here.
} else if (target instanceof UniqueIdTarget uuidTarget) {
    java.util.UUID playerId = uuidTarget.getUniqueId();
} else if (target instanceof UsernameTarget usernameTarget) {
    java.lang.String username = usernameTarget.getUsername();
}

Having said all of that, I think I can implement an event api that is called when a target player inventory is saved tomorrow. Do you think you still need it?

Jannyboy11 commented 1 year ago

As of release 0.23.5-SNAPSHOT the following two events were implemented:

The wiki page have been updated to include the new dependency: https://github.com/Jannyboy11/InvSee-plus-plus/wiki Note that in order to use the events safely, it's best to check for their existence first Class.forName() because servers might still be running old versions of InvSee++. You can check the versions of InvSee++ used in production on the bStats page: https://bstats.org/plugin/bukkit/InvseePlusPlus/9309. Version 0.24.0 (or later) is the version you need to look for since that is the version I released on SpigotMC and that version generally becomes the most popular (although some users don't bother updating anymore a week after the new Minecraft version release).

If these events suit your needs, can you close this issue? If not, do you have more feedback for me? Thanks!

creatorfromhell commented 1 year ago

It seems like the exact events I need. Much appreciated.