From 2ad8fb1c779d87c5ab8cab6cd6b608ea6ad3705f Mon Sep 17 00:00:00 2001 From: TheMode Date: Sat, 24 Jul 2021 04:39:57 +0200 Subject: [PATCH] Use instance players when possible --- .../net/minestom/server/utils/entity/EntityFinder.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/entity/EntityFinder.java b/src/main/java/net/minestom/server/utils/entity/EntityFinder.java index a16f94c33..cb71b27e8 100644 --- a/src/main/java/net/minestom/server/utils/entity/EntityFinder.java +++ b/src/main/java/net/minestom/server/utils/entity/EntityFinder.java @@ -329,19 +329,18 @@ public class EntityFinder { private static @NotNull List<@NotNull Entity> findTarget(@Nullable Instance instance, @NotNull TargetSelector targetSelector, @NotNull Point startPosition, @Nullable Entity self) { + final var players = instance != null ? + instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers(); if (targetSelector == TargetSelector.NEAREST_PLAYER) { - return MinecraftServer.getConnectionManager().getOnlinePlayers().stream() + return players.stream() .min(Comparator.comparingDouble(p -> p.getPosition().distance(startPosition))) .>map(Collections::singletonList).orElse(Collections.emptyList()); } else if (targetSelector == TargetSelector.RANDOM_PLAYER) { - Collection players = instance != null ? - instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers(); final int index = ThreadLocalRandom.current().nextInt(players.size()); final Player player = players.stream().skip(index).findFirst().orElseThrow(); return Collections.singletonList(player); } else if (targetSelector == TargetSelector.ALL_PLAYERS) { - return List.copyOf(instance != null ? - instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers()); + return List.copyOf(players); } else if (targetSelector == TargetSelector.ALL_ENTITIES) { if (instance != null) { return List.copyOf(instance.getEntities());