From b2fb73582f8a11db03dfbce6da3d4383c72f1894 Mon Sep 17 00:00:00 2001 From: wizjany Date: Wed, 24 May 2017 20:59:00 -0400 Subject: [PATCH] Fixed //center not working in some cases. Due to odd rounding and math issues, using //center on a region with an edge of even length in quadrants with negative coordinates would only fill one of the two center blocks. Thanks to Griffrez and up201406036 for the original PR. Fixes WORDLEDIT-2986. --- .../main/java/com/sk89q/worldedit/EditSession.java | 6 ++++-- .../java/com/sk89q/worldedit/math/MathUtils.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 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 abb4f4909..8698a7674 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java @@ -59,6 +59,7 @@ import com.sk89q.worldedit.history.changeset.ChangeSet; import com.sk89q.worldedit.internal.expression.Expression; import com.sk89q.worldedit.internal.expression.ExpressionException; import com.sk89q.worldedit.internal.expression.runtime.RValue; +import com.sk89q.worldedit.math.MathUtils; import com.sk89q.worldedit.math.interpolation.Interpolation; import com.sk89q.worldedit.math.interpolation.KochanekBartelsInterpolation; import com.sk89q.worldedit.math.interpolation.Node; @@ -963,8 +964,9 @@ public class EditSession implements Extent { Vector center = region.getCenter(); Region centerRegion = new CuboidRegion( getWorld(), // Causes clamping of Y range - new Vector((int) center.getX(), (int) center.getY(), (int) center.getZ()), - center.toBlockVector()); + new Vector(((int) center.getX()), ((int) center.getY()), ((int) center.getZ())), + new Vector(MathUtils.roundHalfUp(center.getX()), + center.getY(), MathUtils.roundHalfUp(center.getZ()))); return setBlocks(centerRegion, pattern); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/math/MathUtils.java b/worldedit-core/src/main/java/com/sk89q/worldedit/math/MathUtils.java index 743b6e5da..bb8541cb2 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/math/MathUtils.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/math/MathUtils.java @@ -105,4 +105,16 @@ public final class MathUtils { return Math.sin(Math.toRadians(degrees)); } + /** + * Returns the rounded double of the given value, rounding to the + * nearest integer value away from zero on ties. + * + * This behavior is the same as {@link java.math.RoundingMode#HALF_UP}. + * + * @param value the value + * @return the rounded value + */ + public static double roundHalfUp(double value) { + return Math.signum(value) * Math.round(Math.abs(value)); + } }