mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 16:54:04 +00:00
Merge feat/structure-jigsaw-modules into port/v3-features
Structure placement and jigsaw now live in the built-in modules jar, on top of the port's weighted picking, studio tooling
and procedural object work.
api/CONTRACTS.md keeps both sides' rows: the weighted entries and the studio bake and scene creation types from the port,
and VanillaStructureContext.world, Argument.reading, PlatformStructures, tool worlds and the services lookup from the
structure move.
PackModuleResourceKindTest installs the built-in modules jar into its castle pack, as the structure move requires, and
writes the dimension and region lists as weighted entries, as the port requires.
StructurePlacing takes the structure move's version. Its int sentinel for a sampled height is replaced by an explicit
sampledHeight flag that calls ObjectRun.automatic, and explicit pieces pass their world Y minus the world's minimum, so an
explicit world Y below zero stays a height (e3dd61c17).
StructureOutputPinTest.everyScenarioPlacesThePinnedStructures is re-baselined for the underground scenario's writes hash
only (9176f55f60410c117cdd9d5e to 1025f82c04539d4881a0e92b): pieces at world Y -1 are now placed there instead of on the
surface. The start count, the piece hash and every other scenario are unchanged.
This commit is contained in:
commit
2889274291
514 files changed
+29979
-15110
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.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.volmit.geometry;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/** A chunk column, by chunk coordinates: block X and Z divided by 16, rounded down. */
|
||||
public record ChunkPos(int x, int z) {
|
||||
private static final int CHUNK_SHIFT = 4;
|
||||
|
||||
/** The chunk that contains {@code block}. */
|
||||
public static ChunkPos containing(BlockPosition block) {
|
||||
Objects.requireNonNull(block, "block");
|
||||
return new ChunkPos(block.x() >> CHUNK_SHIFT, block.z() >> CHUNK_SHIFT);
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,26 @@ public interface PlacementTerrain {
|
||||
*/
|
||||
void recordPlacement(int x, int y, int z, PlacementRecord placement);
|
||||
|
||||
/**
|
||||
* Takes back the block written at the position, so the terrain shows through there. The block entity, placement
|
||||
* record and markers kept beside it stay ({@link #clearAttached}).
|
||||
*
|
||||
* @throws UnsupportedOperationException when this terrain cannot take blocks back
|
||||
*/
|
||||
default void clearBlock(int x, int y, int z) {
|
||||
throw new UnsupportedOperationException("This terrain cannot take blocks back");
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes back the block entity, placement record and markers the host kept beside the block, whether or not a block
|
||||
* is still written there.
|
||||
*
|
||||
* @throws UnsupportedOperationException when this terrain cannot take them back
|
||||
*/
|
||||
default void clearAttached(int x, int y, int z) {
|
||||
throw new UnsupportedOperationException("This terrain cannot take what is kept beside a block back");
|
||||
}
|
||||
|
||||
/** The placement recorded at the block ({@link #recordPlacement}); empty when none is. */
|
||||
Optional<PlacementRecord> placementAt(int x, int y, int z);
|
||||
|
||||
|
||||
@@ -69,6 +69,13 @@ public interface PackResources {
|
||||
return ResourceId.ofPackKey(packId().namespace(), key);
|
||||
}
|
||||
|
||||
/** The providers of {@code service} that the pack's modules registered, in module id order, so a module that holds only the
|
||||
* resources, such as a locator listing structures, can ask the modules that own the kinds. Empty by default.
|
||||
*/
|
||||
default <S> List<S> services(Class<S> service) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
/** {@link #find} for a resource that must exist: a missing one throws NoSuchElementException naming kind and id. */
|
||||
default <T> T require(ResourceKind<T> kind, ResourceId id) {
|
||||
return find(kind, id).orElseThrow(() -> new NoSuchElementException("Missing " + kind.id() + " resource: " + id));
|
||||
|
||||
@@ -117,6 +117,15 @@ public final class Argument<T> {
|
||||
});
|
||||
}
|
||||
|
||||
/** One word that {@code reader} turns into the value, for a value only the command's module can check, such as a resource
|
||||
* of a kind its module's neighbour registers. {@code reader} fails with an IllegalArgumentException that says why; the
|
||||
* argument completes nothing until {@link #suggesting} gives it candidates.
|
||||
*/
|
||||
public static <T> Argument<T> reading(String name, Reader<T> reader) {
|
||||
Objects.requireNonNull(reader, "reader");
|
||||
return of(name, false, reader::read, (partial, context) -> List.of());
|
||||
}
|
||||
|
||||
/** An id registered in the platform's {@code registry}, such as {@link Reference#STRUCTURE}; completes
|
||||
* {@link PlatformRegistries#ids}. Written as the platform writes it, {@code minecraft:village_plains}, or without the
|
||||
* {@code minecraft:} namespace.
|
||||
@@ -244,6 +253,12 @@ public final class Argument<T> {
|
||||
return registry.name().toLowerCase(Locale.ROOT).replace('_', ' ');
|
||||
}
|
||||
|
||||
/** Turns the word a sender typed into the argument's value. */
|
||||
@FunctionalInterface
|
||||
public interface Reader<T> {
|
||||
T read(String text, CommandContext context);
|
||||
}
|
||||
|
||||
private interface Parser<T> {
|
||||
T parse(String text, CommandContext context);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.volmit.tooling;
|
||||
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A structure the platform generated in a scratch area and read back.
|
||||
*
|
||||
* @param size the width, height and depth of the box the structure filled
|
||||
* @param blocks every block in that box that is not plain air, with positions from the box's corner
|
||||
*/
|
||||
public record CapturedStructure(BlockPosition size, List<TemplateBlock> blocks) {
|
||||
public CapturedStructure {
|
||||
Objects.requireNonNull(size, "size");
|
||||
blocks = List.copyOf(blocks);
|
||||
}
|
||||
}
|
||||
@@ -30,4 +30,7 @@ public interface CommandContext {
|
||||
|
||||
/** The platform's registries; empty when the host runs without a platform. */
|
||||
Optional<PlatformRegistries> platform();
|
||||
|
||||
/** The structures the platform registers and the templates it loads; empty on a platform that does not offer them. */
|
||||
Optional<PlatformStructures> platformStructures();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.volmit.tooling;
|
||||
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/** What {@link DatapackStructureImport#importAll} did.
|
||||
*
|
||||
* @param total the sources it tried
|
||||
* @param imported the sources it imported
|
||||
* @param skipped the sources it left as they were
|
||||
* @param failed the sources it could not import
|
||||
* @param importedBundles the bundle each fully imported source ended up as, by bundle name
|
||||
* @param retryRequired whether a failure may pass when the import runs again, as opposed to the source being invalid
|
||||
*/
|
||||
public record DatapackImportReport(int total, int imported, int skipped, int failed, Map<String, ResourceId> importedBundles,
|
||||
boolean retryRequired) {
|
||||
public DatapackImportReport {
|
||||
Objects.requireNonNull(importedBundles, "importedBundles");
|
||||
importedBundles = java.util.Collections.unmodifiableMap(new TreeMap<>(importedBundles));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.volmit.tooling;
|
||||
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.service.Providers;
|
||||
import com.volmit.service.Service;
|
||||
import java.util.Collection;
|
||||
|
||||
/** Turns the structures and templates a datapack brought into editable resources of the pack, once the host installed the
|
||||
* datapack. A module that owns a kind of structure provides it; the host asks the pack's first provider and, when the pack has
|
||||
* none, leaves the datapack's structures pending. Re-importing a source replaces what an earlier import made of it.
|
||||
*/
|
||||
@Service(Providers.FIRST)
|
||||
public interface DatapackStructureImport {
|
||||
/** Imports {@code structures} (ids of the platform's registered structures) and {@code templates} (ids of its structure
|
||||
* templates) into {@code pack}, reporting progress to {@code sender}. A source that fails does not stop the others.
|
||||
*/
|
||||
DatapackImportReport importAll(ToolPack pack, PlatformStructures platform, Sender sender,
|
||||
Collection<ResourceId> structures, Collection<ResourceId> templates);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.volmit.tooling;
|
||||
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* What a registered jigsaw structure says about itself.
|
||||
*
|
||||
* @param startPool the template pool the structure starts from
|
||||
* @param maxDepth how many pieces deep the structure grows
|
||||
* @param maxDistance how far from its centre the structure may reach, in blocks; the horizontal reach when the platform
|
||||
* keeps a horizontal and a vertical one
|
||||
* @param gameVersion the platform's own version string, which tools record as where the structure came from
|
||||
* @param shuffleSeed what the platform seeds its shuffle of the connectors of this structure's pool elements with, so
|
||||
* that reading the same structure twice lists them in the same order
|
||||
*/
|
||||
public record JigsawStructureInfo(ResourceId startPool, int maxDepth, int maxDistance, String gameVersion,
|
||||
long shuffleSeed) {
|
||||
public JigsawStructureInfo {
|
||||
Objects.requireNonNull(startPool, "startPool");
|
||||
Objects.requireNonNull(gameVersion, "gameVersion");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.volmit.tooling;
|
||||
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* The structures a platform registers and the templates it loads, for the tools that turn them into pack resources.
|
||||
* A command reaches it as {@link CommandContext#platformStructures()}, which is empty on a platform without it. Every
|
||||
* method may take a while and reads the running server, so a tool calls them from its worker thread and never
|
||||
* from the platform's main or region thread.
|
||||
*
|
||||
* <p>Each reader that cannot answer because the platform's registries cannot be read throws a
|
||||
* {@link StructureAccessException}; a reader that answers empty means the platform has no such thing.
|
||||
*/
|
||||
public interface PlatformStructures {
|
||||
/** The ids of the structure templates the platform can load, such as {@code minecraft:village/plains/houses/plains_small_house_1}, in id order. */
|
||||
List<ResourceId> templates();
|
||||
|
||||
/** The template {@code id}; empty when the platform has none or it holds no palette. */
|
||||
Optional<StructureTemplate> template(ResourceId id);
|
||||
|
||||
/** The ids of the structures the platform registers, in id order. */
|
||||
List<ResourceId> structures();
|
||||
|
||||
/** The ids of the structure sets the platform registers, in id order. */
|
||||
List<ResourceId> structureSets();
|
||||
|
||||
/** What {@code structure} says about itself; empty when it is no jigsaw structure or is not registered. */
|
||||
Optional<JigsawStructureInfo> jigsaw(ResourceId structure);
|
||||
|
||||
/** The template pool {@code pool}; empty when it is not registered. */
|
||||
Optional<TemplatePool> pool(ResourceId pool);
|
||||
|
||||
/** Whether the platform can generate a structure in a scratch area and read it back, which {@link #openCaptureArea} needs. */
|
||||
boolean supportsCapture();
|
||||
|
||||
/**
|
||||
* Opens a throwaway area to generate structures in, for the structures that have no template. Close it when the
|
||||
* captures are done; it is removed then.
|
||||
*
|
||||
* @param sender where the platform reports what it does while it opens and removes the area
|
||||
* @return the area; empty when it could not be opened, which the platform has told the sender
|
||||
*/
|
||||
Optional<CaptureArea> openCaptureArea(Sender sender);
|
||||
|
||||
/** A throwaway area structures are generated in, one after another. */
|
||||
interface CaptureArea extends AutoCloseable {
|
||||
/**
|
||||
* Generates {@code structure} in the next free spot of the area and reads it back.
|
||||
*
|
||||
* @param seed what the platform generates the structure with
|
||||
* @param maxSpan the widest and tallest box, in blocks, the platform reads back
|
||||
* @return the structure; empty when it placed nothing capturable here, such as one wider than {@code maxSpan}
|
||||
* @throws IllegalStateException when the platform could not place it, with the reason
|
||||
*/
|
||||
Optional<CapturedStructure> capture(ResourceId structure, long seed, int maxSpan);
|
||||
|
||||
/** Removes the area. */
|
||||
@Override
|
||||
void close();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.volmit.tooling;
|
||||
|
||||
import com.volmit.tooling.world.ToolPlayer;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@@ -21,4 +22,7 @@ public interface Sender {
|
||||
|
||||
/** The platform's own sender as {@code type}, such as a Bukkit {@code Player}; empty when it is not one. For platform parts. */
|
||||
<T> Optional<T> extension(Class<T> type);
|
||||
|
||||
/** The sender as a player of the platform; empty for the console, a command block, or a platform without tool worlds. */
|
||||
Optional<ToolPlayer> player();
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.volmit.tooling;
|
||||
|
||||
/**
|
||||
* What a {@link PlatformStructures} reader throws when the platform cannot answer, such as a server version whose
|
||||
* registries it cannot read. The message is written for the sender who ran the tool, so a tool shows it as it is.
|
||||
*/
|
||||
public final class StructureAccessException extends RuntimeException {
|
||||
private final boolean retryable;
|
||||
|
||||
/**
|
||||
* @param message what went wrong, as the sender should read it
|
||||
* @param retryable whether asking again later may succeed, as for a registry that was not ready yet
|
||||
*/
|
||||
public StructureAccessException(String message, boolean retryable) {
|
||||
super(message);
|
||||
this.retryable = retryable;
|
||||
}
|
||||
|
||||
public StructureAccessException(String message, boolean retryable, Throwable cause) {
|
||||
super(message, cause);
|
||||
this.retryable = retryable;
|
||||
}
|
||||
|
||||
/** Whether asking again later may succeed. */
|
||||
public boolean retryable() {
|
||||
return retryable;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.volmit.tooling;
|
||||
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A structure template the platform loads, such as a vanilla {@code .nbt}: its size and the blocks of its first palette.
|
||||
*
|
||||
* @param size the template's width, height and depth in blocks
|
||||
* @param paletteCount how many palettes the template has; only the first is in {@code blocks}
|
||||
* @param entityCount how many entities the template holds, which tools do not read
|
||||
* @param blocks the blocks of the first palette, with positions from the template's corner
|
||||
*/
|
||||
public record StructureTemplate(BlockPosition size, int paletteCount, int entityCount, List<TemplateBlock> blocks) {
|
||||
public StructureTemplate {
|
||||
Objects.requireNonNull(size, "size");
|
||||
blocks = List.copyOf(blocks);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.volmit.tooling;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import com.volmit.object.BlockEntityData;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* One block of a {@link StructureTemplate} or {@link CapturedStructure}.
|
||||
*
|
||||
* @param position where the block is, from the template's corner
|
||||
* @param state the block as the platform stores it; a jigsaw block is the jigsaw block itself
|
||||
* @param blockEntity the block entity data of a block that has some; empty for a jigsaw block, which tools resolve
|
||||
* through {@code jigsawFinalState}
|
||||
* @param jigsawFinalState what a jigsaw block turns into when its piece is placed: the {@code final_state} the block
|
||||
* names, as the platform writes it; empty for any other block and for a jigsaw block that names
|
||||
* none
|
||||
*/
|
||||
public record TemplateBlock(BlockPosition position, BlockState state, Optional<BlockEntityData> blockEntity,
|
||||
Optional<String> jigsawFinalState) {
|
||||
public TemplateBlock {
|
||||
Objects.requireNonNull(position, "position");
|
||||
Objects.requireNonNull(state, "state");
|
||||
Objects.requireNonNull(blockEntity, "blockEntity");
|
||||
Objects.requireNonNull(jigsawFinalState, "jigsawFinalState");
|
||||
}
|
||||
|
||||
/** A block with no jigsaw final state. */
|
||||
public static TemplateBlock of(BlockPosition position, BlockState state, Optional<BlockEntityData> blockEntity) {
|
||||
return new TemplateBlock(position, state, blockEntity, Optional.empty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package com.volmit.tooling;
|
||||
|
||||
import com.volmit.geometry.BlockFace;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.LongSupplier;
|
||||
|
||||
/**
|
||||
* A jigsaw template pool the platform registers: what its entries are and where it falls back to.
|
||||
*
|
||||
* @param members the entries in the order the platform lists them, every one of them, so the count is the
|
||||
* pool's own size
|
||||
* @param fallback the pool used when none of the entries fits; empty when it has none
|
||||
* @param fallbackFailure why the fallback could not be read, when it could not; the fallback is then empty
|
||||
*/
|
||||
public record TemplatePool(List<Member> members, Optional<ResourceId> fallback, Optional<String> fallbackFailure) {
|
||||
public TemplatePool {
|
||||
members = List.copyOf(members);
|
||||
Objects.requireNonNull(fallback, "fallback");
|
||||
Objects.requireNonNull(fallbackFailure, "fallbackFailure");
|
||||
}
|
||||
|
||||
/** One entry of a pool. */
|
||||
public sealed interface Member permits Absent, Unreadable, Element {
|
||||
}
|
||||
|
||||
/** An entry that holds no element. */
|
||||
public record Absent() implements Member {
|
||||
}
|
||||
|
||||
/** Which part of an entry could not be read. */
|
||||
public enum Part {
|
||||
/** The element and its weight. */
|
||||
ENTRY,
|
||||
/** Where the element's template is. */
|
||||
TEMPLATE_LOCATION
|
||||
}
|
||||
|
||||
/**
|
||||
* An entry the platform could not read.
|
||||
*
|
||||
* @param part what could not be read
|
||||
* @param detail why, as the platform reports it
|
||||
*/
|
||||
public record Unreadable(Part part, String detail) implements Member {
|
||||
public Unreadable {
|
||||
Objects.requireNonNull(part, "part");
|
||||
Objects.requireNonNull(detail, "detail");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One element of a pool with its weight.
|
||||
*
|
||||
* @param weight how likely the element is among the entries; at least 1
|
||||
* @param type the platform's name for the kind of element, such as {@code SinglePoolElement}, which tells an
|
||||
* empty element from one a tool cannot convert when {@code template} is empty
|
||||
* @param template the template the element places; when the platform wraps several elements in a list, the
|
||||
* first one that places a template; empty for an element that places none
|
||||
* @param listLevels how many list elements wrap the one {@code template} came from
|
||||
* @param omittedElements how many elements those lists held besides the one {@code template} came from
|
||||
* @param connectors reads the jigsaw connectors of the element
|
||||
*/
|
||||
public record Element(int weight, String type, Optional<ResourceId> template, int listLevels, int omittedElements,
|
||||
ConnectorSource connectors) implements Member {
|
||||
public Element {
|
||||
Objects.requireNonNull(type, "type");
|
||||
Objects.requireNonNull(template, "template");
|
||||
Objects.requireNonNull(connectors, "connectors");
|
||||
}
|
||||
}
|
||||
|
||||
/** Reads the jigsaw connectors of one pool element. */
|
||||
@FunctionalInterface
|
||||
public interface ConnectorSource {
|
||||
/**
|
||||
* The connectors of the element, in the order the platform shuffles them with a seed.
|
||||
*
|
||||
* @param shuffleSeeds gives the seed to shuffle with; the platform asks only when it shuffles, so the caller's
|
||||
* random sequence advances once for each element that has connectors to shuffle
|
||||
* @throws StructureAccessException when the platform cannot read the connectors at all
|
||||
*/
|
||||
Connectors read(LongSupplier shuffleSeeds);
|
||||
}
|
||||
|
||||
/** Whether the platform could list an element's connectors. */
|
||||
public enum Availability {
|
||||
READ,
|
||||
/** The platform's version has no way to list them. */
|
||||
UNAVAILABLE,
|
||||
/** The platform answered with no list. */
|
||||
NO_COLLECTION
|
||||
}
|
||||
|
||||
/**
|
||||
* The connectors of an element.
|
||||
*
|
||||
* @param availability whether {@code connectors} were listed
|
||||
* @param connectors one entry for each jigsaw block, in the platform's shuffled order
|
||||
*/
|
||||
public record Connectors(Availability availability, List<Connector> connectors) {
|
||||
public Connectors {
|
||||
Objects.requireNonNull(availability, "availability");
|
||||
connectors = List.copyOf(connectors);
|
||||
}
|
||||
}
|
||||
|
||||
/** One jigsaw block of an element. */
|
||||
public sealed interface Connector permits JigsawConnector, UnreadableConnector {
|
||||
}
|
||||
|
||||
/**
|
||||
* A jigsaw block the platform could not convert.
|
||||
*
|
||||
* @param detail why, as the platform reports it
|
||||
*/
|
||||
public record UnreadableConnector(String detail) implements Connector {
|
||||
public UnreadableConnector {
|
||||
Objects.requireNonNull(detail, "detail");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A jigsaw block.
|
||||
*
|
||||
* @param position where it is, from the element's corner
|
||||
* @param front the side it connects through
|
||||
* @param top the side its top faces, which fixes the roll of a rollable joint
|
||||
* @param pool the pool it pulls its next piece from; empty when it names none
|
||||
* @param name the name other blocks target
|
||||
* @param target the name of the blocks it connects to
|
||||
* @param aligned whether the joint keeps the pieces aligned rather than letting them roll
|
||||
* @param finalState the block it turns into once it connects, as the platform writes block states; empty when
|
||||
* the block names none
|
||||
* @param selectionPriority which block is tried first
|
||||
* @param placementPriority which connected piece is expanded first
|
||||
*/
|
||||
public record JigsawConnector(BlockPosition position, BlockFace front, BlockFace top, Optional<ResourceId> pool,
|
||||
String name, String target, boolean aligned, Optional<String> finalState,
|
||||
int selectionPriority, int placementPriority) implements Connector {
|
||||
public JigsawConnector {
|
||||
Objects.requireNonNull(position, "position");
|
||||
Objects.requireNonNull(front, "front");
|
||||
Objects.requireNonNull(top, "top");
|
||||
Objects.requireNonNull(pool, "pool");
|
||||
Objects.requireNonNull(name, "name");
|
||||
Objects.requireNonNull(target, "target");
|
||||
Objects.requireNonNull(finalState, "finalState");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,10 @@ package com.volmit.tooling;
|
||||
import com.volmit.ModuleLog;
|
||||
import com.volmit.config.PackSettings;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.tooling.world.ToolWorlds;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A pack as tools see it: its current resources, its folder, and the one way to change files in it. Tools are file tools,
|
||||
@@ -32,4 +34,17 @@ public interface ToolPack {
|
||||
|
||||
/** The log of the module that asked. */
|
||||
ModuleLog log();
|
||||
|
||||
/** The worlds this pack's tools open on the platform. */
|
||||
ToolWorlds worlds();
|
||||
|
||||
/**
|
||||
* The providers of {@code service} that the pack's modules registered, in module id order and each module's in the
|
||||
* order it registered them; empty when no module provides it. This is how a module asks another module's service,
|
||||
* such as a structure tool asking who knows a structure.
|
||||
*
|
||||
* @param service an interface marked {@link com.volmit.service.Service}
|
||||
* @throws IllegalArgumentException when {@code service} is not such an interface
|
||||
*/
|
||||
<S> List<S> services(Class<S> service);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
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 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,
|
||||
List<BlockPosition> changed,
|
||||
Optional<ToolPlayer> player,
|
||||
Optional<BlockState> placed
|
||||
) {
|
||||
/** What changes a block. */
|
||||
public enum Cause {
|
||||
/** A player places a block. */
|
||||
PLACE,
|
||||
/** A player breaks a block. */
|
||||
BREAK,
|
||||
/** A player fills or empties a bucket. */
|
||||
BUCKET,
|
||||
/** A block forms, fades or spreads by itself, such as ice, snow, grass or coral. */
|
||||
PHYSICS,
|
||||
/** Water or lava flows. */
|
||||
FLUID,
|
||||
/** A plant or a sapling grows. */
|
||||
GROWTH,
|
||||
/** A block burns. */
|
||||
FIRE,
|
||||
/** Redstone power changes; cancelling keeps the old power. */
|
||||
REDSTONE,
|
||||
/** A piston pushes or pulls blocks. */
|
||||
PISTON,
|
||||
/** An explosion destroys blocks. */
|
||||
EXPLOSION,
|
||||
/** A mob or a falling block changes a block. */
|
||||
ENTITY,
|
||||
/** A player or a hopper changes what a container block holds. */
|
||||
CONTAINER,
|
||||
/** A furnace, a brewing stand, a crafter or a dispenser works. */
|
||||
MACHINE
|
||||
}
|
||||
|
||||
public BlockChange {
|
||||
Objects.requireNonNull(cause, "cause");
|
||||
positions = List.copyOf(positions);
|
||||
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,30 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.object.BlockEntityData;
|
||||
|
||||
/**
|
||||
* The chunk a {@link ChunkPainter} paints: blocks only, with the host's own block-entity handling behind it. A canvas
|
||||
* starts empty (air) and is valid only while {@link ChunkPainter#paint} runs; the host never lets a painter keep it.
|
||||
*
|
||||
* <p>X and Z are local to the chunk, 0 to 15. Y is the world's own Y, from {@link #minY()} up to but not including
|
||||
* {@link #maxY()}, as {@link ToolWorld#minY()} and {@link ToolWorld#maxY()} report it. A coordinate outside these
|
||||
* ranges throws {@link IllegalArgumentException}.
|
||||
*/
|
||||
public interface ChunkCanvas {
|
||||
/** The lowest Y a block can have in this world. */
|
||||
int minY();
|
||||
|
||||
/** The first Y above the world; the highest block has Y {@code maxY() - 1}. */
|
||||
int maxY();
|
||||
|
||||
/** Puts {@code state} at the position, replacing what the painter put there before. */
|
||||
void set(int x, int y, int z, BlockState state);
|
||||
|
||||
/**
|
||||
* Puts {@code state} at the position with {@code data} as its block entity, such as the contents of a chest or the
|
||||
* lines of a sign. The host applies the entity once the chunk is live; a state that has no block entity on the
|
||||
* platform ignores the data. Data in a format the host does not read fails with {@link IllegalArgumentException}.
|
||||
*/
|
||||
void set(int x, int y, int z, BlockState state, BlockEntityData data);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
/**
|
||||
* Where the chunks of a tool world come from: the host asks it for every chunk it generates, and for the ones
|
||||
* {@link ToolWorld#repaint} names. Nothing else shapes a tool world's terrain.
|
||||
*
|
||||
* <p>The host calls {@code paint} from its generation threads, several at once, and again for a chunk whenever it
|
||||
* repaints it, so a painter holds no per-call state and reads what it paints from state that is safe to read from any
|
||||
* thread. It must return quickly and must not call back into the {@link ToolWorld} that is waiting for it. A painter
|
||||
* that throws leaves its chunk empty and the failure in the host's log.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ChunkPainter {
|
||||
/** Paints chunk {@code chunkX}, {@code chunkZ} onto {@code canvas}. */
|
||||
void paint(int chunkX, int chunkZ, ChunkCanvas canvas);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* One slot of a {@link ToolMenu}.
|
||||
*
|
||||
* @param slot the slot counted along the rows from the top left, 0 to 8 in the first row, 9 to 17 in the second
|
||||
* @param icon what the slot shows
|
||||
* @param action called on the player's thread for every click on the slot; it must return quickly
|
||||
*/
|
||||
public record MenuButton(int slot, ToolItem icon, Consumer<MenuClick> action) {
|
||||
public MenuButton {
|
||||
if (slot < 0) {
|
||||
throw new IllegalArgumentException("A menu slot is not negative: " + slot);
|
||||
}
|
||||
Objects.requireNonNull(icon, "icon");
|
||||
Objects.requireNonNull(action, "action");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
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}; 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, 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
/**
|
||||
* One dust particle at a position of a tool world, for {@link ToolPlayer#particles}. The position is in world
|
||||
* coordinates with the world's own Y.
|
||||
*
|
||||
* @param rgb the colour as {@code 0xRRGGBB}
|
||||
* @param size the size of the speck, above zero
|
||||
*/
|
||||
public record Particle(double x, double y, double z, int rgb, float size) {
|
||||
private static final int MAX_RGB = 0xFFFFFF;
|
||||
|
||||
public Particle {
|
||||
if (rgb < 0 || rgb > MAX_RGB) {
|
||||
throw new IllegalArgumentException("A particle colour is 0xRRGGBB: " + Integer.toHexString(rgb));
|
||||
}
|
||||
if (!(size > 0)) {
|
||||
throw new IllegalArgumentException("A particle size is above zero: " + size);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* An item a tool hands to a player, or a menu shows as an icon: the platform's item, a name and lore, and a payload the
|
||||
* host keeps on the item. The host guarantees the payload survives on the item, as a string in the item's persistent
|
||||
* 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; 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, ToolText name, List<ToolText> lore, boolean glowing, String payload) {
|
||||
public ToolItem {
|
||||
Objects.requireNonNull(item, "item");
|
||||
Objects.requireNonNull(name, "name");
|
||||
Objects.requireNonNull(payload, "payload");
|
||||
lore = List.copyOf(lore);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
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; 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(ToolText title, int rows, List<MenuButton> buttons, Runnable onClose) {
|
||||
public static final int MAX_ROWS = 6;
|
||||
public static final int SLOTS_PER_ROW = 9;
|
||||
|
||||
public ToolMenu {
|
||||
Objects.requireNonNull(title, "title");
|
||||
if (rows < 1 || rows > MAX_ROWS) {
|
||||
throw new IllegalArgumentException("A menu has 1 to " + MAX_ROWS + " rows: " + rows);
|
||||
}
|
||||
buttons = List.copyOf(buttons);
|
||||
var used = new HashSet<Integer>();
|
||||
for (var button : buttons) {
|
||||
if (button.slot() >= rows * SLOTS_PER_ROW) {
|
||||
throw new IllegalArgumentException("Slot " + button.slot() + " is outside a menu of " + rows + " rows");
|
||||
}
|
||||
if (!used.add(button.slot())) {
|
||||
throw new IllegalArgumentException("Slot " + button.slot() + " is used twice");
|
||||
}
|
||||
}
|
||||
if (onClose == null) {
|
||||
onClose = () -> { };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.tooling.Sender;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* A player as a tool sees them. Positions are block positions of the world the player stands in, with that world's own Y.
|
||||
*
|
||||
* <p>Threads. The readers ({@link #world}, {@link #position}, {@link #yaw}, {@link #pitch}, {@link #sneaking},
|
||||
* {@link #held}) are valid only on the thread that owns the player, which is where every {@link ToolWorldListener} call
|
||||
* about them and every {@link #run} task runs. The methods that change the player ({@link #teleport} and the ones
|
||||
* below it) are safe from any thread: the host moves the change onto the owning thread. A player stays one object for
|
||||
* as long as they are online, though {@link #id()} is the safer key for a map.
|
||||
*/
|
||||
public interface ToolPlayer {
|
||||
UUID id();
|
||||
|
||||
/** 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. 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. */
|
||||
Optional<ToolWorld> world();
|
||||
|
||||
BlockPosition position();
|
||||
|
||||
/** The direction the player faces, in degrees, as the platform counts them. */
|
||||
float yaw();
|
||||
|
||||
float pitch();
|
||||
|
||||
boolean sneaking();
|
||||
|
||||
/**
|
||||
* The block the player looks at within {@code maxDistance} blocks, fluids not counting; empty when they look at none.
|
||||
* Like the other readers, valid only on the thread that owns the player.
|
||||
*/
|
||||
Optional<BlockPosition> targetBlock(int maxDistance);
|
||||
|
||||
/** Runs {@code task} on the thread that owns the player: at once when the caller is that thread, else later. */
|
||||
void run(Runnable task);
|
||||
|
||||
/**
|
||||
* Moves the player to the position in {@code world} and completes with whether they arrived. The host remembers
|
||||
* where a player came from when they enter a tool world from outside one, and {@link ToolWorld#close()} sends them
|
||||
* back there.
|
||||
*/
|
||||
CompletableFuture<Boolean> teleport(ToolWorld world, double x, double y, double z, float yaw, float pitch);
|
||||
|
||||
/**
|
||||
* Puts the player in creative mode when {@code on}, remembering the mode they had; turns it off by restoring that
|
||||
* mode. The host also restores it when it sends the player out of a tool world.
|
||||
*/
|
||||
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(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.
|
||||
* An empty title keeps the platform's own sidebar title. The sidebar goes away when the player leaves the tool world.
|
||||
*/
|
||||
void sidebar(ToolText title, List<ToolText> lines);
|
||||
|
||||
/** Removes the sidebar {@link #sidebar} showed. */
|
||||
void hideSidebar();
|
||||
|
||||
/** 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, 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();
|
||||
|
||||
/** Opens {@code menu} for the player, closing the menu they had open. */
|
||||
void openMenu(ToolMenu menu);
|
||||
|
||||
/** Closes the menu a tool opened for the player; does nothing when none is open. */
|
||||
void closeMenu();
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import com.volmit.geometry.BlockFace;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* A player using their hand in a tool world, handed to {@link ToolWorldListener#used}.
|
||||
*
|
||||
* @param hand the hand that was used
|
||||
* @param action what the player did
|
||||
* @param item the tool item in that hand; empty when the item is not one
|
||||
* @param block the block clicked; empty for a click in the air
|
||||
* @param face the face of the block clicked; empty for a click in the air
|
||||
*/
|
||||
public record ToolUse(Hand hand, Action action, Optional<ToolItem> item, Optional<BlockPosition> block, Optional<BlockFace> face) {
|
||||
public enum Hand { MAIN, OFF }
|
||||
|
||||
public enum Action {
|
||||
LEFT_CLICK_AIR,
|
||||
LEFT_CLICK_BLOCK,
|
||||
RIGHT_CLICK_AIR,
|
||||
RIGHT_CLICK_BLOCK,
|
||||
/** Stepping on a pressure plate, trampling farmland, touching a tripwire. */
|
||||
PHYSICAL
|
||||
}
|
||||
|
||||
public ToolUse {
|
||||
Objects.requireNonNull(hand, "hand");
|
||||
Objects.requireNonNull(action, "action");
|
||||
Objects.requireNonNull(item, "item");
|
||||
Objects.requireNonNull(block, "block");
|
||||
Objects.requireNonNull(face, "face");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import com.volmit.geometry.ChunkPos;
|
||||
import com.volmit.object.BlockEntityData;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* One open tool world. Every position here is a block of the world itself: X and Z are world coordinates and Y is the
|
||||
* world's own Y, from {@link #minY()} up to but not including {@link #maxY()}. That is not the height above the
|
||||
* dimension's bottom that {@link com.volmit.world.PackWorld} uses.
|
||||
*
|
||||
* <p>Threads. A host may split a world across threads, one for each region of it, as Folia does. {@link #owns} tells
|
||||
* whether the calling thread owns a position; reading and writing blocks is allowed only there, and {@link #run} and
|
||||
* {@link #call} get a task onto the owning thread from any other. Everything else here is safe from any thread.
|
||||
*/
|
||||
public interface ToolWorld {
|
||||
/** The name the tool gave in {@link ToolWorldSpec#name()}. Still answers after the world closed. */
|
||||
String name();
|
||||
|
||||
/** The lowest Y of the world. */
|
||||
int minY();
|
||||
|
||||
/** The first Y above the world. */
|
||||
int maxY();
|
||||
|
||||
/** The players standing in this world now, a snapshot. */
|
||||
List<ToolPlayer> players();
|
||||
|
||||
/** Whether the calling thread owns the block at {@code at}, so that it may read and write it. */
|
||||
boolean owns(BlockPosition at);
|
||||
|
||||
/**
|
||||
* Runs {@code task} on the thread that owns {@code at}: at once when the caller is that thread, else later. A
|
||||
* task that throws is logged by the host.
|
||||
*/
|
||||
void run(BlockPosition at, Runnable task);
|
||||
|
||||
/** Runs {@code task} like {@link #run} and completes with its result, or fails with what it threw. */
|
||||
<T> CompletableFuture<T> call(BlockPosition at, Supplier<T> task);
|
||||
|
||||
/**
|
||||
* The block at {@code at}. The state is complete, with every property.
|
||||
*
|
||||
* @throws IllegalStateException when the calling thread does not own {@code at}, or the world closed
|
||||
*/
|
||||
BlockState block(BlockPosition at);
|
||||
|
||||
/**
|
||||
* The block entity at {@code at}; empty when the block has none.
|
||||
*
|
||||
* @throws IllegalStateException when the calling thread does not own {@code at}, or the world closed
|
||||
*/
|
||||
Optional<BlockEntityData> blockEntity(BlockPosition at);
|
||||
|
||||
/**
|
||||
* Replaces the block at {@code at} with {@code state} and {@code data} as its block entity, without block updates
|
||||
* or {@link ToolWorldListener#blockChanging} calls, since the tool is the one changing it. Empty data leaves the
|
||||
* new block with the platform's default entity, if its state has one.
|
||||
*
|
||||
* @throws IllegalStateException when the calling thread does not own {@code at}, or the world closed
|
||||
*/
|
||||
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
|
||||
* every chunk is written, and fails if one could not be; chunks written before the failure stay written.
|
||||
*/
|
||||
CompletableFuture<Void> repaint(Collection<ChunkPos> chunks);
|
||||
|
||||
/**
|
||||
* Closes the world: sends every player back to where they were before they entered a tool world, restoring their
|
||||
* game mode, then unloads the world and deletes its files. {@link ToolWorldListener#closed()} is called once it
|
||||
* is unloaded. Closing a closed world completes at once. The future fails when the world could not be unloaded or
|
||||
* deleted; the host then deletes what is left when it starts next.
|
||||
*/
|
||||
CompletableFuture<Void> close();
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
/** A host refused to open a tool world for a reason its message tells the user and the pack's author can fix, such as a pack
|
||||
* with blocking errors. {@link ToolWorlds#open} completes with it; it is not a fault, so a tool reports the message and logs no
|
||||
* stack trace.
|
||||
*/
|
||||
public final class ToolWorldDenied extends RuntimeException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public ToolWorldDenied(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
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
|
||||
* it needs.
|
||||
*
|
||||
* <p>Threads. A call runs on the thread that owns the player or the position it is about, and the host waits for it:
|
||||
* return quickly, and hand slow work to another thread. The host calls only for the tool's own world. A listener that
|
||||
* throws is logged by the host and counts as having answered with the method's default.
|
||||
*
|
||||
* <p>The methods that return a boolean decide whether the host lets the thing happen: {@code false} cancels it.
|
||||
*/
|
||||
public interface ToolWorldListener {
|
||||
/** A player came into the world: joined the server in it, teleported or changed world into it, or respawned in it. Once for each stay. */
|
||||
default void entered(ToolPlayer player) {
|
||||
}
|
||||
|
||||
/** A player left the world: quit, teleported or changed world out of it. Once for each stay. */
|
||||
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) {
|
||||
}
|
||||
|
||||
/** A player started or stopped sneaking. */
|
||||
default void sneakToggled(ToolPlayer player, boolean sneaking) {
|
||||
}
|
||||
|
||||
/** A player used their hand. Return {@code false} to cancel what the platform would do with it, such as opening a container or placing a block. */
|
||||
default boolean used(ToolPlayer player, ToolUse use) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Something is about to change blocks. Return {@code false} to cancel it. The default freezes the world. */
|
||||
default boolean blockChanging(BlockChange change) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* A command is about to run: {@code line} is what was typed without the leading slash. {@code sender} is empty for the
|
||||
* console, which is asked on behalf of every tool world that is open. Return {@code false} to cancel it.
|
||||
*/
|
||||
default boolean command(Optional<ToolPlayer> sender, String line) {
|
||||
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) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* What a tool asks {@link ToolWorlds#open} for: a world of the tool's pack whose chunks come only from {@code painter},
|
||||
* and who hears about it.
|
||||
*
|
||||
* <p>The host opens it as a studio world of the tool's pack with its generation swapped for the painter, so its height
|
||||
* range is the pack dimension's; {@link ToolWorld#minY()} and {@link ToolWorld#maxY()} report it.
|
||||
*
|
||||
* @param name the tool's name for the world, unique among the open tool worlds of the pack: lower case letters,
|
||||
* digits, {@code _} and {@code -}, starting with a letter or a digit. It is not the platform's world name.
|
||||
* @param painter the only source of the world's chunks
|
||||
* @param listener told about the players, blocks and commands of the world
|
||||
*/
|
||||
public record ToolWorldSpec(String name, ChunkPainter painter, ToolWorldListener listener) {
|
||||
private static final Pattern NAME = Pattern.compile("[a-z0-9][a-z0-9_-]*");
|
||||
|
||||
public ToolWorldSpec {
|
||||
Objects.requireNonNull(name, "name");
|
||||
Objects.requireNonNull(painter, "painter");
|
||||
Objects.requireNonNull(listener, "listener");
|
||||
if (!NAME.matcher(name).matches()) {
|
||||
throw new IllegalArgumentException("A tool world name is lower case letters, digits, '_' and '-': " + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
* The tool worlds of one pack: worlds a tool opens to show or edit what it builds, such as a jigsaw studio. Reached
|
||||
* through {@link com.volmit.tooling.ToolPack#worlds()}. A platform without tool worlds still answers: {@link #open}
|
||||
* then returns a failed future and {@link #find} an empty result.
|
||||
*
|
||||
* <p>Safe to call from any thread, the server's main thread included.
|
||||
*/
|
||||
public interface ToolWorlds {
|
||||
/**
|
||||
* Opens a world whose chunks come only from {@code spec.painter()}. The future completes, on a host thread, when
|
||||
* players can enter the world. It fails with {@link IllegalStateException} when a tool world of that name is open
|
||||
* already, with {@link ToolWorldDenied} when the host refuses the pack a world (its message says why), and with the
|
||||
* cause when the platform cannot create the world; a failed open leaves no world behind.
|
||||
* The host closes every tool world it still has open when it shuts down: it hears {@link ToolWorldListener#closing}
|
||||
* of each and waits for it before {@link ToolWorldListener#closed}.
|
||||
*
|
||||
* <p>A tool world has no natural mob spawning, weather, daylight cycle, portals, vanilla structures or features.
|
||||
*/
|
||||
CompletableFuture<ToolWorld> open(ToolWorldSpec spec);
|
||||
|
||||
/** The open tool world of this pack called {@code name}; empty when none is open or it closed. */
|
||||
Optional<ToolWorld> find(String name);
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import com.volmit.config.PackSettings;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.resource.PlatformRegistries;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/** The pack one {@link Validator} checks, as the host would load it, and where it reports what it finds. Valid
|
||||
@@ -25,6 +26,16 @@ public interface ValidationContext {
|
||||
*/
|
||||
Optional<PlatformRegistries> platform();
|
||||
|
||||
/**
|
||||
* The providers of {@code service} that the pack's modules registered, in module id order and each module's in the
|
||||
* order it registered them; empty when no module provides it. This is how a validator asks another module, such as
|
||||
* whether a module knows a structure the pack names.
|
||||
*
|
||||
* @param service an interface marked {@link com.volmit.service.Service}
|
||||
* @throws IllegalArgumentException when {@code service} is not such an interface
|
||||
*/
|
||||
<S> List<S> services(Class<S> service);
|
||||
|
||||
/** Records {@code problem}. An {@link Severity#ERROR} stops the pack from loading once every validator has run. */
|
||||
void report(Problem problem);
|
||||
}
|
||||
@@ -2,6 +2,9 @@ package com.volmit.vanilla;
|
||||
|
||||
import com.volmit.config.PackSettings;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.world.PackWorld;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/** The world a {@link com.volmit.service.VanillaStructures} provider decides for. Valid only while the provider runs. */
|
||||
public interface VanillaStructureContext {
|
||||
@@ -10,4 +13,9 @@ public interface VanillaStructureContext {
|
||||
|
||||
/** The module settings the world's dimension declares: its own, and every region and biome it can reach. */
|
||||
PackSettings settings();
|
||||
|
||||
/** The world the provider decides for; empty when it decides before any world exists, such as for the setup of a dimension. */
|
||||
default Optional<PackWorld> world() {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.volmit.placement.Placements;
|
||||
import com.volmit.random.WorldRandom;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
@@ -87,4 +88,15 @@ public interface PackWorld {
|
||||
default <T> Optional<T> extension(Class<T> type) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The providers of {@code service} that the pack's modules registered, in module id order and each module's in the
|
||||
* order it registered them; empty when no module provides it. This is how a module asks another module's service,
|
||||
* such as a placement asking who builds a structure. A provider takes the world as the first argument of its
|
||||
* methods, so pass this one. The list does not change while the world runs.
|
||||
*
|
||||
* @param service an interface marked {@link com.volmit.service.Service}
|
||||
* @throws IllegalArgumentException when {@code service} is not such an interface
|
||||
*/
|
||||
<S> List<S> services(Class<S> service);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.volmit.world;
|
||||
import com.volmit.config.FeatureSettings;
|
||||
import com.volmit.data.SavedKey;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import com.volmit.mantle.CarvedVoid;
|
||||
import com.volmit.placement.PlacementRecord;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.List;
|
||||
@@ -43,6 +44,15 @@ public interface RunningWorld {
|
||||
*/
|
||||
Optional<PlacementRecord> placementAt(int worldX, int y, int worldZ);
|
||||
|
||||
/**
|
||||
* The void the cave carver planned at the block, as the world holds it now; empty where none was, or when the block's
|
||||
* chunk keeps no generation data any more, and on a platform that cannot read it. Y is the height above the
|
||||
* dimension's bottom.
|
||||
*/
|
||||
default Optional<CarvedVoid> carvedAt(int worldX, int y, int worldZ) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The value the module keeps with this world across restarts under {@code key}: the host's one instance of it for
|
||||
* the world, read from the world's storage the first time anyone asks (or {@code key.initial()} when none was
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.volmit.tooling;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.geometry.BlockFace;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PlatformStructuresTest {
|
||||
@Test
|
||||
void aTemplateKeepsItsOwnCopyOfItsBlocks() {
|
||||
var blocks = new ArrayList<>(List.of(TemplateBlock.of(
|
||||
new BlockPosition(0, 0, 0), BlockState.parse("minecraft:stone"), Optional.empty())));
|
||||
var template = new StructureTemplate(new BlockPosition(1, 1, 1), 1, 0, blocks);
|
||||
|
||||
blocks.clear();
|
||||
|
||||
assertEquals(1, template.blocks().size());
|
||||
assertThrows(UnsupportedOperationException.class, () -> template.blocks().clear());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPoolListsEveryMemberWhetherTheToolCanReadItOrNot() {
|
||||
var unreadable = new TemplatePool.Unreadable(TemplatePool.Part.ENTRY, "no weight");
|
||||
var element = new TemplatePool.Element(3, "SinglePoolElement",
|
||||
Optional.of(ResourceId.parse("minecraft:village/house")), 0, 0,
|
||||
seeds -> new TemplatePool.Connectors(TemplatePool.Availability.READ, List.of(
|
||||
new TemplatePool.JigsawConnector(new BlockPosition(1, 2, 3), BlockFace.NORTH, BlockFace.UP,
|
||||
Optional.empty(), "minecraft:door", "minecraft:door", false, Optional.empty(), 0, 0))));
|
||||
var pool = new TemplatePool(List.of(new TemplatePool.Absent(), unreadable, element), Optional.empty(), Optional.empty());
|
||||
|
||||
assertEquals(3, pool.members().size());
|
||||
assertEquals(1, element.connectors().read(() -> 7L).connectors().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAccessFailureSaysWhetherAskingAgainMayHelp() {
|
||||
assertTrue(new StructureAccessException("registry not ready", true).retryable());
|
||||
assertEquals("no such member", new StructureAccessException("no such member", false).getMessage());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package com.volmit.tooling.world;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.geometry.BlockPosition;
|
||||
import com.volmit.geometry.ChunkPos;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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, ToolText.plain("Tool"), List.of(ToolText.plain("line")), false, "");
|
||||
|
||||
@Test
|
||||
void aChunkIsTheBlockCoordinatesDividedBySixteenRoundedDown() {
|
||||
assertEquals(new ChunkPos(0, 0), ChunkPos.containing(new BlockPosition(15, 70, 0)));
|
||||
assertEquals(new ChunkPos(1, -1), ChunkPos.containing(new BlockPosition(16, 0, -1)));
|
||||
assertEquals(new ChunkPos(-2, -1), ChunkPos.containing(new BlockPosition(-17, 0, -16)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aToolWorldNameIsLowerCaseWordsOfLettersDigitsUnderscoresAndDashes() {
|
||||
ChunkPainter painter = (x, z, canvas) -> { };
|
||||
var listener = new ToolWorldListener() { };
|
||||
|
||||
assertEquals("jigsaw-1_a", new ToolWorldSpec("jigsaw-1_a", painter, listener).name());
|
||||
assertThrows(IllegalArgumentException.class, () -> new ToolWorldSpec("Jigsaw", painter, listener));
|
||||
assertThrows(IllegalArgumentException.class, () -> new ToolWorldSpec("two words", painter, listener));
|
||||
assertThrows(IllegalArgumentException.class, () -> new ToolWorldSpec("", painter, listener));
|
||||
assertThrows(IllegalArgumentException.class, () -> new ToolWorldSpec("-dash", painter, listener));
|
||||
assertThrows(NullPointerException.class, () -> new ToolWorldSpec("name", null, listener));
|
||||
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(ToolText.plain("one")));
|
||||
var item = new ToolItem(STICK, ToolText.plain("Name"), lore, true, "payload");
|
||||
lore.add(ToolText.plain("two"));
|
||||
|
||||
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(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(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(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());
|
||||
withHandler.onClose().run();
|
||||
assertTrue(closed.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aBlockChangeNamesAtLeastOneBlock() {
|
||||
var position = new BlockPosition(1, 2, 3);
|
||||
var change = new BlockChange(BlockChange.Cause.PLACE, List.of(position), Optional.empty(),
|
||||
Optional.of(BlockState.parse("minecraft:stone")));
|
||||
|
||||
assertEquals(List.of(position), change.positions());
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> 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() { };
|
||||
var change = new BlockChange(BlockChange.Cause.PISTON, List.of(new BlockPosition(0, 0, 0)), Optional.empty(), Optional.empty());
|
||||
|
||||
assertFalse(listener.blockChanging(change));
|
||||
assertTrue(listener.command(Optional.empty(), "gamemode creative"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aParticleHasAnRgbColourAndAPositiveSize() {
|
||||
assertEquals(0xFF00FF, new Particle(0, 0, 0, 0xFF00FF, 1.5F).rgb());
|
||||
assertThrows(IllegalArgumentException.class, () -> new Particle(0, 0, 0, 0x1000000, 1F));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Particle(0, 0, 0, -1, 1F));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Particle(0, 0, 0, 0, 0F));
|
||||
assertThrows(IllegalArgumentException.class, () -> new Particle(0, 0, 0, 0, Float.NaN));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user