Location.java 6.5 KB

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