1.8.8 Get nearby entities in order of distance

Discussion in 'Spigot Plugin Development' started by WhiteShiro, Oct 5, 2020.

  1. I am making a plugin that has homing arrows. How I want my plugin to work is by looping through every nearby entity, and checking if it is a monster, then checking if it is a pet. But when looping, the array is arranged in a randomized order and not in order of distance from the arrow. How do I accomplish this?
     
  2. Use a comparator, sort the list, and compare on the distance squared, so like Double.compare(x1, x2) and compare the distanceSquared values and you can sort it the way you need. Then the top will be the desired item. Or you can search for the compare result instead of sorting which is O(n)