LimboManager/src/main/java/wtf/beatrice/limbomanager/objects/LocationCheckRunnable.java

63 lines
1.8 KiB
Java

package wtf.beatrice.limbomanager.objects;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import wtf.beatrice.limbomanager.Cache;
import wtf.beatrice.limbomanager.utils.LocationUtils;
import java.util.ArrayList;
import java.util.List;
public class LocationCheckRunnable implements Runnable
{
public BukkitTask task;
List<Player> playersToCheck = new ArrayList<>();
private final static int distanceMax = 100;
// we give players a distanceMax block range to move in.
// if they go out, we teleport them back to their island's spawnpoint.
@Override
public void run()
{
// if we checked all players, refill the list and skip the check for this time (in case server is empty).
if(playersToCheck.isEmpty()) {
playersToCheck.addAll(Bukkit.getServer().getOnlinePlayers());
return;
}
// load the player
Player player = null;
while(player == null || !player.isOnline()) // check if player left and in case, move to next one
{
if(!playersToCheck.isEmpty()) player = playersToCheck.get(0);
if(player != null) playersToCheck.remove(player);
}
Location playerLocation = player.getLocation();
Coordinates playerIslandCoordinates = Cache.playerIslands.get(player.getName());
int distanceX = Math.abs(playerLocation.getBlockX() - playerIslandCoordinates.getX());
int distanceZ = Math.abs(playerLocation.getBlockZ() - playerIslandCoordinates.getZ());
if(distanceX > distanceMax || distanceZ > distanceMax)
{
LocationUtils.teleportToOwnIsland(player);
player.sendMessage("Out of island limits");
}
}
}
// 1500 1000
// 1000 1000
// 700 1000