我有一个项目,它应该在右键单击带有特定块的块时添加它的坐标。坐标存储在 NBTTagList 中。
问题是不会保存对 ItemStack 的更改。它没有保存在 level.dat 中,它在单人游戏中保存了玩家的项目。使用数据的 addInformation 方法也不会得到任何东西。
消息来源:
目标块类的onBlockActivate方法:
if (player.getCurrentEquippedItem() != null &&
player.getCurrentEquippedItem().getItem() == ModItems.teleportationTablet) {
if (world.isRemote) {
ItemStack teletab = player.getCurrentEquippedItem().copy();
if (teletab.stackTagCompound == null)
teletab.stackTagCompound = new NBTTagCompound();
NBTTagList targets = teletab.stackTagCompound.getTagList("targets", 10);
NBTTagCompound location = new NBTTagCompound();
location.setInteger("x", x);
location.setInteger("y", y);
location.setInteger("z", z);
location.setInteger("dim", world.provider.dimensionId);
targets.appendTag(location);
teletab.stackTagCompound.setTag("targets", targets);
player.addChatMessage(new ChatComponentText("Your teleportation tablet is now linked!"));
}
return true;
}
return false;
}
项目类的方法:
@Override
public void addInformation(ItemStack teletab, EntityPlayer player, List list, boolean par4) {
NBTTagCompound tag = teletab.getTagCompound();
if (tag != null) {
NBTTagList targets = tag.getTagList("targets", 10);
if (targets.tagCount() != 0)
for (int i = 0; i < targets.tagCount(); i++) {
NBTTagCompound target = targets.getCompoundTagAt(i);
list.add(String.format("Linked with target at X=%d, Y=%d, Z=%d, Dim=%d", target.getInteger("x"), target.getInteger("y"), target.getInteger("z"), target.getInteger("dim")));
}
}
}
@Override
public void onCreated(ItemStack stack, World world, EntityPlayer player) {
stack.setTagCompound(new NBTTagCompound());
stack.stackTagCompound.setTag("targets", new NBTTagList());
}