Getting right clicked block/entity?

How would I be able to get the block or entity that’s been right clicked?

Tried looking in the docs but can’t seem to find this section specifically.

What I’m trying to do is prevent something from being placed on the itemframe.

For itemframe interactions for placing something there, you’ll want to listen to the main-hand secondary event…

@Listener
public void whatever(InteractEntityEvent.Secondary.MainHand event, @Root Player player){
    Entity entity = event.getTargetEntity();
    if (entity.getType() == EntityTypes.ITEM_FRAME) {

For testing if the itemframe is empty or contains an object already, ie the player is rotating an object in the itemframe

if (entity.get(Keys.REPRESENTED_ITEM)).isPresent()) { // something in itemframe

if it is empty, then the interaction will slap an object into the itemframe IF the player is holding an object in their main hand, if it is full then they just rotate it.
So you’ve got a few other checks and things to do as you wish.

1 Like

Thank you! Let me test that out :smiley:

Hey I’m having trouble fixing this. So, your method worked, but it only can block the item from being placed if it’s a stack. if it’s just 1 on your hand, it will place it regardless.

@Listener
	public void onRightClickonItemFrame(InteractEntityEvent.Secondary.MainHand event, @Root Player player) {
	    Entity entity = event.getTargetEntity();
	    if (entity.getType() == EntityTypes.ITEM_FRAME) {
			Optional<ItemStack> item = player.getItemInHand(HandTypes.MAIN_HAND);

			if(item.isPresent()) {
				ItemStack itemStack = item.get();
				if(itemStack.getItem().equals(ItemTypes.EMERALD)) {
					if (itemStack.getQuantity() <= 1) {
						player.setItemInHand(HandTypes.MAIN_HAND, null);
						event.setCancelled(true);
					}
					else {
						event.setCancelled(true);
					}
				}
	    	}
	    }
	}

This is what I’ve put. Any ideas how to fix this?