Location.java 6.4 KB

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