Location.java 6.4 KB

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