Querz / NBT

A java implementation of the NBT protocol, including a way to implement custom tags.
MIT License
182 stars 48 forks source link

Charset problem while recreating Minecraft servers.dat file #64

Open Plugner opened 3 years ago

Plugner commented 3 years ago

My code: {servers:[{name:Server,ip:"0.0.0.0"}]} Minecraft Generated code: {servers:[{name:Servidor,ip:"0.0.0.0"}]}

My tests

        File f = new File("servers.dat");
        System.out.println(SNBTUtil.toSNBT(NBTUtil.read(f).getTag())); // Testing with the original file

        CompoundTag compoundTag = new CompoundTag();
        CompoundTag serversCT = new CompoundTag();

        ListTag<CompoundTag> servers = new ListTag<>(CompoundTag.class);

        CompoundTag server = new CompoundTag();
        server.putString("ip", "0.0.0.0");
        server.putString("name", "Server");

        servers.add(server);

        compoundTag.put("servers", servers);
        System.out.println(SNBTUtil.toSNBT(compoundTag)); // Testing with generated code
        NBTUtil.write(compoundTag, "output.dat");

But the binary is completely different:

MC Generated: image

Generated by my code: image

Thanks!

Dotteex commented 2 years ago

I had the same problem as you and I found the solution. On Minecraft Wiki you can found, that servers.dat is stored as an uncompressed NBT file, but this NBT library automatically converts the output file into gzip, so you have to turn off the compression.

The last line should be like this: NBTUtil.write(compoundTag, "output.dat", false);