123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- package me.km.api;
- import java.util.Objects;
- import java.util.regex.PatternSyntaxException;
- import me.hammerle.code.Script;
- import me.km.dimensions.ModDimensions;
- import me.km.exception.IllegalStringLocationException;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.entity.Entity;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.MathHelper;
- import net.minecraft.util.math.Vec3d;
- import net.minecraft.world.World;
- public class Location
- {
- private final World w;
- private double x;
- private double y;
- private double z;
- private float yaw;
- private float pitch;
-
- public Location(World w, double x, double y, double z, float yaw, float pitch)
- {
- this.w = w;
- this.x = x;
- this.y = y;
- this.z = z;
- this.yaw = yaw;
- this.pitch = pitch;
- }
-
- public Location(World w, Vec3d pos, float yaw, float pitch)
- {
- this(w, pos.xCoord, pos.yCoord, pos.zCoord, yaw, pitch);
- }
-
- public Location(World w, Vec3d pos)
- {
- this(w, pos, 0, 0);
- }
-
- public Location(World w, BlockPos pos, float yaw, float pitch)
- {
- this(w, pos.getX(), pos.getY(), pos.getZ(), yaw, pitch);
- }
-
- public Location(World w, BlockPos pos)
- {
- this(w, pos, 0, 0);
- }
-
- public Location(Entity ent)
- {
- this(ent.world, ent.posX, ent.posY, ent.posZ, ent.rotationYaw, ent.rotationPitch);
- }
-
- public Location(Script sc, String location) throws IllegalStringLocationException
- {
- try
- {
- String[] parts = location.split(":");
- if(parts.length >= 6)
- {
- this.w = ModDimensions.getWorldFromName(parts[0]);
- this.x = Double.parseDouble(parts[1]);
- this.y = Double.parseDouble(parts[2]);
- this.z = Double.parseDouble(parts[3]);
- this.yaw = Float.parseFloat(parts[4]);
- this.pitch = Float.parseFloat(parts[5]);
- return;
- }
- else if(parts.length >= 4)
- {
- this.w = ModDimensions.getWorldFromName(parts[0]);
- this.x = Double.parseDouble(parts[1]);
- this.y = Double.parseDouble(parts[2]);
- this.z = Double.parseDouble(parts[3]);
- this.yaw = 0;
- this.pitch = 0;
- return;
- }
- throw new IllegalStringLocationException(sc, location);
- }
- catch(PatternSyntaxException | NumberFormatException ex)
- {
- throw new IllegalStringLocationException(sc, location);
- }
- }
- @Override
- public int hashCode()
- {
- int hash = 7;
- hash = 17 * hash + Objects.hashCode(this.w);
- hash = 17 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
- hash = 17 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
- hash = 17 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
- hash = 17 * hash + Float.floatToIntBits(this.yaw);
- hash = 17 * hash + Float.floatToIntBits(this.pitch);
- return hash;
- }
- @Override
- public boolean equals(Object obj)
- {
- if(this == obj)
- {
- return true;
- }
- if(obj == null || getClass() != obj.getClass())
- {
- return false;
- }
- final Location other = (Location) obj;
- return Double.doubleToLongBits(this.x) == Double.doubleToLongBits(other.x) &&
- Double.doubleToLongBits(this.y) == Double.doubleToLongBits(other.y) &&
- Double.doubleToLongBits(this.z) == Double.doubleToLongBits(other.z) &&
- Float.floatToIntBits(this.yaw) == Float.floatToIntBits(other.yaw) &&
- Float.floatToIntBits(this.pitch) == Float.floatToIntBits(other.pitch) &&
- Objects.equals(this.w, other.w);
- }
- public void add(double x, double y, double z)
- {
- this.x += x;
- this.y += y;
- this.z += z;
- }
-
- public Location copyAdd(double x, double y, double z)
- {
- return new Location(w, this.x + x, this.y + y, this.z + z, this.yaw, this.pitch);
- }
-
- public void addX(double x)
- {
- this.x += x;
- }
-
- public void addY(double y)
- {
- this.y += y;
- }
-
- public void addZ(double z)
- {
- this.z += z;
- }
-
- public void set(double x, double y, double z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- public double getX()
- {
- return x;
- }
- public double getY()
- {
- return y;
- }
- public double getZ()
- {
- return z;
- }
-
- public void round()
- {
- this.x = MathHelper.floor(this.x);
- this.y = MathHelper.floor(this.y);
- this.z = MathHelper.floor(this.z);
- }
- public World getWorld()
- {
- return w;
- }
- public Vec3d getPos()
- {
- return new Vec3d(x, y, z);
- }
-
- public BlockPos getBlockPos()
- {
- return new BlockPos(x, y, z);
- }
- public float getYaw()
- {
- return yaw;
- }
- public float getPitch()
- {
- return pitch;
- }
- public void setPitch(float pitch)
- {
- this.pitch = pitch;
- }
- public void setYaw(float yaw)
- {
- this.yaw = yaw;
- }
- public IBlockState getBlockState()
- {
- return w.getBlockState(new BlockPos(x, y, z));
- }
-
- public IBlockState getRelativeBlockState(int x, int y, int z)
- {
- return w.getBlockState(new BlockPos(this.x + x, this.y + y, this.z + z));
- }
- @Override
- public String toString()
- {
- StringBuilder sb = new StringBuilder(ModDimensions.getWorldName(w));
- sb.append(":");
- sb.append(x);
- sb.append(":");
- sb.append(y);
- sb.append(":");
- sb.append(z);
- sb.append(":");
- sb.append(yaw);
- sb.append(":");
- sb.append(pitch);
- return sb.toString();
- }
-
- public String toBlockString()
- {
- StringBuilder sb = new StringBuilder(ModDimensions.getWorldName(w));
- sb.append(":");
- sb.append(MathHelper.floor(x));
- sb.append(":");
- sb.append(MathHelper.floor(y));
- sb.append(":");
- sb.append(MathHelper.floor(z));
- return sb.toString();
- }
- public Location copy()
- {
- return new Location(w, x, y, z, yaw, pitch);
- }
- }
|