我正在为 Minecraft Forge 模块编写一些代码,我需要在其中将 TileEntity 定义保存到字符串和从字符串恢复。
要将 TileEntity 转换为字符串,我可以使用:
NBTCompoundTag nbt = new NBTCompoundTag();
TileEntity te = world.getTileEntity(x,y,z);
te.writeToNBT(nbt);
String nbtStr = nbt.toString();
但是,要从 String 转换回 TileEntitiy,我错过了 toString 方法的反向(某种 NBT 解析器)。
String nbtStr;
NBTTagCompound nbtTag = new NBTTagCompound();
// this function does not exist
// nbtTag.parseString(nbtStr);
TileEntity te = TileEntity.createAndLoadEntity(nbtTag);
world.setTileEntity(x,y,z,te);
我搜索了各种文档,但找不到可以将字符串表示形式转换为解析的 NBTCompoundTag 对象的函数。
我的问题是,在包含 NBT 的字符串中解析的方法是什么?