[SOLVED] ChangeBlockEvent.Break - how to get broken block?

Hello everyone! I have a problem because idk how to get broken block from event “ChangeBlockEvent.Break”
For example, if player breaks a log, it will do nothing, but if player breaks not a log, the event will be cancelled. Thanks everyone for help!
My code: @Listenerpublic void onBreakBlock(ChangeBlockEvent.Break e, @Root Player p) { - Pastebin.com

Uhm, i think you are mixing Sponge and Bukkit here @MoseMister ^^

@BOT_MrLenya
Sponges ChangeBlock events don’t have a single location, they have multiple transactions, because one event can affect multiple Blocks, e.g. with an explosion.
So you will have to go through all transactions and check the block there.
With transaction.setValid(false); you can “cancel” a single block-change.
From a transaction you can also get the original Block directly: transaction.getOriginal()

So here is how i would do it:

@Listener
public void onBlockBreak(ChangeBlockEvent.Break evt, @Root Player player) {
	//go through all changes (will mostly be only one)
	for (Transaction<BlockSnapshot> tr : evt.getTransactions()) {
		//check if the original state was a LOG
		if (tr.getOriginal().getState().getType() != BlockTypes.LOG) {
			//if not, set the transaction invalid
			tr.setValid(false);
		}
	}
}
2 Likes

Sorry. Been working on bukkit reasently XD

1 Like

Huge thank you!

And one more question: how I can get the X, Y and Z of broken block?

tr.getOriginal().getLocation() gets you an Optional<Location> that is present in most cases, so:

Optional<Location<World>> optLocation = tr.getOriginal().getLocation();
if (optLocation.isPresent()) {
	Vector3i position = optLocation.get().getBlockPosition();
}
1 Like