Solved Why it is impossible to get inventory title from Inventory object?

Discussion in 'Spigot Plugin Development' started by Msql, Feb 13, 2020.

  1. Why it is impossible to get inventory title from Inventory, for example inventory.getTitle()?
    As I remember good it was possible in older versions.
    It is problematic when I want to serialize inventory and I can not get this type of data from Inventory object.
  2. I believe what you're looking for is

    Code (Java):
    public void name(InventoryClickEvent e) {
            String inv = e.getView().getTitle();
            if (inv.equals("My Custom Inventory!") {
    // Do Important Stuff
    • Like Like x 1
  3. FrostedSnowman

    Resource Staff

    It was deprecated and eventually removed because difference instances of the same inventory could potentially have different titles. You can now access it through InventoryView::getTitle. You can access InventoryView's through all, if not most of the inventory events.

    Is there a reason you need the title when serializing it? Realistically all the serialization is doing is serializing the itemstacks themselves. Unless you're binding the title as some identifier, there should be no need for a title to be involved.
  4. I need to access it from Inventory, because I am creating inventory using Bukkit.createInventory(...) method. I can not do it through event because I serialize inventory on plugin disable (and in saving task) to save it and then deserialize when plugin will enable. To do that correctly I need inventory name because only ItemStacks can be serialized. I see that I have to create my custom class that will implements Inventory and it will hold title inside?
    #4 Msql, Feb 13, 2020
    Last edited: Feb 13, 2020
  5. I see that Inventory class is an interface so what exactly object is being returned from Bukkit.createInventory(...) that implements Inventory?
  6. almost every class in the bukkit api is an interface, you should take a look at craftbukkit packages and look for the class that implements that interface, usually it has the same name with a "Craft" at the begining, for example "CraftInventory"
    • Like Like x 2
    • Agree Agree x 1
    • Useful Useful x 1
  7. Unlucky CraftInventory class also do not have .getTitle method, but I see in 1.13 it has. There is no way to get title name without event?
  8. Try to check where it is saved the name on the Bukkit.createInventory(...) method
  9. I assume you are storing your inventories in your plugin? I would create a HashMap or something to store the inventory and its title. Then the data is always available, regardless of if the inventory is being opened or not.
  10. Where can I find source code of bukkit/craftbukkit?
  11. You can use BuildTools and you will get the code, but you can also use a decompiler. I will take a look using a decompiler.
    • Friendly Friendly x 1
  12. Thats what I plan if there will be no way to grab this name from Inventory object. New class that will implements Inventory.
  13. I have looked into source but from file explorer it is hard to find classes that implements "Server" interface. In one of this classes there is createIventory method and probably String title field :(
  14. i found it inside CraftInventoryCustom.class, this should work:
    Code (Java):
    Inventory inv; //Your bukkit inv

    CraftInventory cinv = (CraftInventory) inv;

    MinecraftInventory minv = (MinecraftInventory)cinv.getInventory();

    String title = minv.getTitle();
    Edit: if you want to look, the class that implements Server is called CraftServer
    • Winner Winner x 1
  15. Thanks a lot <3
    I just wonder why it was nessesary to remove getTitle() from Inventory interface. I bet that it is related to 1.14 upgrades where Mojang added lots of new inventory types where there is no title but spigot devs could create easier way to get title or make null return if inventory does not contain title :(
    • Like Like x 1
  16. By the way, I can not import MinecraftInventory class using 1.15.2 spigot :/
  17. Try with "IInventory" (with two "I") instead of MinecraftInventory, i will see the code again
  18. IInventory works but there is no .getTitle() method :c
    Damn.. This spagetti code xD
  19. Oh the class is insithe CraftInventoryCustom class so change MinecraftInventory to CraftInventoryCustom.MinecraftInventory
    Code (Java):
    Inventory inv; //Your bukkit inv

    CraftInventory cinv = (CraftInventory) inv;

    CraftInventoryCustom.MinecraftInventory minv = (CraftInventoryCustom.MinecraftInventory)cinv.getInventory();

    String title = minv.getTitle();
  20. Unlucky, I think it is probably nessesary to set it public using reflection but it is waste of performance :|
    The best way it will be to probably create own class that will implements Inventory and add title field & method