Solved Parse .nbt file in 1.10

Discussion in 'Spigot Plugin Development' started by awesomebing1, Jun 17, 2016.

  1. I'm trying to parse a .nbt file generated from a structure block in the 1.10 update. Loading the file is simple:
    Code (Text):
    NBTTagCompound nbt = NBTCompressedStreamTools.a(fis);
    with fis being a FileInputStream. However, parsing is turning out to be the hard part. The following is an example of the file format, outputted by nbt.toString():
    Code (Text):
    {
        size: [0: 21, 1: 7, 2: 2],
        entities: [],
        blocks: [0: {
            pos: [0: 0, 1: 0, 2: 1],
            state: 0
        }, 1: {
            pos: [0: 3, 1: 0, 2: 1],
            state: 0
        }, 2: {
            pos: [0: 6, 1: 0, 2: 1],
            state: 0
        },
    insert many, many, many more block arrays here
    Code (Text):
    ],
        author: "",
        palette: [0: {
            Properties: {
                variant: "default"
            },
            Name: "minecraft:quartz_block"
        }, 1: {
            Name: "minecraft:air"
        }],
        version: 1
    }
    As you can see, this isn't valid JSON as none of the variable names have quotes. Gson appears to agree with that, giving something along the lines of "Expected a string but was BEGIN_ARRAY at line 1 column 10" whenever I've attempted to parse it. Does anyone know how to make NBTTagCompound output valid JSON? I tried making a custom class extending NBTTagCompound, hoping to modify the toString() method to produce values with quotes (making it valid JSON), however it required a variable which was private in its own class, and I had too much trouble accessing it.

    Note: I'm still getting the hang of Java, however I mostly know what I'm talking about here. Please tell me if I skipped over something, and I'll do my best to update the post.
     
    • Useful Useful x 1
  2. Hm, didn't know that.

    After experimenting some more, I figured out that the issue was my class that I was serializing to. I had set it to a String when it should have been an Array.

    I now feel really dumb, but thank you for that cool option! Should be helpful later on in life.
     
    • Funny Funny x 1