Custom Inventory (Help/2)

Discussion in 'Plugin Development' started by ShadowBalrog, Apr 8, 2014.

Thread Status:
Not open for further replies.
  1. Offline


    So I am making a plugin with custom inventories, basically when you type /backpack you get a custom inventory. The problem I have is I want to save the item stored into the backpack as it doesn't after I /stop and re start the server up. Please note I am new to Java coding so don't expect me to know everything :p (although I know quite a lot of the basics.)
  2. Offline


    Store it in a temp yml file (Or just in the config)
  3. Offline


    Use a config file to store a list of strings under each player's name. When onDisable() is called, iterate through each item in each inventory, serialize it, and put it in that player's section of the config. When onEnable() is called, iterate through each list, deserialize the strings back into itemstacks, and put those into the respective inventories.

    You will need to find some way of converting itemstacks to and from strings. Here are the methods that I often use for config files that would also work here:
    1. String serializeItem(ItemStack item) {
    2. String str = String.valueOf(item.getTypeId());
    3. if (item.getAmount() > 1)
    4. str += ":" + item.getAmount();
    5. if (item.getDurability() != 0)
    6. str += ":" + item.getDurability();
    7. return str;
    8. }
    10. ItemStack parseItem(String str) {
    11. String[] split = str.split(":");
    12. if (split.length > 0) {
    13. ItemStack item = new ItemStack(Integer.valueOf(split[0]);
    14. if (split.length > 1)
    15. item.setAmount(Integer.valueOf(split[1]));
    16. if (split.length > 2)
    17. item.setDurability(Short.valueOf(split[2]));
    18. return item;
    19. }
    20. return null;
    21. }
  4. Offline


    coasterman10 , hey coasterman. If this is my code: ...
    where shall I put the code above you sent me? I tried putting in the onEnable/Disable and it didn't work :p... please help,
    Much appreciated,
  5. Offline


    ShadowBalrog The code I gave you is a pair of methods that can be useful in saving/getting individual itemstacks from a config, but it won't work on its own.

    The proper way to use it would be to put it outside of the other methods. In onDisable(), loop over all the slots of your inventory, then save the value that serializeItem returns for that. Then, to get it back from the config, you do the same thing, but save the ItemStack that deserializeItem returns for the string from the config to the inventory.
Thread Status:
Not open for further replies.

Share This Page