diff --git a/core/src/main/java/com/volmit/iris/core/commands/CommandStudio.java b/core/src/main/java/com/volmit/iris/core/commands/CommandStudio.java index 217da715f..6a4e93f80 100644 --- a/core/src/main/java/com/volmit/iris/core/commands/CommandStudio.java +++ b/core/src/main/java/com/volmit/iris/core/commands/CommandStudio.java @@ -250,7 +250,7 @@ public class CommandStudio implements DecreeExecutor { public void noise() { if (noGUI()) return; sender().sendMessage(C.GREEN + "Opening Noise Explorer!"); - NoiseExplorerGUI.launch(); + NoiseExplorerGUI.launch(engine()); } @Decree(description = "Preview the palettes around you (External GUI)", aliases = "pal", origin = DecreeOrigin.PLAYER) @@ -293,7 +293,7 @@ public class CommandStudio implements DecreeExecutor { ) { if (noGUI()) return; sender().sendMessage(C.GREEN + "Opening Noise Explorer!"); - NoiseExplorerGUI.launchGeneratorKey(generator == null ? null : generator.getLoadKey(), generator, seed); + NoiseExplorerGUI.launchGeneratorKey(engine(), generator == null ? null : generator.getLoadKey(), generator, seed); } @Decree(description = "Hotload a studio", aliases = {"reload", "h"}) diff --git a/core/src/main/java/com/volmit/iris/core/gui/GuiHost.java b/core/src/main/java/com/volmit/iris/core/gui/GuiHost.java index 51dc56454..0de92faf5 100644 --- a/core/src/main/java/com/volmit/iris/core/gui/GuiHost.java +++ b/core/src/main/java/com/volmit/iris/core/gui/GuiHost.java @@ -20,7 +20,9 @@ package com.volmit.iris.core.gui; import com.volmit.iris.Iris; import com.volmit.iris.core.events.IrisEngineHotloadEvent; +import com.volmit.iris.core.project.IrisProject; import com.volmit.iris.core.service.PreservationSVC; +import com.volmit.iris.core.service.StudioSVC; import com.volmit.iris.core.tools.IrisToolbelt; import com.volmit.iris.engine.framework.Engine; import com.volmit.iris.engine.platform.PlatformChunkGenerator; @@ -94,7 +96,15 @@ public final class GuiHost { return new Dimension(Math.min(preferred.width, usable.width), Math.min(preferred.height, usable.height)); } + /** + * The engine of the open studio project, which reads the pack folder you edit, or else the first Iris world. + * A normal Iris world runs on its own copy of the pack, made when the world was created. + */ public static Engine findActiveEngine() { + Engine studioEngine = findStudioEngine(); + if (studioEngine != null) { + return studioEngine; + } try { for (World world : new ArrayList<>(Bukkit.getWorlds())) { Engine engine = engineOf(world); @@ -107,6 +117,18 @@ public final class GuiHost { return null; } + private static Engine findStudioEngine() { + try { + StudioSVC studio = Iris.service(StudioSVC.class); + IrisProject project = studio == null || !studio.isProjectOpen() ? null : studio.getActiveProject(); + PlatformChunkGenerator provider = project == null ? null : project.getActiveProvider(); + Engine engine = provider == null ? null : provider.getEngine(); + return engine == null || engine.isClosed() ? null : engine; + } catch (Throwable ignored) { + return null; + } + } + public static Engine findEngine(UUID worldId) { World world = Bukkit.getWorld(worldId); return world == null ? null : engineOf(world); diff --git a/core/src/main/java/com/volmit/iris/core/gui/NoiseExplorerGUI.java b/core/src/main/java/com/volmit/iris/core/gui/NoiseExplorerGUI.java index c19868931..4b1afa6a8 100644 --- a/core/src/main/java/com/volmit/iris/core/gui/NoiseExplorerGUI.java +++ b/core/src/main/java/com/volmit/iris/core/gui/NoiseExplorerGUI.java @@ -29,6 +29,7 @@ import com.volmit.iris.engine.object.NoiseStyle; import com.volmit.iris.util.function.NoiseProvider; import com.volmit.iris.util.math.RNG; import com.volmit.iris.util.noise.CNG; +import org.bukkit.World; import javax.swing.AbstractAction; import javax.swing.BorderFactory; @@ -208,23 +209,26 @@ public final class NoiseExplorerGUI extends JPanel { GuiHost.registerHotloadHook(hotloadHook); } - public static void launch() { - Engine engine = GuiHost.findActiveEngine(); + /** + * @param engine the engine whose pack generators to list, or null for the open studio or first Iris world + */ + public static void launch(Engine engine) { + Engine sourceEngine = engine != null ? engine : GuiHost.findActiveEngine(); EventQueue.invokeLater(() -> { StudioTheme.install(); - NoiseExplorerGUI explorer = new NoiseExplorerGUI(ExplorerMode.NOISE, engine, null, null, null, DEFAULT_SEED, null); + NoiseExplorerGUI explorer = new NoiseExplorerGUI(ExplorerMode.NOISE, sourceEngine, null, null, null, DEFAULT_SEED, null); buildNoiseFrame("Noise Explorer", explorer); }); } - public static void launchGeneratorKey(String generatorKey, IrisGenerator fallbackGenerator, long initialSeed) { - Engine engine = GuiHost.findActiveEngine(); + public static void launchGeneratorKey(Engine engine, String generatorKey, IrisGenerator fallbackGenerator, long initialSeed) { + Engine sourceEngine = engine != null ? engine : GuiHost.findActiveEngine(); String displayName = generatorKey == null || generatorKey.isBlank() ? "Custom Generator" : generatorKey; EventQueue.invokeLater(() -> { StudioTheme.install(); NoiseExplorerGUI explorer = new NoiseExplorerGUI( ExplorerMode.NOISE, - engine, + sourceEngine, generatorKey, fallbackGenerator, displayName, @@ -1080,13 +1084,22 @@ public final class NoiseExplorerGUI extends JPanel { }); } + // A hotload replaces the engine; the explorer stays on the world it was opened for. + private Engine engineAfterHotload() { + if (paletteStart != null) { + return GuiHost.findEngine(paletteStart.worldId()); + } + World world = activeEngine == null ? null : activeEngine.getWorld().realWorld(); + return world == null ? GuiHost.findActiveEngine() : GuiHost.findEngine(world.getUID()); + } + void resetViewport() { viewport = initialViewport; requestPreview(); } private void handleHotload() { - Engine refreshedEngine = paletteStart == null ? GuiHost.findActiveEngine() : GuiHost.findEngine(paletteStart.worldId()); + Engine refreshedEngine = engineAfterHotload(); EventQueue.invokeLater(() -> { if (disposed) { return;