Jump to content

onItemUse event not firing when ItemBlock is placed


lendrick

Recommended Posts

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?

   

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
    }

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.