Add island bounds async checker
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-11-11 23:13:04 +01:00
parent ea09d351bf
commit b84e96f031
4 changed files with 87 additions and 4 deletions

View File

@ -1,6 +1,10 @@
package wtf.beatrice.limbomanager;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import wtf.beatrice.limbomanager.objects.Coordinates;
import wtf.beatrice.limbomanager.objects.LocationCheckRunnable;
import java.util.HashMap;
@ -9,5 +13,18 @@ public class Cache
public static final HashMap<String, Coordinates> playerIslands = new HashMap<>();
public static LocationCheckRunnable locationCheckRunnable;
public static void teleportToOwnIsland(Player player)
{
Coordinates islandCoords = playerIslands.get(player.getName());
Location targetLocation = new Location(player.getWorld(), islandCoords.getX(), 100, islandCoords.getZ());
Bukkit.getScheduler().runTask(LimboManager.getInstance(), () -> {
targetLocation.getWorld().loadChunk(targetLocation.getChunk().getX(), targetLocation.getChunk().getZ(), true);
player.teleport(targetLocation);
});
}
}

View File

@ -7,6 +7,7 @@ import wtf.beatrice.limbomanager.listeners.CommandCanceller;
import wtf.beatrice.limbomanager.listeners.PlayerHider;
import wtf.beatrice.limbomanager.listeners.PlayerTeleporter;
import wtf.beatrice.limbomanager.listeners.RiskyBlocksHandler;
import wtf.beatrice.limbomanager.objects.LocationCheckRunnable;
import java.io.File;
@ -32,6 +33,10 @@ public class LimboManager extends JavaPlugin {
schematicsFolderPath = getDataFolder().getAbsolutePath() + File.separator + "schematics";
getSchematicsFolder().mkdirs();
// start location check runnable
Cache.locationCheckRunnable = new LocationCheckRunnable();
Cache.locationCheckRunnable.task = Bukkit.getScheduler().runTaskTimerAsynchronously(this, Cache.locationCheckRunnable, 10L, 5L);
}
@ -39,6 +44,12 @@ public class LimboManager extends JavaPlugin {
public void onDisable()
{
// cancel running tasks
if(Cache.locationCheckRunnable != null && Cache.locationCheckRunnable.task != null)
{
Bukkit.getScheduler().cancelTask(Cache.locationCheckRunnable.task.getTaskId());
}
}
public static LimboManager getInstance()

View File

@ -22,10 +22,7 @@ public class PlayerHider implements Listener
LimboManager plugin = LimboManager.getInstance();
Player joiner = event.getPlayer();
// hide player from everyone
// hide player from everyone and hide everyone from player
for(Player otherPlayer : Bukkit.getServer().getOnlinePlayers())
{
otherPlayer.hidePlayer(plugin, joiner);

View File

@ -0,0 +1,58 @@
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 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;
}
Player player = playersToCheck.get(0);
Location playerLocation = player.getLocation();
playersToCheck.remove(player);
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)
{
Cache.teleportToOwnIsland(player);
player.sendMessage("Out of island limits");
}
}
}
// 1500 1000
// 1000 1000
// 700 1000