[SOLVED] Correct way of checking if a block interacted is the same as a given?

Is this the right way of checking if a block interacted with is a chest?

@Subscribe
public void onInteract(PlayerInteractBlockEvent event) {
	Location block = event.getBlock();
	if (event.getInteractionType().equals(EntityInteractionTypes.USE)) {
		BlockType chest = game.getRegistry().getType(BlockType.class, "minecraft:chest").get();
		if (block.equals(chest)) {
			// BLOCK IS A CHEST!!!
		}
	}
}

Shouldn’t block.equals(chest) be block.equals(BlockTypes.CHEST) (Untested)

true… :smiley: always hard to understand the differents of BlockType and BlockTypes

A Location object is not the same as a BlockType object, therefore the two can never be equal.

As @WetSponge pointed out, you can get a chest block type from BlockTypes.CHEST, but the two are equal, the following returns true: (not tested, Sponge is broken if this doesn’t return true).

game.getRegistry().getType(BlockType.class, "minecraft:chest").get().equals(BlockTypes.CHEST);

However, more to the point, you have to do block.getBlockType().equals(BlockTypes.CHEST)

So the final code looks like:

@Subscribe
public void onInteract(PlayerInteractBlockEvent event) {
    Location block = event.getBlock();
    if (event.getInteractionType().equals(EntityInteractionTypes.USE)) {
        if (block.getBlockType().equals(BlockTypes.CHEST)) {
            // BLOCK IS A CHEST!!!
        }
    }
}
2 Likes

Not really: BlockType is just “the” interface and BlockTypes is a pseudo-enum containing all vanilla block types. Same with all CatalogTypes.

Additonally you may want to analyse why you are comparing against a chest. Is it because chests have a GUI inventory? If so use an interface that represents blocks that have a GUI/inventory.

Suddenly your code now works for modded blocks and new blocks introduced in new updated.

1 Like