Solved Get player Inventory

Discussion in 'Plugin Development' started by Aragone, Mar 29, 2020.

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

    Aragone

    Hi there,

    I'd like to save player inventory so I need to get every item and then get the durability of tools. How can I do that ? Because I don't see anything about that in the itemmeta proccesses.

    Thank you.
     
  2. Offline

    KarimAKL

    @Aragone I'm not exactly sure but i think you need to do this now:
    Code:Java
    1. ItemStack item = ...; // Your item
    2.  
    3. ItemMeta meta = item.getItemMeta();
    4.  
    5. if (meta instanceof org.bukkit.inventory.meta.Damageable) {
    6. Damageable damage = (Damageable) meta;
    7.  
    8. //damage.getDamage()
    9. }
     
  3. Offline

    Aragone

    @KarimAKL I changed of method. It doesn't work and I don't know why. Can you help me ?
    Don't pay attention to all of lines in the main code, that's just animation and teleporting. The most important is Is.serializeInventory()
    Plus, no file created in the folder InventorySerializer

    InventorySerializer class : https://pastebin.com/raw/B4sjWPYh
    Main class : https://pastebin.com/raw/0RLXGX1v


    Console : upload_2020-3-29_15-26-21.png
     
  4. Offline

    NukerFall

    If your inventory is not full, and u try to get all slots as itemstacks, you will have troubles with NullPointerException
    Try doing all of your checks without using object for ItemStacks
     
  5. Offline

    Aragone

    Ok I will try that. Do you think put ItemMeta directly in ArrayList will work ?
     
  6. Offline

    KarimAKL

    @Aragone No, but you should be able to set the ItemStack itself, then use ConfigurationSection#getItemStack(String path).
     
  7. Offline

    NukerFall

    By the way
    If you want to store inventory, you have to add {
    Material
    Amount
    Name
    Lore
    NBTTags
    Damage
    Enchantments
    ...
    }
    are u ready for this?

    Code:
    for (ItemStack i : inventory.getContents()) {
        if (i!=null) {
            Material m = i.getType();
            Integer amount = i.getAmount();
            if (i instanceof Damageable) {
                Integer damage = (Damageable)(i)).getDamage();
            }
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 29, 2020
  8. Offline

    Aragone

    I don't understand what you mean by configuration section. What is it ?

    It's enough ? We don't need to save the enchants etc ?
     
  9. Offline

    KarimAKL

    Look at the javadocs.
     
  10. Offline

    timtower Administrator Administrator Moderator

    ItemStack is serializable by default.
    There is even a getItemStack for ConfigurationSection
    @Aragone
     
  11. Offline

    NukerFall

    but work with nbt will be hard anyway
     
  12. Offline

    Aragone

    Yes I saw that in the java docs but I don't understand how to use it. Where do I have to put it in my code ?
     
  13. Offline

    timtower Administrator Administrator Moderator

    Start by using YML instead of custom txt files.
     
  14. Offline

    Aragone

    Ok so I changed : https://pastebin.com/raw/ebNrwsQq

    Now I don't know how to use getItemStack.
     
  15. Offline

    timtower Administrator Administrator Moderator

    @Aragone You need a FileConfiguration for that, a Bukkit class.
    Not a FileOutputStrea
     
  16. Offline

    Aragone

    :/ What are you speaking for ?
    Let's sum. I need to save the player inventory in a file. So I have to build a list which will contain the itemstacks and their configurations (type, amount, enchantments etc.)

    So for that, you said, you can use getItemStack method. But the javadocs is really blurry. I don't understand how to use. Do I have to separate the code in two blocks ? One for build the list and a second one to save the list in a file or do you want me to make it in one block ?

    I'm so sorry but I'm a french guy so that's very complicated for me to understand you, mostly a technical vocabulary like that...
     
    Last edited: Mar 29, 2020
  17. Offline

    KarimAKL

  18. Offline

    timtower Administrator Administrator Moderator

  19. Offline

    Aragone

    @timtower
    Oh man I gonna be crazy. For a start, in French it's already complicated for me to undertsand all, but in english it's impossible. I understand two words in each sentences. Too technical.

    I think I will just drop.
     
  20. Offline

    NukerFall

    Don't drop. Do
    getConfig().set("test", itemstack);
    saveConfig();
    and see whaat it does
     
  21. Offline

    Aragone

    Ok I found I think the only tutorial in French about that.

    I try something like that : https://pastebin.com/raw/v4bGkeWW

    The problem is on the File line : File playerInventory = new File(...);

    Console : upload_2020-3-29_22-30-50.png
     
  22. Offline

    KarimAKL

  23. Offline

    Aragone

    Ok so I resolved a lot of problems. Now the problem is that the program failed to find the folder. I would like to put my file in InventorySerializer folder.

    Here's the code : https://pastebin.com/raw/cK3EF94W

    upload_2020-3-30_15-18-18.png
     
  24. Offline

    timtower Administrator Administrator Moderator

    @Aragone main.getDataFolder() will return a folder in the plugins folder.
    Where things like this should be, not in the server root.
     
  25. Offline

    Aragone

    @timtower
    Ok I tried to change the code as :

    File file = new File(main.getDataFolder() + "/InventorySerializer/" + "myfile.yml")
    but it doesn't work. I tried also with File.separator instead of "/"

    upload_2020-3-30_15-32-11.png
     
  26. Offline

    timtower Administrator Administrator Moderator

  27. Offline

    Aragone

  28. Offline

    timtower Administrator Administrator Moderator

    @Aragone And how did you test that?
     
  29. Offline

    Aragone

    File file = new FIle(main.getDataFolder() + "myfile.yml");

    So after I tried to append InventorySerializer folder and the server said nop
     
  30. Offline

    timtower Administrator Administrator Moderator

    @Aragone You can also print the location of the main.getDataFolder(), might be better for now, no making files.
     
Thread Status:
Not open for further replies.

Share This Page