From b592bb5d5e9416681bfa8953ef6ac595540768a5 Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Fri, 7 Jul 2023 17:51:51 -0500 Subject: [PATCH] IPos objects now use double values in calculations for distance, addition, cross product, etc. --- .../java/noppes/npcs/scripted/NpcAPI.java | 10 +-- .../noppes/npcs/scripted/ScriptBlockPos.java | 65 ++++++++++++------- .../noppes/npcs/scripted/ScriptParticle.java | 2 +- .../noppes/npcs/scripted/ScriptSound.java | 6 +- .../npcs/scripted/ScriptTileEntity.java | 2 +- .../npcs/scripted/entity/ScriptEntity.java | 10 +-- .../npcs/scripted/entity/ScriptPlayer.java | 6 +- 7 files changed, 58 insertions(+), 43 deletions(-) diff --git a/src/main/java/noppes/npcs/scripted/NpcAPI.java b/src/main/java/noppes/npcs/scripted/NpcAPI.java index 6fb0f7ff3..428ee39dc 100644 --- a/src/main/java/noppes/npcs/scripted/NpcAPI.java +++ b/src/main/java/noppes/npcs/scripted/NpcAPI.java @@ -276,16 +276,16 @@ public class NpcAPI extends AbstractNpcAPI { return new ScriptBlockPos(pos); } - public IPos getIPos(int x, int y, int z) { - return new ScriptBlockPos(new BlockPos(x,y,z)); + public IPos getIPos(double x, double y, double z) { + return this.getIPos(new BlockPos(x,y,z)); } - public IPos getIPos(double x, double y, double z) { - return this.getIPos((int)x,(int)y,(int)z); + public IPos getIPos(int x, int y, int z) { + return this.getIPos((double) x, (double) y, (double) z); } public IPos getIPos(float x, float y, float z) { - return this.getIPos((int)x,(int)y,(int)z); + return this.getIPos((double) x, (double) y, (double) z); } public IPos getIPos(long serializedPos) { diff --git a/src/main/java/noppes/npcs/scripted/ScriptBlockPos.java b/src/main/java/noppes/npcs/scripted/ScriptBlockPos.java index aa144d096..b70d5766b 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptBlockPos.java +++ b/src/main/java/noppes/npcs/scripted/ScriptBlockPos.java @@ -29,11 +29,23 @@ public class ScriptBlockPos implements IPos { return this.blockPos.getZ(); } + public double getXD() { + return this.blockPos.getXD(); + } + + public double getYD() { + return this.blockPos.getYD(); + } + + public double getZD() { + return this.blockPos.getZD(); + } + public IPos up() { return NpcAPI.Instance().getIPos(this.blockPos.up()); } - public IPos up(int n) { + public IPos up(double n) { return NpcAPI.Instance().getIPos(this.blockPos.up(n)); } @@ -41,7 +53,7 @@ public class ScriptBlockPos implements IPos { return NpcAPI.Instance().getIPos(this.blockPos.down()); } - public IPos down(int n) { + public IPos down(double n) { return NpcAPI.Instance().getIPos(this.blockPos.down(n)); } @@ -49,7 +61,7 @@ public class ScriptBlockPos implements IPos { return NpcAPI.Instance().getIPos(this.blockPos.north()); } - public IPos north(int n) { + public IPos north(double n) { return NpcAPI.Instance().getIPos(this.blockPos.north(n)); } @@ -57,7 +69,7 @@ public class ScriptBlockPos implements IPos { return NpcAPI.Instance().getIPos(this.blockPos.east()); } - public IPos east(int n) { + public IPos east(double n) { return NpcAPI.Instance().getIPos(this.blockPos.east(n)); } @@ -65,7 +77,7 @@ public class ScriptBlockPos implements IPos { return NpcAPI.Instance().getIPos(this.blockPos.south()); } - public IPos south(int n) { + public IPos south(double n) { return NpcAPI.Instance().getIPos(this.blockPos.south(n)); } @@ -73,11 +85,11 @@ public class ScriptBlockPos implements IPos { return NpcAPI.Instance().getIPos(this.blockPos.west()); } - public IPos west(int n) { + public IPos west(double n) { return NpcAPI.Instance().getIPos(this.blockPos.west(n)); } - public IPos add(int x, int y, int z) { + public IPos add(double x, double y, double z) { return NpcAPI.Instance().getIPos(this.blockPos.add(x, y, z)); } @@ -85,33 +97,33 @@ public class ScriptBlockPos implements IPos { return NpcAPI.Instance().getIPos(this.blockPos.add(this.blockPos)); } - public IPos subtract(int x, int y, int z) { + public IPos subtract(double x, double y, double z) { return NpcAPI.Instance().getIPos(this.blockPos.add(-x, -y, -z)); } public IPos subtract(IPos pos) { - return NpcAPI.Instance().getIPos(this.blockPos.add(-pos.getX(), -pos.getY(), -pos.getZ())); + return NpcAPI.Instance().getIPos(this.blockPos.add(-pos.getXD(), -pos.getYD(), -pos.getZD())); } public IPos offset(int direction) { return NpcAPI.Instance().getIPos(this.blockPos.offset(EnumFacing.values()[direction])); } - public IPos offset(int direction, int n) { + public IPos offset(int direction, double n) { return NpcAPI.Instance().getIPos(this.blockPos.offset(EnumFacing.values()[direction], n)); } - public IPos crossProduct(int x, int y, int z) { + public IPos crossProduct(double x, double y, double z) { return this.crossProduct(NpcAPI.Instance().getIPos(x,y,z)); } public IPos crossProduct(IPos pos) { - return NpcAPI.Instance().getIPos(this.blockPos.crossProduct(new Vec3i(-pos.getX(), -pos.getY(), -pos.getZ()))); + return NpcAPI.Instance().getIPos(this.blockPos.crossProduct(new Vec3i(-pos.getXD(), -pos.getYD(), -pos.getZD()))); } - public IPos divide(float scalar) { + public IPos divide(double scalar) { return NpcAPI.Instance().getIPos( - getX()/scalar , getY()/scalar, getZ()/scalar + getXD()/scalar , getYD()/scalar, getZD()/scalar ); } @@ -125,26 +137,26 @@ public class ScriptBlockPos implements IPos { } public IPos normalize() { - double d = Math.sqrt((double)(this.blockPos.getX() * this.blockPos.getX() + this.blockPos.getY() * this.blockPos.getY() + this.blockPos.getZ() * this.blockPos.getZ())); - return NpcAPI.Instance().getIPos((double)this.getX() / d, (double)this.getY() / d, (double)this.getZ() / d); + double d = Math.sqrt(this.blockPos.getXD() * this.blockPos.getXD() + this.blockPos.getYD() * this.blockPos.getYD() + this.blockPos.getZD() * this.blockPos.getZD()); + return NpcAPI.Instance().getIPos(this.getXD() / d, this.getYD() / d, this.getZD() / d); } public double[] normalizeDouble() { - double d = Math.sqrt((double)(this.blockPos.getX() * this.blockPos.getX() + this.blockPos.getY() * this.blockPos.getY() + this.blockPos.getZ() * this.blockPos.getZ())); - return new double[]{(double)this.getX() / d, (double)this.getY() / d, (double)this.getZ() / d}; + double d = Math.sqrt(this.blockPos.getXD() * this.blockPos.getXD() + this.blockPos.getYD() * this.blockPos.getYD() + this.blockPos.getZD() * this.blockPos.getZD()); + return new double[]{this.getXD() / d, this.getYD() / d, this.getZD() / d}; } public double distanceTo(IPos pos) { - double d0 = (double)(this.getX() - pos.getX()); - double d1 = (double)(this.getY() - pos.getY()); - double d2 = (double)(this.getZ() - pos.getZ()); + double d0 = this.getXD() - pos.getXD(); + double d1 = this.getYD() - pos.getYD(); + double d2 = this.getZD() - pos.getZD(); return Math.sqrt(d0 * d0 + d1 * d1 + d2 * d2); } public double distanceTo(double x, double y, double z) { - double d0 = (double)(this.getX() - x); - double d1 = (double)(this.getY() - y); - double d2 = (double)(this.getZ() - z); + double d0 = this.getXD() - x; + double d1 = this.getYD() - y; + double d2 = this.getZD() - z; return Math.sqrt(d0 * d0 + d1 * d1 + d2 * d2); } @@ -153,7 +165,10 @@ public class ScriptBlockPos implements IPos { } public String toString() { - return "(" + this.getX() + ", " + this.getY() + ", " + this.getZ() + ")"; + double xd = Math.floor(this.getXD() * 1000)/1000.0D; + double yd = Math.floor(this.getYD() * 1000)/1000.0D; + double zd = Math.floor(this.getZD() * 1000)/1000.0D; + return "(" + xd + ", " + yd + ", " + zd + ")"; } public boolean equals(Object object) { diff --git a/src/main/java/noppes/npcs/scripted/ScriptParticle.java b/src/main/java/noppes/npcs/scripted/ScriptParticle.java index 8b37ee811..8cc313d80 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptParticle.java +++ b/src/main/java/noppes/npcs/scripted/ScriptParticle.java @@ -233,7 +233,7 @@ public class ScriptParticle implements IParticle { } public void setPosition(IPos pos) { - this.setPosition(pos.getX(),pos.getY(),pos.getZ()); + this.setPosition(pos.getXD(),pos.getYD(),pos.getZD()); } public void getPos() { NpcAPI.Instance().getIPos(x,y,z); diff --git a/src/main/java/noppes/npcs/scripted/ScriptSound.java b/src/main/java/noppes/npcs/scripted/ScriptSound.java index c2f9c471c..a2a46f201 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptSound.java +++ b/src/main/java/noppes/npcs/scripted/ScriptSound.java @@ -143,9 +143,9 @@ public class ScriptSound implements ISound { } public void setPosition(IPos pos) { - this.xPosF = pos.getX(); - this.yPosF = pos.getY(); - this.zPosF = pos.getZ(); + this.xPosF = (float) pos.getXD(); + this.yPosF = (float) pos.getYD(); + this.zPosF = (float) pos.getZD(); } public void setPosition(float x, float y, float z) { diff --git a/src/main/java/noppes/npcs/scripted/ScriptTileEntity.java b/src/main/java/noppes/npcs/scripted/ScriptTileEntity.java index 861507367..1d83bffa0 100644 --- a/src/main/java/noppes/npcs/scripted/ScriptTileEntity.java +++ b/src/main/java/noppes/npcs/scripted/ScriptTileEntity.java @@ -44,7 +44,7 @@ public class ScriptTileEntity implements ITileEntity { } public double getDistanceFrom(IPos pos){ - return this.getDistanceFrom(pos.getX(),pos.getY(),pos.getZ()); + return this.getDistanceFrom(pos.getXD(),pos.getYD(),pos.getZD()); } public IBlock getBlockType(){ diff --git a/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java b/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java index 750afcb5b..a3bbf4e59 100644 --- a/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java +++ b/src/main/java/noppes/npcs/scripted/entity/ScriptEntity.java @@ -363,7 +363,7 @@ public class ScriptEntity implements IEntity { } public void setMotion(IPos pos) { - this.setMotion(pos.getX(),pos.getY(),pos.getZ()); + this.setMotion(pos.getXD(),pos.getYD(),pos.getZD()); } public IPos getMotion() { @@ -413,7 +413,7 @@ public class ScriptEntity implements IEntity { } public void setPos(IPos pos) { - this.entity.setPosition((double)((float)pos.getX() + 0.5F), (double)pos.getY(), (double)((float)pos.getZ() + 0.5F)); + this.entity.setPosition(pos.getXD(), pos.getYD(), pos.getZD()); } public void setPosition(IPos pos) { @@ -776,14 +776,14 @@ public class ScriptEntity implements IEntity { entity.velocityChanged = true; } - public void knockback(int xpower, int ypower, int zpower, float direction){ + public void knockback(double xpower, double ypower, double zpower, float direction){ float v = direction * (float)Math.PI / 180.0F; - entity.addVelocity(-MathHelper.sin(v) * (float)xpower, ypower, MathHelper.cos(v) * (float)zpower); + entity.addVelocity(-MathHelper.sin(v) * xpower, ypower, MathHelper.cos(v) * zpower); entity.velocityChanged = true; } public void knockback(IPos pos, float direction){ - this.knockback(pos.getX(),pos.getY(),pos.getZ(),direction); + this.knockback(pos.getXD(),pos.getYD(),pos.getZD(),direction); } public void setImmune(int ticks) { diff --git a/src/main/java/noppes/npcs/scripted/entity/ScriptPlayer.java b/src/main/java/noppes/npcs/scripted/entity/ScriptPlayer.java index 653c0a705..8b379bcf3 100644 --- a/src/main/java/noppes/npcs/scripted/entity/ScriptPlayer.java +++ b/src/main/java/noppes/npcs/scripted/entity/ScriptPlayer.java @@ -87,7 +87,7 @@ public class ScriptPlayer extends ScriptLivingBase } public void setPosition(IPos pos) { - this.setPosition(pos.getX(),pos.getY(),pos.getZ()); + this.setPosition(pos.getXD(),pos.getYD(),pos.getZD()); } public void setPos(IPos pos) { this.setPosition(pos); @@ -110,13 +110,13 @@ public class ScriptPlayer extends ScriptLivingBase } public void setPosition(IPos pos, int dimensionId) { - this.setPosition(pos.getX(),pos.getY(),pos.getZ(), dimensionId); + this.setPosition(pos.getXD(),pos.getYD(),pos.getZD(), dimensionId); } public void setPos(IPos pos, int dimensionId) { this.setPosition(pos,dimensionId); } public void setPosition(IPos pos, IWorld world) { - this.setPosition(pos.getX(),pos.getY(),pos.getZ(),world.getDimensionID()); + this.setPosition(pos.getXD(),pos.getYD(),pos.getZD(),world.getDimensionID()); } public void setPos(IPos pos, IWorld world) { this.setPosition(pos,world.getDimensionID());