Jump to content

[1.9.4] Tile NBT


p455w0rd

Recommended Posts

I'm trying to create a wrench function for my blocks. I'm trying to retrieve the tile's NBT via getTileData(). getTileData() always returns an empty tag. The tile retains the NBT data so long as the block is placed. I can verify this because the block is a solar panel that generates RF. It retains the RF data through logging out and restarting the client. I have implemented packet handling to send the server data to the client as well. Following are the relevant methods:

 

BlockSolarPanel#onBlockActivated

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {

	TileEntitySolarPanel te = getTE(worldIn, pos);
	if (te != null) {
		if (!playerIn.isSneaking()) {
			if (worldIn.isRemote) {
				GuiHandler.launchGui(Globals.GUINUM_SOLARPANEL, playerIn, worldIn, pos.getX(), pos.getY(), pos.getZ());
			}
		}
		else {
			BlockUtils.wrenchBlock(worldIn, pos, state, te);
		}
	}
	return true;
}

 

BlockSolarPanel#getTE

private TileEntitySolarPanel getTE(World worldIn, BlockPos pos) {
	return (TileEntitySolarPanel) BlockUtils.getTE(worldIn, pos);
}

 

BlockUtils#wrenchBlock

public static void wrenchBlock(World worldIn, BlockPos pos, IBlockState state, TileEntity te) {
	if (!worldIn.isRemote) {
		ItemStack blockStack = new ItemStack(Item.getItemFromBlock(worldIn.getBlockState(pos).getBlock()), 1);

		if (te.getBlockMetadata() > 0) {
			blockStack.setItemDamage(te.getBlockMetadata());
		}
		if (te.getTileData() != null) {
			NBTTagCompound nbt = te.getTileData();
			blockStack.setTagCompound(nbt);
		}
		EntityItem entityItem = new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), blockStack);
		if (te != null) {
			worldIn.spawnEntityInWorld(entityItem);
			dropTEItems(worldIn, pos);
			worldIn.setBlockToAir(pos);
		}
	}
}

Link to comment
Share on other sites

TileEntity#getTileData

is for storing external data on a

TileEntity

, it doesn't contain the

TileEntity

's own data.

 

Use

INBTSerializable#serializeNBT

(inherited by

TileEntity

) to write an

INBTSerializable

object to NBT.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.