Reputation:
I have an item that should add the coordinates of a specific block when right-clicking the block with it. The coordinates are stored in a NBTTagList.
The problem is that the changes to the ItemStack will not be saved. It is not saved in the level.dat, which hold the Player's items in single player. Also the addInformation method which uses the data will not get anything.
The sources:
The onBlockActivate method of the target block class:
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;
}
Methods from the item class:
@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());
}
Upvotes: 0
Views: 614
Reputation: 581
Try change
if (world.isRemote) {
to
if (!world.isRemote) {
Edit #1:
The isRemote
flag can be confusing. Its used to show whether the reference to the world is remote. For a client, a remote world (where isRemote == true
) shows that the world actually lies on the server so no changes should be made. This can be used (like it commonly is) to show whether the currently running code is server side or client side.
That means that the world should only be altered when isRemote == false
, meaning that the currently running code has access to directly change things.
Since you run code that changes stuff (a NBT tag of an ItemStack to be exact) where isRemote == true
a problem occurs, since the currently running code is not able to apply those changes, as the actual world object is on the server.
Upvotes: 1