mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Restore the Jigsaw Studio behaviour the port had dropped
Styled text crosses the tool api as ToolText, so menus, items, sidebar and chat show the colours they showed before. give reports a full inventory, containers and machines mark their bay dirty again through containerChanged, teleports and portals reach the studio when they happen, a chunk is loaded when the platform says so, the open tool world is the open studio project to every command that asks, materialization compares whole block states, a block that cannot be turned stays distinct from air, and a piston is checked and marked by the same blocks as before.
This commit is contained in:
1 parent
6879af515e
commit
23c93e6e74
52 files changed
+1683
-509
No files matched your search
@@ -68,6 +68,17 @@ public interface BlockRegistry {
|
||||
*/
|
||||
BlockState rotate(BlockState state, BlockTurn turn);
|
||||
|
||||
/**
|
||||
* Like {@link #rotate}, but says when the block cannot face the way it is turned instead of answering air, so a
|
||||
* caller can tell a block that was dropped from air that stayed air. The default asks {@link #rotate} and takes air
|
||||
* from a block that is not air for a block that was dropped.
|
||||
*/
|
||||
default Optional<BlockState> tryRotate(BlockState state, BlockTurn turn) {
|
||||
BlockState turned = rotate(state, turn);
|
||||
boolean dropped = hasTrait(turned, BlockTrait.AIR) && !hasTrait(state, BlockTrait.AIR);
|
||||
return dropped ? Optional.empty() : Optional.of(turned);
|
||||
}
|
||||
|
||||
/** {@code update} laid over {@code base}: the platform's merge of two states of one block, such as the sides of a
|
||||
* fence, wall or pane; {@code update} when the platform does not merge them.
|
||||
*/
|
||||
|
||||
@@ -10,12 +10,21 @@ import java.util.Optional;
|
||||
* Something that is about to change blocks in a tool world, handed to {@link ToolWorldListener#blockChanging}.
|
||||
*
|
||||
* @param cause what is changing them
|
||||
* @param positions the blocks that change, at least one: the block a player breaks or places, the blocks an explosion
|
||||
* would destroy one at a time, all the blocks a piston would move together
|
||||
* @param positions the blocks a protected area is checked against: the block a player breaks or places, the blocks an
|
||||
* explosion would destroy one at a time, the blocks a piston moves with the blocks they leave and
|
||||
* enter. Empty only when {@code changed} is not, such as a piston that moves nothing.
|
||||
* @param changed the blocks whose content changes, which a tool may want to save; the same as {@code positions}
|
||||
* unless the cause touches other blocks than it is checked against, as a piston does with its own block
|
||||
* @param player the player behind it, when there is one
|
||||
* @param placed the state being placed, for {@link Cause#PLACE} and {@link Cause#BUCKET}; empty when it is not known
|
||||
*/
|
||||
public record BlockChange(Cause cause, List<BlockPosition> positions, Optional<ToolPlayer> player, Optional<BlockState> placed) {
|
||||
public record BlockChange(
|
||||
Cause cause,
|
||||
List<BlockPosition> positions,
|
||||
List<BlockPosition> changed,
|
||||
Optional<ToolPlayer> player,
|
||||
Optional<BlockState> placed
|
||||
) {
|
||||
/** What changes a block. */
|
||||
public enum Cause {
|
||||
/** A player places a block. */
|
||||
@@ -49,10 +58,16 @@ public record BlockChange(Cause cause, List<BlockPosition> positions, Optional<T
|
||||
public BlockChange {
|
||||
Objects.requireNonNull(cause, "cause");
|
||||
positions = List.copyOf(positions);
|
||||
if (positions.isEmpty()) {
|
||||
changed = List.copyOf(changed);
|
||||
if (positions.isEmpty() && changed.isEmpty()) {
|
||||
throw new IllegalArgumentException("A block change names at least one block");
|
||||
}
|
||||
Objects.requireNonNull(player, "player");
|
||||
Objects.requireNonNull(placed, "placed");
|
||||
}
|
||||
|
||||
/** A change that is checked against the blocks it changes. */
|
||||
public BlockChange(Cause cause, List<BlockPosition> positions, Optional<ToolPlayer> player, Optional<BlockState> placed) {
|
||||
this(cause, positions, positions, player, placed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
/**
|
||||
* The sixteen named colours of Minecraft text, in the order of their formatting codes. Every platform shows them the same
|
||||
* way, so a tool picks a colour by name and the host maps it.
|
||||
*/
|
||||
public enum TextColor {
|
||||
BLACK('0'),
|
||||
DARK_BLUE('1'),
|
||||
DARK_GREEN('2'),
|
||||
DARK_AQUA('3'),
|
||||
DARK_RED('4'),
|
||||
DARK_PURPLE('5'),
|
||||
GOLD('6'),
|
||||
GRAY('7'),
|
||||
DARK_GRAY('8'),
|
||||
BLUE('9'),
|
||||
GREEN('a'),
|
||||
AQUA('b'),
|
||||
RED('c'),
|
||||
LIGHT_PURPLE('d'),
|
||||
YELLOW('e'),
|
||||
WHITE('f');
|
||||
|
||||
private final char code;
|
||||
|
||||
TextColor(char code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/** The character that follows the formatting sign in Minecraft's legacy codes, such as {@code 'a'} for green. */
|
||||
public char code() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
@@ -10,13 +10,16 @@ import java.util.Objects;
|
||||
* data on Bukkit and as custom data on modded platforms, and hands it back through {@link ToolPlayer#held()} and
|
||||
* {@link ToolUse#item()}. A tool tells its own items apart by their payload.
|
||||
*
|
||||
* <p>The host shows the name and the lore the way the platform shows text a plugin sets on an item, in the colours of
|
||||
* the {@link ToolText}. An item the host reads back from a player carries its name and lore as plain text.
|
||||
*
|
||||
* @param item the platform's item, such as {@code minecraft:stick}
|
||||
* @param name the display name as plain text; empty keeps the item's own name
|
||||
* @param lore the lines under the name, plain text
|
||||
* @param name the display name; empty keeps the item's own name
|
||||
* @param lore the lines under the name
|
||||
* @param glowing whether the item shows the enchantment glint
|
||||
* @param payload what the tool keeps on the item; empty for nothing
|
||||
*/
|
||||
public record ToolItem(ResourceId item, String name, List<String> lore, boolean glowing, String payload) {
|
||||
public record ToolItem(ResourceId item, ToolText name, List<ToolText> lore, boolean glowing, String payload) {
|
||||
public ToolItem {
|
||||
Objects.requireNonNull(item, "item");
|
||||
Objects.requireNonNull(name, "name");
|
||||
|
||||
@@ -8,14 +8,14 @@ import java.util.Objects;
|
||||
* A chest-style menu a tool opens for a player with {@link ToolPlayer#openMenu}. Menus are the host's own inventory
|
||||
* windows, so they work the same on every platform; a tool that wants another page opens a new menu from a click.
|
||||
*
|
||||
* @param title the title as plain text; the host shortens it to what the window can show
|
||||
* @param title the title; the host shortens it to what the window can show
|
||||
* @param rows the number of rows, 1 to {@value #MAX_ROWS}
|
||||
* @param buttons the slots that do something; the others show a filler. No slot is used twice and every slot is inside
|
||||
* the rows.
|
||||
* @param onClose called on the player's thread when the menu closes for any reason: the player closed it, the world
|
||||
* closed, or the tool closed or replaced it. Null stands for nothing to do.
|
||||
*/
|
||||
public record ToolMenu(String title, int rows, List<MenuButton> buttons, Runnable onClose) {
|
||||
public record ToolMenu(ToolText title, int rows, List<MenuButton> buttons, Runnable onClose) {
|
||||
public static final int MAX_ROWS = 6;
|
||||
public static final int SLOTS_PER_ROW = 9;
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ public interface ToolPlayer {
|
||||
/** The player's name as the platform shows it. */
|
||||
String name();
|
||||
|
||||
/** The player as a command sender; its messages are plain text, delivered on the thread the platform needs. */
|
||||
/** The player as a command sender; its messages are plain text, delivered on the thread the platform needs. For styled text use {@link #message}. */
|
||||
Sender sender();
|
||||
|
||||
/** The tool world the player stands in; empty when they stand in another world, even another pack's tool world. */
|
||||
@@ -54,17 +54,20 @@ public interface ToolPlayer {
|
||||
*/
|
||||
void creative(boolean on);
|
||||
|
||||
/** Sends {@code text} to the player's chat, in its colours. */
|
||||
void message(ToolText text);
|
||||
|
||||
/** Shows {@code text} above the hotbar. */
|
||||
void actionBar(String text);
|
||||
void actionBar(ToolText text);
|
||||
|
||||
/** Plays {@code sound}, such as {@code minecraft:block.note_block.pling}, to this player only, where they stand. */
|
||||
void sound(ResourceId sound, float volume, float pitch);
|
||||
|
||||
/**
|
||||
* Shows a sidebar with {@code title} and {@code lines}, top to bottom, replacing the one this player has from a tool.
|
||||
* Text is plain: the host removes formatting codes. The sidebar goes away when the player leaves the tool world.
|
||||
* An empty title keeps the platform's own sidebar title. The sidebar goes away when the player leaves the tool world.
|
||||
*/
|
||||
void sidebar(String title, List<String> lines);
|
||||
void sidebar(ToolText title, List<ToolText> lines);
|
||||
|
||||
/** Removes the sidebar {@link #sidebar} showed. */
|
||||
void hideSidebar();
|
||||
@@ -72,8 +75,12 @@ public interface ToolPlayer {
|
||||
/** Shows dust particles to this player only. Positions are in the world the player stands in. */
|
||||
void particles(List<Particle> dust);
|
||||
|
||||
/** Puts {@code item} in the player's inventory, in the first free slot. */
|
||||
void give(ToolItem item);
|
||||
/**
|
||||
* Puts {@code item} in the player's inventory, in the first free slot, and returns whether it fitted. An item that did
|
||||
* not fit is not given and not dropped. Called off the player's own thread, the item is given later and the answer
|
||||
* is true.
|
||||
*/
|
||||
boolean give(ToolItem item);
|
||||
|
||||
/** The tool item in the player's main hand; empty when the held item is not one, which is when it carries no payload. */
|
||||
Optional<ToolItem> held();
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Styled text a tool shows a player: item names and lore, menu titles, sidebars, the action bar and chat. It is a list of
|
||||
* spans, each a piece of text with its own colour and style, so the text carries no formatting codes and a platform shows
|
||||
* it with its own text components. A value is immutable.
|
||||
*
|
||||
* <p>Build it with {@link #builder()}, or use {@link #plain(String)} and {@link #colored(TextColor, String)} for one span.
|
||||
* Text of a span is shown as it is: a formatting sign in it is just a character.
|
||||
*
|
||||
* @param spans the pieces in order
|
||||
*/
|
||||
public record ToolText(List<Span> spans) {
|
||||
/** Nothing at all. */
|
||||
public static final ToolText EMPTY = new ToolText(List.of());
|
||||
|
||||
public ToolText {
|
||||
spans = List.copyOf(spans);
|
||||
}
|
||||
|
||||
/**
|
||||
* A piece of text in one style.
|
||||
*
|
||||
* @param text what is shown, never empty
|
||||
* @param color the colour; null leaves the platform's default colour for that kind of text
|
||||
* @param bold whether the text is bold
|
||||
* @param italic whether the text is italic
|
||||
* @param strikethrough whether the text has a line through it
|
||||
*/
|
||||
public record Span(String text, TextColor color, boolean bold, boolean italic, boolean strikethrough) {
|
||||
public Span {
|
||||
Objects.requireNonNull(text, "text");
|
||||
if (text.isEmpty()) {
|
||||
throw new IllegalArgumentException("A span has text");
|
||||
}
|
||||
}
|
||||
|
||||
/** A span in {@code color} and no other style. */
|
||||
public static Span of(TextColor color, String text) {
|
||||
return new Span(text, color, false, false, false);
|
||||
}
|
||||
|
||||
boolean sameStyle(Span other) {
|
||||
return color == other.color && bold == other.bold && italic == other.italic
|
||||
&& strikethrough == other.strikethrough;
|
||||
}
|
||||
}
|
||||
|
||||
/** One span in the platform's default colour. */
|
||||
public static ToolText plain(String text) {
|
||||
return builder().append(text).build();
|
||||
}
|
||||
|
||||
/** One span in {@code color}. */
|
||||
public static ToolText colored(TextColor color, String text) {
|
||||
return builder().append(color, text).build();
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/** The text without any style, as one string. */
|
||||
public String plainText() {
|
||||
StringBuilder text = new StringBuilder();
|
||||
for (Span span : spans) {
|
||||
text.append(span.text());
|
||||
}
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return spans.isEmpty();
|
||||
}
|
||||
|
||||
/** Builds a {@link ToolText} span by span. Empty text is skipped, and neighbours of one style become one span. */
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public static final class Builder {
|
||||
private final List<Span> spans = new ArrayList<>();
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
|
||||
/** Adds {@code text} in the platform's default colour; empty text adds nothing. */
|
||||
public Builder append(String text) {
|
||||
Objects.requireNonNull(text, "text");
|
||||
return text.isEmpty() ? this : append(new Span(text, null, false, false, false));
|
||||
}
|
||||
|
||||
/** Adds {@code text} in {@code color}; empty text adds nothing. */
|
||||
public Builder append(TextColor color, String text) {
|
||||
Objects.requireNonNull(color, "color");
|
||||
Objects.requireNonNull(text, "text");
|
||||
return text.isEmpty() ? this : append(Span.of(color, text));
|
||||
}
|
||||
|
||||
/** Adds {@code span}, joined to the span before it when they share a style. */
|
||||
public Builder append(Span span) {
|
||||
Objects.requireNonNull(span, "span");
|
||||
if (!spans.isEmpty() && spans.getLast().sameStyle(span)) {
|
||||
Span last = spans.removeLast();
|
||||
spans.add(new Span(last.text() + span.text(), last.color(), last.bold(), last.italic(),
|
||||
last.strikethrough()));
|
||||
return this;
|
||||
}
|
||||
spans.add(span);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder append(ToolText text) {
|
||||
Objects.requireNonNull(text, "text");
|
||||
text.spans().forEach(this::append);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder black(String text) {
|
||||
return append(TextColor.BLACK, text);
|
||||
}
|
||||
|
||||
public Builder darkBlue(String text) {
|
||||
return append(TextColor.DARK_BLUE, text);
|
||||
}
|
||||
|
||||
public Builder darkGreen(String text) {
|
||||
return append(TextColor.DARK_GREEN, text);
|
||||
}
|
||||
|
||||
public Builder darkAqua(String text) {
|
||||
return append(TextColor.DARK_AQUA, text);
|
||||
}
|
||||
|
||||
public Builder darkRed(String text) {
|
||||
return append(TextColor.DARK_RED, text);
|
||||
}
|
||||
|
||||
public Builder darkPurple(String text) {
|
||||
return append(TextColor.DARK_PURPLE, text);
|
||||
}
|
||||
|
||||
public Builder gold(String text) {
|
||||
return append(TextColor.GOLD, text);
|
||||
}
|
||||
|
||||
public Builder gray(String text) {
|
||||
return append(TextColor.GRAY, text);
|
||||
}
|
||||
|
||||
public Builder darkGray(String text) {
|
||||
return append(TextColor.DARK_GRAY, text);
|
||||
}
|
||||
|
||||
public Builder blue(String text) {
|
||||
return append(TextColor.BLUE, text);
|
||||
}
|
||||
|
||||
public Builder green(String text) {
|
||||
return append(TextColor.GREEN, text);
|
||||
}
|
||||
|
||||
public Builder aqua(String text) {
|
||||
return append(TextColor.AQUA, text);
|
||||
}
|
||||
|
||||
public Builder red(String text) {
|
||||
return append(TextColor.RED, text);
|
||||
}
|
||||
|
||||
public Builder lightPurple(String text) {
|
||||
return append(TextColor.LIGHT_PURPLE, text);
|
||||
}
|
||||
|
||||
public Builder yellow(String text) {
|
||||
return append(TextColor.YELLOW, text);
|
||||
}
|
||||
|
||||
public Builder white(String text) {
|
||||
return append(TextColor.WHITE, text);
|
||||
}
|
||||
|
||||
public ToolText build() {
|
||||
return new ToolText(spans);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,9 @@ public interface ToolWorld {
|
||||
*/
|
||||
void setBlock(BlockPosition at, BlockState state, Optional<BlockEntityData> data);
|
||||
|
||||
/** Whether the platform has {@code chunk} loaded right now. Safe from any thread; a tool treats it as a hint. */
|
||||
boolean loaded(ChunkPos chunk);
|
||||
|
||||
/**
|
||||
* Runs the painter again for {@code chunks}, writes the differences into the live chunks and sends them to the
|
||||
* players. Block entities the painter sets replace those of the chunk. The future completes on a host thread when
|
||||
|
||||
@@ -23,6 +23,17 @@ public interface ToolWorldListener {
|
||||
default void left(ToolPlayer player) {
|
||||
}
|
||||
|
||||
/**
|
||||
* A player arrived at a position of the world by other means than walking: they joined the server in it, changed
|
||||
* world into it, respawned in it, or were teleported into it or inside it by anyone, such as a command, another
|
||||
* plugin or a portal. It is told at the time of the teleport, with the position they arrive at, and for a player
|
||||
* coming in after {@link #entered}. A player teleported out only hears {@link #left}. The default treats it as
|
||||
* {@link #moved}.
|
||||
*/
|
||||
default void teleported(ToolPlayer player, BlockPosition to) {
|
||||
moved(player, to);
|
||||
}
|
||||
|
||||
/** A player moved to another block of the world; not called for moves inside one block. */
|
||||
default void moved(ToolPlayer player, BlockPosition to) {
|
||||
}
|
||||
@@ -49,6 +60,18 @@ public interface ToolWorldListener {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* What is inside a container or a machine changed without a block changing: a player closed a container block's
|
||||
* inventory, a furnace started smelting, a brewing stand started brewing. It is told after the fact and cannot be
|
||||
* cancelled; {@link #blockChanging} with {@link BlockChange.Cause#CONTAINER} or {@link BlockChange.Cause#MACHINE}
|
||||
* decides the changes that can be.
|
||||
*
|
||||
* @param player the player behind it, when there is one
|
||||
* @param at the block of the container or machine
|
||||
*/
|
||||
default void containerChanged(Optional<ToolPlayer> player, BlockPosition at) {
|
||||
}
|
||||
|
||||
/** A chunk of the world finished loading, painted or read back. */
|
||||
default void chunkLoaded(int chunkX, int chunkZ) {
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ToolWorldRecordsTest {
|
||||
private static final ResourceId STICK = ResourceId.parse("minecraft:stick");
|
||||
private static final ToolItem ICON = new ToolItem(STICK, "Tool", List.of("line"), false, "");
|
||||
private static final ToolItem ICON = new ToolItem(STICK, ToolText.plain("Tool"), List.of(ToolText.plain("line")), false, "");
|
||||
|
||||
@Test
|
||||
void aChunkIsTheBlockCoordinatesDividedBySixteenRoundedDown() {
|
||||
@@ -48,34 +48,74 @@ class ToolWorldRecordsTest {
|
||||
|
||||
@Test
|
||||
void aToolItemKeepsItsOwnCopyOfTheLore() {
|
||||
var lore = new ArrayList<>(List.of("one"));
|
||||
var item = new ToolItem(STICK, "Name", lore, true, "payload");
|
||||
lore.add("two");
|
||||
var lore = new ArrayList<>(List.of(ToolText.plain("one")));
|
||||
var item = new ToolItem(STICK, ToolText.plain("Name"), lore, true, "payload");
|
||||
lore.add(ToolText.plain("two"));
|
||||
|
||||
assertEquals(List.of("one"), item.lore());
|
||||
assertThrows(UnsupportedOperationException.class, () -> item.lore().add("three"));
|
||||
assertThrows(NullPointerException.class, () -> new ToolItem(STICK, "Name", List.of(), false, null));
|
||||
assertEquals(List.of(ToolText.plain("one")), item.lore());
|
||||
assertThrows(UnsupportedOperationException.class, () -> item.lore().add(ToolText.plain("three")));
|
||||
assertThrows(NullPointerException.class, () -> new ToolItem(STICK, ToolText.plain("Name"), List.of(), false, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void textIsSpansOfColourAndStyleAndAdjoiningSpansOfOneStyleJoin() {
|
||||
var text = ToolText.builder().gray("Capacity: ").gray("16x16x16").white(" now").append(" plain").build();
|
||||
|
||||
assertEquals(3, text.spans().size());
|
||||
assertEquals("Capacity: 16x16x16 now plain", text.plainText());
|
||||
assertEquals(new ToolText.Span("Capacity: 16x16x16", TextColor.GRAY, false, false, false), text.spans().getFirst());
|
||||
assertNull(text.spans().getLast().color());
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyTextAddsNoSpanAndPlainTextOfNothingIsEmpty() {
|
||||
var text = ToolText.builder().red("").append("").build();
|
||||
|
||||
assertTrue(text.isEmpty());
|
||||
assertEquals("", text.plainText());
|
||||
assertEquals(ToolText.EMPTY, text);
|
||||
assertThrows(IllegalArgumentException.class, () -> new ToolText.Span("", null, false, false, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void textKeepsItsOwnCopyOfTheSpans() {
|
||||
var spans = new ArrayList<>(List.of(ToolText.Span.of(TextColor.RED, "a")));
|
||||
var text = new ToolText(spans);
|
||||
spans.add(ToolText.Span.of(TextColor.RED, "b"));
|
||||
|
||||
assertEquals("a", text.plainText());
|
||||
assertThrows(UnsupportedOperationException.class, () -> text.spans().add(ToolText.Span.of(TextColor.RED, "c")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theSixteenColoursHaveTheirFormattingCodes() {
|
||||
var codes = new StringBuilder();
|
||||
for (TextColor color : TextColor.values()) {
|
||||
codes.append(color.code());
|
||||
}
|
||||
|
||||
assertEquals("0123456789abcdef", codes.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMenuHasOneToSixRowsAndEveryButtonInsideThemOnItsOwnSlot() {
|
||||
var button = new MenuButton(17, ICON, click -> { });
|
||||
|
||||
assertEquals(1, new ToolMenu("t", 1, List.of(new MenuButton(8, ICON, click -> { })), null).rows());
|
||||
assertEquals(2, new ToolMenu("t", 2, List.of(button), null).rows());
|
||||
assertThrows(IllegalArgumentException.class, () -> new ToolMenu("t", 0, List.of(), null));
|
||||
assertThrows(IllegalArgumentException.class, () -> new ToolMenu("t", 7, List.of(), null));
|
||||
assertThrows(IllegalArgumentException.class, () -> new ToolMenu("t", 1, List.of(button), null), "slot 17 is in the second row");
|
||||
assertEquals(1, new ToolMenu(ToolText.plain("t"),1, List.of(new MenuButton(8, ICON, click -> { })), null).rows());
|
||||
assertEquals(2, new ToolMenu(ToolText.plain("t"),2, List.of(button), null).rows());
|
||||
assertThrows(IllegalArgumentException.class, () -> new ToolMenu(ToolText.plain("t"),0, List.of(), null));
|
||||
assertThrows(IllegalArgumentException.class, () -> new ToolMenu(ToolText.plain("t"),7, List.of(), null));
|
||||
assertThrows(IllegalArgumentException.class, () -> new ToolMenu(ToolText.plain("t"),1, List.of(button), null), "slot 17 is in the second row");
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> new ToolMenu("t", 2, List.of(button, new MenuButton(17, ICON, click -> { })), null));
|
||||
() -> new ToolMenu(ToolText.plain("t"),2, List.of(button, new MenuButton(17, ICON, click -> { })), null));
|
||||
assertThrows(IllegalArgumentException.class, () -> new MenuButton(-1, ICON, click -> { }));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMenuWithoutAnOnCloseHasOneThatDoesNothing() {
|
||||
var closed = new AtomicBoolean();
|
||||
var withHandler = new ToolMenu("t", 1, List.of(), () -> closed.set(true));
|
||||
var without = new ToolMenu("t", 1, List.of(), null);
|
||||
var withHandler = new ToolMenu(ToolText.plain("t"),1, List.of(), () -> closed.set(true));
|
||||
var without = new ToolMenu(ToolText.plain("t"),1, List.of(), null);
|
||||
|
||||
without.onClose().run();
|
||||
assertFalse(closed.get());
|
||||
@@ -94,6 +134,36 @@ class ToolWorldRecordsTest {
|
||||
() -> new BlockChange(BlockChange.Cause.BREAK, List.of(), Optional.empty(), Optional.empty()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whatAChangeChangesIsWhatItIsCheckedAgainstUnlessSaidOtherwise() {
|
||||
var position = new BlockPosition(1, 2, 3);
|
||||
var aside = new BlockPosition(4, 5, 6);
|
||||
|
||||
assertEquals(List.of(position), new BlockChange(BlockChange.Cause.PLACE, List.of(position), Optional.empty(), Optional.empty()).changed());
|
||||
var piston = new BlockChange(BlockChange.Cause.PISTON, List.of(), List.of(aside), Optional.empty(), Optional.empty());
|
||||
assertEquals(List.of(), piston.positions());
|
||||
assertEquals(List.of(aside), piston.changed());
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> new BlockChange(BlockChange.Cause.PISTON, List.of(), List.of(), Optional.empty(), Optional.empty()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aTeleportIsAMoveToAListenerThatDoesNotCareAndNothingToCareAboutInContainers() {
|
||||
var moves = new ArrayList<BlockPosition>();
|
||||
var listener = new ToolWorldListener() {
|
||||
@Override
|
||||
public void moved(ToolPlayer player, BlockPosition to) {
|
||||
moves.add(to);
|
||||
}
|
||||
};
|
||||
var to = new BlockPosition(7, 8, 9);
|
||||
|
||||
listener.teleported(null, to);
|
||||
listener.containerChanged(Optional.empty(), to);
|
||||
|
||||
assertEquals(List.of(to), moves);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aListenerThatImplementsNothingFreezesTheWorldAndAllowsEverythingElse() {
|
||||
var listener = new ToolWorldListener() { };
|
||||
|
||||
Reference in new issue
Block a user