Solved Read offline player inventory and save it to Bukkit#Inventory

Discussion in 'Plugin Development' started by NukerFall, Apr 11, 2020.

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

    NukerFall

    I got the file and inv to put my result in.
    Code:
    File inventoryFile = new File(getServer().getWorldContainer()
    + getConfig().getString("world-i-need") + File.separator
    + "playerdata", id.toString() + ".dat");
    Inventory inv;
    Need to grab info from inventoryFile and put it to inv. But i don't know how to do it ;(
    [​IMG]
     
  2. @NukerFall I think that file is encoded with NBT, it'd probably be easier to just save the inventory to a plain text file when they leave the server.
     
  3. Offline

    NukerFall

    I need player inventory which is not known by my plugin. Maybe i would need inventory of player who played 2 years ago last time
     
  4. @NukerFall Maybe you can create a fake player and load the inventory onto that entity? I'm not sure if that's possible though.

    Then you would be able to get the inventory from that entity.
     
  5. Offline

    NukerFall

    What is unknown in words "player data file"
     
  6. @NukerFall I understood that, i was talking about loading the inventory onto a fake player with NMS. (I don't know if creating a fake player with the same UUID as the player you want will give it the inventory though, maybe there's another way if that doesn't work?)
     
  7. Offline

    caderapee

    @NukerFall Here a way to print to a string format the inventory saved. But i dun know how to transform a nbt to a craftinventory.
     
  8. @caderapee Does that decode the file into an NBTTagCompound object? If so, you could get the values from that.
     
  9. Offline

    caderapee

    @KarimAKL It returns a NBTBase Object when you get a key.

    but with the NBTTagCompound, you have acces to a list of keys with the method c(), like inventory, health food etc...
     
    Last edited: Apr 12, 2020
  10. @caderapee NBTTagCompound has a getKeys() method.

    This seems pretty useful, thanks for sharing.
     
  11. Offline

    NukerFall

    Okay i got the NBTBase of in, but it prints empty line...[​IMG]

    okay i got progress upload_2020-4-16_21-52-28.png
    how to transform it xD
     
    Last edited by a moderator: Apr 16, 2020
  12. @NukerFall I've never tried this myself, so i probably can't help much but, i found this, it seems like it could be what you're looking for.
     
  13. Offline

    NukerFall

    Finally done, but my code is too complicated)
     
  14. Then add comments explaining it, that way you can still understand it if you need it again another time.
     
  15. Offline

    timtower Administrator Moderator

    And to add onto KarimAKL, split it up into smaller parts if possible.
     
  16. Offline

    NukerFall

    Code:
    try {
      NBTTagCompound nbt = NBTCompressedStreamTools.a((InputStream) (new FileInputStream(inventoryFile)));
      NBTTagList inventory = (NBTTagList) nbt.get("Inventory");
      Inventory inv = new CraftInventoryCustom(null, inventory.size());
      for (int i = 0; i < inventory.size() - 1; i++) {
        NBTTagCompound compound = (NBTTagCompound) inventory.get(i);
        if (!compound.isEmpty()) {
          ItemStack stack = CraftItemStack.asBukkitCopy(net.minecraft.server.v1_15_R1.ItemStack.a(compound));
          inv.setItem(i, stack);
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    
     
Thread Status:
Not open for further replies.

Share This Page