InteractBlockEvent Question

Hello,

How would I get the player of this event and check if the block is a bed?

Thanks,

The player is contained within the event cause and can be retrieved using a couple of different methods, each with their own use. However, you can also use event filters (specifically parameter filters) to achieve the same thing more seamlessly. Note that causes are significantly different in API 7, but the part you’re concerned with is effectively the same.

Checking if the block is a bed should be simple enough - just compare the block’s type to BlockTypes.BED. Info on working with blocks can be found here.

However, there are two other puzzles neither of which I have complete answers for - the orientation of the bed and what half it is. The data manipulator for beds has not been implemented (see this issue), so I don’t believe you can tell if it’s the head or the foot using any native method in the API. As for direction, you should be able to use Keys.DIRECTION because it’s not specific to beds, but you’ll have to test that for yourself.

I’d also like to remind you that while the docs are not updated to API 7, much of the information is still accurate especially with the basic actions - use them to your advantage before creating a thread!

1 Like

Block Traits should let you know that.

@Listener
    public void onInteract(InteractBlockEvent event) {
        BlockSnapshot block = event.getTargetBlock();
        if (event.getCause().first(Player.class).isPresent()) {
            if (block.getState().getType().equals(BlockTypes.STANDING_SIGN)) {
                System.out.println("A sign was clicked!");
            }


        }
    }

Just tested this, works fine for me. Prints out in my console when I left and/or right click on a sign.