Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.15.2] Trouble finding the nearest player

Featured Replies

Posted

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

  • Author

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

Guest
This topic is now closed to further replies.

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.