From 4de5487c510971d9d9a2740a354a58b23d24b69a Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Thu, 4 Apr 2019 23:36:19 +1100 Subject: [PATCH] Change Vector hash codes (#456) And add additional unit vectors where needed. --- .../java/com/sk89q/worldedit/EditSession.java | 2 +- .../worldedit/command/SelectionCommands.java | 12 +++++------ .../function/visitor/BreadthFirstSearch.java | 21 ++++++++++--------- .../function/visitor/DownwardVisitor.java | 10 ++++----- .../function/visitor/NonRisingVisitor.java | 10 ++++----- .../sk89q/worldedit/math/BlockVector2.java | 5 +---- .../sk89q/worldedit/math/BlockVector3.java | 9 ++++---- .../worldedit/math/interpolation/Node.java | 2 +- .../com/sk89q/worldedit/util/Direction.java | 4 +++- 9 files changed, 37 insertions(+), 38 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java index 07d3d084c..bf26a0a1f 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -1182,7 +1182,7 @@ public class EditSession implements Extent, AutoCloseable { checkNotNull(pattern); BlockReplace replace = new BlockReplace(this, pattern); - RegionOffset offset = new RegionOffset(BlockVector3.at(0, 1, 0), replace); + RegionOffset offset = new RegionOffset(BlockVector3.UNIT_Y, replace); GroundFunction ground = new GroundFunction(new ExistingBlockMask(this), offset); LayerVisitor visitor = new LayerVisitor(asFlatRegion(region), minimumBlockY(region), maximumBlockY(region), ground); Operations.completeLegacy(visitor); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java index 9ded39f33..24e92070b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SelectionCommands.java @@ -568,15 +568,15 @@ public class SelectionCommands { int change = args.getInteger(0); if (!args.hasFlag('h')) { - changes.add((BlockVector3.at(0, 1, 0)).multiply(change)); - changes.add((BlockVector3.at(0, -1, 0)).multiply(change)); + changes.add((BlockVector3.UNIT_Y).multiply(change)); + changes.add((BlockVector3.UNIT_MINUS_Y).multiply(change)); } if (!args.hasFlag('v')) { - changes.add((BlockVector3.at(1, 0, 0)).multiply(change)); - changes.add((BlockVector3.at(-1, 0, 0)).multiply(change)); - changes.add((BlockVector3.at(0, 0, 1)).multiply(change)); - changes.add((BlockVector3.at(0, 0, -1)).multiply(change)); + changes.add((BlockVector3.UNIT_X).multiply(change)); + changes.add((BlockVector3.UNIT_MINUS_X).multiply(change)); + changes.add((BlockVector3.UNIT_Z).multiply(change)); + changes.add((BlockVector3.UNIT_MINUS_Z).multiply(change)); } return changes.toArray(new BlockVector3[0]); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/BreadthFirstSearch.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/BreadthFirstSearch.java index f7260491e..26b14f38c 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/BreadthFirstSearch.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/BreadthFirstSearch.java @@ -26,6 +26,7 @@ import com.sk89q.worldedit.function.RegionFunction; import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.operation.RunContext; import com.sk89q.worldedit.math.BlockVector3; +import com.sk89q.worldedit.util.Direction; import java.util.ArrayDeque; import java.util.ArrayList; @@ -85,22 +86,22 @@ public abstract class BreadthFirstSearch implements Operation { * Add the directions along the axes as directions to visit. */ protected void addAxes() { - directions.add(BlockVector3.at(0, -1, 0)); - directions.add(BlockVector3.at(0, 1, 0)); - directions.add(BlockVector3.at(-1, 0, 0)); - directions.add(BlockVector3.at(1, 0, 0)); - directions.add(BlockVector3.at(0, 0, -1)); - directions.add(BlockVector3.at(0, 0, 1)); + directions.add(BlockVector3.UNIT_MINUS_Y); + directions.add(BlockVector3.UNIT_Y); + directions.add(BlockVector3.UNIT_MINUS_X); + directions.add(BlockVector3.UNIT_X); + directions.add(BlockVector3.UNIT_MINUS_Z); + directions.add(BlockVector3.UNIT_Z); } /** * Add the diagonal directions as directions to visit. */ protected void addDiagonal() { - directions.add(BlockVector3.at(1, 0, 1)); - directions.add(BlockVector3.at(-1, 0, -1)); - directions.add(BlockVector3.at(1, 0, -1)); - directions.add(BlockVector3.at(-1, 0, 1)); + directions.add(Direction.NORTHEAST.toBlockVector()); + directions.add(Direction.SOUTHEAST.toBlockVector()); + directions.add(Direction.SOUTHWEST.toBlockVector()); + directions.add(Direction.NORTHWEST.toBlockVector()); } /** diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/DownwardVisitor.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/DownwardVisitor.java index 6c1740ce5..518eab096 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/DownwardVisitor.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/DownwardVisitor.java @@ -53,11 +53,11 @@ public class DownwardVisitor extends RecursiveVisitor { Collection directions = getDirections(); directions.clear(); - directions.add(BlockVector3.at(1, 0, 0)); - directions.add(BlockVector3.at(-1, 0, 0)); - directions.add(BlockVector3.at(0, 0, 1)); - directions.add(BlockVector3.at(0, 0, -1)); - directions.add(BlockVector3.at(0, -1, 0)); + directions.add(BlockVector3.UNIT_X); + directions.add(BlockVector3.UNIT_MINUS_X); + directions.add(BlockVector3.UNIT_Z); + directions.add(BlockVector3.UNIT_MINUS_Z); + directions.add(BlockVector3.UNIT_MINUS_Y); } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/NonRisingVisitor.java b/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/NonRisingVisitor.java index fd25108c9..752e00a1d 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/NonRisingVisitor.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/function/visitor/NonRisingVisitor.java @@ -40,11 +40,11 @@ public class NonRisingVisitor extends RecursiveVisitor { super(mask, function); Collection directions = getDirections(); directions.clear(); - directions.add(BlockVector3.at(1, 0, 0)); - directions.add(BlockVector3.at(-1, 0, 0)); - directions.add(BlockVector3.at(0, 0, 1)); - directions.add(BlockVector3.at(0, 0, -1)); - directions.add(BlockVector3.at(0, -1, 0)); + directions.add(BlockVector3.UNIT_X); + directions.add(BlockVector3.UNIT_MINUS_X); + directions.add(BlockVector3.UNIT_Z); + directions.add(BlockVector3.UNIT_MINUS_Z); + directions.add(BlockVector3.UNIT_MINUS_Y); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector2.java b/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector2.java index 5924899e5..92548fc99 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector2.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector2.java @@ -525,10 +525,7 @@ public final class BlockVector2 { @Override public int hashCode() { - int hash = 17; - hash = 31 * hash + Integer.hashCode(x); - hash = 31 * hash + Integer.hashCode(z); - return hash; + return (x << 16) ^ z; } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java b/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java index db545c716..ffb6f6347 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/math/BlockVector3.java @@ -35,6 +35,9 @@ public final class BlockVector3 { public static final BlockVector3 UNIT_X = new BlockVector3(1, 0, 0); public static final BlockVector3 UNIT_Y = new BlockVector3(0, 1, 0); public static final BlockVector3 UNIT_Z = new BlockVector3(0, 0, 1); + public static final BlockVector3 UNIT_MINUS_X = new BlockVector3(-1, 0, 0); + public static final BlockVector3 UNIT_MINUS_Y = new BlockVector3(0, -1, 0); + public static final BlockVector3 UNIT_MINUS_Z = new BlockVector3(0, 0, -1); public static final BlockVector3 ONE = new BlockVector3(1, 1, 1); public static BlockVector3 at(double x, double y, double z) { @@ -609,11 +612,7 @@ public final class BlockVector3 { @Override public int hashCode() { - int hash = 17; - hash = 31 * hash + Integer.hashCode(x); - hash = 31 * hash + Integer.hashCode(y); - hash = 31 * hash + Integer.hashCode(z); - return hash; + return (x ^ (z << 12)) ^ (y << 24); } @Override diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/math/interpolation/Node.java b/worldedit-core/src/main/java/com/sk89q/worldedit/math/interpolation/Node.java index c29190b77..3a020653e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/math/interpolation/Node.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/math/interpolation/Node.java @@ -38,7 +38,7 @@ public class Node { private double continuity; public Node() { - this(Vector3.at(0, 0, 0)); + this(Vector3.ZERO); } public Node(Node other) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/Direction.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/Direction.java index dea0528d0..d40df5935 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/Direction.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/Direction.java @@ -58,8 +58,10 @@ public enum Direction { private final Vector3 direction; private final int flags; + private final BlockVector3 blockPoint; Direction(Vector3 vector, int flags) { + this.blockPoint = vector.toBlockPoint(); this.direction = vector.normalize(); this.flags = flags; } @@ -121,7 +123,7 @@ public enum Direction { * @return the vector */ public BlockVector3 toBlockVector() { - return direction.toBlockPoint(); + return blockPoint; } /**