Changeblock.Place event's block is always air

Hey,
today I noticed, that there is something wrong with my plugin, so I started debugging it.
When a player places a block, I normally get it with following code: (result should be minecraft:stone for example)

@Listener
    public void onBlockPlace(ChangeBlockEvent.Place event) {
        //JOB
        Optional<Player> optPlayer = event.getCause().first(Player.class);
        if (optPlayer.isPresent()) {
            player = optPlayer.get();
            logger.info("Block: " + event.getTransactions().get(0).getOriginal().getState().getType().getName());
        }
    }

So this returns “minecraft:air”, no matter which block was placed.
Any ideas?
Thank you guys…

Yes, I think I know what your problem is.
If you want to get the block that was placed you need to get the final of the transaction.
This would mean your logger line would need to look like this:

logger.info("Block: " + event.getTransactions().get(0).getFinal().getState().getType().getName());

Otherwise you always get the block that was there before the Player placed it.

1 Like

Thanks man :smile:
Do you know, how I can get the new API now (because they are using forge’s repo…) ?
Would be great if you could say me, how to add it with maven / or where I can download a local api,
greets

I think it stayed the same, you can follow the documentation here for more information
https://docs.spongepowered.org/en/plugin/workspace/dependencies.html

1 Like

You can also use your code, except change the cause .first to cause .last

Edit… either will work in this case, but yes, the FINAL blockstate not original needs to be grabbed from the transaction

Why is the title referring to Break as opposed to Place?

Changed title from “Changeblock.Break event’s block is always air” to “Changeblock.Place event’s block is always air”

This seems to be related to ChangeBlockEvent.Place not Break

I don’t understand… I have changed the title at least one day ago :slight_smile: