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

170 lines
5.6 KiB
Java
Raw Normal View History

package wtf.beatrice.limbomanager.objects;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.scheduler.BukkitTask;
import wtf.beatrice.limbomanager.Cache;
import wtf.beatrice.limbomanager.LimboManager;
import java.util.ArrayList;
import java.util.List;
public class AreaCleanerRunnable implements Runnable
{
public BukkitTask task;
Location min, max;
List<Location> blocksList = new ArrayList<>();
Location current;
int blocksPerRun = 384;
String playerName, cleanUpID;
public AreaCleanerRunnable(Location min, Location max, String playerName, String cleanUpID)
{
this.playerName = playerName;
this.cleanUpID = cleanUpID;
this.min = min;
this.max = max;
current = new Location(min.getWorld(), min.getBlockX(), min.getBlockY(), min.getBlockZ());
LimboManager.getInstance().getLogger().info(min.getBlockX() + " " + min.getBlockY() + " " + min.getBlockZ());
LimboManager.getInstance().getLogger().info(max.getBlockX() + " " + max.getBlockY() + " " + max.getBlockZ());
}
@Override
public void run()
{
boolean cancelled = false;
for(int i = 0; i < blocksPerRun; i++)
{
if(cancelled) break;
Bukkit.getScheduler().runTask(LimboManager.getInstance(), () ->
{
current.getBlock().setType(Material.AIR);
if(current.getBlockY() < max.getBlockY()) {
current.setY(current.getBlockY() + 1 );
} else {
current.setY(min.getBlockY());
if (current.getBlockX() < max.getBlockX()) {
current.setX(current.getBlockX() + 1);
} else {
current.setX(min.getBlockX());
if (current.getBlockZ() < max.getBlockZ()) {
current.setZ(current.getBlockZ() + 1);
LimboManager.getInstance().getLogger().info("c: " + current.getBlockX() + " " + current.getBlockY() + " " + current.getBlockZ());
} else {
// we finished, free the location and cancel the task.
Cache.playerIslands.remove(cleanUpID);
LimboManager.getInstance().getLogger().info("finished cleaning area for " + playerName);
task.cancel();
return;
}
}
}
});
}
/*
int locationsDone = 0;
for(int x = min.getBlockX(); x < max.getBlockX(); x++) {
for (int y = min.getBlockY(); y < max.getBlockY(); y++) {
for (int z = min.getBlockZ(); z < max.getBlockZ(); z++) {
if(locationsDone == blocksPerRun)
{
}
blocksList.add(new Location(min.getWorld(), x, y, z));
locationsDone++;
}
}
}
if(blocksList.isEmpty())
{
// we finished, free the location and cancel the task.
Cache.playerIslands.remove(playerName); // todo: what if player rejoins in the meanwhile
LimboManager.getInstance().getLogger().info("finished cleaning area for " + playerName);
task.cancel();
}
for (int i = 0; i < blocksPerRun; i++)
{
if(i >= blocksList.size())
{
// we finished, free the location and cancel the task.
Cache.playerIslands.remove(playerName); // todo: what if player rejoins in the meanwhile
LimboManager.getInstance().getLogger().info("finished cleaning area for " + playerName);
task.cancel();
break;
}
Location loc = blocksList.get(i);
Bukkit.getScheduler().runTask(LimboManager.getInstance(), () ->
{
LimboManager.getInstance().getLogger().info("c: " + loc.getBlockX() + " " + loc.getBlockY() + " " + loc.getBlockZ());
loc.getBlock().setType(Material.AIR);
});
}*/
/*for(int i = 0; i < blocksPerRun; i++)
{
// sync block write
Bukkit.getScheduler().runTask(LimboManager.getInstance(), () ->
{
LimboManager.getInstance().getLogger().info("c: " + current.getBlockX() + " " + current.getBlockY() + " " + current.getBlockZ());
current.getBlock().setType(Material.AIR);
// update current coordinates
if(current.getBlockX() < max.getBlockX())
{
current.setX(current.getBlockX() + 1);
}
else {
current.setX(min.getBlockX());
if(current.getBlockY() < max.getBlockY())
{
current.setY(max.getBlockY() + 1);
} else {
current.setY(min.getBlockY());
if(current.getBlockZ() < max.getBlockZ())
{
current.setZ(max.getBlockZ() + 1);
} else {
// we finished, free the location and cancel the task.
Cache.playerIslands.remove(playerName); // todo: what if player rejoins in the meanwhile
LimboManager.getInstance().getLogger().info("finished cleaning area for " + playerName);
task.cancel();
return;
}
}
}
});
}*/
}
}