Solved Detecting a player having an item added to their inventory

Discussion in 'Spigot Plugin Development' started by PCPSells, Jan 13, 2020.

Thread Status:
Not open for further replies.
  1. Hello fellow users of SpigotMC! I'd like to thank you for taking the time to read my post and possibly helping me out! :)

    Note: Help for any MC version is fine with me. Haven't started working on the plugin yet as I thought of this issue.

    What I'm looking for is a method to detect if an item has been added to a players inventory.
    Typically most plugins use the 'player.getInventory().addItem(ItemStack item);' method
    and I was wondering if anyone knew about a method to track this void being ran without having to use a timer to compare all players inventories every x amount of seconds.

    I thought I'd have to make a simple custom event to fire inside of Spigot's Inventory class, but of course that'd require other people to have some sort of custom Spigot.jar. (And the Inventory class doesn't have a source linked)

    Any help would be greatly appreciated!
  2. This is my first instinct, but that, of course, could get pretty heavy. I don't know of any Spigot events that can get this accomplished.

    I see a few other threads ask this same question, they don't include plugins modifying inventories.

    There is a chance the plugins that modify a Player's inventory have events for this kind of thing, but not many plugins have that extensive APIs (You could always ask the developers to add that). Of course, this means that you have to hook into any plugin that changes the contents of a Player's inventory.

    May I ask the reason why you want to track items being changed? Perhaps I can be of more use then?

    They wouldn't need a custom.jar, they would just need to add your plugin as a dependency. You should look into custom events.
  3. Joshb_


    Besides a timer, I really only see you checking to see if the plugins have an API you can access.

    Heres the only events I see that change the player's inventory.
  4. Haha, hey man. I actually did some googling around 1st before posting this and saw you respond to the other one here on Spigot.

    The reason for why I'm interested in this is because I want to make a Custom Backpack resource for mainly prison type servers.
    Basically when mining or even just opening a crate, it would check if the player has a Custom Backpack and if so, add it to said Custom Backpacks inventory (if not full, or the player has another Custom Backpack of the same type and that one isn't full).
    The player wouldn't even need the Custom Backpack in their inventory for this to work.

    I actually already have this plugin coded for my server and works just fine, but I was looking into recoding it to be more efficient and thought why not just making a fully configurable one for Spigot.
    Would be kind of revolutionary for the whole backpack scene for prison servers.

    Of course I want this to work with all plugins, instead of having to bug every single developer with adding support as this would be such a broad span type of resource where I'd have to bug VK (TokenEnchant), almost all Crate plugin developers and of course, multiple others.
  5. Haha, which one?

    Create some sort of update method that checks if a player has a backpack, and searches through their inventory and puts that item into the backpack's inventory.

    On a (long) timer, call this method.

    Some other things to do:
    • Listen for players breaking blocks, and just put that block in their inventory without letting the item drop. Save yourself that computing
    • Hook into popular crates plugins and call the update method for players that open a crate
    • Listen for players clicking in inventories to see if the clicked block is of a certain material, ....
    • Listen for players picking up items
    So in the end, you have little "tricks" to try and avoid the timer, but spigot doesn't provide enough to accomplish what you want.
Thread Status:
Not open for further replies.