how to get nearest entity to an entity

Started by carnivowrist on

Topic category: Help with Minecraft modding (Java Edition)

Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
how to get nearest entity to an entity

I'm trying to make a procedure in which each tick (or every X ticks) it will set the attack target of an entity to the nearest entity to it, but if i use "get nearest entity" it will select itself, and so if i make it the attack target, the entity attacks itself as it considers itself the nearest. I tried other things like "for each entity as 'entity iterator' at X Y Z" --> set the attack target to entity iterator, which works well but this sets the attack target of the entity to the furthest entity to it.. i have no solutions

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
try this, I haven't tested…
Thu, 08/28/2025 - 14:49

try this, I haven't tested it,

Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
hello i tested but it didn't…
Fri, 08/29/2025 - 11:06

hello i tested but it didn't work, it still gets the furthest entity. I don't know if it's because of different Mcreator version as yours looks slightly different (i have 2023.4)

 

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
what does yours look like?…
Fri, 08/29/2025 - 11:09

what does yours look like? can you send a screenshot

Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
this what it looks like…
Fri, 08/29/2025 - 11:18

this what it looks like https://ibb.co/6JLnYL5j
first it checks if cooldown is over, then it checks if the entity is the right one (if i remove my stuff it still doesn't work)

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
hm, this worked, world…
Fri, 08/29/2025 - 11:51

hm, this worked,

world.getEntitiesOfClass(LivingEntity.class,
						AABB.ofSize(new Vec3(x, y, z), 5, 5, 5)).stream()
				.filter(e -> !(e instanceof Player))
				.min(Comparator.comparingDouble(e -> e.distanceToSqr(x, y, z)))
				.orElse(null);
Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
with this you can make a tag…
Fri, 08/29/2025 - 12:04

with this you can make a tag for the ones to skip,

world.getEntitiesOfClass(LivingEntity.class, AABB.ofSize(new Vec3(x, y, z), 5, 5, 5)).stream()
				.filter(e -> e.getType().is(TagKey.create(Registries.ENTITY_TYPE, ResourceLocation.parse("minecraft:skeletons"))))
				.min(Comparator.comparingDouble(e -> e.distanceToSqr(x, y, z)))
				.orElse(null)

replace "minecraft:skeletons"  with your tag

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
oh yeah and the 5, 5, 5 in…
Fri, 08/29/2025 - 12:06

oh yeah and the 5, 5, 5 in the first line is the size it should look in

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
oh you can't add players to…
Fri, 08/29/2025 - 12:08

oh you can't add players to entity tags?

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
what entities do you need to…
Fri, 08/29/2025 - 12:09

what entities do you need to not be affected?

Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
it's okay if i can't exclude…
Fri, 08/29/2025 - 17:52

it's okay if i can't exclude the player like in the screenshot but sometimes it will be 5 to 20 type of entities

Joined Dec 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
oh then you should probably…
Fri, 08/29/2025 - 18:02

oh then you should probably use the tag method, 

Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
thank you! if you're good…
Sat, 08/30/2025 - 21:46

thank you! if you're good with custom code do you by chance know how to disable entity invulnerability for some cases? I have an entity with fast attacks or a weapon that shoots fast and the invulnerability ruins it, i heard it can be done with custom code but i couldn't get it right

Joined Jun 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
i think i don't have this…
Sat, 08/30/2025 - 21:57

i think i don't have this block in my version.. atleast i can't find it i got 2023.4 and i can't change it