Bukkit  1.5.2-R1.0
 All Classes Namespaces Files Functions Variables Enumerator Pages
Location.java
Go to the documentation of this file.
1 package org.bukkit;
2 
3 import org.bukkit.block.Block;
4 import org.bukkit.util.NumberConversions;
5 import org.bukkit.util.Vector;
6 
10 public class Location implements Cloneable {
11  private World world;
12  private double x;
13  private double y;
14  private double z;
15  private float pitch;
16  private float yaw;
17 
26  public Location(final World world, final double x, final double y, final double z) {
27  this(world, x, y, z, 0, 0);
28  }
29 
40  public Location(final World world, final double x, final double y, final double z, final float yaw, final float pitch) {
41  this.world = world;
42  this.x = x;
43  this.y = y;
44  this.z = z;
45  this.pitch = pitch;
46  this.yaw = yaw;
47  }
48 
54  public void setWorld(World world) {
55  this.world = world;
56  }
57 
63  public World getWorld() {
64  return world;
65  }
66 
72  public Chunk getChunk() {
73  return world.getChunkAt(this);
74  }
75 
81  public Block getBlock() {
82  return world.getBlockAt(this);
83  }
84 
90  public void setX(double x) {
91  this.x = x;
92  }
93 
99  public double getX() {
100  return x;
101  }
102 
109  public int getBlockX() {
110  return locToBlock(x);
111  }
112 
118  public void setY(double y) {
119  this.y = y;
120  }
121 
127  public double getY() {
128  return y;
129  }
130 
137  public int getBlockY() {
138  return locToBlock(y);
139  }
140 
146  public void setZ(double z) {
147  this.z = z;
148  }
149 
155  public double getZ() {
156  return z;
157  }
158 
165  public int getBlockZ() {
166  return locToBlock(z);
167  }
168 
174  public void setYaw(float yaw) {
175  this.yaw = yaw;
176  }
177 
183  public float getYaw() {
184  return yaw;
185  }
186 
192  public void setPitch(float pitch) {
193  this.pitch = pitch;
194  }
195 
201  public float getPitch() {
202  return pitch;
203  }
204 
210  public Vector getDirection() {
211  Vector vector = new Vector();
212 
213  double rotX = this.getYaw();
214  double rotY = this.getPitch();
215 
216  vector.setY(-Math.sin(Math.toRadians(rotY)));
217 
218  double h = Math.cos(Math.toRadians(rotY));
219 
220  vector.setX(-h * Math.sin(Math.toRadians(rotX)));
221  vector.setZ(h * Math.cos(Math.toRadians(rotX)));
222 
223  return vector;
224  }
225 
234  public Location add(Location vec) {
235  if (vec == null || vec.getWorld() != getWorld()) {
236  throw new IllegalArgumentException("Cannot add Locations of differing worlds");
237  }
238 
239  x += vec.x;
240  y += vec.y;
241  z += vec.z;
242  return this;
243  }
244 
252  public Location add(Vector vec) {
253  this.x += vec.getX();
254  this.y += vec.getY();
255  this.z += vec.getZ();
256  return this;
257  }
258 
268  public Location add(double x, double y, double z) {
269  this.x += x;
270  this.y += y;
271  this.z += z;
272  return this;
273  }
274 
283  public Location subtract(Location vec) {
284  if (vec == null || vec.getWorld() != getWorld()) {
285  throw new IllegalArgumentException("Cannot add Locations of differing worlds");
286  }
287 
288  x -= vec.x;
289  y -= vec.y;
290  z -= vec.z;
291  return this;
292  }
293 
301  public Location subtract(Vector vec) {
302  this.x -= vec.getX();
303  this.y -= vec.getY();
304  this.z -= vec.getZ();
305  return this;
306  }
307 
318  public Location subtract(double x, double y, double z) {
319  this.x -= x;
320  this.y -= y;
321  this.z -= z;
322  return this;
323  }
324 
336  public double length() {
337  return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2));
338  }
339 
347  public double lengthSquared() {
348  return Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2);
349  }
350 
363  public double distance(Location o) {
364  return Math.sqrt(distanceSquared(o));
365  }
366 
375  public double distanceSquared(Location o) {
376  if (o == null) {
377  throw new IllegalArgumentException("Cannot measure distance to a null location");
378  } else if (o.getWorld() == null || getWorld() == null) {
379  throw new IllegalArgumentException("Cannot measure distance to a null world");
380  } else if (o.getWorld() != getWorld()) {
381  throw new IllegalArgumentException("Cannot measure distance between " + getWorld().getName() + " and " + o.getWorld().getName());
382  }
383 
384  return Math.pow(x - o.x, 2) + Math.pow(y - o.y, 2) + Math.pow(z - o.z, 2);
385  }
386 
395  public Location multiply(double m) {
396  x *= m;
397  y *= m;
398  z *= m;
399  return this;
400  }
401 
408  public Location zero() {
409  x = 0;
410  y = 0;
411  z = 0;
412  return this;
413  }
414 
415  @Override
416  public boolean equals(Object obj) {
417  if (obj == null) {
418  return false;
419  }
420  if (getClass() != obj.getClass()) {
421  return false;
422  }
423  final Location other = (Location) obj;
424 
425  if (this.world != other.world && (this.world == null || !this.world.equals(other.world))) {
426  return false;
427  }
428  if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x)) {
429  return false;
430  }
431  if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y)) {
432  return false;
433  }
434  if (Double.doubleToLongBits(this.z) != Double.doubleToLongBits(other.z)) {
435  return false;
436  }
437  if (Float.floatToIntBits(this.pitch) != Float.floatToIntBits(other.pitch)) {
438  return false;
439  }
440  if (Float.floatToIntBits(this.yaw) != Float.floatToIntBits(other.yaw)) {
441  return false;
442  }
443  return true;
444  }
445 
446  @Override
447  public int hashCode() {
448  int hash = 3;
449 
450  hash = 19 * hash + (this.world != null ? this.world.hashCode() : 0);
451  hash = 19 * hash + (int) (Double.doubleToLongBits(this.x) ^ (Double.doubleToLongBits(this.x) >>> 32));
452  hash = 19 * hash + (int) (Double.doubleToLongBits(this.y) ^ (Double.doubleToLongBits(this.y) >>> 32));
453  hash = 19 * hash + (int) (Double.doubleToLongBits(this.z) ^ (Double.doubleToLongBits(this.z) >>> 32));
454  hash = 19 * hash + Float.floatToIntBits(this.pitch);
455  hash = 19 * hash + Float.floatToIntBits(this.yaw);
456  return hash;
457  }
458 
459  @Override
460  public String toString() {
461  return "Location{" + "world=" + world + ",x=" + x + ",y=" + y + ",z=" + z + ",pitch=" + pitch + ",yaw=" + yaw + '}';
462  }
463 
469  public Vector toVector() {
470  return new Vector(x, y, z);
471  }
472 
473  @Override
474  public Location clone() {
475  try {
476  return (Location) super.clone();
477  } catch (CloneNotSupportedException e) {
478  throw new Error(e);
479  }
480  }
481 
488  public static int locToBlock(double loc) {
489  return NumberConversions.floor(loc);
490  }
491 }