diff --git a/core/src/main/java/com/volmit/iris/core/commands/CommandLazyPregen.java b/core/src/main/java/com/volmit/iris/core/commands/CommandLazyPregen.java index cc1c7f6aa..f906056e9 100644 --- a/core/src/main/java/com/volmit/iris/core/commands/CommandLazyPregen.java +++ b/core/src/main/java/com/volmit/iris/core/commands/CommandLazyPregen.java @@ -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.format.C; import com.volmit.iris.util.math.Position2; +import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.util.Vector; @@ -36,6 +37,7 @@ import java.io.File; @Decree(name = "lazypregen", aliases = "lazy", description = "Pregenerate your Iris worlds!") public class CommandLazyPregen implements DecreeExecutor { + public String worldName; @Decree(description = "Pregenerate a world") public void start( @Param(description = "The radius of the pregen in blocks", aliases = "size") @@ -46,10 +48,11 @@ public class CommandLazyPregen implements DecreeExecutor { Vector center, @Param(aliases = "maxcpm", description = "Limit the chunks per minute the pregen will generate", defaultValue = "999999999") int cpm, - @Param(aliases = "maxcpm", description = "Limit the chunks per minute the pregen will generate", defaultValue = "false") - boolean dummySilent + @Param(aliases = "silent", description = "Silent generation", defaultValue = "false") + boolean silent ) { - String worldName = world.getName(); + + worldName = world.getName(); try { if (sender().isPlayer() && access() == null) { sender().sendMessage(C.RED + "The engine access for this world is null!"); @@ -63,10 +66,12 @@ public class CommandLazyPregen implements DecreeExecutor { .chunksPerMinute(cpm) .radiusBlocks(radius) .position(0) - .silent(dummySilent) + .silent(silent) .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(); 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") public void stop() { - if (PregeneratorJob.shutdownInstance()) { - Iris.info( C.BLUE + "Finishing up mca region..."); + if (LazyPregenerator.getInstance() != null) { + LazyPregenerator.getInstance().shutdownInstance(); + Iris.info( C.BLUE + "Shutting down all Lazy Pregens"); } else { 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"}) public void pause() { - if (PregeneratorJob.pauseResume()) { - sender().sendMessage(C.GREEN + "Paused/unpaused pregeneration task, now: " + (PregeneratorJob.isPaused() ? "Paused" : "Running") + "."); + if (LazyPregenerator.getInstance() != null) { + LazyPregenerator.getInstance().setPausedLazy(); + sender().sendMessage(C.GREEN + "Paused/unpaused Lazy Pregen, now: " + (LazyPregenerator.getInstance().isPausedLazy() ? "Paused" : "Running") + "."); } else { - sender().sendMessage(C.YELLOW + "No active pregeneration tasks to pause/unpause."); + sender().sendMessage(C.YELLOW + "No active Lazy Pregen tasks to pause/unpause."); + } } } diff --git a/core/src/main/java/com/volmit/iris/core/pregenerator/LazyPregenerator.java b/core/src/main/java/com/volmit/iris/core/pregenerator/LazyPregenerator.java index 10d528c90..0883bd0a0 100644 --- a/core/src/main/java/com/volmit/iris/core/pregenerator/LazyPregenerator.java +++ b/core/src/main/java/com/volmit/iris/core/pregenerator/LazyPregenerator.java @@ -3,6 +3,7 @@ package com.volmit.iris.core.pregenerator; import com.google.gson.Gson; import com.volmit.iris.Iris; 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.Form; 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.scheduling.ChronoLatch; import com.volmit.iris.util.scheduling.J; +import io.lumine.mythic.bukkit.utils.lib.jooq.False; import io.papermc.lib.PaperLib; import lombok.Builder; import lombok.Data; +import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.event.EventHandler; @@ -28,10 +31,13 @@ import java.io.IOException; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; public class LazyPregenerator extends Thread implements Listener { + @Getter + private static LazyPregenerator instance; private final LazyPregenJob job; private final File destination; private final int maxPosition; @@ -56,10 +62,8 @@ public class LazyPregenerator extends Thread implements Listener { chunksPerSecond = new RollingSequence(10); lazyGeneratedChunks = new AtomicInteger(0); generatedLast = new AtomicInteger(0); - lazyTotalChunks = new AtomicInteger(); - - int radius = job.getRadiusBlocks(); - lazyTotalChunks.set((int) Math.ceil(Math.pow((2.0 * radius) / 16, 2))); + lazyTotalChunks = new AtomicInteger((int) Math.ceil(Math.pow((2.0 * job.getRadiusBlocks()) / 16, 2))); + LazyPregenerator.instance = this; } public LazyPregenerator(File file) throws IOException { @@ -103,7 +107,7 @@ public class LazyPregenerator extends Thread implements Listener { } public void tick() { - if (latch.flip()) { + if (latch.flip() && !job.paused) { long eta = computeETA(); save(); int secondGenerated = lazyGeneratedChunks.get() - generatedLast.get(); @@ -113,9 +117,6 @@ public class LazyPregenerator extends Thread implements Listener { 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("Debug: " + maxPosition); - //Iris.info("Debug1: " + job.getPosition()); - // todo: Maxpos borked } 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 { IO.writeAll(this.destination, new Gson().toJson(job)); @@ -219,5 +237,7 @@ public class LazyPregenerator extends Thread implements Listener { private int position = 0; @Builder.Default boolean silent = false; + @Builder.Default + boolean paused = false; } }