I'm building a mod that, among other things, contains teleporters. The location of target teleporters is stored in punch cards. On right-click on a teleporter with a blank punch card, the blank card is replaced with a written card. Then, the location of the target teleporter is written to the written card's NBT.
All of that works just fine. The problem comes when reloading the world; for whatever reason, written punch cards don't persist in inventories through a world reload. I suspect the problem lies in my implementation of NBT. I don't totally understand NBT; it makes a convenient scapegoat.
The code that replaces punch cards:
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ) {
//Get the tile entity of this teleporter
TeleporterTileEntity t = (TeleporterTileEntity) worldIn.getTileEntity(pos);
//Check if they're holding a blank punch card
ItemStack item = playerIn.getCurrentEquippedItem();
if(item != null && item.getItem() == EnderScience.blankPunch) {
//Replace with written card
ItemStack card = new ItemStack(EnderScience.writtenPunch, 1);
int slot = playerIn.inventory.currentItem;
playerIn.replaceItemInInventory(slot, card);
//Store this teleporter's coordinates in the new punch card
NBTTagCompound tag = new NBTTagCompound();
tag.setString("teleporter", pos.getX() + " " + pos.getY() + " " + pos.getZ());
card.setTagCompound(tag);
if(playerIn.getCurrentEquippedItem().getTagCompound() == null)
System.out.println("[Ender Science]: Failed to write card's NBT.");
System.out.println("[Ender Science]: Teleporter coordinates: " + tag.getString("teleporter"));
return true;
}
else if (item != null && item.getItem() == EnderScience.writtenPunch){
//TODO: Fix teleport call
t.teleport(playerIn, worldIn, item.getTagCompound().getString("teleporter"));
return true;
}
return true;
}
And the base class for the written punch card:
public class WrittenPunchCard extends Item {
public WrittenPunchCard()
{
setMaxStackSize(1);
setUnlocalizedName("writtenpunch");
setCreativeTab(EnderScience.tab);
}
@Override
public void addInformation(ItemStack stack, EntityPlayer playerIn, List tooltip, boolean advanced)
{
tooltip.add("Coordinates: " + stack.getTagCompound().getString("teleporter"));
}
}
The problem persists when I comment out the code that writes the teleporter coordinates to the punch card.