Skip to content

Commit

Permalink
Improve world border logic
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 committed May 4, 2024
1 parent 4366da2 commit 9f20fec
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 27 deletions.
65 changes: 38 additions & 27 deletions util/src/main/java/tc/oc/pgm/util/bukkit/WorldBorders.java
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);
}
}
}
4 changes: 4 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,8 @@ static AttributeMap buildAttributeMap(Player player) {
static void postToMainThread(Plugin plugin, boolean priority, Runnable task) {
INSTANCE.postToMainThread(plugin, priority, task);
}

static int getMaxWorldSize(World world) {
return INSTANCE.getMaxWorldSize(world);
}
}
5 changes: 5 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/nms/NMSHacksNoOp.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,9 @@ public void postToMainThread(Plugin plugin, boolean priority, Runnable task) {
// runs the task on the next tick, not a perfect replacement
plugin.getServer().getScheduler().runTask(plugin, task);
}

@Override
public int getMaxWorldSize(World world) {
return 29999984; // Vanilla's default
}
}
2 changes: 2 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/nms/NMSHacksPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,6 @@ Object teamPacket(
AttributeMap buildAttributeMap(Player player);

void postToMainThread(Plugin plugin, boolean priority, Runnable task);

int getMaxWorldSize(World world);
}
5 changes: 5 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/nms/v1_8/NMSHacks1_8.java
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,11 @@ public double getTPS() {
return 20.0;
}

@Override
public int getMaxWorldSize(World world) {
return ((CraftWorld) world).getHandle().getWorldBorder().l();
}

enum TeamPacketFields {
a,
b,
Expand Down

0 comments on commit 9f20fec

Please sign in to comment.