Rearranging objects in Java (e.g. for Minecraft NBT files)


December 2018


216 time


These are some preliminary sorts for something that is going to be a conversion tool for NBT-Files. This is a file format providing nested objects, those of you knowing Minecraft might know it.

So lets consider we have an object like this: = String = int
object.randomInformation = Object
object.storedObjects = Object[]

What I'm trying to achieve is this: The user should be able to give config files for different versions of the object. This may look like this:

Version1.dat: = 1

Version2.dat: = 2

So on conversion from Version1 to Version 2 the program reads the NBT-File, search for any object with an id of 1, change it to 2 and write it into another file. So far not a big deal.

The Problem starts, when it comes to real changes in the version. This includes:

  • renamed properties
  • moved properties
  • added/deleted properties
  • properties that contains objects which have to be treated as if they where a "normal" object itself (if you are into Minecraft, you may think of items in a chest)

I thought about something like this:



This would mean on conversion from 1 to 2:

  • change id from 1 to 2
  • rename randomInformation to usefulInformation
  • add a property addedInformation, which is an integer
  • convert every Object in object.storedObjects just like a normal object
  • just copy every other property

This would mean i have to implement a whole "language" for the config file and convert that into a list of "commands" that have to be done to each object. Isn't there an easier way to do this? Or is this already a good idea?

Remember, I'll have a whole lot of different objects in each version, where mostly only the id has to be changed. But on some objects, identified by their id, various changes has to be made. And I don't want to change the program code for every new version.

1 answers


Это не так трудно! Но вы должны написать методы преобразования из двух объектов или, если второй объект проходит первый объект, то вы можете просто копировать поля первого объекта. Если поля не видны, то вы можете использовать отражение для их копирования.

Для управления Minecraft NBT вы можете использовать библиотеку JNBT.