Test if a block has a tile entity

While handling an InteractBlockEvent, I’d like to check if the interacted block (getTargetBlock) has a tile entity (for example it could be a Chest) without individually checking each block type (mod support).

How would I do this?

Something like this would work.

	Optional<Location<World>> optLocation = event.getTargetBlock().getLocation();
		Optional<TileEntity> optTileEntity = optLocation.get().getTileEntity();
			TileEntity tileEntity = optTileEntity.get();

I’m not excluding InteractBlockEvent.Break, how would I do it even when that event triggers?

Edit: There is no InteractBlockEvent.Break, I’m using InteractBlockEvent.Secondary, which is when it’s right clicked, right?

Whoops, I was thinking of a different event. You can ignore that…comment about Break.

Do you mean ChangeBlockEvent.Break?

Yeah, head in the clouds today. The code I posted will still work for InteractBlockEvent though

ChangeBlockEvent.Break is fired when a player break a block
InteractBlockEvent.Primary is fired when a player left click a block
InteractBlockEvent.Secondary is fired when a player right click a block
I hope this can help you

1 Like