Location.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package me.km.api;
  2. import java.util.Objects;
  3. import java.util.regex.PatternSyntaxException;
  4. import me.hammerle.snuviscript.code.Script;
  5. import me.km.dimensions.ModDimensions;
  6. import me.km.exception.IllegalLocationStringException;
  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.x, pos.y, pos.z, 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(String location) throws IllegalLocationStringException
  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 IllegalLocationStringException(location);
  76. }
  77. catch(PatternSyntaxException | NumberFormatException ex)
  78. {
  79. throw new IllegalLocationStringException(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. return hash;
  91. }
  92. @Override
  93. public boolean equals(Object obj)
  94. {
  95. if(this == obj)
  96. {
  97. return true;
  98. }
  99. if(obj == null || getClass() != obj.getClass())
  100. {
  101. return false;
  102. }
  103. final Location other = (Location) obj;
  104. return Double.doubleToLongBits(this.x) == Double.doubleToLongBits(other.x) &&
  105. Double.doubleToLongBits(this.y) == Double.doubleToLongBits(other.y) &&
  106. Double.doubleToLongBits(this.z) == Double.doubleToLongBits(other.z) &&
  107. Objects.equals(this.w, other.w);
  108. }
  109. public void add(double x, double y, double z)
  110. {
  111. this.x += x;
  112. this.y += y;
  113. this.z += z;
  114. }
  115. public Location copyAdd(double x, double y, double z)
  116. {
  117. return new Location(w, this.x + x, this.y + y, this.z + z, this.yaw, this.pitch);
  118. }
  119. public void addX(double x)
  120. {
  121. this.x += x;
  122. }
  123. public void addY(double y)
  124. {
  125. this.y += y;
  126. }
  127. public void addZ(double z)
  128. {
  129. this.z += z;
  130. }
  131. public void setX(double x)
  132. {
  133. this.x = x;
  134. }
  135. public void setY(double y)
  136. {
  137. this.y = y;
  138. }
  139. public void setZ(double z)
  140. {
  141. this.z = z;
  142. }
  143. public void set(double x, double y, double z)
  144. {
  145. this.x = x;
  146. this.y = y;
  147. this.z = z;
  148. }
  149. public double getX()
  150. {
  151. return x;
  152. }
  153. public double getY()
  154. {
  155. return y;
  156. }
  157. public double getZ()
  158. {
  159. return z;
  160. }
  161. public void round()
  162. {
  163. this.x = MathHelper.floor(this.x);
  164. this.y = MathHelper.floor(this.y);
  165. this.z = MathHelper.floor(this.z);
  166. }
  167. public World getWorld()
  168. {
  169. return w;
  170. }
  171. public Vec3d getPos()
  172. {
  173. return new Vec3d(x, y, z);
  174. }
  175. public BlockPos getBlockPos()
  176. {
  177. return new BlockPos(x, y, z);
  178. }
  179. public float getYaw()
  180. {
  181. return yaw;
  182. }
  183. public float getPitch()
  184. {
  185. return pitch;
  186. }
  187. public void setPitch(float pitch)
  188. {
  189. this.pitch = pitch;
  190. }
  191. public void setYaw(float yaw)
  192. {
  193. this.yaw = yaw;
  194. }
  195. public IBlockState getBlockState()
  196. {
  197. return w.getBlockState(new BlockPos(x, y, z));
  198. }
  199. public IBlockState getRelativeBlockState(int x, int y, int z)
  200. {
  201. return w.getBlockState(new BlockPos(this.x + x, this.y + y, this.z + z));
  202. }
  203. @Override
  204. public String toString()
  205. {
  206. StringBuilder sb = new StringBuilder(ModDimensions.getWorldName(w));
  207. sb.append(":");
  208. sb.append(x);
  209. sb.append(":");
  210. sb.append(y);
  211. sb.append(":");
  212. sb.append(z);
  213. sb.append(":");
  214. sb.append(yaw);
  215. sb.append(":");
  216. sb.append(pitch);
  217. return sb.toString();
  218. }
  219. public String toBlockString()
  220. {
  221. StringBuilder sb = new StringBuilder(ModDimensions.getWorldName(w));
  222. sb.append(":");
  223. sb.append(MathHelper.floor(x));
  224. sb.append(":");
  225. sb.append(MathHelper.floor(y));
  226. sb.append(":");
  227. sb.append(MathHelper.floor(z));
  228. return sb.toString();
  229. }
  230. public Location copy()
  231. {
  232. return new Location(w, x, y, z, yaw, pitch);
  233. }
  234. }