Select nearest entity which is not event/target entity

Started by LunarArmageddon99 on

Topic category: Help with Minecraft modding (Java Edition)

Joined Aug 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Select nearest entity which is not event/target entity

I can't believe I'm unable to find anyone else who has tried to do this.  I have an item which I want to make apply an effect to the nearest player when you hit somebody with it, but the nearest player is you.

I've already tried using the get nearest entity procedure on actions the player takes, last time it was for a potion that would teleport you to the nearest player, which just teleported you to yourself.  Now, I want an item which you hit somebody with, it clears all effects on them, and it breaks.  But it'll just clear all effects on the player who hits anything else with it.

Also, while I'm at it, why exactly did I have to turn off my VPN to post this?  Most websites which have accounts just let you through if you sign in with a verifiable, legitimate account, even on a VPN.  But apparently that's just too much to ask for this site, I'm just not allowed to post at all without turning off my VPN.  Imagine if you connected to a website with Microsoft Edge and the website blocked your connection and told you to go use Google Chrome because more bots connect with Chrome.

Joined Apr 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
You can use "for each entity…
Mon, 04/06/2026 - 20:50

You can use "for each entity in square cube size [number] at X [X] Y [Y] Z [Z] as entity iterator" to get all entities in an area.

For each one you can check if it is a player and not equal to event/target entity.

If so, get its distance by adding the absolute X-entity iterator X position, plus absolute Y-entity iterator Y position, plus absolute Z-entity iterator Z position. Compare that to a local number variable, and if it is lower than the variable, set the number variable to the current entity iterator's distance and set a local entity variable to the entity iterator. You'll need to set the base distance to something big before running this, of course.

That will get the nearest player other than event/target entity, provided there is a player other than event/target entity in the area. You should probably make sure that the entity variable isn't null before you try to use it after getting the nearest player, just to be sure.