Location.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package me.km.utils;
  2. import java.util.Objects;
  3. import java.util.regex.PatternSyntaxException;
  4. import net.minecraft.block.BlockState;
  5. import net.minecraft.entity.Entity;
  6. import net.minecraft.server.MinecraftServer;
  7. import net.minecraft.util.math.BlockPos;
  8. import net.minecraft.util.math.MathHelper;
  9. import net.minecraft.util.math.vector.Vector3d;
  10. import net.minecraft.world.World;
  11. public class Location {
  12. private final World w;
  13. private double x;
  14. private double y;
  15. private double z;
  16. private float yaw;
  17. private float pitch;
  18. public Location(World w, double x, double y, double z, float yaw, float pitch) {
  19. this.w = w;
  20. this.x = x;
  21. this.y = y;
  22. this.z = z;
  23. this.yaw = yaw;
  24. this.pitch = pitch;
  25. }
  26. public Location(World w, Vector3d pos, float yaw, float pitch) {
  27. this(w, pos.x, pos.y, pos.z, yaw, pitch);
  28. }
  29. public Location(World w, Vector3d pos) {
  30. this(w, pos, 0, 0);
  31. }
  32. public Location(World w, BlockPos pos, float yaw, float pitch) {
  33. this(w, pos.getX(), pos.getY(), pos.getZ(), yaw, pitch);
  34. }
  35. public Location(World w, BlockPos pos) {
  36. this(w, pos, 0, 0);
  37. }
  38. public Location(Entity ent) {
  39. this(ent.world, ent.getPosX(), ent.getPosY(), ent.getPosZ(), ent.rotationYaw,
  40. ent.rotationPitch);
  41. }
  42. public Location(MinecraftServer server, String location) {
  43. try {
  44. String[] parts = location.split(":");
  45. if(parts.length >= 6) {
  46. this.w = Utils.getWorldFromName(server, parts[0]);
  47. this.x = Double.parseDouble(parts[1]);
  48. this.y = Double.parseDouble(parts[2]);
  49. this.z = Double.parseDouble(parts[3]);
  50. this.yaw = Float.parseFloat(parts[4]);
  51. this.pitch = Float.parseFloat(parts[5]);
  52. return;
  53. } else if(parts.length >= 4) {
  54. this.w = Utils.getWorldFromName(server, parts[0]);
  55. this.x = Double.parseDouble(parts[1]);
  56. this.y = Double.parseDouble(parts[2]);
  57. this.z = Double.parseDouble(parts[3]);
  58. this.yaw = 0;
  59. this.pitch = 0;
  60. return;
  61. }
  62. throw new IllegalArgumentException(
  63. String.format("'%s' is not a valid location string", location));
  64. } catch (PatternSyntaxException | NumberFormatException ex) {
  65. throw new IllegalArgumentException(
  66. String.format("'%s' is not a valid location string", location));
  67. }
  68. }
  69. @Override
  70. public int hashCode() {
  71. int hash = 7;
  72. hash = 17 * hash + Objects.hashCode(this.w);
  73. hash = 17 * hash + (int) (Double.doubleToLongBits(this.x)
  74. ^ (Double.doubleToLongBits(this.x) >>> 32));
  75. hash = 17 * hash + (int) (Double.doubleToLongBits(this.y)
  76. ^ (Double.doubleToLongBits(this.y) >>> 32));
  77. hash = 17 * hash + (int) (Double.doubleToLongBits(this.z)
  78. ^ (Double.doubleToLongBits(this.z) >>> 32));
  79. return hash;
  80. }
  81. @Override
  82. public boolean equals(Object obj) {
  83. if(this == obj) {
  84. return true;
  85. }
  86. if(obj == null || getClass() != obj.getClass()) {
  87. return false;
  88. }
  89. final Location other = (Location) obj;
  90. return Double.doubleToLongBits(this.x) == Double.doubleToLongBits(other.x)
  91. && Double.doubleToLongBits(this.y) == Double.doubleToLongBits(other.y)
  92. && Double.doubleToLongBits(this.z) == Double.doubleToLongBits(other.z)
  93. && Objects.equals(this.w, other.w);
  94. }
  95. public void add(double x, double y, double z) {
  96. this.x += x;
  97. this.y += y;
  98. this.z += z;
  99. }
  100. public Location copyAdd(double x, double y, double z) {
  101. return new Location(w, this.x + x, this.y + y, this.z + z, this.yaw, this.pitch);
  102. }
  103. public void addX(double x) {
  104. this.x += x;
  105. }
  106. public void addY(double y) {
  107. this.y += y;
  108. }
  109. public void addZ(double z) {
  110. this.z += z;
  111. }
  112. public void setX(double x) {
  113. this.x = x;
  114. }
  115. public void setY(double y) {
  116. this.y = y;
  117. }
  118. public void setZ(double z) {
  119. this.z = z;
  120. }
  121. public void set(double x, double y, double z) {
  122. this.x = x;
  123. this.y = y;
  124. this.z = z;
  125. }
  126. public double getX() {
  127. return x;
  128. }
  129. public double getY() {
  130. return y;
  131. }
  132. public double getZ() {
  133. return z;
  134. }
  135. public void round() {
  136. this.x = MathHelper.floor(this.x);
  137. this.y = MathHelper.floor(this.y);
  138. this.z = MathHelper.floor(this.z);
  139. }
  140. public World getWorld() {
  141. return w;
  142. }
  143. public Vector3d getPos() {
  144. return new Vector3d(x, y, z);
  145. }
  146. public BlockPos getBlockPos() {
  147. return new BlockPos(x, y, z);
  148. }
  149. public float getYaw() {
  150. return yaw;
  151. }
  152. public float getPitch() {
  153. return pitch;
  154. }
  155. public void setPitch(float pitch) {
  156. this.pitch = pitch;
  157. }
  158. public void setYaw(float yaw) {
  159. this.yaw = yaw;
  160. }
  161. public BlockState getBlockState() {
  162. return w.getBlockState(new BlockPos(x, y, z));
  163. }
  164. public BlockState getRelativeBlockState(int x, int y, int z) {
  165. return w.getBlockState(new BlockPos(this.x + x, this.y + y, this.z + z));
  166. }
  167. @Override
  168. public String toString() {
  169. StringBuilder sb = new StringBuilder(Utils.getWorldName(w));
  170. sb.append(":");
  171. sb.append(x);
  172. sb.append(":");
  173. sb.append(y);
  174. sb.append(":");
  175. sb.append(z);
  176. sb.append(":");
  177. sb.append(yaw);
  178. sb.append(":");
  179. sb.append(pitch);
  180. return sb.toString();
  181. }
  182. public String toBlockString() {
  183. StringBuilder sb = new StringBuilder(Utils.getWorldName(w));
  184. sb.append(":");
  185. sb.append(MathHelper.floor(x));
  186. sb.append(":");
  187. sb.append(MathHelper.floor(y));
  188. sb.append(":");
  189. sb.append(MathHelper.floor(z));
  190. return sb.toString();
  191. }
  192. public Location copy() {
  193. return new Location(w, x, y, z, yaw, pitch);
  194. }
  195. }