Location.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package me.km.api;
  2. import java.util.Objects;
  3. import me.km.dimensions.ModDimensions;
  4. import net.minecraft.block.state.IBlockState;
  5. import net.minecraft.entity.Entity;
  6. import net.minecraft.util.math.BlockPos;
  7. import net.minecraft.util.math.Vec3d;
  8. import net.minecraft.world.World;
  9. public class Location
  10. {
  11. private final World w;
  12. private Vec3d pos;
  13. private float yaw;
  14. private float pitch;
  15. public Location(World w, Vec3d pos, float yaw, float pitch)
  16. {
  17. this.w = w;
  18. this.pos = pos;
  19. this.yaw = yaw;
  20. this.pitch = pitch;
  21. }
  22. public Location(World w, Vec3d pos)
  23. {
  24. this(w, pos, 0, 0);
  25. }
  26. public Location(World w, BlockPos pos, float yaw, float pitch)
  27. {
  28. this(w, new Vec3d(pos.getX(), pos.getY(), pos.getZ()), yaw, pitch);
  29. }
  30. public Location(World w, BlockPos pos)
  31. {
  32. this(w, pos, 0, 0);
  33. }
  34. public Location(Entity ent)
  35. {
  36. this(ent.world, ent.getPositionVector(), ent.rotationYaw, ent.rotationPitch);
  37. }
  38. public static Location rounded(Entity ent)
  39. {
  40. return new Location(ent.world, ent.getPosition());
  41. }
  42. @Override
  43. public int hashCode()
  44. {
  45. int hash = 5;
  46. hash = 73 * hash + Objects.hashCode(this.w);
  47. hash = 73 * hash + Objects.hashCode(this.pos);
  48. hash = 73 * hash + Float.floatToIntBits(this.yaw);
  49. hash = 73 * hash + Float.floatToIntBits(this.pitch);
  50. return hash;
  51. }
  52. @Override
  53. public boolean equals(Object obj)
  54. {
  55. if(obj.getClass() == Location.class)
  56. {
  57. Location l = (Location) obj;
  58. Vec3d v = l.getPos();
  59. return l.getWorld().equals(w) && v.equals(pos) && yaw == l.yaw && pitch == l.pitch;
  60. }
  61. return false;
  62. }
  63. public Location add(double x, double y, double z)
  64. {
  65. pos = pos.addVector(x, y, z);
  66. return this;
  67. }
  68. public World getWorld()
  69. {
  70. return w;
  71. }
  72. public Vec3d getPos()
  73. {
  74. return pos;
  75. }
  76. public BlockPos getBlockPos()
  77. {
  78. return new BlockPos(pos.xCoord, pos.yCoord, pos.zCoord);
  79. }
  80. public float getYaw()
  81. {
  82. return yaw;
  83. }
  84. public float getPitch()
  85. {
  86. return pitch;
  87. }
  88. public void setPitch(float pitch)
  89. {
  90. this.pitch = pitch;
  91. }
  92. public void setYaw(float yaw)
  93. {
  94. this.yaw = yaw;
  95. }
  96. public static IBlockState getRelativeBlock(World w, BlockPos pos, int x, int y, int z)
  97. {
  98. return w.getBlockState(pos.add(x, y, z));
  99. }
  100. @Override
  101. public String toString()
  102. {
  103. return ModDimensions.getWorldName(w) + ":" + pos.xCoord + ":" + pos.yCoord +
  104. ":" + pos.zCoord + ":" + yaw + ":" + pitch;
  105. }
  106. }