-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pablo Herrera <[email protected]>
- Loading branch information
1 parent
4366da2
commit 9f20fec
Showing
5 changed files
with
54 additions
and
27 deletions.
There are no files selected for viewing
65 changes: 38 additions & 27 deletions
65
util/src/main/java/tc/oc/pgm/util/bukkit/WorldBorders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,60 @@ | ||
package tc.oc.pgm.util.bukkit; | ||
|
||
import org.bukkit.Location; | ||
import org.bukkit.World; | ||
import org.bukkit.WorldBorder; | ||
import org.bukkit.util.NumberConversions; | ||
import tc.oc.pgm.util.nms.NMSHacks; | ||
|
||
public interface WorldBorders { | ||
public class WorldBorders { | ||
|
||
static boolean isInsideBorder(Location location) { | ||
WorldBorder border = location.getWorld().getWorldBorder(); | ||
Location center = border.getCenter(); | ||
double radius = border.getSize() / 2d; | ||
return Math.abs(location.getX() - center.getX()) < radius | ||
&& Math.abs(location.getZ() - center.getZ()) < radius; | ||
private WorldBorders() {} | ||
|
||
public static boolean isInsideBorder(Location location) { | ||
BorderRect rect = new BorderRect(location.getWorld()); | ||
return location.getBlockX() >= rect.xMin | ||
&& location.getBlockX() <= rect.xMax | ||
&& location.getBlockZ() >= rect.zMin | ||
&& location.getBlockZ() <= rect.zMax; | ||
} | ||
|
||
static boolean clampToBorder(Location location) { | ||
WorldBorder border = location.getWorld().getWorldBorder(); | ||
Location center = border.getCenter(); | ||
double radius = border.getSize() / 2d; | ||
double xMin = center.getX() - radius; | ||
double xMax = center.getX() + radius; | ||
double zMin = center.getZ() - radius; | ||
double zMax = center.getZ() + radius; | ||
public static boolean clampToBorder(Location location) { | ||
BorderRect rect = new BorderRect(location.getWorld()); | ||
|
||
boolean moved = false; | ||
|
||
if (location.getX() < xMin) { | ||
location.setX(xMin); | ||
if (location.getX() <= rect.xMin) { | ||
location.setX(rect.xMin + 0.5); | ||
moved = true; | ||
} | ||
|
||
if (location.getX() > xMax) { | ||
location.setX(xMax); | ||
} else if (location.getX() >= rect.xMax) { | ||
location.setX(rect.xMax - 0.5); | ||
moved = true; | ||
} | ||
|
||
if (location.getZ() < zMin) { | ||
location.setZ(zMin); | ||
if (location.getZ() <= rect.zMin) { | ||
location.setZ(rect.zMin + 0.5); | ||
moved = true; | ||
} | ||
|
||
if (location.getZ() > zMax) { | ||
location.setZ(zMax); | ||
} else if (location.getZ() >= rect.zMax) { | ||
location.setZ(rect.zMax - 0.5); | ||
moved = true; | ||
} | ||
|
||
return moved; | ||
} | ||
|
||
private static class BorderRect { | ||
public final int xMin, xMax, zMin, zMax; | ||
|
||
public BorderRect(World world) { | ||
WorldBorder border = world.getWorldBorder(); | ||
Location center = border.getCenter(); | ||
double radius = border.getSize() / 2d; | ||
int maxWorldSize = NMSHacks.getMaxWorldSize(world); | ||
|
||
xMin = Math.max(NumberConversions.floor(center.getX() - radius), -maxWorldSize); | ||
xMax = Math.min(NumberConversions.ceil(center.getX() + radius), maxWorldSize); | ||
zMin = Math.max(NumberConversions.floor(center.getZ() - radius), -maxWorldSize); | ||
zMax = Math.min(NumberConversions.ceil(center.getZ() + radius), maxWorldSize); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters