Location.java 6.1 KB

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