mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 14:38:26 +01:00
Use instance players when possible
This commit is contained in:
parent
19e3bee23c
commit
2ad8fb1c77
@ -329,19 +329,18 @@ public class EntityFinder {
|
|||||||
private static @NotNull List<@NotNull Entity> findTarget(@Nullable Instance instance,
|
private static @NotNull List<@NotNull Entity> findTarget(@Nullable Instance instance,
|
||||||
@NotNull TargetSelector targetSelector,
|
@NotNull TargetSelector targetSelector,
|
||||||
@NotNull Point startPosition, @Nullable Entity self) {
|
@NotNull Point startPosition, @Nullable Entity self) {
|
||||||
|
final var players = instance != null ?
|
||||||
|
instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers();
|
||||||
if (targetSelector == TargetSelector.NEAREST_PLAYER) {
|
if (targetSelector == TargetSelector.NEAREST_PLAYER) {
|
||||||
return MinecraftServer.getConnectionManager().getOnlinePlayers().stream()
|
return players.stream()
|
||||||
.min(Comparator.comparingDouble(p -> p.getPosition().distance(startPosition)))
|
.min(Comparator.comparingDouble(p -> p.getPosition().distance(startPosition)))
|
||||||
.<List<Entity>>map(Collections::singletonList).orElse(Collections.emptyList());
|
.<List<Entity>>map(Collections::singletonList).orElse(Collections.emptyList());
|
||||||
} else if (targetSelector == TargetSelector.RANDOM_PLAYER) {
|
} else if (targetSelector == TargetSelector.RANDOM_PLAYER) {
|
||||||
Collection<Player> players = instance != null ?
|
|
||||||
instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers();
|
|
||||||
final int index = ThreadLocalRandom.current().nextInt(players.size());
|
final int index = ThreadLocalRandom.current().nextInt(players.size());
|
||||||
final Player player = players.stream().skip(index).findFirst().orElseThrow();
|
final Player player = players.stream().skip(index).findFirst().orElseThrow();
|
||||||
return Collections.singletonList(player);
|
return Collections.singletonList(player);
|
||||||
} else if (targetSelector == TargetSelector.ALL_PLAYERS) {
|
} else if (targetSelector == TargetSelector.ALL_PLAYERS) {
|
||||||
return List.copyOf(instance != null ?
|
return List.copyOf(players);
|
||||||
instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers());
|
|
||||||
} else if (targetSelector == TargetSelector.ALL_ENTITIES) {
|
} else if (targetSelector == TargetSelector.ALL_ENTITIES) {
|
||||||
if (instance != null) {
|
if (instance != null) {
|
||||||
return List.copyOf(instance.getEntities());
|
return List.copyOf(instance.getEntities());
|
||||||
|
Loading…
Reference in New Issue
Block a user