Location.java 6.2 KB

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