Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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?

   

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-

  • 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.

  • 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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.