Add dedicated method for checking if a position is out of bounds

This commit is contained in:
Allink 2023-06-17 20:14:02 +01:00
parent 42b68011ea
commit cca95dc3f1
No known key found for this signature in database

View File

@ -2,7 +2,6 @@ package me.totalfreedom.totalfreedommod;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import java.util.Objects;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -32,12 +31,17 @@ public class MovementValidator extends FreedomService
public void onPlayerTeleport(PlayerTeleportEvent event) public void onPlayerTeleport(PlayerTeleportEvent event)
{ {
// Check absolute value to account for negatives // Check absolute value to account for negatives
if (Math.abs(Objects.requireNonNull(event.getTo()).getX()) >= MAX_XYZ_COORD || Math.abs(event.getTo().getZ()) >= MAX_XYZ_COORD || Math.abs(event.getTo().getY()) >= MAX_XYZ_COORD) if (isOutOfBounds(event.getTo()))
{ {
event.setCancelled(true); // illegal position, cancel it event.setCancelled(true); // illegal position, cancel it
} }
} }
private boolean isOutOfBounds(final Location position)
{
return Math.abs(position.getX()) >= MAX_XYZ_COORD || Math.abs(position.getY()) >= MAX_XYZ_COORD || Math.abs(position.getZ()) >= MAX_XYZ_COORD;
}
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH)
public void onPlayerMove(PlayerMoveEvent event) public void onPlayerMove(PlayerMoveEvent event)
{ {
@ -52,7 +56,7 @@ public class MovementValidator extends FreedomService
player.kickPlayer(ChatColor.RED + "You were moving too quickly!"); player.kickPlayer(ChatColor.RED + "You were moving too quickly!");
} }
// Check absolute value to account for negatives // Check absolute value to account for negatives
if (Math.abs(event.getTo().getX()) >= MAX_XYZ_COORD || Math.abs(event.getTo().getZ()) >= MAX_XYZ_COORD || Math.abs(event.getTo().getY()) >= MAX_XYZ_COORD) if (isOutOfBounds(event.getTo()))
{ {
event.setCancelled(true); event.setCancelled(true);
PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation()); PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation());
@ -65,7 +69,7 @@ public class MovementValidator extends FreedomService
final Player player = event.getPlayer(); final Player player = event.getPlayer();
// Validate position // Validate position
if (Math.abs(player.getLocation().getX()) >= MAX_XYZ_COORD || Math.abs(player.getLocation().getZ()) >= MAX_XYZ_COORD || Math.abs(player.getLocation().getY()) >= MAX_XYZ_COORD) if (isOutOfBounds(player.getLocation()))
{ {
PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation()); // Illegal position, teleport to spawn PaperLib.teleportAsync(player, player.getWorld().getSpawnLocation()); // Illegal position, teleport to spawn
} }