1

我正在为 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 的字符串中解析的方法是什么?

4

1 回答 1

2

从字符串转换回来所需的函数在 minecraftJsonToNBT类中。Forge(还)没有对它进行去混淆处理,因此它的名称相当无用。

转换为字符串表示(实际上是 JSON 格式):

NBTTagCompound nbt = new NBTTagCompound();
String nbtJsonStr = nbt.toString();   

从字符串转换回 NBTTagCompound:

String nbtJsonStr = "{foo:2,}";
NBTTagCompound nbt = (NBTTagCompound) JsonToNBT.func_150315_a(nbtJsonStr);

不过,使用这个很可能会使模块高度依赖于特定版本的 Minecraft。

于 2015-01-04T13:38:43.647 回答