diff options
author | sk89q <the.sk89q@gmail.com> | 2011-02-05 23:20:06 -0800 |
---|---|---|
committer | sk89q <the.sk89q@gmail.com> | 2011-02-05 23:20:06 -0800 |
commit | 1d8cd948d20e4180aa2fe337b9a5ded374af2bad (patch) | |
tree | 48d7b5a2c08277ba152bb60d9248b6440a7cb35b | |
parent | 481aec9ee6903257bedc46d58c4be1690a21ef29 (diff) | |
download | bukkit-1d8cd948d20e4180aa2fe337b9a5ded374af2bad.tar bukkit-1d8cd948d20e4180aa2fe337b9a5ded374af2bad.tar.gz bukkit-1d8cd948d20e4180aa2fe337b9a5ded374af2bad.tar.lz bukkit-1d8cd948d20e4180aa2fe337b9a5ded374af2bad.tar.xz bukkit-1d8cd948d20e4180aa2fe337b9a5ded374af2bad.zip |
Added BlockVector a la WorldEdit as requested by #246. This BlockVector is safe to be used as keys in hash sets and hash maps, but it is mutable and careful attention must be paid to not modify the vector post-insertion into a set or map.
-rw-r--r-- | src/main/java/org/bukkit/util/BlockVector.java | 107 | ||||
-rw-r--r-- | src/main/java/org/bukkit/util/Vector.java | 9 |
2 files changed, 116 insertions, 0 deletions
diff --git a/src/main/java/org/bukkit/util/BlockVector.java b/src/main/java/org/bukkit/util/BlockVector.java new file mode 100644 index 00000000..ded28503 --- /dev/null +++ b/src/main/java/org/bukkit/util/BlockVector.java @@ -0,0 +1,107 @@ +package org.bukkit.util;
+
+/**
+ * A vector with a hash function that floors the X, Y, Z components, a la
+ * BlockVector in WorldEdit. BlockVectors can be used in hash sets and
+ * hash maps. Be aware that BlockVectors are mutable, but it is important
+ * that BlockVectors are never changed once put into a hash set or hash map.
+ *
+ * @author sk89q
+ */
+public class BlockVector extends Vector {
+ /**
+ * Construct the vector with all components as 0.
+ */
+ public BlockVector() {
+ this.x = 0;
+ this.y = 0;
+ this.z = 0;
+ }
+
+ /**
+ * Construct the vector with another vector.
+ */
+ public BlockVector(Vector vec) {
+ this.x = vec.getX();
+ this.y = vec.getY();
+ this.z = vec.getZ();
+ }
+
+ /**
+ * Construct the vector with provided integer components.
+ *
+ * @param x
+ * @param y
+ * @param z
+ */
+ public BlockVector(int x, int y, int z) {
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ }
+
+ /**
+ * Construct the vector with provided double components.
+ *
+ * @param x
+ * @param y
+ * @param z
+ */
+ public BlockVector(double x, double y, double z) {
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ }
+
+ /**
+ * Construct the vector with provided float components.
+ *
+ * @param x
+ * @param y
+ * @param z
+ */
+ public BlockVector(float x, float y, float z) {
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ }
+
+ /**
+ * Checks if another object is equivalent.
+ *
+ * @param obj
+ * @return whether the other object is equivalent
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof BlockVector)) {
+ return false;
+ }
+ BlockVector other = (BlockVector)obj;
+ return (int)other.getX() == (int)this.x && (int)other.getY() == (int)this.y
+ && (int)other.getZ() == (int)this.z;
+
+ }
+
+ /**
+ * Returns a hash code for this vector.
+ *
+ * @return hash code
+ */
+ @Override
+ public int hashCode() {
+ return (Integer.valueOf((int)x).hashCode() >> 13) ^
+ (Integer.valueOf((int)y).hashCode() >> 7) ^
+ Integer.valueOf((int)z).hashCode();
+ }
+
+ /**
+ * Get a new block vector.
+ *
+ * @return vector
+ */
+ @Override
+ public BlockVector clone() {
+ return new BlockVector(x, y, z);
+ }
+}
diff --git a/src/main/java/org/bukkit/util/Vector.java b/src/main/java/org/bukkit/util/Vector.java index fae10eff..b3cf7ab3 100644 --- a/src/main/java/org/bukkit/util/Vector.java +++ b/src/main/java/org/bukkit/util/Vector.java @@ -563,6 +563,15 @@ public class Vector implements Cloneable { public Location toLocation(World world, float yaw, float pitch) { return new Location(world, x, y, z, yaw, pitch); } + + /** + * Get the block vector of this vector. + * + * @return + */ + public BlockVector toBlockVector() { + return new BlockVector(x, y, z); + } /** * Get the threshold used for equals(). |