From 1c10f2ebf1362e03aed8d00273672a4ffe70e6cd Mon Sep 17 00:00:00 2001 From: c0508383 Date: Wed, 6 Jul 2022 18:12:32 -0500 Subject: [PATCH] IWorld: -getEntitiesNear sorts by which one is nearest to the position. --- .../noppes/npcs/scripted/ScriptWorld.java | 29 ++++++++++--------- .../npcs/scripted/interfaces/IWorld.java | 4 +-- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/main/java/noppes/npcs/scripted/ScriptWorld.java b/src/main/java/noppes/npcs/scripted/ScriptWorld.java index c3a57cb98..abf55c634 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptWorld.java +++ b/src/main/java/noppes/npcs/scripted/ScriptWorld.java @@ -192,19 +192,6 @@ public class ScriptWorld implements IWorld { return list.toArray(new IEntity[0]); } - public IEntity[] getEntitiesNear(double x, double y, double z, double range) { - ArrayList list = new ArrayList<>(); - - List entities = world.getEntitiesWithinAABB(Entity.class, AxisAlignedBB.getBoundingBox( - x - range, y - range, z - range, - x + range, y + range, z + range)); - for(Entity entity : entities){ - list.add(NpcAPI.Instance().getIEntity(entity)); - } - - return list.toArray(new IEntity[0]); - } - public IEntity[] getEntitiesNear(IPos position, double range) { ArrayList list = new ArrayList<>(); @@ -215,9 +202,25 @@ public class ScriptWorld implements IWorld { list.add(NpcAPI.Instance().getIEntity(entity)); } + list.sort((e1, e2) -> { + double dist1 = e1.getPos().distanceTo(position); + double dist2 = e2.getPos().distanceTo(position); + + if (dist1 > dist2) { + return 1; + } else if (dist1 < dist2) { + return -1; + } + return 0; + }); + return list.toArray(new IEntity[0]); } + public IEntity[] getEntitiesNear(double x, double y, double z, double range) { + return getEntitiesNear(new ScriptBlockPos(new BlockPos(x,y,z)), range); + } + public void setTileEntity(int x, int y, int z, ITileEntity tileEntity){ world.setTileEntity(x,y,z,tileEntity.getMCTileEntity()); } diff --git a/src/main/java/noppes/npcs/scripted/interfaces/IWorld.java b/src/main/java/noppes/npcs/scripted/interfaces/IWorld.java index 58b6c6f60..ec04b534c 100644 --- a/src/main/java/noppes/npcs/scripted/interfaces/IWorld.java +++ b/src/main/java/noppes/npcs/scripted/interfaces/IWorld.java @@ -100,10 +100,10 @@ public interface IWorld { IEntity[] getLoadedEntities(); - IEntity[] getEntitiesNear(double x, double y, double z, double range); - IEntity[] getEntitiesNear(IPos position, double range); + IEntity[] getEntitiesNear(double x, double y, double z, double range); + /** * Sets the block's tile entity at the given position. */