How can I modify an offline players inventory

Discussion in 'Spigot Plugin Development' started by minion325, May 29, 2018.

Thread Status:
Not open for further replies.
  1. I don't need any sort of code whatsoever.. it's just that I cant imagine a lightweight for it to be done.

    My current thought is to save the item that needs to be added to their inventory to a yml file and check if their UUID is contained in the file every time someone comes on. Why i don't want to do this is because larger servers have manny players and the yml file can become pretty large.

    Anyone has any idea how i may be able to do this?

    PS. i only want to add items to the inventory.
  2. Is this item will be added everytime a player joins? Even the same player?

    I also think that you probably dont need to save the item since you are about to give it when player logs in, or am I wrong?
  3. JanTuck


    You can modify the NBT data file located in the playerdata world folder.
    • Agree Agree x 2
  4. Its a different item each time and not every player gets it. only certain players on the list
  5. Will try this thanks
  6. A way is by having a Per Player UUID data file if that's your issue for big servers.
  7. I don't want to have to read and write files a lot since that can be expesive if done regularly

    NBT looks more promising illI try it.
  8. Does anyone know what the var2 in NBTTagCompound#getList(String var1, int var2) method is used for?

    Thinking about it, ill go with having a file for each player.. i dont mind learning more about NBT though if anyone who knows about it a lot comes about reading this
    #8 minion325, May 29, 2018
    Last edited: May 29, 2018
  9. JanTuck


    You are free to google it, here is how the inventory is stored in the dat file.

  10. Yeah i ised nbteditor and realized its stored in a tree like manner but when i put random numbers for var2 i get an empty nbtlist
  11. I used to store players inventory as a base64 string, then reconstruct it into an ItemStack[] to view the items. This also relied on SQLite for data saving.
  12. Nah this is getting to complex.

    I just want to add an item to their inventory.. i guess i can use files to do so since the server already loads up a player file on join. Ill jist be loading another one
  13. You have to save his inventory on the config file before he logout.
  14. Just save a .yml file named to the player in question's uuid, and when a player joins the game attempt to load said file. if(File#exists()) add the ItemStack saved in the config file to their inventory, and remove it as well. You can use a list of itemstacks as well to be able to add as many items as you want. Inventory#addItems() returns an array of things that couldn't be added to a player's inventory so you could add them to the config file under a different key and use it as a "collection box" of sorts.
    • Agree Agree x 1
  15. You can use PowerNBT plugin, its simple and easier to use.
    • Informative Informative x 1
  16. Answering this just in case somebody else comes across this thread with the same question...
    It's the expected type of the NBT List, as follows (copied from my NMS utility class):
    Code (Java):
    public final class NBTTagType {
        public static final int COMPOUND = 10;
        public static final int LIST = 9;
        public static final int STRING = 8;
        public static final int LONG_ARRAY = 12;
        public static final int INT_ARRAY = 11;
        public static final int BYTE_ARRAY = 7;
        public static final int DOUBLE = 6;
        public static final int FLOAT = 5;
        public static final int LONG = 4;
        public static final int INT = 3;
        public static final int SHORT = 2;
        public static final int BYTE = 1;
        public static final int BOOLEAN = 1;
    In this case, like JanTuck showed in the picture, you'll be expecting compounds - so the 'var2' should be 10.
    • Like Like x 1
    • Useful Useful x 1
  17. I already did this.. the only reason i didn't mark this thread as solved is because i wanted to learn a bit more about NBT.
  18. Thank you soo much <3

    If i i ne more help I'd post it here
  19. Problem solved.. No more help needed thanks everyone. :)
Thread Status:
Not open for further replies.