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.
This commit is contained in:
wizjany 2017-05-24 20:59:00 -04:00
parent a4f1f57c9d
commit b2fb73582f
2 changed files with 16 additions and 2 deletions

View File

@ -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);
}

View File

@ -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));
}
}