Solved ProtocolLib and NBT - load NBT of offline player (from file)?

Discussion in 'Plugin Development' started by Smerfa, Mar 29, 2014.

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

    Smerfa

    How to load NBT from file?
    like nickname.dat from world/players folder.

    (Using ProtocolLib)
    I don't see any option for that ;/

    refresh?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    RawCode

    how ProtocolLib related to offline players and files?
     
  3. Offline

    MisterErwin

    €: Smerfa
    You have to work with the file itself - ProtocolLib is for Protocols.

    So...
    First of all - You need JNBT: http://jnbt.sourceforge.net/
    Code:java
    1. //Lets make a function that loads the .dat file
    2. private CompoundTag load(String name) {
    3. File file = new File(path + name + ".dat");
    4. FileInputStream input_stream;
    5. try {
    6. input_stream = new FileInputStream(file);
    7. } catch (FileNotFoundException e) {
    8. e.printStackTrace();
    9. return null;
    10. }
    11. NBTInputStream nbt_input;
    12. try {
    13. nbt_input = new NBTInputStream(input_stream);
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. return null;
    17. }
    18. CompoundTag tag;
    19. try {
    20. tag = (CompoundTag) nbt_input.readTag();
    21. } catch (IOException e) {
    22. tag = null;
    23. e.printStackTrace();
    24. }
    25. try {
    26. nbt_input.close();
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. return null;
    30. }
    31.  
    32. return tag;
    33. }
    34.  


    To read the data:

    Code:java
    1. CompoundTag tag = load("MisterErwin");
    2. //Lets access the Inventory:
    3. for (Tag t : ((ListTag) tag.getValue().get("Inventory")).getValue()) {
    4. int slot = ((ByteTag) ((CompoundTag) t).getValue().get("Slot")).getValue().byteValue();
    5. short id = ((ShortTag)((CompoundTag) t).getValue().get("id")).getValue();
    6. }


    If you aren't sure, how to NBT "tree" is build, use a programm like NBTExplorer.

    ~MisterErwin
     
    Smerfa likes this.
  4. Offline

    Smerfa

    Hyyym, ok thanks!
    How fast is loading this files?
     
  5. Offline

    MisterErwin

    You're welcome,

    but idk - I guess it depends on the size - Just test it :p
     
    Smerfa likes this.
Thread Status:
Not open for further replies.

Share This Page