Getting the Entity the player is aiming at

Hello,
I want to get an Entity when the player right clicks and the entity is in it’s crosshair, however I can’t seem to find the right Events to do so, because entity interact event only works when the player is close to the entity.

BlockRay is what you are looking for https://github.com/SpongePowered/SpongeAPI/tree/master/src/main/java/org/spongepowered/api/util/blockray
I think the right click event also works when clicking in air.

However this only works when I’m aiming at the block underneath or close to the entity. If I aim at an entity while looking forward it goes through the entity… Right? So then how would I get the entity?

I think there is no such thing in Sponge but I am sure this can be implemented. But don’t ask how :joy: Anything with trigonometry, maybe?
Edit: I think add one block in direction the player is looking at and look after each addition for Entites nearby(1 block radius?). Stop adding when it found an entity

I can maybe try shooting a snowball projectile, then making it invisible and checking what gets hit by it, but how can I
1: Make it invisible
2: Make it travel in a straight line
3: Know by what item it was fired and by what player

Do anyone have any ideas?

And how do I do that?

  • Listen to a right click(don’t know exactly which event)
  • add to the player position the rotation the player has
  • after each addition look for entites nearby(there is a method for that), low radius like 1 block, you have to test
  • if there is an entity stop adding
  • that entity is the first entity the player is probably looking at

The lower the radius the more exact the player has to look at an entity.
Don’t add infinitely, set a limit.

Do you know what method that would be?
And how do I listen to a right click?
Because the interact event is for both the right and left click and there is no filter for it…

Use InteractEvent.Secondary - it’s right-click only.

Yes, however that doesn’t seem to exist. The InteractEvent does, but Interact.Secondary doesn’t…

Ah… Use InteractEntityEvent.Secondary. It also has a getTargetEntity() method that can help you.

But that does only work when you are can reach the entity.

I am very interested in whether there is such a “interact with air”-event

1 Like

It sеems like it’s planned for API 4.0.0 (There is no API or implementation at the moment yet, though):

1 Like

Thanks a lot for this link! I am really looking forward for APIv4! :smiley:

Does anyone know how far this project is and when it will be released?

API 4.0 has recently been released.

I don’t think it has been implemented yet…
If it is: Could you/someone else tell me what method it is and from what class?

The issue has been pushed back to 5.0

It’s possible to detect when a player left-clicks, this fires InteractBlockEvent.Primary. If the player is punching air then the blocksnapshot returned from getTargetBlock() is BlockSnapshot.NONE
Right-clicking is only detectable when the player is holding an item. In this case, it fires InteractBlockEvent.Secondary again with BlockSnapshot.NONE if in the air.