mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-29 09:15:27 +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
+12
-12
@@ -20,8 +20,8 @@ package art.arcane.iris.core.toolworld;
|
||||
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.tooling.world.ToolItem;
|
||||
import com.volmit.tooling.world.ToolText;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
@@ -43,7 +43,11 @@ final class BukkitToolItems {
|
||||
private BukkitToolItems() {
|
||||
}
|
||||
|
||||
/** A new stack of {@code item}. */
|
||||
/**
|
||||
* A new stack of {@code item}. The name and lore are set as legacy strings, the way a plugin sets text on an item
|
||||
* with the platform's own calls, so the stack shows what such a stack always showed.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
static ItemStack create(ToolItem item) {
|
||||
Material material = Material.matchMaterial(item.item().toString());
|
||||
if (material == null || !material.isItem()) {
|
||||
@@ -55,10 +59,10 @@ final class BukkitToolItems {
|
||||
throw new IllegalStateException("The item " + item.item() + " cannot carry a name or a payload");
|
||||
}
|
||||
if (!item.name().isEmpty()) {
|
||||
meta.displayName(plain(item.name()));
|
||||
meta.setDisplayName(BukkitToolText.legacy(item.name()));
|
||||
}
|
||||
if (!item.lore().isEmpty()) {
|
||||
meta.lore(item.lore().stream().map(BukkitToolItems::plain).toList());
|
||||
meta.setLore(item.lore().stream().map(BukkitToolText::legacy).toList());
|
||||
}
|
||||
if (item.glowing()) {
|
||||
meta.setEnchantmentGlintOverride(true);
|
||||
@@ -86,22 +90,18 @@ final class BukkitToolItems {
|
||||
}
|
||||
return Optional.of(new ToolItem(
|
||||
ResourceId.parse(stack.getType().getKey().toString()),
|
||||
meta.hasDisplayName() ? plainText(meta.displayName()) : "",
|
||||
meta.hasDisplayName() ? plainText(meta.displayName()) : ToolText.EMPTY,
|
||||
meta.hasLore() ? lore(meta) : List.of(),
|
||||
Boolean.TRUE.equals(meta.getEnchantmentGlintOverride()),
|
||||
payload));
|
||||
}
|
||||
|
||||
private static List<String> lore(ItemMeta meta) {
|
||||
private static List<ToolText> lore(ItemMeta meta) {
|
||||
List<Component> lines = meta.lore();
|
||||
return lines == null ? List.of() : lines.stream().map(BukkitToolItems::plainText).toList();
|
||||
}
|
||||
|
||||
private static Component plain(String text) {
|
||||
return Component.text(text).decoration(TextDecoration.ITALIC, false);
|
||||
}
|
||||
|
||||
private static String plainText(Component component) {
|
||||
return component == null ? "" : PlainTextComponentSerializer.plainText().serialize(component);
|
||||
private static ToolText plainText(Component component) {
|
||||
return component == null ? ToolText.EMPTY : ToolText.plain(PlainTextComponentSerializer.plainText().serialize(component));
|
||||
}
|
||||
}
|
||||
+11
-4
@@ -30,6 +30,7 @@ import com.volmit.tooling.world.MenuButton;
|
||||
import com.volmit.tooling.world.MenuClick;
|
||||
import com.volmit.tooling.world.ToolItem;
|
||||
import com.volmit.tooling.world.ToolMenu;
|
||||
import com.volmit.tooling.world.ToolText;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@@ -129,9 +130,9 @@ final class BukkitToolMenus {
|
||||
}
|
||||
UIElement element = new UIElement("slot-" + button.slot())
|
||||
.setMaterial(new MaterialBlock(material))
|
||||
.setName(icon.name())
|
||||
.setName(BukkitToolText.legacy(icon.name()))
|
||||
.setEnchanted(icon.glowing());
|
||||
icon.lore().forEach(element::addLore);
|
||||
icon.lore().forEach(line -> element.addLore(BukkitToolText.legacy(line)));
|
||||
element.onLeftClick(clicked -> click(viewer, button, false, false));
|
||||
element.onRightClick(clicked -> click(viewer, button, true, false));
|
||||
element.onShiftLeftClick(clicked -> click(viewer, button, false, true));
|
||||
@@ -151,8 +152,14 @@ final class BukkitToolMenus {
|
||||
return slot % SLOTS_PER_ROW - CENTER_SLOT_OFFSET;
|
||||
}
|
||||
|
||||
static String shortened(String title) {
|
||||
return title.length() > MAX_TITLE_LENGTH ? title.substring(0, MAX_TITLE_LENGTH) : title;
|
||||
/** The title in the legacy codes the window takes, cut to what it can show without leaving half a code. */
|
||||
static String shortened(ToolText title) {
|
||||
String legacy = BukkitToolText.legacy(title);
|
||||
if (legacy.length() <= MAX_TITLE_LENGTH) {
|
||||
return legacy;
|
||||
}
|
||||
String cut = legacy.substring(0, MAX_TITLE_LENGTH);
|
||||
return cut.endsWith("§") ? cut.substring(0, MAX_TITLE_LENGTH - 1) : cut;
|
||||
}
|
||||
|
||||
private static void runGuarded(Runnable task, String what) {
|
||||
|
||||
+17
-7
@@ -23,6 +23,8 @@ import art.arcane.iris.core.service.BukkitToolingSender;
|
||||
import art.arcane.iris.platform.bukkit.BukkitPlatform;
|
||||
import art.arcane.iris.spi.IrisServices;
|
||||
import art.arcane.iris.util.common.scheduling.J;
|
||||
import art.arcane.volmlib.util.plugin.ComponentMessenger;
|
||||
import art.arcane.volmlib.util.plugin.ComponentText;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.tooling.Sender;
|
||||
@@ -30,6 +32,7 @@ import com.volmit.tooling.world.Particle;
|
||||
import com.volmit.tooling.world.ToolItem;
|
||||
import com.volmit.tooling.world.ToolMenu;
|
||||
import com.volmit.tooling.world.ToolPlayer;
|
||||
import com.volmit.tooling.world.ToolText;
|
||||
import com.volmit.tooling.world.ToolWorld;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Color;
|
||||
@@ -140,8 +143,13 @@ final class BukkitToolPlayer implements ToolPlayer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionBar(String text) {
|
||||
J.runEntity(player, () -> player.sendActionBar(text));
|
||||
public void message(ToolText text) {
|
||||
J.runEntity(player, () -> ComponentMessenger.send(player, ComponentText.component(BukkitToolText.component(text))));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionBar(ToolText text) {
|
||||
J.runEntity(player, () -> player.sendActionBar(BukkitToolText.component(text)));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -150,7 +158,7 @@ final class BukkitToolPlayer implements ToolPlayer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sidebar(String title, List<String> lines) {
|
||||
public void sidebar(ToolText title, List<ToolText> lines) {
|
||||
BoardSVC board = IrisServices.getOrNull(BoardSVC.class);
|
||||
if (board == null) {
|
||||
return;
|
||||
@@ -174,11 +182,13 @@ final class BukkitToolPlayer implements ToolPlayer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void give(ToolItem item) {
|
||||
public boolean give(ToolItem item) {
|
||||
ItemStack stack = BukkitToolItems.create(item);
|
||||
J.runEntity(player, () -> player.getInventory().addItem(stack)
|
||||
.values()
|
||||
.forEach(leftover -> player.getWorld().dropItem(player.getLocation(), leftover)));
|
||||
if (J.isOwnedByCurrentRegion(player)) {
|
||||
return player.getInventory().addItem(stack).isEmpty();
|
||||
}
|
||||
J.runEntity(player, () -> player.getInventory().addItem(stack));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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.core.toolworld;
|
||||
|
||||
import com.volmit.tooling.world.TextColor;
|
||||
import com.volmit.tooling.world.ToolText;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** {@link ToolText} as the text Bukkit shows: an Adventure component, or the legacy string the platform's older calls take. */
|
||||
final class BukkitToolText {
|
||||
private static final char SECTION = '§';
|
||||
private static final Map<TextColor, NamedTextColor> COLORS = new EnumMap<>(Map.ofEntries(
|
||||
Map.entry(TextColor.BLACK, NamedTextColor.BLACK),
|
||||
Map.entry(TextColor.DARK_BLUE, NamedTextColor.DARK_BLUE),
|
||||
Map.entry(TextColor.DARK_GREEN, NamedTextColor.DARK_GREEN),
|
||||
Map.entry(TextColor.DARK_AQUA, NamedTextColor.DARK_AQUA),
|
||||
Map.entry(TextColor.DARK_RED, NamedTextColor.DARK_RED),
|
||||
Map.entry(TextColor.DARK_PURPLE, NamedTextColor.DARK_PURPLE),
|
||||
Map.entry(TextColor.GOLD, NamedTextColor.GOLD),
|
||||
Map.entry(TextColor.GRAY, NamedTextColor.GRAY),
|
||||
Map.entry(TextColor.DARK_GRAY, NamedTextColor.DARK_GRAY),
|
||||
Map.entry(TextColor.BLUE, NamedTextColor.BLUE),
|
||||
Map.entry(TextColor.GREEN, NamedTextColor.GREEN),
|
||||
Map.entry(TextColor.AQUA, NamedTextColor.AQUA),
|
||||
Map.entry(TextColor.RED, NamedTextColor.RED),
|
||||
Map.entry(TextColor.LIGHT_PURPLE, NamedTextColor.LIGHT_PURPLE),
|
||||
Map.entry(TextColor.YELLOW, NamedTextColor.YELLOW),
|
||||
Map.entry(TextColor.WHITE, NamedTextColor.WHITE)));
|
||||
|
||||
private BukkitToolText() {
|
||||
}
|
||||
|
||||
static Component component(ToolText text) {
|
||||
TextComponent.Builder whole = Component.text();
|
||||
for (ToolText.Span span : text.spans()) {
|
||||
Component piece = Component.text(span.text());
|
||||
if (span.color() != null) {
|
||||
piece = piece.color(COLORS.get(span.color()));
|
||||
}
|
||||
piece = piece
|
||||
.decoration(TextDecoration.BOLD, span.bold() ? TextDecoration.State.TRUE : TextDecoration.State.NOT_SET)
|
||||
.decoration(TextDecoration.ITALIC, span.italic() ? TextDecoration.State.TRUE : TextDecoration.State.NOT_SET)
|
||||
.decoration(TextDecoration.STRIKETHROUGH,
|
||||
span.strikethrough() ? TextDecoration.State.TRUE : TextDecoration.State.NOT_SET);
|
||||
whole.append(piece);
|
||||
}
|
||||
return whole.build();
|
||||
}
|
||||
|
||||
/** The legacy string of {@code text}: formatting signs for the colour and style of each span, a reset between uncoloured ones. */
|
||||
static String legacy(ToolText text) {
|
||||
StringBuilder legacy = new StringBuilder();
|
||||
for (ToolText.Span span : text.spans()) {
|
||||
if (span.color() != null) {
|
||||
legacy.append(SECTION).append(span.color().code());
|
||||
} else if (!legacy.isEmpty()) {
|
||||
legacy.append(SECTION).append('r');
|
||||
}
|
||||
if (span.bold()) {
|
||||
legacy.append(SECTION).append('l');
|
||||
}
|
||||
if (span.italic()) {
|
||||
legacy.append(SECTION).append('o');
|
||||
}
|
||||
if (span.strikethrough()) {
|
||||
legacy.append(SECTION).append('m');
|
||||
}
|
||||
legacy.append(span.text());
|
||||
}
|
||||
return legacy.toString();
|
||||
}
|
||||
}
|
||||
+21
-1
@@ -19,6 +19,7 @@
|
||||
package art.arcane.iris.core.toolworld;
|
||||
|
||||
import art.arcane.iris.core.nms.INMS;
|
||||
import art.arcane.iris.core.project.IrisProject;
|
||||
import art.arcane.iris.core.runtime.InPlaceChunkRegenerator;
|
||||
import art.arcane.iris.core.runtime.StudioOpenCoordinator;
|
||||
import art.arcane.iris.core.runtime.StudioPainterSlots;
|
||||
@@ -42,6 +43,7 @@ import org.bukkit.Chunk;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -75,6 +77,7 @@ final class BukkitToolWorld implements ToolWorld, StudioSlotOccupant {
|
||||
private final AtomicBoolean closing = new AtomicBoolean();
|
||||
private final AtomicBoolean gone = new AtomicBoolean();
|
||||
private final CompletableFuture<Void> closed = new CompletableFuture<>();
|
||||
private final IrisProject project;
|
||||
private volatile World world;
|
||||
|
||||
BukkitToolWorld(
|
||||
@@ -95,6 +98,18 @@ final class BukkitToolWorld implements ToolWorld, StudioSlotOccupant {
|
||||
this.pending = pending;
|
||||
this.registry = registry;
|
||||
this.players = players;
|
||||
this.project = new IrisProject(new File(workspaceFolder(), packName));
|
||||
}
|
||||
|
||||
/** The studio project this world is, as the studio service shows the open studio to its commands. */
|
||||
@Override
|
||||
public IrisProject project() {
|
||||
return project;
|
||||
}
|
||||
|
||||
private static File workspaceFolder() {
|
||||
StudioSVC studios = IrisServices.getOrNull(StudioSVC.class);
|
||||
return studios == null ? new File(".") : studios.getWorkspaceFolder();
|
||||
}
|
||||
|
||||
String packName() {
|
||||
@@ -199,6 +214,11 @@ final class BukkitToolWorld implements ToolWorld, StudioSlotOccupant {
|
||||
data.ifPresent(entity -> BukkitToolBlocks.applyBlockEntity(block, entity));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean loaded(ChunkPos chunk) {
|
||||
return bukkit().isChunkLoaded(chunk.x(), chunk.z());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> repaint(Collection<ChunkPos> chunks) {
|
||||
if (isClosed()) {
|
||||
@@ -214,7 +234,7 @@ final class BukkitToolWorld implements ToolWorld, StudioSlotOccupant {
|
||||
return closed;
|
||||
}
|
||||
sendPlayersBack()
|
||||
.thenCompose(ignored -> StudioOpenCoordinator.get().closeToolWorld(bukkit()))
|
||||
.thenCompose(ignored -> StudioOpenCoordinator.get().closeToolWorld(bukkit(), project))
|
||||
.whenComplete((result, failure) -> finishClose(result, failure));
|
||||
return closed;
|
||||
}
|
||||
|
||||
+1
-1
@@ -85,7 +85,7 @@ final class BukkitToolWorlds implements ToolWorlds {
|
||||
private StudioOpenCoordinator.StudioOpenRequest openRequest(String storageName, BukkitToolWorld toolWorld) {
|
||||
return new StudioOpenCoordinator.StudioOpenRequest(
|
||||
packName,
|
||||
null,
|
||||
toolWorld.project(),
|
||||
null,
|
||||
TOOL_WORLD_SEED,
|
||||
storageName,
|
||||
|
||||
+92
-18
@@ -49,6 +49,7 @@ import org.bukkit.event.block.BlockPistonRetractEvent;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.block.BlockRedstoneEvent;
|
||||
import org.bukkit.event.block.BlockSpreadEvent;
|
||||
import org.bukkit.event.block.BrewingStartEvent;
|
||||
import org.bukkit.event.block.CrafterCraftEvent;
|
||||
import org.bukkit.event.entity.CreatureSpawnEvent;
|
||||
import org.bukkit.event.entity.EntityChangeBlockEvent;
|
||||
@@ -56,6 +57,8 @@ import org.bukkit.event.entity.EntityExplodeEvent;
|
||||
import org.bukkit.event.inventory.BrewEvent;
|
||||
import org.bukkit.event.inventory.BrewingStandFuelEvent;
|
||||
import org.bukkit.event.inventory.FurnaceBurnEvent;
|
||||
import org.bukkit.event.inventory.FurnaceStartSmeltEvent;
|
||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.event.inventory.FurnaceSmeltEvent;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryDragEvent;
|
||||
@@ -184,14 +187,12 @@ public final class ToolWorldEvents implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onPistonExtend(BlockPistonExtendEvent event) {
|
||||
allowOrCancel(event, Cause.PISTON, event.getBlock().getWorld(),
|
||||
pistonPositions(event.getBlock(), event.getBlocks(), event.getDirection()), Optional.empty(), Optional.empty());
|
||||
allowPistonOrCancel(event, event.getBlock(), event.getBlocks(), event.getDirection());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onPistonRetract(BlockPistonRetractEvent event) {
|
||||
allowOrCancel(event, Cause.PISTON, event.getBlock().getWorld(),
|
||||
pistonPositions(event.getBlock(), event.getBlocks(), event.getDirection()), Optional.empty(), Optional.empty());
|
||||
allowPistonOrCancel(event, event.getBlock(), event.getBlocks(), event.getDirection());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
@@ -270,6 +271,33 @@ public final class ToolWorldEvents implements Listener {
|
||||
allowOrCancel(event, Cause.MACHINE, event.getBlock(), Optional.empty());
|
||||
}
|
||||
|
||||
// What changed inside containers and machines, told after the fact.
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onInventoryClose(InventoryCloseEvent event) {
|
||||
Player player = event.getPlayer() instanceof Player closer ? closer : null;
|
||||
tellContainerChanged(containerBlocks(event.getView().getTopInventory()), Optional.ofNullable(player));
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onFurnaceStartSmelt(FurnaceStartSmeltEvent event) {
|
||||
tellContainerChanged(List.of(event.getBlock()), Optional.empty());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onBrewingStart(BrewingStartEvent event) {
|
||||
tellContainerChanged(List.of(event.getBlock()), Optional.empty());
|
||||
}
|
||||
|
||||
private void tellContainerChanged(List<Block> blocks, Optional<Player> player) {
|
||||
for (Block block : blocks) {
|
||||
Optional<ToolPlayer> toolPlayer = player.map(players::of).map(ToolPlayer.class::cast);
|
||||
BlockPosition at = positionOf(block);
|
||||
worlds.forWorld(block.getWorld()).ifPresent(world ->
|
||||
world.tell(listener -> listener.containerChanged(toolPlayer, at)));
|
||||
}
|
||||
}
|
||||
|
||||
// What players do.
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
|
||||
@@ -327,7 +355,8 @@ public final class ToolWorldEvents implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onJoin(PlayerJoinEvent event) {
|
||||
worlds.forWorld(event.getPlayer().getWorld()).ifPresent(world -> enter(world, event.getPlayer()));
|
||||
Player player = event.getPlayer();
|
||||
worlds.forWorld(player.getWorld()).ifPresent(world -> enter(world, player, player.getLocation()));
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
@@ -341,17 +370,20 @@ public final class ToolWorldEvents implements Listener {
|
||||
public void onChangedWorld(PlayerChangedWorldEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
worlds.forWorld(event.getFrom()).ifPresent(world -> leave(world, player));
|
||||
worlds.forWorld(player.getWorld()).ifPresent(world -> enter(world, player));
|
||||
worlds.forWorld(player.getWorld()).ifPresent(world -> enter(world, player, player.getLocation()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Every teleport of any player by anyone, portals included, is told to the tool worlds it touches at the time it
|
||||
* happens, with the position it goes to: the world it leaves hears {@code left}, the one it enters {@code entered},
|
||||
* and the one it arrives in {@code teleported}.
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onTeleport(PlayerTeleportEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
Location destination = event.getTo();
|
||||
Optional<BukkitToolWorld> from = worlds.forWorld(event.getFrom().getWorld());
|
||||
Optional<BukkitToolWorld> to = event.getTo() == null ? Optional.empty() : worlds.forWorld(event.getTo().getWorld());
|
||||
if (from.isPresent() && from.equals(to)) {
|
||||
return;
|
||||
}
|
||||
Optional<BukkitToolWorld> to = destination == null ? Optional.empty() : worlds.forWorld(destination.getWorld());
|
||||
if (from.isEmpty() && to.isPresent()) {
|
||||
players.rememberOrigin(player, event.getFrom());
|
||||
}
|
||||
@@ -359,13 +391,16 @@ public final class ToolWorldEvents implements Listener {
|
||||
players.restoreGameMode(player);
|
||||
players.takeOrigin(player);
|
||||
}
|
||||
from.ifPresent(world -> leave(world, player));
|
||||
to.ifPresent(world -> enter(world, player));
|
||||
if (!from.equals(to)) {
|
||||
from.ifPresent(world -> leave(world, player));
|
||||
}
|
||||
to.ifPresent(world -> enter(world, player, destination));
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR)
|
||||
public void onRespawn(PlayerRespawnEvent event) {
|
||||
worlds.forWorld(event.getRespawnLocation().getWorld()).ifPresent(world -> enter(world, event.getPlayer()));
|
||||
Location respawn = event.getRespawnLocation();
|
||||
worlds.forWorld(respawn.getWorld()).ifPresent(world -> enter(world, event.getPlayer(), respawn));
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
@@ -405,10 +440,13 @@ public final class ToolWorldEvents implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
private void enter(BukkitToolWorld world, Player player) {
|
||||
/** The player is in the world at {@code at}: told as having entered on the first of their arrivals, and as having arrived on each. */
|
||||
private void enter(BukkitToolWorld world, Player player, Location at) {
|
||||
if (world.presence().enter(player.getUniqueId())) {
|
||||
world.tell(listener -> listener.entered(players.of(player)));
|
||||
}
|
||||
BlockPosition arrival = BukkitToolBlocks.positionOf(at);
|
||||
world.tell(listener -> listener.teleported(players.of(player), arrival));
|
||||
}
|
||||
|
||||
private void leave(BukkitToolWorld world, Player player) {
|
||||
@@ -434,6 +472,19 @@ public final class ToolWorldEvents implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
private void allowPistonOrCancel(Cancellable event, Block piston, List<Block> moved, BlockFace direction) {
|
||||
boolean allowed = isAllowed(
|
||||
Cause.PISTON,
|
||||
piston.getWorld(),
|
||||
pistonChecked(moved, direction),
|
||||
pistonChanged(piston, moved, direction),
|
||||
Optional.empty(),
|
||||
Optional.empty());
|
||||
if (!allowed) {
|
||||
event.setCancelled(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void allowContainerOrCancel(Cancellable event, Inventory inventory, Player player) {
|
||||
List<Block> blocks = containerBlocks(inventory);
|
||||
if (blocks.isEmpty()) {
|
||||
@@ -450,12 +501,24 @@ public final class ToolWorldEvents implements Listener {
|
||||
List<BlockPosition> positions,
|
||||
Optional<Player> player,
|
||||
Optional<BlockState> placed
|
||||
) {
|
||||
return isAllowed(cause, world, positions, positions, player, placed);
|
||||
}
|
||||
|
||||
private boolean isAllowed(
|
||||
Cause cause,
|
||||
World world,
|
||||
List<BlockPosition> positions,
|
||||
List<BlockPosition> changed,
|
||||
Optional<Player> player,
|
||||
Optional<BlockState> placed
|
||||
) {
|
||||
Optional<BukkitToolWorld> toolWorld = worlds.forWorld(world);
|
||||
if (toolWorld.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
BlockChange change = new BlockChange(cause, positions, player.map(players::of).map(ToolPlayer.class::cast), placed);
|
||||
BlockChange change = new BlockChange(
|
||||
cause, positions, changed, player.map(players::of).map(ToolPlayer.class::cast), placed);
|
||||
return toolWorld.get().ask(listener -> listener.blockChanging(change), false);
|
||||
}
|
||||
|
||||
@@ -466,10 +529,9 @@ public final class ToolWorldEvents implements Listener {
|
||||
exploded.removeIf(block -> !isAllowed(Cause.EXPLOSION, world, List.of(positionOf(block)), Optional.empty(), Optional.empty()));
|
||||
}
|
||||
|
||||
/** The piston, each block it moves, and the block each of them leaves and enters, as a protected area is checked. */
|
||||
static List<BlockPosition> pistonPositions(Block piston, List<Block> moved, BlockFace direction) {
|
||||
/** Each block a piston moves and the block each of them enters and leaves, which a protected area is checked against; not the piston. */
|
||||
static List<BlockPosition> pistonChecked(List<Block> moved, BlockFace direction) {
|
||||
List<BlockPosition> positions = new ArrayList<>();
|
||||
positions.add(positionOf(piston));
|
||||
for (Block block : moved) {
|
||||
positions.add(positionOf(block));
|
||||
positions.add(positionOf(block.getRelative(direction)));
|
||||
@@ -478,6 +540,18 @@ public final class ToolWorldEvents implements Listener {
|
||||
return positions.stream().distinct().toList();
|
||||
}
|
||||
|
||||
/** The piston and the block ahead of it, then each block it moves and the block each enters: what a piston changes. */
|
||||
static List<BlockPosition> pistonChanged(Block piston, List<Block> moved, BlockFace direction) {
|
||||
List<BlockPosition> positions = new ArrayList<>();
|
||||
positions.add(positionOf(piston));
|
||||
positions.add(positionOf(piston.getRelative(direction)));
|
||||
for (Block block : moved) {
|
||||
positions.add(positionOf(block));
|
||||
positions.add(positionOf(block.getRelative(direction)));
|
||||
}
|
||||
return positions.stream().distinct().toList();
|
||||
}
|
||||
|
||||
/** The blocks a container inventory is made of: none for a player's or a menu, two for a double chest. */
|
||||
static List<Block> containerBlocks(Inventory inventory) {
|
||||
if (inventory == null) {
|
||||
|
||||
+4
-3
@@ -2,6 +2,7 @@ package art.arcane.iris.core.toolworld;
|
||||
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.tooling.world.ToolItem;
|
||||
import com.volmit.tooling.world.ToolText;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -41,8 +42,8 @@ public class BukkitToolItemsTest {
|
||||
|
||||
Optional<ToolItem> item = BukkitToolItems.read(stackWith(data, meta));
|
||||
|
||||
assertEquals(Optional.of(new ToolItem(ResourceId.parse("minecraft:stick"), "Select bay",
|
||||
List.of("Right-click to use"), true, "select:bay-1")), item);
|
||||
assertEquals(Optional.of(new ToolItem(ResourceId.parse("minecraft:stick"), ToolText.plain("Select bay"),
|
||||
List.of(ToolText.plain("Right-click to use")), true, "select:bay-1")), item);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -54,7 +55,7 @@ public class BukkitToolItemsTest {
|
||||
ToolItem tagged = BukkitToolItems.read(stackWith(empty, mock(ItemMeta.class))).orElseThrow();
|
||||
|
||||
assertEquals("", tagged.payload());
|
||||
assertEquals("", tagged.name());
|
||||
assertEquals(ToolText.EMPTY, tagged.name());
|
||||
assertEquals(List.of(), tagged.lore());
|
||||
assertTrue(BukkitToolItems.read(stackWith(missing, mock(ItemMeta.class))).isEmpty());
|
||||
}
|
||||
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package art.arcane.iris.core.toolworld;
|
||||
|
||||
import com.volmit.tooling.world.TextColor;
|
||||
import com.volmit.tooling.world.ToolText;
|
||||
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class BukkitToolTextTest {
|
||||
private static final String SECTION = "§";
|
||||
|
||||
@Test
|
||||
public void legacyTextHasTheColourCodeOfEachSpanAsTheOldMenusWroteThem() {
|
||||
ToolText text = ToolText.builder().aqua("Structure").gray(": village").build();
|
||||
|
||||
assertEquals(SECTION + "bStructure" + SECTION + "7: village", BukkitToolText.legacy(text));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anUncolouredSpanAfterAnotherResets() {
|
||||
ToolText text = ToolText.builder().yellow("Walk").append(" plain").build();
|
||||
|
||||
assertEquals(SECTION + "eWalk" + SECTION + "r plain", BukkitToolText.legacy(text));
|
||||
assertEquals("plain", BukkitToolText.legacy(ToolText.plain("plain")));
|
||||
assertEquals("", BukkitToolText.legacy(ToolText.EMPTY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stylesFollowTheColour() {
|
||||
ToolText text = ToolText.builder()
|
||||
.append(new ToolText.Span("x", TextColor.GRAY, true, true, true)).build();
|
||||
|
||||
assertEquals(SECTION + "7" + SECTION + "l" + SECTION + "o" + SECTION + "mx", BukkitToolText.legacy(text));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aComponentCarriesTheColoursAndTheTextOfEverySpan() {
|
||||
ToolText text = ToolText.builder().red("a").append("b").build();
|
||||
|
||||
Component component = BukkitToolText.component(text);
|
||||
|
||||
assertEquals("ab", PlainTextComponentSerializer.plainText().serialize(component));
|
||||
assertEquals(NamedTextColor.RED, component.children().getFirst().color());
|
||||
assertEquals(null, component.children().getLast().color());
|
||||
assertEquals(TextDecoration.State.NOT_SET, component.children().getFirst().decoration(TextDecoration.ITALIC));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aTitleTooLongForTheWindowIsCutWithoutLeavingHalfACode() {
|
||||
ToolText coloured = ToolText.colored(TextColor.GREEN, "x".repeat(40));
|
||||
|
||||
String cut = BukkitToolMenus.shortened(coloured);
|
||||
|
||||
assertEquals(32, cut.length());
|
||||
assertEquals(SECTION + "a", cut.substring(0, 2));
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -4,6 +4,8 @@ import art.arcane.iris.core.runtime.StudioPainterSlots.PaintedBlockEntity;
|
||||
import art.arcane.iris.core.runtime.StudioPainterSlots.PaintedChunk;
|
||||
import com.volmit.object.BlockEntityData;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.tooling.world.TextColor;
|
||||
import com.volmit.tooling.world.ToolText;
|
||||
import org.bukkit.World;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -105,8 +107,9 @@ public class ToolWorldBookkeepingTest {
|
||||
|
||||
@Test
|
||||
public void aMenuTitleIsShortenedToWhatTheWindowCanShow() {
|
||||
assertEquals("short", BukkitToolMenus.shortened("short"));
|
||||
assertEquals(32, BukkitToolMenus.shortened("x".repeat(50)).length());
|
||||
assertEquals("short", BukkitToolMenus.shortened(ToolText.plain("short")));
|
||||
assertEquals(32, BukkitToolMenus.shortened(ToolText.plain("x".repeat(50))).length());
|
||||
assertEquals("§a" + "x".repeat(30), BukkitToolMenus.shortened(ToolText.colored(TextColor.GREEN, "x".repeat(50))));
|
||||
}
|
||||
|
||||
private static BukkitToolWorld toolWorld(OpenToolWorlds worlds, String pack, String name, String storageName) {
|
||||
|
||||
+120
-5
@@ -26,6 +26,7 @@ import org.bukkit.event.player.PlayerBucketEmptyEvent;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.event.player.PlayerRespawnEvent;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
import org.bukkit.event.server.ServerCommandEvent;
|
||||
import org.bukkit.event.block.Action;
|
||||
@@ -80,6 +81,16 @@ public class ToolWorldEventsTest {
|
||||
heard.add("left " + player.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void teleported(ToolPlayer player, BlockPosition to) {
|
||||
heard.add("teleported " + player.name() + " " + to.x() + "," + to.y() + "," + to.z());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void containerChanged(Optional<ToolPlayer> player, BlockPosition at) {
|
||||
heard.add("container " + player.map(ToolPlayer::name).orElse("nobody") + " " + at.x() + "," + at.y() + "," + at.z());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean used(ToolPlayer player, ToolUse use) {
|
||||
heard.add("used " + use.action() + " " + use.hand());
|
||||
@@ -202,21 +213,53 @@ public class ToolWorldEventsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPistonNamesItselfAndEveryBlockItMovesAndTheirNeighboursInTheDirectionOfTheMove() {
|
||||
public void aPistonIsCheckedAgainstEveryBlockItMovesAndTheBlocksTheyLeaveAndEnterButNotItself() {
|
||||
Block piston = block(toolBukkitWorld, 0, 64, 0);
|
||||
Block moved = block(toolBukkitWorld, 1, 64, 0);
|
||||
Block ahead = block(toolBukkitWorld, 2, 64, 0);
|
||||
when(moved.getRelative(BlockFace.EAST)).thenReturn(ahead);
|
||||
when(moved.getRelative(BlockFace.WEST)).thenReturn(piston);
|
||||
|
||||
List<BlockPosition> positions = ToolWorldEvents.pistonPositions(piston, List.of(moved), BlockFace.EAST);
|
||||
List<BlockPosition> positions = ToolWorldEvents.pistonChecked(List.of(moved), BlockFace.EAST);
|
||||
|
||||
assertEquals(List.of(new BlockPosition(0, 64, 0), new BlockPosition(1, 64, 0), new BlockPosition(2, 64, 0)), positions);
|
||||
assertEquals(List.of(new BlockPosition(1, 64, 0), new BlockPosition(2, 64, 0), new BlockPosition(0, 64, 0)), positions);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPistonChangesItselfTheBlockAheadOfItAndEveryBlockItMovesAndWhereTheyGo() {
|
||||
Block piston = block(toolBukkitWorld, 0, 64, 0);
|
||||
Block moved = block(toolBukkitWorld, 1, 64, 0);
|
||||
Block ahead = block(toolBukkitWorld, 2, 64, 0);
|
||||
when(piston.getRelative(BlockFace.EAST)).thenReturn(moved);
|
||||
when(moved.getRelative(BlockFace.EAST)).thenReturn(ahead);
|
||||
|
||||
List<BlockPosition> changed = ToolWorldEvents.pistonChanged(piston, List.of(moved), BlockFace.EAST);
|
||||
|
||||
assertEquals(List.of(new BlockPosition(0, 64, 0), new BlockPosition(1, 64, 0), new BlockPosition(2, 64, 0)), changed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPistonThatMovesNothingStillChangesItsOwnBlocks() {
|
||||
Block piston = block(toolBukkitWorld, 0, 64, 0);
|
||||
Block ahead = block(toolBukkitWorld, 1, 64, 0);
|
||||
when(piston.getRelative(BlockFace.EAST)).thenReturn(ahead);
|
||||
BlockPistonExtendEvent event = mock(BlockPistonExtendEvent.class);
|
||||
when(event.getBlock()).thenReturn(piston);
|
||||
when(event.getBlocks()).thenReturn(List.of());
|
||||
when(event.getDirection()).thenReturn(BlockFace.EAST);
|
||||
|
||||
events.onPistonExtend(event);
|
||||
|
||||
BlockChange change = onlyChange();
|
||||
assertEquals(List.of(), change.positions());
|
||||
assertEquals(List.of(new BlockPosition(0, 64, 0), new BlockPosition(1, 64, 0)), change.changed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPistonExtendingInAToolWorldIsAPistonChange() {
|
||||
Block piston = block(toolBukkitWorld, 0, 64, 0);
|
||||
Block ahead = block(toolBukkitWorld, 1, 64, 0);
|
||||
when(piston.getRelative(BlockFace.EAST)).thenReturn(ahead);
|
||||
BlockPistonExtendEvent event = mock(BlockPistonExtendEvent.class);
|
||||
when(event.getBlock()).thenReturn(piston);
|
||||
when(event.getBlocks()).thenReturn(List.of());
|
||||
@@ -351,7 +394,76 @@ public class ToolWorldEventsTest {
|
||||
events.onTeleport(teleport);
|
||||
events.onTeleport(teleport);
|
||||
|
||||
assertEquals(List.of("entered Tester"), heard);
|
||||
assertEquals(List.of("entered Tester", "teleported Tester 0,64,0", "teleported Tester 0,64,0"), heard);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aTeleportInsideAToolWorldIsToldAtOnceWithItsDestination() {
|
||||
PlayerTeleportEvent teleport = mock(PlayerTeleportEvent.class);
|
||||
when(teleport.getPlayer()).thenReturn(bukkitPlayer);
|
||||
when(teleport.getFrom()).thenReturn(new Location(toolBukkitWorld, 0, 64, 0));
|
||||
when(teleport.getTo()).thenReturn(new Location(toolBukkitWorld, 40, 70, -3));
|
||||
|
||||
events.onTeleport(teleport);
|
||||
|
||||
assertEquals(List.of("entered Tester", "teleported Tester 40,70,-3"), heard);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aTeleportOutOfAToolWorldIsToldAsLeaving() {
|
||||
PlayerTeleportEvent in = mock(PlayerTeleportEvent.class);
|
||||
when(in.getPlayer()).thenReturn(bukkitPlayer);
|
||||
when(in.getFrom()).thenReturn(new Location(otherBukkitWorld, 0, 64, 0));
|
||||
when(in.getTo()).thenReturn(new Location(toolBukkitWorld, 0, 64, 0));
|
||||
PlayerTeleportEvent out = mock(PlayerTeleportEvent.class);
|
||||
when(out.getPlayer()).thenReturn(bukkitPlayer);
|
||||
when(out.getFrom()).thenReturn(new Location(toolBukkitWorld, 0, 64, 0));
|
||||
when(out.getTo()).thenReturn(new Location(otherBukkitWorld, 5, 64, 5));
|
||||
|
||||
events.onTeleport(in);
|
||||
events.onTeleport(out);
|
||||
|
||||
assertEquals(List.of("entered Tester", "teleported Tester 0,64,0", "left Tester"), heard);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void respawningInAToolWorldIsAnArrivalAtTheRespawnPoint() {
|
||||
PlayerRespawnEvent respawn = mock(PlayerRespawnEvent.class);
|
||||
when(respawn.getPlayer()).thenReturn(bukkitPlayer);
|
||||
when(respawn.getRespawnLocation()).thenReturn(new Location(toolBukkitWorld, 8, 66, 8));
|
||||
|
||||
events.onRespawn(respawn);
|
||||
|
||||
assertEquals(List.of("entered Tester", "teleported Tester 8,66,8"), heard);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void closingAContainerInAToolWorldTellsTheToolWhichBlockChanged() {
|
||||
Block chest = block(toolBukkitWorld, 4, 65, 4);
|
||||
org.bukkit.inventory.Inventory inventory = mock(org.bukkit.inventory.Inventory.class);
|
||||
org.bukkit.inventory.BlockInventoryHolder holder = mock(org.bukkit.inventory.BlockInventoryHolder.class);
|
||||
when(holder.getBlock()).thenReturn(chest);
|
||||
when(inventory.getHolder()).thenReturn(holder);
|
||||
org.bukkit.inventory.InventoryView view = mock(org.bukkit.inventory.InventoryView.class);
|
||||
when(view.getTopInventory()).thenReturn(inventory);
|
||||
org.bukkit.event.inventory.InventoryCloseEvent close = mock(org.bukkit.event.inventory.InventoryCloseEvent.class);
|
||||
when(close.getPlayer()).thenReturn(bukkitPlayer);
|
||||
when(close.getView()).thenReturn(view);
|
||||
|
||||
events.onInventoryClose(close);
|
||||
|
||||
assertEquals(List.of("container Tester 4,65,4"), heard);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aFurnaceStartingToSmeltTellsTheToolWithoutAPlayer() {
|
||||
Block furnace = block(toolBukkitWorld, 2, 64, 2);
|
||||
org.bukkit.event.inventory.FurnaceStartSmeltEvent smelt = mock(org.bukkit.event.inventory.FurnaceStartSmeltEvent.class);
|
||||
when(smelt.getBlock()).thenReturn(furnace);
|
||||
|
||||
events.onFurnaceStartSmelt(smelt);
|
||||
|
||||
assertEquals(List.of("container nobody 2,64,2"), heard);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -367,7 +479,7 @@ public class ToolWorldEventsTest {
|
||||
events.onQuit(quit);
|
||||
events.onQuit(quit);
|
||||
|
||||
assertEquals(List.of("entered Tester", "left Tester"), heard);
|
||||
assertEquals(List.of("entered Tester", "teleported Tester 0,64,0", "left Tester"), heard);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -413,6 +525,9 @@ public class ToolWorldEventsTest {
|
||||
assertPriority(byMethod, checked, "onTeleport", EventPriority.MONITOR, true);
|
||||
assertPriority(byMethod, checked, "onMove", EventPriority.MONITOR, true);
|
||||
assertPriority(byMethod, checked, "onRespawn", EventPriority.MONITOR, false);
|
||||
for (String containerNews : List.of("onInventoryClose", "onFurnaceStartSmelt", "onBrewingStart")) {
|
||||
assertPriority(byMethod, checked, containerNews, EventPriority.MONITOR, false);
|
||||
}
|
||||
assertPriority(byMethod, checked, "onWorldUnload", EventPriority.MONITOR, true);
|
||||
assertEquals("every handler of the class was checked", byMethod.keySet(), checked);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user