72 lines
2.5 KiB
Java
72 lines
2.5 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<>();
|
|
|
|
|
|
// 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);
|
|
}
|
|
|
|
if(!Cache.playerIslands.containsKey(player.getName()))
|
|
return; //it means the player just joined and still didn't get an island (reminder that this is async)
|
|
|
|
Location playerLocation = player.getLocation();
|
|
|
|
int spawnOffsetX = Cache.getConfiguration().getInt("island.spawn-offset.x");
|
|
int spawnOffsetZ = Cache.getConfiguration().getInt("island.spawn-offset.z");
|
|
|
|
|
|
// we are copying the object because otherwise we'd be directly editing the reference in the hashmap
|
|
// and causing a lot of trouble by moving the spawnpoint itself.
|
|
Coordinates islandSpawnCoordinates = new Coordinates(Cache.playerIslands.get(player.getName()));
|
|
|
|
islandSpawnCoordinates.setX(islandSpawnCoordinates.getX() + spawnOffsetX);
|
|
islandSpawnCoordinates.setZ(islandSpawnCoordinates.getZ() + spawnOffsetZ);
|
|
|
|
|
|
// calculate absolute distance from spawnpoint
|
|
int distanceX = Math.abs(playerLocation.getBlockX() - islandSpawnCoordinates.getX());
|
|
int distanceZ = Math.abs(playerLocation.getBlockZ() - islandSpawnCoordinates.getZ());
|
|
|
|
// check if player is inside bounds
|
|
if(distanceX > Cache.islandWalkingRange || distanceZ > Cache.islandWalkingRange)
|
|
{
|
|
LocationUtils.teleportToOwnIsland(player);
|
|
player.sendMessage("Out of bounds");
|
|
|
|
|
|
}
|
|
}
|
|
}
|