From 19e3bee23cc4a364cdb391afd655c7c67f383a7c Mon Sep 17 00:00:00 2001 From: TheMode Date: Sat, 24 Jul 2021 04:31:35 +0200 Subject: [PATCH] Lambda cleanup --- .../net/minestom/server/entity/Entity.java | 6 ++- .../server/utils/entity/EntityFinder.java | 45 +++++++++---------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 3b35d5311..c1496587d 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -823,6 +823,10 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler this.gravityAcceleration = gravityAcceleration; } + public double getDistance(@NotNull Point point) { + return getPosition().distance(point); + } + /** * Gets the distance between two entities. * @@ -830,7 +834,7 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler * @return the distance between this and {@code entity} */ public double getDistance(@NotNull Entity entity) { - return getPosition().distance(entity.getPosition()); + return getDistance(entity.getPosition()); } /** 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 cf365db4d..a16f94c33 100644 --- a/src/main/java/net/minestom/server/utils/entity/EntityFinder.java +++ b/src/main/java/net/minestom/server/utils/entity/EntityFinder.java @@ -149,10 +149,9 @@ public class EntityFinder { if (distance != null) { final int minDistance = distance.getMinimum(); final int maxDistance = distance.getMaximum(); - result = result.stream().filter(entity -> { - final int distance = (int) entity.getPosition().distance(pos); - return MathUtils.isBetween(distance, minDistance, maxDistance); - }).collect(Collectors.toList()); + result = result.stream() + .filter(entity -> MathUtils.isBetween(entity.getDistance(pos), minDistance, maxDistance)) + .collect(Collectors.toList()); } // Diff X/Y/Z @@ -180,46 +179,42 @@ public class EntityFinder { // Entity type if (!entityTypes.isEmpty()) { - result = result.stream().filter(entity -> - filterToggleableMap(entity.getEntityType(), entityTypes)) + result = result.stream() + .filter(entity -> filterToggleableMap(entity.getEntityType(), entityTypes)) .collect(Collectors.toList()); } // GameMode if (!gameModes.isEmpty()) { - result = result.stream().filter(entity -> { - if (!(entity instanceof Player)) - return false; - return filterToggleableMap(((Player) entity).getGameMode(), gameModes); - }).collect(Collectors.toList()); + result = result.stream() + .filter(Player.class::isInstance) + .filter(entity -> filterToggleableMap(((Player) entity).getGameMode(), gameModes)) + .collect(Collectors.toList()); } // Level if (level != null) { final int minLevel = level.getMinimum(); final int maxLevel = level.getMaximum(); - result = result.stream().filter(entity -> { - if (!(entity instanceof Player)) - return false; - - final int level = ((Player) entity).getLevel(); - return MathUtils.isBetween(level, minLevel, maxLevel); - }).collect(Collectors.toList()); + result = result.stream() + .filter(Player.class::isInstance) + .filter(entity -> MathUtils.isBetween(((Player) entity).getLevel(), minLevel, maxLevel)) + .collect(Collectors.toList()); } // Name if (!names.isEmpty()) { - result = result.stream().filter(entity -> { - if (!(entity instanceof Player)) - return false; - return filterToggleableMap(((Player) entity).getUsername(), names); - }).collect(Collectors.toList()); + result = result.stream() + .filter(Player.class::isInstance) + .filter(entity -> filterToggleableMap(((Player) entity).getUsername(), names)) + .collect(Collectors.toList()); } // UUID if (!uuids.isEmpty()) { - result = result.stream().filter(entity -> - filterToggleableMap(entity.getUuid(), uuids)).collect(Collectors.toList()); + result = result.stream() + .filter(entity -> filterToggleableMap(entity.getUuid(), uuids)) + .collect(Collectors.toList()); }