Posted August 20, 201312 yr I'm trying to save TileEntity information when a block is broken. In my block's removeBlockByPlayer() function, I spawn a BlockItem with the TileEntity information copied to it. Here's the relevant code from my Block subclass: public boolean removeBlockByPlayer(World world, EntityPlayer player, int x, int y, int z) { sideLog(java.util.logging.Level.WARNING, "removeBlockByPlayer"); if (!world.isRemote && world.getGameRules().getGameRuleBooleanValue("doTileDrops")) { ItemStack itemStack = new ItemStack(DeepStorageTanks.itemBlockDeepStorageTank); LiquidTank dst = null; TileEntity te = world.getBlockTileEntity(x, y, z); if(te instanceof TileEntityDeepStorageTank) { dst = ((TileEntityDeepStorageTank)te).tank; } if (dst != null && dst.containsValidLiquid() && dst.getLiquid().amount > 0) { NBTTagCompound tag = new NBTTagCompound(); tag.setTag("tank", dst.getLiquid().writeToNBT(new NBTTagCompound())); itemStack.setTagCompound(tag); } EntityItem entityitem = new EntityItem(world, x, y, z, itemStack); entityitem.delayBeforeCanPickup = 10; //world.spawnEntityInWorld(entityitem); } return world.setBlockToAir(x, y, z); } For some reason, when I place the resulting ItemBlock, the onItemUse() function (see below) is not firing. @Override public boolean onItemUse(ItemStack itemStack, EntityPlayer par2EntityPlayer, World world, int x, int y, int z, int par7, float par8, float par9, float par10) { sideLog(java.util.logging.Level.WARNING, "onItemUse"); boolean s = super.onItemUse(itemStack, par2EntityPlayer, world, x, y, z, par7, par8, par9, par10); NBTTagCompound data = itemStack.getTagCompound(); TileEntity te = world.getBlockTileEntity(x, y, z); if (data != null && te != null && te instanceof TileEntityDeepStorageTank) { ((TileEntityDeepStorageTank)te).readFromNBT(data); } return s; } I'm not entirely sure what the issue is, but I never see anything in the log from onItemUse. (Note that I know that removeBlockByPlayer() is being called when the block is broken because I had a logging function in it until recently.) Any ideas what might be going on?
August 20, 201312 yr maybe onItemUse isnt the right thing to use ?, theres a lot of similar function in Item.java how to debug 101:http://www.minecraftforge.net/wiki/Debug_101 -hydroflame, author of the forge revolution-
August 20, 201312 yr Author Figured out my mistake. It wasn't in the ItemBlock class itself; rather, when you register a block with a corresponding ItemBlock, rather than this: GameRegistry.registerBlock(blockDeepStorageTank, "deepStorageTank"); GameRegistry.registerItem(itemBlockDeepStorageTank, "deepStorageTankItem"); you do this: GameRegistry.registerBlock(blockDeepStorageTank, ItemBlockDeepStorageTank.class, "deepStorageTank"); The method is being called correctly now.
August 20, 201312 yr Author Worked on this a little more. For anyone finding this forum thread later on, the TileEntity data is only available once ItemBlock.placeBlockAt() is called. Here's my code in my ItemBlock subclass. @Override public boolean placeBlockAt(ItemStack itemStack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata) { // TODO Auto-generated method stub sideLog(java.util.logging.Level.WARNING, "placeBlockAt"); boolean b = super.placeBlockAt(itemStack, player, world, x, y, z, side, hitX, hitY, hitZ, metadata); TileEntity te = world.getBlockTileEntity(x, y, z); if(te != null) { sideLog(java.util.logging.Level.WARNING, "placeBlockAt: block has TileEntity"); } NBTTagCompound data = itemStack.getTagCompound(); if (data != null && te != null && te instanceof TileEntityDeepStorageTank) { ((TileEntityDeepStorageTank)te).readFromNBT(data); } return b; }
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.