Jump to content

[1.15.2] Trouble finding the nearest player


Alternis

Recommended Posts

Hello !

 

I have been trying to make a "simple" item that, when used :

  • Finds the nearest player in a range
  • Sends a snowball in its direction if a player was found
  • Sends a message in chat if no player was found in range

 

I achieved some sort of success by using this :

List<Entity> entities = worldIn.getEntitiesWithinAABBExcludingEntity(playerIn, playerIn.getBoundingBox().expand(50000.0D, 50000.0D, 50000.0D));

Then, I iterated through the list using instanceof like the bad encapsulator I am. Little I knew that it would make the game freeze a few seeconds each time I used the item AND would have a ridiculously low range.

 

I tried using a lot of other stuff, but no luck, so I'm asking you Forge experts :)

Thanks for reading me,

alternis

Link to comment
Share on other sites

Thank you very much, it worked flawlessly. I did have to fiddle around with predicates for a while but that's because I never touched the functional part of interfaces. I ended up reading these examples which helped me very much understand how to.

 

For future reference, this is how my "player finder" looks :

Predicate<Entity> predicate = i -> !(i.equals(playerIn));
PlayerEntity nearestPlayer = worldIn.getClosestPlayer(launcherXPos, launcherYPos, launcherZPos, 5000, predicate);

 

Have a nice day,

alternis

Edited by Alternis
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.