- Saving the LazyPregen Progress in world folder

- Added option to pause to LazyPregen
- Added option to stop to LazyPregen
- Re-Added the functionality continue pregen on startup
This commit is contained in:
RePixelatedMC
2023-12-07 17:57:45 +01:00
parent 7d2062f298
commit 9745f23fb2
2 changed files with 46 additions and 18 deletions
@@ -29,6 +29,7 @@ import com.volmit.iris.util.decree.annotations.Decree;
import com.volmit.iris.util.decree.annotations.Param; import com.volmit.iris.util.decree.annotations.Param;
import com.volmit.iris.util.format.C; import com.volmit.iris.util.format.C;
import com.volmit.iris.util.math.Position2; import com.volmit.iris.util.math.Position2;
import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@@ -36,6 +37,7 @@ import java.io.File;
@Decree(name = "lazypregen", aliases = "lazy", description = "Pregenerate your Iris worlds!") @Decree(name = "lazypregen", aliases = "lazy", description = "Pregenerate your Iris worlds!")
public class CommandLazyPregen implements DecreeExecutor { public class CommandLazyPregen implements DecreeExecutor {
public String worldName;
@Decree(description = "Pregenerate a world") @Decree(description = "Pregenerate a world")
public void start( public void start(
@Param(description = "The radius of the pregen in blocks", aliases = "size") @Param(description = "The radius of the pregen in blocks", aliases = "size")
@@ -46,10 +48,11 @@ public class CommandLazyPregen implements DecreeExecutor {
Vector center, Vector center,
@Param(aliases = "maxcpm", description = "Limit the chunks per minute the pregen will generate", defaultValue = "999999999") @Param(aliases = "maxcpm", description = "Limit the chunks per minute the pregen will generate", defaultValue = "999999999")
int cpm, int cpm,
@Param(aliases = "maxcpm", description = "Limit the chunks per minute the pregen will generate", defaultValue = "false") @Param(aliases = "silent", description = "Silent generation", defaultValue = "false")
boolean dummySilent boolean silent
) { ) {
String worldName = world.getName();
worldName = world.getName();
try { try {
if (sender().isPlayer() && access() == null) { if (sender().isPlayer() && access() == null) {
sender().sendMessage(C.RED + "The engine access for this world is null!"); sender().sendMessage(C.RED + "The engine access for this world is null!");
@@ -63,10 +66,12 @@ public class CommandLazyPregen implements DecreeExecutor {
.chunksPerMinute(cpm) .chunksPerMinute(cpm)
.radiusBlocks(radius) .radiusBlocks(radius)
.position(0) .position(0)
.silent(dummySilent) .silent(silent)
.build(); .build();
LazyPregenerator pregenerator = new LazyPregenerator(pregenJob, new File("plugins/Iris/lazygen.json")); File worldDirectory = new File(Bukkit.getWorldContainer(), worldName);
File lazyGenFile = new File(worldDirectory, "lazygen.json");
LazyPregenerator pregenerator = new LazyPregenerator(pregenJob, lazyGenFile);
pregenerator.start(); pregenerator.start();
String msg = C.GREEN + "LazyPregen started in " + C.GOLD + worldName + C.GREEN + " of " + C.GOLD + (radius * 2) + C.GREEN + " by " + C.GOLD + (radius * 2) + C.GREEN + " blocks from " + C.GOLD + center.getX() + "," + center.getZ(); String msg = C.GREEN + "LazyPregen started in " + C.GOLD + worldName + C.GREEN + " of " + C.GOLD + (radius * 2) + C.GREEN + " by " + C.GOLD + (radius * 2) + C.GREEN + " blocks from " + C.GOLD + center.getX() + "," + center.getZ();
@@ -81,8 +86,9 @@ public class CommandLazyPregen implements DecreeExecutor {
@Decree(description = "Stop the active pregeneration task", aliases = "x") @Decree(description = "Stop the active pregeneration task", aliases = "x")
public void stop() { public void stop() {
if (PregeneratorJob.shutdownInstance()) { if (LazyPregenerator.getInstance() != null) {
Iris.info( C.BLUE + "Finishing up mca region..."); LazyPregenerator.getInstance().shutdownInstance();
Iris.info( C.BLUE + "Shutting down all Lazy Pregens");
} else { } else {
sender().sendMessage(C.YELLOW + "No active pregeneration tasks to stop"); sender().sendMessage(C.YELLOW + "No active pregeneration tasks to stop");
} }
@@ -90,10 +96,12 @@ public class CommandLazyPregen implements DecreeExecutor {
@Decree(description = "Pause / continue the active pregeneration task", aliases = {"t", "resume", "unpause"}) @Decree(description = "Pause / continue the active pregeneration task", aliases = {"t", "resume", "unpause"})
public void pause() { public void pause() {
if (PregeneratorJob.pauseResume()) { if (LazyPregenerator.getInstance() != null) {
sender().sendMessage(C.GREEN + "Paused/unpaused pregeneration task, now: " + (PregeneratorJob.isPaused() ? "Paused" : "Running") + "."); LazyPregenerator.getInstance().setPausedLazy();
sender().sendMessage(C.GREEN + "Paused/unpaused Lazy Pregen, now: " + (LazyPregenerator.getInstance().isPausedLazy() ? "Paused" : "Running") + ".");
} else { } else {
sender().sendMessage(C.YELLOW + "No active pregeneration tasks to pause/unpause."); sender().sendMessage(C.YELLOW + "No active Lazy Pregen tasks to pause/unpause.");
} }
} }
} }
@@ -3,6 +3,7 @@ package com.volmit.iris.core.pregenerator;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.volmit.iris.Iris; import com.volmit.iris.Iris;
import com.volmit.iris.core.IrisSettings; import com.volmit.iris.core.IrisSettings;
import com.volmit.iris.core.gui.PregeneratorJob;
import com.volmit.iris.util.format.C; import com.volmit.iris.util.format.C;
import com.volmit.iris.util.format.Form; import com.volmit.iris.util.format.Form;
import com.volmit.iris.util.io.IO; import com.volmit.iris.util.io.IO;
@@ -14,9 +15,11 @@ import com.volmit.iris.util.parallel.BurstExecutor;
import com.volmit.iris.util.parallel.MultiBurst; import com.volmit.iris.util.parallel.MultiBurst;
import com.volmit.iris.util.scheduling.ChronoLatch; import com.volmit.iris.util.scheduling.ChronoLatch;
import com.volmit.iris.util.scheduling.J; import com.volmit.iris.util.scheduling.J;
import io.lumine.mythic.bukkit.utils.lib.jooq.False;
import io.papermc.lib.PaperLib; import io.papermc.lib.PaperLib;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import lombok.Getter;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@@ -28,10 +31,13 @@ import java.io.IOException;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
public class LazyPregenerator extends Thread implements Listener { public class LazyPregenerator extends Thread implements Listener {
@Getter
private static LazyPregenerator instance;
private final LazyPregenJob job; private final LazyPregenJob job;
private final File destination; private final File destination;
private final int maxPosition; private final int maxPosition;
@@ -56,10 +62,8 @@ public class LazyPregenerator extends Thread implements Listener {
chunksPerSecond = new RollingSequence(10); chunksPerSecond = new RollingSequence(10);
lazyGeneratedChunks = new AtomicInteger(0); lazyGeneratedChunks = new AtomicInteger(0);
generatedLast = new AtomicInteger(0); generatedLast = new AtomicInteger(0);
lazyTotalChunks = new AtomicInteger(); lazyTotalChunks = new AtomicInteger((int) Math.ceil(Math.pow((2.0 * job.getRadiusBlocks()) / 16, 2)));
LazyPregenerator.instance = this;
int radius = job.getRadiusBlocks();
lazyTotalChunks.set((int) Math.ceil(Math.pow((2.0 * radius) / 16, 2)));
} }
public LazyPregenerator(File file) throws IOException { public LazyPregenerator(File file) throws IOException {
@@ -103,7 +107,7 @@ public class LazyPregenerator extends Thread implements Listener {
} }
public void tick() { public void tick() {
if (latch.flip()) { if (latch.flip() && !job.paused) {
long eta = computeETA(); long eta = computeETA();
save(); save();
int secondGenerated = lazyGeneratedChunks.get() - generatedLast.get(); int secondGenerated = lazyGeneratedChunks.get() - generatedLast.get();
@@ -113,9 +117,6 @@ public class LazyPregenerator extends Thread implements Listener {
if (!job.isSilent()) { if (!job.isSilent()) {
Iris.info("LazyGen: " + C.IRIS + world.getName() + C.RESET + " RTT: " + Form.f(lazyGeneratedChunks.get()) + " of " + Form.f(lazyTotalChunks.get()) + " " + Form.f((int) chunksPerSecond.getAverage()) + "/s ETA: " + Form.duration((double) eta, 2)); Iris.info("LazyGen: " + C.IRIS + world.getName() + C.RESET + " RTT: " + Form.f(lazyGeneratedChunks.get()) + " of " + Form.f(lazyTotalChunks.get()) + " " + Form.f((int) chunksPerSecond.getAverage()) + "/s ETA: " + Form.duration((double) eta, 2));
} }
//Iris.info("Debug: " + maxPosition);
//Iris.info("Debug1: " + job.getPosition());
// todo: Maxpos borked
} }
if (lazyGeneratedChunks.get() >= lazyTotalChunks.get()) { if (lazyGeneratedChunks.get() >= lazyTotalChunks.get()) {
@@ -198,6 +199,23 @@ public class LazyPregenerator extends Thread implements Listener {
} }
}); });
} }
public void setPausedLazy(){
if (!job.paused) {
save();
Iris.info(C.BLUE + "LazyGen: " + C.IRIS + world + C.BLUE + " Paused");
job.setPaused(true);
} else {
Iris.info(C.BLUE + "LazyGen: " + C.IRIS + world + C.BLUE + " Resumes");
job.setPaused(false);
}
}
public boolean isPausedLazy(){
return job.isPaused();
}
public void shutdownInstance() {
save();
interrupt();
}
public void saveNow() throws IOException { public void saveNow() throws IOException {
IO.writeAll(this.destination, new Gson().toJson(job)); IO.writeAll(this.destination, new Gson().toJson(job));
@@ -219,5 +237,7 @@ public class LazyPregenerator extends Thread implements Listener {
private int position = 0; private int position = 0;
@Builder.Default @Builder.Default
boolean silent = false; boolean silent = false;
@Builder.Default
boolean paused = false;
} }
} }