Jump to content

[SOLVED] Getting TileEntity in onBlockDestroyed events


BenignBanana

Recommended Posts

Hey everyone, I ran into a bit of a snag when I tried to do multiblock cleanup on block break. If I try to get the TileEntity of my block inside the onBlockDestroyedByPlayer/Explosion methods it always returns null, preventing me from calling the cleanup method on it. Why is it always returning null and/or is there any other event I can use that still has the TileEntity active? The code is a bit obtuse because of the large framework I've built and the fact that it's kotlin, but I hope these snippets are enough to understand it.

Inside my framework base block class:

    override fun onBlockDestroyedByPlayer(worldIn: World, pos: BlockPos, state: IBlockState) {
        breakEvent.fire(BlockBrokenEvent(worldIn, pos))
        super.onBlockDestroyedByPlayer(worldIn, pos, state)
    }

    override fun onBlockDestroyedByExplosion(worldIn: World, pos: BlockPos, explosionIn: Explosion) {
        breakEvent.fire(BlockBrokenEvent(worldIn, pos))
        super.onBlockDestroyedByExplosion(worldIn, pos, explosionIn)
    }

And inside the multiblock part block base class:

    init {
        placeEvent += {
            if(!it.world.isRemote) {
                val te = it.world.getTileEntity(it.pos) as TileEntityMultiblockPart
                te.rebuild(it.pos)
            }
        }
        breakEvent += {
            if(!it.world.isRemote) {
                val te = it.world.getTileEntity(it.pos) as? TileEntityMultiblockPart //This is always null
                if(te != null && te.multiblockId.value != -1) (it.world.getEntityByID(te.multiblockId.value) as? EntityMultiblock)?.destroy()
            }
        }
    }

Cheers

Edited by BenignBanana
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.