1.12.2 Create Complex inventory system

Discussion in 'Spigot Plugin Development' started by GabrielVitza, Jul 17, 2021.

  1. Hi all!

    Can you suggest me the best way to create a complex inventory system with many items.

    How better:
    - Store each item in a list as an ItemStack and clone each item each time a player opens an inventory because those items contain placeholders that need to be replaced.
    - To store a serialization of items like String, to replace those placeholders and then to deserialize to ItemStack every time a player opens an inventory.

    Which of these two methods is more efficient: Cloning ItemStacks or deserializing them
    It would also be good to store each inventory in another list or create it each time a player opens a new inventory.

    sorry for my bad english!

    Serialization and deserialization methods:

    Code (Text):
    public static String serialize(ItemStack itemStack) {
            YamlConfiguration config = new YamlConfiguration();
            config.set("i", itemStack);
            return config.saveToString();
        }
     
    public static ItemStack deserialize(String item) {
            YamlConfiguration config = new YamlConfiguration();
            try {
                config.loadFromString(item);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return config.getItemStack("i", null);
        }
     
  2. There are many libraries people have created for GUIs - I suggest you use one of them or create one of your own.
    It will make GUIs SOOOO much easier.
    And don't store the items serialized (unless you get them from a config cuz it's configurable).
     
    • Like Like x 2
  3. There are many libraries people have created for GUIs - I suggest you use one of them or create one of your own. It will make GUIs SOOOO much easier. And don't store the items serialized (unless you get them from a config cuz it's configurable).
    I already have my own library through which I create inventories.
    what I wanted to know is if it would be better to store all serialized items in a list, create them all when the server starts and store them in a list as ItemStack or create the items of each if a player opens an inventory.

    I want to specify that in the description and display name of those items there are variables that need to be replaced and I thought that if they are saved as String in lists it will be much easier for me to replace those variables.

    So you're saying it's more efficient to store items like ItemStack in a list?

    Thanks!
     
  4. Store the items somehow - list, map, individual items, something else - and they'll be your templates.
    Then create a function that clones and edits those template items as needed.
     
    • Like Like x 1
  5. Ok I understand!

    for inventory, do you think it would be better to store them in a list or to create them every time a player opens that inventory?
     
  6. Store them in a list and put them in the inventories on their opening. To avoid future bugs I suggest to clone the items via the ItemStack#clone() method before adding them to the inventory, or every action done on them will be reflected on your list.
     
    • Like Like x 1
  7. ok, I understand, the best way is to store them in a list!

    In the case of inventories, do you think it would be good to put them on a list or to create them every time a player opens a new inventory?
     
  8. It depends on how many items you have. If you have 5 items you can use 5 variables to store them. Then copy them in your inventory when you create it. If you have a lot of items it should be preferred to use a list ot a hashmap (with indexes as keys and itemstacks as values).
    Remember that, for example, to create the classic glass pane border around your inventory (usually this ia done for guis) you just need 1 variable, the glass pane item. Then you can copy it and place it in your inventory all around in a cicle, but you need only just 1 variable.
     
    • Like Like x 1
  9. No, you didn't get it right ...

    I was referring to inventory as an object, for example:
    Inventory shop = Bukkit.createInventory (null, "Shop Inventory", 6 * 9);

    Do you think that these inventories should be created once the server starts and I store them in a list or create them every time a player opens a certain inventory?
     
  10. Create new inventory for each player, each time they open it a new one - definitely!
    If you have information updating per player then using shared (saved) inventories will be a mess when multiple players open it at once - they'll be seeing other people's info, items will be duped, etc.
     
    • Like Like x 1
  11. Ad @DMan16 pointed out, you should definitely create an inventory each time. Moreover, if you open the same inventory to two players at the same time every action one does on it will be reflected to the other players, since you opened to them the same inventory.
     
    • Like Like x 1
  12. yes, I know that, but inventories can be cloned just like ItemStacks...
     
  13. They can be cloned by creating a new one and copying the items... Which is exactly what we said - create a new inventory.
     
    • Like Like x 1
  14. Ok, I understand!
    Thanks for the help!