mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Switch the studio between packs from the title bar
- List the workspace packs with their names and versions in the pack menu - Open the chosen pack as the studio world and reconnect to its bridge - Close the bridge of the world the studio switched away from - Keep Disconnect in the menu for the world list
This commit is contained in:
1 parent
26c7ed9c40
commit
fe99d5bda5
14 files changed
+450
-17
No files matched your search
@@ -79,6 +79,7 @@ import art.arcane.iris.studio.jigsaw.JigsawStudioService;
|
||||
import art.arcane.iris.studio.object.ObjectSVC;
|
||||
import art.arcane.iris.studio.object.ObjectStudioSaveService;
|
||||
import art.arcane.iris.studio.view.BukkitGuiHost;
|
||||
import art.arcane.iris.studio.view.BukkitStudioPackSwitch;
|
||||
import art.arcane.iris.studio.view.GuiHost;
|
||||
import art.arcane.iris.studio.view.PregeneratorJob;
|
||||
import art.arcane.iris.studio.wand.WandSVC;
|
||||
@@ -977,6 +978,7 @@ public final class IrisRuntime implements IrisPluginRuntime {
|
||||
return;
|
||||
}
|
||||
BukkitGuiHost.install();
|
||||
BukkitStudioPackSwitch.install();
|
||||
StudioAppInstaller.installInBackground();
|
||||
// super.onEnable() already registers this instance as a listener.
|
||||
plugin.enablePlugin();
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Bukkit Servers
|
||||
* Copyright (c) 2022 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.studio.view;
|
||||
|
||||
import art.arcane.iris.Iris;
|
||||
import art.arcane.iris.platform.bukkit.plugin.VolmitSender;
|
||||
import art.arcane.iris.studio.StudioSVC;
|
||||
import art.arcane.iris.studio.bridge.StudioBridge;
|
||||
import art.arcane.iris.studio.bridge.StudioBridges;
|
||||
import art.arcane.iris.studio.bridge.StudioPack;
|
||||
import art.arcane.iris.studio.bridge.StudioPackSwitch;
|
||||
import art.arcane.iris.studio.object.StudioOpenCoordinator;
|
||||
import art.arcane.iris.world.IrisToolbelt;
|
||||
import art.arcane.iris.world.task.J;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/** Switches the studio between the packs in the workspace by opening a studio world for the chosen one. */
|
||||
public final class BukkitStudioPackSwitch implements StudioPackSwitch {
|
||||
/** The seed {@code /iris studio open} opens with unless told otherwise. */
|
||||
private static final long STUDIO_SEED = 1337L;
|
||||
|
||||
public static void install() {
|
||||
StudioBridges.usePackSwitch(new BukkitStudioPackSwitch());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StudioPack> packs() {
|
||||
return StudioPack.inWorkspace(Iris.service(StudioSVC.class).getWorkspaceFolder());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<StudioBridge> open(String pack, UUID openerId) {
|
||||
CompletableFuture<StudioOpenCoordinator.StudioOpenResult> opening = new CompletableFuture<>();
|
||||
J.s(() -> Iris.service(StudioSVC.class)
|
||||
.openTracked(senderFor(openerId), STUDIO_SEED, pack, StudioOpenCoordinator.StudioOpenKind.STANDARD, () -> {
|
||||
}, world -> {
|
||||
})
|
||||
.whenComplete((result, failure) -> {
|
||||
if (failure != null) {
|
||||
opening.completeExceptionally(failure);
|
||||
return;
|
||||
}
|
||||
opening.complete(result);
|
||||
}));
|
||||
return opening.thenApply(result -> bridgeTo(result.world(), openerId));
|
||||
}
|
||||
|
||||
/** The opener, so the new studio moves them as {@code /iris studio open} would; the console when they are gone. */
|
||||
private static VolmitSender senderFor(UUID openerId) {
|
||||
Player opener = openerId == null ? null : Bukkit.getPlayer(openerId);
|
||||
return new VolmitSender(opener == null ? Bukkit.getConsoleSender() : opener);
|
||||
}
|
||||
|
||||
private static StudioBridge bridgeTo(World world, UUID openerId) {
|
||||
try {
|
||||
return StudioBridges.open(world.getName(), IrisToolbelt.access(world)::getEngine, openerId);
|
||||
} catch (IOException error) {
|
||||
throw new UncheckedIOException(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,6 +98,9 @@ public final class StudioBridge implements AutoCloseable {
|
||||
private static final long ASSET_CACHE_SECONDS = 86_400L;
|
||||
private static final String OVERRIDE_PREFIX = "set.";
|
||||
private static final int LARGEST_BODY_BYTES = 4 * 1024 * 1024;
|
||||
private static final long PACK_OPEN_TIMEOUT_MINUTES = 5;
|
||||
/** How long a bridge the studio switched away from stays up, so its answer naming the new bridge gets out first. */
|
||||
private static final long REPLACED_BRIDGE_GRACE_SECONDS = 5;
|
||||
private static final String VIEW_BIOMES = "biomes";
|
||||
private static final String VIEW_REGIONS = "regions";
|
||||
private static final String VIEW_RIVERS = "rivers";
|
||||
@@ -277,6 +280,8 @@ public final class StudioBridge implements AutoCloseable {
|
||||
case "saved-worlds" -> sendJson(exchange, STATUS_OK, List.of());
|
||||
case "events" -> streamEvents(exchange);
|
||||
case "teleport" -> teleport(exchange, query);
|
||||
case "packs" -> sendJson(exchange, STATUS_OK, packs(exchange));
|
||||
case "open-pack" -> openPack(exchange, query);
|
||||
case "open" -> throw new RequestError(STATUS_NOT_FOUND, "Opening a pack file from the studio is not available in Iris v4");
|
||||
default -> throw new RequestError(STATUS_NOT_FOUND, "No such endpoint: " + endpoint);
|
||||
}
|
||||
@@ -645,6 +650,41 @@ public final class StudioBridge implements AutoCloseable {
|
||||
sendJson(exchange, STATUS_OK, new MessageBody(null));
|
||||
}
|
||||
|
||||
private List<StudioPack> packs(HttpExchange exchange) {
|
||||
requireMethod(exchange, "GET");
|
||||
StudioPackSwitch packSwitch = StudioBridges.packSwitch();
|
||||
return packSwitch == null ? List.of() : packSwitch.packs();
|
||||
}
|
||||
|
||||
/** Replaces the studio world with one of another pack and answers with the new world's bridge, then closes this one. */
|
||||
private void openPack(HttpExchange exchange, Query query) throws IOException {
|
||||
requireMethod(exchange, "POST");
|
||||
StudioPackSwitch packSwitch = StudioBridges.packSwitch();
|
||||
if (packSwitch == null) {
|
||||
throw new RequestError(STATUS_NOT_FOUND, "This server cannot switch packs from the studio");
|
||||
}
|
||||
StudioBridge opened = awaitPackOpen(packSwitch.open(query.text("pack"), world.openerId()));
|
||||
sendJson(exchange, STATUS_OK, new OpenedBridge(opened.worldName(), opened.port(), opened.token()));
|
||||
if (opened != this) {
|
||||
CompletableFuture.runAsync(() -> StudioBridges.close(worldName()),
|
||||
CompletableFuture.delayedExecutor(REPLACED_BRIDGE_GRACE_SECONDS, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
|
||||
private static StudioBridge awaitPackOpen(CompletableFuture<StudioBridge> opening) {
|
||||
try {
|
||||
return opening.get(PACK_OPEN_TIMEOUT_MINUTES, TimeUnit.MINUTES);
|
||||
} catch (ExecutionException failure) {
|
||||
Throwable cause = failure.getCause() == null ? failure : failure.getCause();
|
||||
throw new RequestError(STATUS_CONFLICT, "Could not open the pack: " + cause.getMessage());
|
||||
} catch (TimeoutException slow) {
|
||||
throw new RequestError(STATUS_UNAVAILABLE, "The pack is still opening; the server will show it when it is ready");
|
||||
} catch (InterruptedException interrupted) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RequestError(STATUS_UNAVAILABLE, "The bridge stopped while the pack opened");
|
||||
}
|
||||
}
|
||||
|
||||
private void streamEvents(HttpExchange exchange) throws IOException {
|
||||
requireMethod(exchange, "GET");
|
||||
Headers headers = exchange.getResponseHeaders();
|
||||
@@ -797,6 +837,9 @@ public final class StudioBridge implements AutoCloseable {
|
||||
private record ErrorBody(String error) {
|
||||
}
|
||||
|
||||
private record OpenedBridge(String world, int port, String token) {
|
||||
}
|
||||
|
||||
private record MessageBody(String message) {
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ public final class StudioBridges {
|
||||
private static final Path CONNECTION_FOLDER = Path.of(System.getProperty("user.home"), ".iris", "studio", "bridges");
|
||||
private static final String OWNER_ONLY = "rw-------";
|
||||
private static final Map<String, StudioBridge> OPEN = new ConcurrentHashMap<>();
|
||||
private static volatile StudioPackSwitch packSwitch;
|
||||
|
||||
private StudioBridges() {
|
||||
}
|
||||
@@ -61,6 +62,24 @@ public final class StudioBridges {
|
||||
return bridge;
|
||||
}
|
||||
|
||||
/** Lets the studio switch packs through {@code packSwitch}; without one the studio offers no other pack. */
|
||||
public static void usePackSwitch(StudioPackSwitch packSwitch) {
|
||||
StudioBridges.packSwitch = packSwitch;
|
||||
}
|
||||
|
||||
static StudioPackSwitch packSwitch() {
|
||||
return packSwitch;
|
||||
}
|
||||
|
||||
public static synchronized void close(String worldName) {
|
||||
StudioBridge bridge = OPEN.remove(worldName);
|
||||
if (bridge == null) {
|
||||
return;
|
||||
}
|
||||
bridge.close();
|
||||
withdraw(bridge);
|
||||
}
|
||||
|
||||
public static synchronized void closeAll() {
|
||||
StudioWindow.closeAll();
|
||||
for (StudioBridge bridge : OPEN.values()) {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Servers
|
||||
* Copyright (c) 2026 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.studio.bridge;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A pack the studio can open: its folder, and the name and version its dimension file gives.
|
||||
*
|
||||
* @param key the pack's folder, which names its dimension file too
|
||||
*/
|
||||
public record StudioPack(String key, String name, int version) {
|
||||
private static final int UNVERSIONED = 1;
|
||||
|
||||
/** The packs in the workspace by name; a folder without a readable dimension file of its own name is no pack. */
|
||||
public static List<StudioPack> inWorkspace(File workspace) {
|
||||
File[] folders = workspace.listFiles(File::isDirectory);
|
||||
if (folders == null) {
|
||||
return List.of();
|
||||
}
|
||||
return Arrays.stream(folders)
|
||||
.map(StudioPack::read)
|
||||
.filter(Objects::nonNull)
|
||||
.sorted(Comparator.comparing(StudioPack::name, String.CASE_INSENSITIVE_ORDER))
|
||||
.toList();
|
||||
}
|
||||
|
||||
static StudioPack read(File folder) {
|
||||
String key = folder.getName();
|
||||
File dimensionFile = new File(folder, "dimensions/" + key + ".json");
|
||||
if (!dimensionFile.isFile()) {
|
||||
return null;
|
||||
}
|
||||
try (Reader reader = Files.newBufferedReader(dimensionFile.toPath(), StandardCharsets.UTF_8)) {
|
||||
JsonObject dimension = JsonParser.parseReader(reader).getAsJsonObject();
|
||||
return new StudioPack(key, textOr(dimension.get("name"), key), versionOf(dimension.get("version")));
|
||||
} catch (IOException | JsonParseException | IllegalStateException | NumberFormatException unreadable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String textOr(JsonElement element, String fallback) {
|
||||
return element != null && element.isJsonPrimitive() && !element.getAsString().isBlank() ? element.getAsString() : fallback;
|
||||
}
|
||||
|
||||
private static int versionOf(JsonElement element) {
|
||||
return element != null && element.isJsonPrimitive() ? element.getAsInt() : UNVERSIONED;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Iris is a World Generator for Minecraft Servers
|
||||
* Copyright (c) 2026 Arcane Arts (Volmit Software)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package art.arcane.iris.studio.bridge;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/** Opens the studio on another pack, on a platform that can replace the open studio world. */
|
||||
public interface StudioPackSwitch {
|
||||
/** The packs the studio can open, in the order to list them. */
|
||||
List<StudioPack> packs();
|
||||
|
||||
/**
|
||||
* Replaces the open studio world with one of {@code pack} and opens a bridge to the new world.
|
||||
*
|
||||
* @param openerId the player the old studio was opened for, who moves to the new one; null for the console
|
||||
*/
|
||||
CompletableFuture<StudioBridge> open(String pack, UUID openerId);
|
||||
}
|
||||
@@ -51,6 +51,10 @@ final class StudioWorld {
|
||||
return name;
|
||||
}
|
||||
|
||||
UUID openerId() {
|
||||
return openerId;
|
||||
}
|
||||
|
||||
/** The world's engine and what the bridge draws it with; null while the world has no running engine. */
|
||||
synchronized Session session() {
|
||||
if (session != null && isRunning(session.engine())) {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package art.arcane.iris.studio.bridge;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StudioPackTest {
|
||||
@Rule
|
||||
public TemporaryFolder workspace = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void aPackIsNamedAndVersionedByItsDimensionFile() throws IOException {
|
||||
dimension("natureplanet", "{\"name\": \"Etopia\", \"version\": 100}");
|
||||
|
||||
assertEquals(List.of(new StudioPack("natureplanet", "Etopia", 100)), StudioPack.inWorkspace(workspace.getRoot()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commentsInTheDimensionFileAreRead() throws IOException {
|
||||
dimension("overworld", "{\n // the pack's name\n \"name\": \"Overworld\",\n \"version\": 3\n}");
|
||||
|
||||
assertEquals(List.of(new StudioPack("overworld", "Overworld", 3)), StudioPack.inWorkspace(workspace.getRoot()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aDimensionWithoutNameOrVersionFallsBackToItsFolderAndTheFirstVersion() throws IOException {
|
||||
dimension("bare", "{}");
|
||||
|
||||
assertEquals(List.of(new StudioPack("bare", "bare", 1)), StudioPack.inWorkspace(workspace.getRoot()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void foldersWithoutTheirDimensionFileAreNoPacks() throws IOException {
|
||||
workspace.newFolder("notes");
|
||||
dimension("broken", "{\"name\": ");
|
||||
dimension("zeta", "{\"name\": \"zeta\"}");
|
||||
dimension("alpha", "{\"name\": \"Alpha\"}");
|
||||
|
||||
assertEquals(List.of("Alpha", "zeta"), StudioPack.inWorkspace(workspace.getRoot()).stream().map(StudioPack::name).toList());
|
||||
}
|
||||
|
||||
private void dimension(String pack, String json) throws IOException {
|
||||
File dimensions = new File(workspace.getRoot(), pack + "/dimensions");
|
||||
Files.createDirectories(dimensions.toPath());
|
||||
Files.writeString(new File(dimensions, pack + ".json").toPath(), json, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,11 @@ export function App(): React.JSX.Element {
|
||||
}
|
||||
}, [])
|
||||
|
||||
const openPack = useCallback(async (client: BridgeClient, packKey: string) => {
|
||||
const opened = new BridgeClient(await client.openPack(packKey))
|
||||
setSession({ client: opened, world: await opened.world() })
|
||||
}, [])
|
||||
|
||||
const connectLink = useCallback(
|
||||
(link: string) => {
|
||||
const connection = parseStudioLink(link)
|
||||
@@ -62,6 +67,7 @@ export function App(): React.JSX.Element {
|
||||
client={session.client}
|
||||
world={session.world}
|
||||
theme={theme}
|
||||
onOpenPack={(packKey) => openPack(session.client, packKey)}
|
||||
onDisconnect={() => setSession(null)}
|
||||
/>
|
||||
)
|
||||
|
||||
@@ -27,6 +27,7 @@ import type {
|
||||
SceneEntry,
|
||||
SceneViewInfo,
|
||||
ServerUsage,
|
||||
StudioPack,
|
||||
WorldInfo
|
||||
} from './types'
|
||||
|
||||
@@ -137,6 +138,16 @@ export class BridgeClient {
|
||||
return this.json('entities')
|
||||
}
|
||||
|
||||
/** The packs the server can switch the studio to; none on a server that cannot. */
|
||||
packs(): Promise<StudioPack[]> {
|
||||
return this.json('packs')
|
||||
}
|
||||
|
||||
/** Replaces the studio world with one of the pack; the new world's bridge, once the world is ready. */
|
||||
openPack(packKey: string): Promise<BridgeConnection> {
|
||||
return this.json(`open-pack?${new URLSearchParams({ pack: packKey })}`, { method: 'POST' })
|
||||
}
|
||||
|
||||
async teleport(worldX: number, worldZ: number): Promise<void> {
|
||||
await this.json(`teleport?x=${worldX}&z=${worldZ}`, { method: 'POST' })
|
||||
}
|
||||
|
||||
@@ -301,6 +301,14 @@ export interface SavedTile {
|
||||
noWater: number
|
||||
}
|
||||
|
||||
/** A pack the server can open the studio on. */
|
||||
export interface StudioPack {
|
||||
/** The pack's folder, which names its dimension too. */
|
||||
key: string
|
||||
name: string
|
||||
version: number
|
||||
}
|
||||
|
||||
export interface WorldInfo {
|
||||
world: string
|
||||
dimension: string
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import type { BridgeClient, MapLayerQuery, WorldRect } from '../bridge/BridgeClient'
|
||||
import type { MapHover, MapLabel, Marker, ModuleViewInfo, ServerUsage, ViewInfo, WorldInfo } from '../bridge/types'
|
||||
import type { MapHover, MapLabel, Marker, ModuleViewInfo, ServerUsage, StudioPack, ViewInfo, WorldInfo } from '../bridge/types'
|
||||
import type { Share } from '../map/census'
|
||||
import type { MapController, MapStatus, MapToggles } from '../map/MapController'
|
||||
import type { Highlight } from '../map/tileStyle'
|
||||
import { Waves } from 'lucide-react'
|
||||
import { HelpDialog } from './HelpDialog'
|
||||
import { HoverCard } from './HoverCard'
|
||||
import { Glyph } from './Glyph'
|
||||
import { Legend } from './Legend'
|
||||
import { MapCanvas, type CanvasPoint } from './MapCanvas'
|
||||
import { PickInspector, hasPickSections } from './PickInspector'
|
||||
@@ -38,6 +39,8 @@ interface MapStudioProps {
|
||||
client: BridgeClient
|
||||
world: WorldInfo
|
||||
theme: Theme
|
||||
/** Switches the studio to another pack; settles once the studio is on it or the server refused. */
|
||||
onOpenPack(packKey: string): Promise<void>
|
||||
onDisconnect(): void
|
||||
}
|
||||
|
||||
@@ -47,7 +50,7 @@ export interface StudioToggles extends MapToggles {
|
||||
}
|
||||
|
||||
/** The map of one world: views on the left, the map in the middle, what is on screen on the right. */
|
||||
export function MapStudio({ client, world, theme, onDisconnect }: MapStudioProps): React.JSX.Element {
|
||||
export function MapStudio({ client, world, theme, onOpenPack, onDisconnect }: MapStudioProps): React.JSX.Element {
|
||||
const controller = useRef<MapController | null>(null)
|
||||
const [viewId, setViewId] = useState(world.views[0]?.id ?? 'biomes')
|
||||
const [toggles, setToggles] = useState<StudioToggles>({ shading: true, regionBorders: false, grid: false, blocks: false, follow: false, entities: false })
|
||||
@@ -70,6 +73,7 @@ export function MapStudio({ client, world, theme, onDisconnect }: MapStudioProps
|
||||
const [sceneSummary, setSceneSummary] = useState<string | null>(null)
|
||||
const [sceneProgress, setSceneProgress] = useState<number | null>(null)
|
||||
const [sidebarSlot, setSidebarSlot] = useState<HTMLElement | null>(null)
|
||||
const [openingPack, setOpeningPack] = useState<StudioPack | null>(null)
|
||||
const loadSceneViews = useCallback(() => client.sceneViews(), [client])
|
||||
const loadPatternViews = useCallback(() => client.patternViews(), [client])
|
||||
const sceneViews = useModuleViews(loadSceneViews, packRevision)
|
||||
@@ -90,6 +94,18 @@ export function MapStudio({ client, world, theme, onDisconnect }: MapStudioProps
|
||||
|
||||
const tell = useCallback((message: string) => setNotice(message), [])
|
||||
|
||||
const loadPacks = useCallback(() => client.packs(), [client])
|
||||
const openPack = useCallback(
|
||||
(pack: StudioPack) => {
|
||||
setOpeningPack(pack)
|
||||
onOpenPack(pack.key).catch((failure: Error) => {
|
||||
setOpeningPack(null)
|
||||
tell(failure.message)
|
||||
})
|
||||
},
|
||||
[onOpenPack, tell]
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (notice === null) {
|
||||
return
|
||||
@@ -217,9 +233,17 @@ export function MapStudio({ client, world, theme, onDisconnect }: MapStudioProps
|
||||
|
||||
return (
|
||||
<div className="flex h-full flex-col">
|
||||
<StudioToolbar world={world} onHelp={() => setHelpOpen(true)} onSettings={() => setSettingsOpen(true)} onDisconnect={onDisconnect} />
|
||||
<StudioToolbar world={world} openingPack={openingPack} loadPacks={loadPacks} onOpenPack={openPack} onHelp={() => setHelpOpen(true)} onSettings={() => setSettingsOpen(true)} onDisconnect={onDisconnect} />
|
||||
<SidebarSlot.Provider value={sidebarSlot}>
|
||||
<div className="flex min-h-0 flex-1 gap-1.5 pr-1.5">
|
||||
<div className="relative flex min-h-0 flex-1 gap-1.5 pr-1.5">
|
||||
{openingPack !== null && (
|
||||
<div className="absolute inset-0 z-20 flex items-center justify-center bg-background/70 backdrop-motion">
|
||||
<div className="popup flex items-center gap-2 px-4 py-3 dialog-motion">
|
||||
<Glyph glyph="refresh" className="animate-spin" />
|
||||
Opening {openingPack.name}…
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<ViewList views={views} current={viewId} onSelect={selectView} slotRef={setSidebarSlot} />
|
||||
{viewId === 'regions' && regions.length > 1 && (
|
||||
<SidebarPanel className="flex-1 overflow-y-auto p-1.5">
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import { useState } from 'react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'
|
||||
import { cn } from '@/lib/utils'
|
||||
import type { StudioPack, WorldInfo } from '../bridge/types'
|
||||
import { Glyph } from './Glyph'
|
||||
|
||||
interface PackSwitcherProps {
|
||||
world: WorldInfo
|
||||
/** The pack the studio is switching to, while the server opens it. */
|
||||
opening: StudioPack | null
|
||||
loadPacks(): Promise<StudioPack[]>
|
||||
onOpenPack(pack: StudioPack): void
|
||||
onDisconnect(): void
|
||||
}
|
||||
|
||||
/** The open pack in the window's bar, which lists the server's packs to switch the studio to, as IntelliJ's project widget does. */
|
||||
export function PackSwitcher({ world, opening, loadPacks, onOpenPack, onDisconnect }: PackSwitcherProps): React.JSX.Element {
|
||||
const [packs, setPacks] = useState<StudioPack[] | null>(null)
|
||||
const refreshPacks = (open: boolean): void => {
|
||||
if (!open) {
|
||||
return
|
||||
}
|
||||
loadPacks()
|
||||
.then(setPacks)
|
||||
.catch(() => setPacks([]))
|
||||
}
|
||||
const shownName = opening?.name ?? world.dimensionName
|
||||
const shownVersion = opening?.version ?? world.packVersion
|
||||
return (
|
||||
<DropdownMenu onOpenChange={refreshPacks}>
|
||||
<DropdownMenuTrigger asChild disabled={opening !== null}>
|
||||
<Button variant="ghost" size="sm" className="h-[30px] max-w-[50%] gap-2 px-2 font-semibold data-[state=open]:bg-accent">
|
||||
<PackBadge name={shownName} />
|
||||
<span className="truncate">{shownName}</span>
|
||||
{shownVersion !== undefined && <span className="shrink-0 font-normal text-secondary-text tabular-nums">v{shownVersion}</span>}
|
||||
<Glyph glyph={opening === null ? 'chevronDown' : 'refresh'} className={cn(opening !== null && 'animate-spin')} />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start" className="w-64">
|
||||
{packs === null && <p className="px-2 py-1 text-muted-foreground">Listing packs…</p>}
|
||||
{packs?.length === 0 && <p className="px-2 py-1 text-muted-foreground">No packs to switch to</p>}
|
||||
{packs?.map((pack) => {
|
||||
const current = pack.key === world.dimension
|
||||
return (
|
||||
<DropdownMenuItem key={pack.key} onSelect={() => !current && onOpenPack(pack)} className={cn(current && 'bg-selection')}>
|
||||
<PackBadge name={pack.name} />
|
||||
<span className="min-w-0 flex-1 truncate">{pack.name}</span>
|
||||
<span className="shrink-0 text-[11.5px] text-secondary-text tabular-nums">v{pack.version}</span>
|
||||
</DropdownMenuItem>
|
||||
)
|
||||
})}
|
||||
<div className="-mx-1 my-1 h-px bg-border" />
|
||||
<DropdownMenuItem onSelect={onDisconnect}>Disconnect</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)
|
||||
}
|
||||
|
||||
function PackBadge({ name }: { name: string }): React.JSX.Element {
|
||||
return (
|
||||
<span className="flex size-5 shrink-0 items-center justify-center rounded-[5px] bg-primary text-[11px] font-semibold text-primary-foreground uppercase">
|
||||
{name.charAt(0)}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -5,35 +5,31 @@ import { Kbd } from '@/components/ui/kbd'
|
||||
import { Separator } from '@/components/ui/separator'
|
||||
import { Toggle } from '@/components/ui/toggle'
|
||||
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
|
||||
import type { WorldInfo } from '../bridge/types'
|
||||
import type { StudioPack, WorldInfo } from '../bridge/types'
|
||||
import { Glyph } from './Glyph'
|
||||
import type { StudioToggles } from './MapStudio'
|
||||
import type { ShortcutAction } from './shortcuts'
|
||||
import { useKeyText } from './useBindings'
|
||||
import { IrisLogo } from './IrisLogo'
|
||||
import { PackSwitcher } from './PackSwitcher'
|
||||
|
||||
interface StudioToolbarProps {
|
||||
world: WorldInfo
|
||||
/** The pack the studio is switching to, while the server opens it. */
|
||||
openingPack: StudioPack | null
|
||||
loadPacks(): Promise<StudioPack[]>
|
||||
onOpenPack(pack: StudioPack): void
|
||||
onHelp(): void
|
||||
onSettings(): void
|
||||
onDisconnect(): void
|
||||
}
|
||||
|
||||
/** The window's bar: the pack the studio is connected to, which leads back to the world list, and the studio's own buttons. */
|
||||
export function StudioToolbar({ world, onHelp, onSettings, onDisconnect }: StudioToolbarProps): React.JSX.Element {
|
||||
/** The window's bar: the pack the studio is on, which switches to another, and the studio's own buttons. */
|
||||
export function StudioToolbar({ world, openingPack, loadPacks, onOpenPack, onHelp, onSettings, onDisconnect }: StudioToolbarProps): React.JSX.Element {
|
||||
return (
|
||||
<header className="flex h-10 shrink-0 items-center gap-1 px-1.5">
|
||||
<IrisLogo className="mx-[5px] size-5" />
|
||||
<IconHint hint="Back to the world list">
|
||||
<Button variant="ghost" size="sm" className="h-[30px] max-w-[50%] gap-2 px-2 font-semibold" onClick={onDisconnect}>
|
||||
<span className="flex size-5 shrink-0 items-center justify-center rounded-[5px] bg-primary text-[11px] font-semibold text-primary-foreground uppercase">
|
||||
{world.dimensionName.charAt(0)}
|
||||
</span>
|
||||
<span className="truncate">{world.dimensionName}</span>
|
||||
{world.packVersion !== undefined && <span className="shrink-0 font-normal text-secondary-text tabular-nums">v{world.packVersion}</span>}
|
||||
<Glyph glyph="chevronDown" />
|
||||
</Button>
|
||||
</IconHint>
|
||||
<PackSwitcher world={world} opening={openingPack} loadPacks={loadPacks} onOpenPack={onOpenPack} onDisconnect={onDisconnect} />
|
||||
<div className="flex-1" />
|
||||
<MainToolbarButton action="help" hint="Show every mouse and key shortcut" onClick={onHelp}>
|
||||
<Glyph glyph="help" large />
|
||||
|
||||
Reference in new issue
Block a user