2015-11-15 23:32:04 +00:00
|
|
|
package me.totalfreedom.totalfreedommod;
|
|
|
|
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.player.PlayerLoginEvent;
|
2017-10-21 17:28:20 +00:00
|
|
|
import org.bukkit.event.player.PlayerMoveEvent;
|
2015-11-15 23:32:04 +00:00
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
|
|
|
|
2016-03-01 16:47:01 +00:00
|
|
|
public class MovementValidator extends FreedomService
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
|
2017-10-21 17:28:20 +00:00
|
|
|
public static final int MAX_XYZ_COORD = 29999998;
|
2015-11-15 23:32:04 +00:00
|
|
|
|
|
|
|
public MovementValidator(TotalFreedomMod plugin)
|
|
|
|
{
|
|
|
|
super(plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStart()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStop()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.HIGH)
|
|
|
|
public void onPlayerTeleport(PlayerTeleportEvent event)
|
|
|
|
{
|
|
|
|
// Check absolute value to account for negatives
|
2017-10-21 17:28:20 +00:00
|
|
|
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)
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
event.setCancelled(true); // illegal position, cancel it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-21 17:28:20 +00:00
|
|
|
@EventHandler(priority = EventPriority.HIGH)
|
|
|
|
public void onPlayerMove(PlayerMoveEvent event)
|
|
|
|
{
|
|
|
|
final Player player = event.getPlayer();
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
event.setCancelled(true);
|
|
|
|
player.teleport(player.getWorld().getSpawnLocation());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
@EventHandler(priority = EventPriority.HIGH)
|
|
|
|
public void onPlayerLogin(PlayerLoginEvent event)
|
|
|
|
{
|
|
|
|
final Player player = event.getPlayer();
|
|
|
|
|
|
|
|
// Validate position
|
2017-10-21 17:28:20 +00:00
|
|
|
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)
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
player.teleport(player.getWorld().getSpawnLocation()); // Illegal position, teleport to spawn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|