1.16.5 Inventories

Discussion in 'Spigot Plugin Development' started by DJCowGaming, Nov 26, 2020.

  1. I'm working on a plugin to make custom blocks like forges and whatnot. I have a class called AbstractBlock that can have an inventory, and when a player interacts with the block, it opens the inventory. My problem is I have no way of checking if the inventory is that specific inventory in the click event or the close event, as multiple inventories have the same name. Any ideas on how to get the specific inventory in an event? It would be really nice if the was a way to have events on the inventory like in swing or fx, or for there to be inventory metadata or something.
    • Friendly Friendly x 1
  2. Fairly certain you should compare the InventoryView, accessible with #getView.
  3. Your only option outside of that from my knowledge is checking the contents of the Inventory.

    Edit: I could be wrong, The option that Pika provided above could also work
  4. But how do I get an instance of an InventoryView from an inventory?
  5. InventoryView is not an inventory, it is like "opened inventory" and you can't get it from Inventory. But you can get it from InventoryEvent#getView() and HumanEntity#getOpenInventory()
  6. Oh, I'm trying to compare a newly created inventory. If I use InventoryEvent#getInventory(), can I use .equals to my inventory?
  7. Can you change the inventory name? or add § (code color) at the end of the name for each one
  8. But I don't know how many instances there are going to be with the same inventory. Is there another way, maybe text that cannot be seen with more options than the color codes?
  9. yes
  10. Comparing the names of the inventories is the best solution.


    You can compare them w/ .equals();, just make sure if you have color codes that theyre either stripped or correctly inputted
  11. I've always instead just used the hash code to compare the inventories in my systems, since the hash should be unique for the object (create your own hashing method if you like). That way you can always be sure you're addressing the same inventory. At least that's worked for me for the last few years.
  12. You can know which inventory is the one that is open simply by checking the inventory name