Let a tool world refuse to close and hear that it is closing, and tell middle clicks apart

ToolWorldListener gets closeRefusal() and closing(by) so a tool world that holds the
studio slot can keep its owner's work safe. MenuClick tells a middle click from a
left or right one.
This commit is contained in:
piexel committed 2026-11-08 16:52:09 +01:00
1 parent 3691611c6c
commit 18c6b189d1
8 files changed
+43 -412

No files matched your search

@@ -3,12 +3,18 @@ package com.volmit.tooling.world;
import java.util.Objects;
/**
* A click on a {@link MenuButton}. A left click has {@code right} false; holding shift is {@code shift}.
* A click on a {@link MenuButton}. A left click has {@code right} false; holding shift is {@code shift}; the middle
* mouse button is {@code middle}, with {@code right} and {@code shift} false.
*
* @param player the player who clicked
*/
public record MenuClick(ToolPlayer player, boolean right, boolean shift) {
public record MenuClick(ToolPlayer player, boolean right, boolean shift, boolean middle) {
public MenuClick {
Objects.requireNonNull(player, "player");
}
/** A left or right click, with or without shift. */
public MenuClick(ToolPlayer player, boolean right, boolean shift) {
this(player, right, shift, false);
}
}
@@ -2,6 +2,7 @@ package com.volmit.tooling.world;
import com.volmit.geometry.BlockPosition;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
/**
* What a tool hears about its world. Every method has a default that does nothing, so a tool implements only the ones
@@ -52,6 +53,26 @@ public interface ToolWorldListener {
default void chunkLoaded(int chunkX, int chunkZ) {
}
/**
* Whether the host may close the world now, asked before the host closes it for any reason other than the tool's own
* {@link ToolWorld#close()}: another studio opening, the studio close command, the server stopping. A tool world takes
* the pack's studio slot, so those paths go through the world. Return a message for the user to veto the close, such
* as "autosave is still running"; empty lets it happen. Asked on any thread, and must return quickly.
*/
default Optional<String> closeRefusal() {
return Optional.empty();
}
/**
* Another studio wants this world's slot. Get the world ready to close, such as finishing a save, and complete the
* future when it is ready; the host then closes the world. Complete it exceptionally to refuse, with a message for the
* user. {@code by} is the player who asked, empty when the console did. Called on any thread; the future may complete
* on any thread.
*/
default CompletableFuture<Void> closing(Optional<ToolPlayer> by) {
return CompletableFuture.completedFuture(null);
}
/** The world was unloaded and is gone; nothing can be done with it any more. */
default void closed() {
}
@@ -37,6 +37,15 @@ class ToolWorldRecordsTest {
assertThrows(NullPointerException.class, () -> new ToolWorldSpec("name", painter, null));
}
@Test
void aListenerThatSaysNothingAboutClosingLetsTheWorldCloseAtOnce() {
var listener = new ToolWorldListener() { };
assertEquals(java.util.Optional.empty(), listener.closeRefusal());
assertEquals(true, listener.closing(java.util.Optional.empty()).isDone());
assertEquals(false, listener.closing(java.util.Optional.empty()).isCompletedExceptionally());
}
@Test
void aToolItemKeepsItsOwnCopyOfTheLore() {
var lore = new ArrayList<>(List.of("one"));