mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 14:38:26 +01:00
Cleanup EntityFinder
This commit is contained in:
parent
6aa55ad7b6
commit
f7d30feb7f
@ -35,7 +35,7 @@ public class EntityFinder {
|
||||
private EntitySort entitySort = EntitySort.ARBITRARY;
|
||||
|
||||
// Position
|
||||
private Point startPosition = Vec.ZERO;
|
||||
private Point startPosition;
|
||||
private Float dx, dy, dz;
|
||||
private IntRange distance;
|
||||
|
||||
@ -127,8 +127,7 @@ public class EntityFinder {
|
||||
* @param self the source of the query, null if not any
|
||||
* @return all entities validating the conditions, can be empty
|
||||
*/
|
||||
@NotNull
|
||||
public List<Entity> find(@Nullable Instance instance, @Nullable Entity self) {
|
||||
public @NotNull List<@NotNull Entity> find(@Nullable Instance instance, @Nullable Entity self) {
|
||||
if (targetSelector == TargetSelector.MINESTOM_USERNAME) {
|
||||
Check.notNull(constantName, "The player name should not be null when searching for it");
|
||||
final Player player = MinecraftServer.getConnectionManager().getPlayer(constantName);
|
||||
@ -139,8 +138,9 @@ public class EntityFinder {
|
||||
return entity != null ? Collections.singletonList(entity) : Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Entity> result = findTarget(instance, targetSelector, startPosition, self);
|
||||
final Point pos = startPosition != null ? startPosition : (self != null ? self.getPosition() : Vec.ZERO);
|
||||
|
||||
List<Entity> result = findTarget(instance, targetSelector, pos, self);
|
||||
// Fast exit if there is nothing to process
|
||||
if (result.isEmpty())
|
||||
return result;
|
||||
@ -150,7 +150,7 @@ public class EntityFinder {
|
||||
final int minDistance = distance.getMinimum();
|
||||
final int maxDistance = distance.getMaximum();
|
||||
result = result.stream().filter(entity -> {
|
||||
final int distance = (int) entity.getDistance(self);
|
||||
final int distance = (int) entity.getPosition().distance(pos);
|
||||
return MathUtils.isBetween(distance, minDistance, maxDistance);
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
@ -161,17 +161,17 @@ public class EntityFinder {
|
||||
final var entityPosition = entity.getPosition();
|
||||
if (dx != null && !MathUtils.isBetweenUnordered(
|
||||
entityPosition.x(),
|
||||
startPosition.x(), dx))
|
||||
pos.x(), dx))
|
||||
return false;
|
||||
|
||||
if (dy != null && !MathUtils.isBetweenUnordered(
|
||||
entityPosition.y(),
|
||||
startPosition.y(), dy))
|
||||
pos.y(), dy))
|
||||
return false;
|
||||
|
||||
if (dz != null && !MathUtils.isBetweenUnordered(
|
||||
entityPosition.z(),
|
||||
startPosition.z(), dz))
|
||||
pos.z(), dz))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@ -181,7 +181,7 @@ public class EntityFinder {
|
||||
// Entity type
|
||||
if (!entityTypes.isEmpty()) {
|
||||
result = result.stream().filter(entity ->
|
||||
filterToggleableMap(entity.getEntityType(), entityTypes))
|
||||
filterToggleableMap(entity.getEntityType(), entityTypes))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@ -233,12 +233,12 @@ public class EntityFinder {
|
||||
// RANDOM is handled below
|
||||
return 1;
|
||||
case FURTHEST:
|
||||
return startPosition.distance(ent1.getPosition()) >
|
||||
startPosition.distance(ent2.getPosition()) ?
|
||||
return pos.distance(ent1.getPosition()) >
|
||||
pos.distance(ent2.getPosition()) ?
|
||||
1 : 0;
|
||||
case NEAREST:
|
||||
return startPosition.distance(ent1.getPosition()) <
|
||||
startPosition.distance(ent2.getPosition()) ?
|
||||
return pos.distance(ent1.getPosition()) <
|
||||
pos.distance(ent2.getPosition()) ?
|
||||
1 : 0;
|
||||
}
|
||||
return 1;
|
||||
@ -251,12 +251,10 @@ public class EntityFinder {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<Entity> find(@NotNull CommandSender sender) {
|
||||
public @NotNull List<@NotNull Entity> find(@NotNull CommandSender sender) {
|
||||
if (sender.isPlayer()) {
|
||||
Player player = sender.asPlayer();
|
||||
return find(player.getInstance(), player);
|
||||
@ -272,8 +270,7 @@ public class EntityFinder {
|
||||
* @return the first player returned by {@link #find(Instance, Entity)}
|
||||
* @see #find(Instance, Entity)
|
||||
*/
|
||||
@Nullable
|
||||
public Player findFirstPlayer(@Nullable Instance instance, @Nullable Entity self) {
|
||||
public @Nullable Player findFirstPlayer(@Nullable Instance instance, @Nullable Entity self) {
|
||||
final List<Entity> entities = find(instance, self);
|
||||
for (Entity entity : entities) {
|
||||
if (entity instanceof Player) {
|
||||
@ -283,8 +280,7 @@ public class EntityFinder {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Player findFirstPlayer(@NotNull CommandSender sender) {
|
||||
public @Nullable Player findFirstPlayer(@NotNull CommandSender sender) {
|
||||
if (sender.isPlayer()) {
|
||||
final Player player = sender.asPlayer();
|
||||
return findFirstPlayer(player.getInstance(), player);
|
||||
@ -293,8 +289,7 @@ public class EntityFinder {
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Entity findFirstEntity(@Nullable Instance instance, @Nullable Entity self) {
|
||||
public @Nullable Entity findFirstEntity(@Nullable Instance instance, @Nullable Entity self) {
|
||||
final List<Entity> entities = find(instance, self);
|
||||
for (Entity entity : entities) {
|
||||
return entity;
|
||||
@ -302,8 +297,7 @@ public class EntityFinder {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Entity findFirstEntity(@NotNull CommandSender sender) {
|
||||
public @Nullable Entity findFirstEntity(@NotNull CommandSender sender) {
|
||||
if (sender.isPlayer()) {
|
||||
final Player player = sender.asPlayer();
|
||||
return findFirstEntity(player.getInstance(), player);
|
||||
@ -337,62 +331,45 @@ public class EntityFinder {
|
||||
private static class ToggleableMap<T> extends Object2BooleanOpenHashMap<T> {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<Entity> findTarget(@Nullable Instance instance, @NotNull TargetSelector targetSelector,
|
||||
@NotNull Point startPosition, @Nullable Entity self) {
|
||||
|
||||
private static @NotNull List<@NotNull Entity> findTarget(@Nullable Instance instance,
|
||||
@NotNull TargetSelector targetSelector,
|
||||
@NotNull Point startPosition, @Nullable Entity self) {
|
||||
if (targetSelector == TargetSelector.NEAREST_PLAYER) {
|
||||
Entity entity = null;
|
||||
double closestDistance = Double.MAX_VALUE;
|
||||
|
||||
Collection<Player> instancePlayers = instance != null ?
|
||||
instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers();
|
||||
for (Player player : instancePlayers) {
|
||||
final double distance = player.getPosition().distance(startPosition);
|
||||
if (distance < closestDistance) {
|
||||
entity = player;
|
||||
closestDistance = distance;
|
||||
}
|
||||
}
|
||||
return Collections.singletonList(entity);
|
||||
return MinecraftServer.getConnectionManager().getOnlinePlayers().stream()
|
||||
.min(Comparator.comparingDouble(p -> p.getPosition().distance(startPosition)))
|
||||
.<List<Entity>>map(Collections::singletonList).orElse(Collections.emptyList());
|
||||
} else if (targetSelector == TargetSelector.RANDOM_PLAYER) {
|
||||
Collection<Player> instancePlayers = instance != null ?
|
||||
Collection<Player> players = instance != null ?
|
||||
instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers();
|
||||
final int index = ThreadLocalRandom.current().nextInt(instancePlayers.size());
|
||||
final Player player = instancePlayers.stream().skip(index).findFirst().orElseThrow();
|
||||
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 new ArrayList<>(instance != null ?
|
||||
return List.copyOf(instance != null ?
|
||||
instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers());
|
||||
} else if (targetSelector == TargetSelector.ALL_ENTITIES) {
|
||||
if (instance != null) {
|
||||
return new ArrayList<>(instance.getEntities());
|
||||
} else {
|
||||
// Get entities from every instance
|
||||
var instances = MinecraftServer.getInstanceManager().getInstances();
|
||||
List<Entity> entities = new LinkedList<>();
|
||||
for (Instance inst : instances) {
|
||||
entities.addAll(inst.getEntities());
|
||||
}
|
||||
return entities;
|
||||
return List.copyOf(instance.getEntities());
|
||||
}
|
||||
// Get entities from every instance
|
||||
var instances = MinecraftServer.getInstanceManager().getInstances();
|
||||
List<Entity> entities = new ArrayList<>();
|
||||
for (Instance inst : instances) {
|
||||
entities.addAll(inst.getEntities());
|
||||
}
|
||||
return entities;
|
||||
} else if (targetSelector == TargetSelector.SELF) {
|
||||
return Collections.singletonList(self);
|
||||
return self != null ? Collections.singletonList(self) : Collections.emptyList();
|
||||
}
|
||||
throw new IllegalStateException("Weird thing happened: " + targetSelector);
|
||||
}
|
||||
|
||||
private static <T> boolean filterToggleableMap(@NotNull T value, @NotNull ToggleableMap<T> map) {
|
||||
for (Object2BooleanMap.Entry<T> entry : map.object2BooleanEntrySet()) {
|
||||
final T key = entry.getKey();
|
||||
final boolean include = entry.getBooleanValue();
|
||||
|
||||
final boolean equals = Objects.equals(value, key);
|
||||
if (include && !equals || !include && equals) {
|
||||
if (entry.getBooleanValue() != Objects.equals(value, entry.getKey())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user