diff --git a/paper-api/src/main/java/org/bukkit/Location.java b/paper-api/src/main/java/org/bukkit/Location.java index fe7c4b0913..bcc963bf7f 100644 --- a/paper-api/src/main/java/org/bukkit/Location.java +++ b/paper-api/src/main/java/org/bukkit/Location.java @@ -1,6 +1,7 @@ package org.bukkit; import org.bukkit.block.Block; +import org.bukkit.util.NumberConversions; import org.bukkit.util.Vector; /** @@ -485,6 +486,6 @@ public class Location implements Cloneable { * @return Block coordinate */ public static int locToBlock(double loc) { - return (int) Math.floor(loc); + return NumberConversions.floor(loc); } } diff --git a/paper-api/src/main/java/org/bukkit/util/BlockIterator.java b/paper-api/src/main/java/org/bukkit/util/BlockIterator.java index dbb5e39a9f..6783609852 100644 --- a/paper-api/src/main/java/org/bukkit/util/BlockIterator.java +++ b/paper-api/src/main/java/org/bukkit/util/BlockIterator.java @@ -1,5 +1,7 @@ package org.bukkit.util; +import static org.bukkit.util.NumberConversions.*; + import org.bukkit.World; import org.bukkit.Location; import org.bukkit.block.Block; @@ -66,7 +68,7 @@ public class BlockIterator implements Iterator { double secondPosition = 0; double thirdPosition = 0; - Block startBlock = this.world.getBlockAt((int) Math.floor(startClone.getX()), (int) Math.floor(startClone.getY()), (int) Math.floor(startClone.getZ())); + Block startBlock = this.world.getBlockAt(floor(startClone.getX()), floor(startClone.getY()), floor(startClone.getZ())); if (getXLength(direction) > mainDirection) { mainFace = getXFace(direction); @@ -117,10 +119,10 @@ public class BlockIterator implements Iterator { // Guarantee that the ray will pass though the start block. // It is possible that it would miss due to rounding // This should only move the ray by 1 grid position - secondError = (int) (Math.floor(secondd * gridSize)); - secondStep = (int) (Math.round(secondDirection / mainDirection * gridSize)); - thirdError = (int) (Math.floor(thirdd * gridSize)); - thirdStep = (int) (Math.round(thirdDirection / mainDirection * gridSize)); + secondError = floor(secondd * gridSize); + secondStep = round(secondDirection / mainDirection * gridSize); + thirdError = floor(thirdd * gridSize); + thirdStep = round(thirdDirection / mainDirection * gridSize); if (secondError + secondStep <= 0) { secondError = -secondStep + 1; @@ -168,7 +170,7 @@ public class BlockIterator implements Iterator { } // Calculate the number of planes passed to give max distance - maxDistanceInt = (int) Math.round(maxDistance / (Math.sqrt(mainDirection * mainDirection + secondDirection * secondDirection + thirdDirection * thirdDirection) / mainDirection)); + maxDistanceInt = round(maxDistance / (Math.sqrt(mainDirection * mainDirection + secondDirection * secondDirection + thirdDirection * thirdDirection) / mainDirection)); } diff --git a/paper-api/src/main/java/org/bukkit/util/NumberConversions.java b/paper-api/src/main/java/org/bukkit/util/NumberConversions.java index 9c0b132f56..e482ceeb61 100644 --- a/paper-api/src/main/java/org/bukkit/util/NumberConversions.java +++ b/paper-api/src/main/java/org/bukkit/util/NumberConversions.java @@ -6,6 +6,15 @@ package org.bukkit.util; public final class NumberConversions { private NumberConversions() {} + public static int floor(double num) { + final int floor = (int) num; + return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63); + } + + public static int round(double num) { + return floor(num + 0.5d); + } + public static int toInt(Object object) { if (object instanceof Number) { return ((Number) object).intValue(); diff --git a/paper-api/src/main/java/org/bukkit/util/Vector.java b/paper-api/src/main/java/org/bukkit/util/Vector.java index 9b1376bfc6..af4d5770fa 100644 --- a/paper-api/src/main/java/org/bukkit/util/Vector.java +++ b/paper-api/src/main/java/org/bukkit/util/Vector.java @@ -365,7 +365,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return block X */ public int getBlockX() { - return (int) Math.floor(x); + return NumberConversions.floor(x); } /** @@ -384,7 +384,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return block y */ public int getBlockY() { - return (int) Math.floor(y); + return NumberConversions.floor(y); } /** @@ -403,7 +403,7 @@ public class Vector implements Cloneable, ConfigurationSerializable { * @return block z */ public int getBlockZ() { - return (int) Math.floor(z); + return NumberConversions.floor(z); } /**