detect nearest entity

Started by Unmiet on

Topic category: Help with modding (Java Edition)

Last seen on 16:41, 22. Apr 2024
Joined Apr 2024
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
detect nearest entity
Wed, 04/03/2024 - 22:40 (edited)

I wanted to make a chestplate, that you wear it, and any mob that gets close to you (if you are using the chestplate) the mob despawns, but no matter what I do, I can't do it right, if anyone knows how do this, help me pls

Edited by Unmiet on Wed, 04/03/2024 - 22:40
Last seen on 10:38, 22. Apr 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
There are a couple ways to…
Sat, 04/06/2024 - 13:29

There are a couple ways to do this, depends on how specifically you want it to work. The easiest would probably be to make an 'on armor tick' or 'on player tick update' procedure that checks if a living entity exists in a 16-block radius or so, and if it does, despawns the nearest entity. (You can do this using the 'does entity of type X exist in X block radius' function, and the 'nearest entity of type X in X block radius' function.) It is VERY IMPORTANT you use an if bracket to check that an entity exists before attempting to despawn it, otherwise this procedure will crash the game.

Another method would be to use an entity iterator bracket to check every entity nearby. Essentially, the bracket lets you run a procedure on every entity within its radius. Inside the bracket, check if the entity iterator, (the currently selected entity in the radius), meets your criteria, and if it does, despawn it. This is generally the better method, as it lets you be more specific about the conditions for your procedure.