Skip to content

Commit

Permalink
Added protection against console error spam if island size is zero.
Browse files Browse the repository at this point in the history
If the protection range is 0 then the caluclations to teleport players
back into the border will result in infinite values and other
strangeness so this prevents that. This is an edge case and only really
happens when the island size has been set wrongly.
  • Loading branch information
tastybento committed Jan 3, 2024
1 parent a491faf commit 08df85f
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.event.vehicle.VehicleMoveEvent;
import org.bukkit.util.NumberConversions;
import org.bukkit.util.RayTraceResult;
import org.bukkit.util.Vector;

Expand Down Expand Up @@ -151,10 +152,15 @@ public void onPlayerLeaveIsland(PlayerMoveEvent e) {
addon.getIslands().getIslandAt(p.getLocation()).ifPresent(i -> {
Vector unitVector = i.getProtectionCenter().toVector().subtract(p.getLocation().toVector()).normalize()
.multiply(new Vector(1,0,1));
if (unitVector.lengthSquared() <= 0D) {
// Direction is zero, so nothing to do; cannot move.
return;
}
RayTraceResult r = i.getProtectionBoundingBox().rayTrace(p.getLocation().toVector(), unitVector, i.getRange());
if (r != null) {
if (r != null && checkFinite(r.getHitPosition())) {
inTeleport.add(p.getUniqueId());
Location targetPos = r.getHitPosition().toLocation(p.getWorld(), p.getLocation().getYaw(), p.getLocation().getPitch());

if (!e.getPlayer().isFlying() && addon.getSettings().isReturnTeleportBlock()
&& !addon.getIslands().isSafeLocation(targetPos)) {
switch (targetPos.getWorld().getEnvironment()) {
Expand All @@ -174,6 +180,11 @@ public void onPlayerLeaveIsland(PlayerMoveEvent e) {
});
}

public boolean checkFinite(Vector toCheck) {
return NumberConversions.isFinite(toCheck.getX()) && NumberConversions.isFinite(toCheck.getY())
&& NumberConversions.isFinite(toCheck.getZ());
}

/**
* Check if the player is outside the island protection zone that they are supposed to be in.
* @param player - player moving
Expand Down

0 comments on commit 08df85f

Please sign in to comment.