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