Add pattern views to the studio tooling

A StudioView is now either a SceneView (block scenes) or a PatternView:
entries a module lays out from above, one block per column, at a depth
below the top, over a PatternArea. BlockPattern holds the picks and the
blocks they index. Both kinds register through ToolingRegistry.registerView
and share one id space in the host.
This commit is contained in:
piexel committed 2026-11-06 02:39:40 +01:00
1 parent a658cd2446
commit 2befcb9487
13 files changed
+275 -59

No files matched your search

@@ -0,0 +1,63 @@
package com.volmit.tooling;
import com.volmit.block.BlockState;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* What a {@link PatternView} places over a square of columns: the distinct blocks, and which of them each column holds,
* row by row from the north west.
*/
public final class BlockPattern {
/** What {@link #pickAt} gives for a column the pattern places nothing at. */
public static final int NO_BLOCK = -1;
private final int columns;
private final List<BlockState> blocks;
private final int[] picks;
private BlockPattern(int columns, List<BlockState> blocks, int[] picks) {
this.columns = columns;
this.blocks = List.copyOf(blocks);
this.picks = picks;
}
/**
* The pattern of {@code columns} by {@code columns} columns holding {@code columnBlocks}, row by row from the north
* west, null where nothing is placed.
*/
public static BlockPattern of(int columns, BlockState[] columnBlocks) {
if (columns < 1 || columnBlocks.length != columns * columns) {
throw new IllegalArgumentException("A pattern " + columns + " columns across holds " + columns * columns
+ " columns, not " + columnBlocks.length);
}
List<BlockState> blocks = new ArrayList<>();
Map<BlockState, Integer> indexByBlock = new HashMap<>();
int[] picks = new int[columnBlocks.length];
for (int column = 0; column < columnBlocks.length; column++) {
BlockState block = columnBlocks[column];
picks[column] = block == null ? NO_BLOCK : indexByBlock.computeIfAbsent(block, added -> {
blocks.add(added);
return blocks.size() - 1;
});
}
return new BlockPattern(columns, blocks, picks);
}
public int columns() {
return columns;
}
/** The distinct blocks the pattern places, in the order they first appear. */
public List<BlockState> blocks() {
return blocks;
}
/** Which of {@link #blocks()} the column holds, or {@link #NO_BLOCK}. */
public int pickAt(int column, int row) {
return picks[row * columns + column];
}
}
@@ -0,0 +1,26 @@
package com.volmit.tooling;
/**
* A square of columns a {@link PatternView} is asked for: {@code columns} by {@code columns} of them, one every
* {@code blocksPerColumn} blocks, the first at block ({@code x}, {@code z}), row by row from the north west.
*/
public record PatternArea(int x, int z, int columns, int blocksPerColumn) {
public PatternArea {
if (columns < 1) {
throw new IllegalArgumentException("A pattern area has at least 1 column across, not " + columns);
}
if (blocksPerColumn < 1) {
throw new IllegalArgumentException("A pattern area has a column every block or farther apart, not every " + blocksPerColumn);
}
}
/** The block x of the column {@code column} across. */
public int blockX(int column) {
return x + column * blocksPerColumn;
}
/** The block z of the row {@code row} down. */
public int blockZ(int row) {
return z + row * blocksPerColumn;
}
}
@@ -0,0 +1,28 @@
package com.volmit.tooling;
import java.util.List;
import java.util.Objects;
/**
* One entry of a {@link PatternView}'s catalogue.
*
* @param key what the view names the entry by in {@link PatternView#pattern}
* @param name what the studio lists it as, such as "Layer 1"
* @param group what the studio lists it under, such as its biome's name; blank for none
* @param depths how many blocks deep the entry can reach, at least 1
* @param facts short lines the studio shows with the entry, such as "2 to 4 blocks thick"
*/
public record PatternEntry(String key, String name, String group, int depths, List<String> facts) {
public PatternEntry {
Objects.requireNonNull(key, "key");
Objects.requireNonNull(name, "name");
Objects.requireNonNull(group, "group");
facts = List.copyOf(facts);
if (key.isBlank()) {
throw new IllegalArgumentException("A pattern entry needs a key");
}
if (depths < 1) {
throw new IllegalArgumentException("Pattern entry " + key + " reaches " + depths + " blocks deep; an entry reaches at least 1");
}
}
}
@@ -0,0 +1,23 @@
package com.volmit.tooling;
import com.volmit.world.PackWorld;
import java.util.List;
import java.util.Optional;
/**
* A studio view of the block patterns a module lays over the world, such as a biome's palettes: a catalogue of
* entries, each drawn from above, one block per column, at a depth below its top.
*/
public non-sealed interface PatternView extends StudioView {
/** The view's entries in the world's pack, in the order the studio lists them. */
List<PatternEntry> entries(PackWorld world);
/**
* The blocks the entry named {@code key} places {@code depth} blocks below its top across {@code area}, as the world
* places them; empty when the pack has no such entry.
*
* @param depth from 0, the top block, to the entry's {@link PatternEntry#depths()}, exclusive
*/
Optional<BlockPattern> pattern(PackWorld world, String key, int depth, PatternArea area);
}
@@ -1,7 +1,6 @@
package com.volmit.tooling;
import com.volmit.object.GeneratedObject;
import com.volmit.resource.ResourceId;
import com.volmit.world.PackWorld;
import java.util.List;
@@ -9,19 +8,9 @@ import java.util.Optional;
/**
* A studio view of the block scenes a module builds from the pack, such as its objects: a catalogue of entries, each
* with variants the studio draws in 3D. The studio asks on its own threads, several at once, for a world of the pack
* it shows; a view keeps nothing between calls that the world's state ({@link PackWorld#state}) should keep.
* with variants the studio draws in 3D.
*/
public interface SceneView {
/** The module's id, or one starting with it and {@code /}, such as {@code iris:objects}. */
ResourceId id();
/** The view's name in the studio's list of views, such as "Objects". */
String label();
/** One line on what the view shows. */
String description();
public non-sealed interface SceneView extends StudioView {
/** The view's entries in the world's pack, in the order the studio lists them. */
List<SceneEntry> entries(PackWorld world);
@@ -0,0 +1,20 @@
package com.volmit.tooling;
import com.volmit.resource.ResourceId;
import com.volmit.world.PackWorld;
/**
* A view a module adds to the studio, of one of the kinds the studio draws. The studio asks on its own threads, several
* at once, for a world of the pack it shows; a view keeps nothing between calls that the world's state
* ({@link PackWorld#state}) should keep.
*/
public sealed interface StudioView permits SceneView, PatternView {
/** The module's id, or one starting with it and {@code /}, such as {@code iris:objects}. */
ResourceId id();
/** The view's name in the studio's list of views, such as "Objects". */
String label();
/** One line on what the view shows. */
String description();
}
@@ -3,9 +3,9 @@ package com.volmit.tooling;
/**
* Where a module adds its tools to the studio, given in {@link com.volmit.ModuleSetup#tooling()} and accepted only while
* {@link com.volmit.IrisModule#setup} runs. A view's id is the module's id or starts with it and {@code /}; a second
* view with an id already registered fails the pack load, naming both modules.
* view with an id already registered, of either kind, fails the pack load, naming both modules.
*/
public interface ToolingRegistry {
/** Adds a view of block scenes the module builds from the pack, such as its objects. */
void registerSceneView(SceneView view);
/** Adds a view to the studio: block scenes such as the pack's objects, or block patterns such as its palettes. */
void registerView(StudioView view);
}
@@ -0,0 +1,42 @@
package com.volmit.tooling;
import com.volmit.block.BlockState;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class BlockPatternTest {
private static final BlockState GRASS = BlockState.parse("minecraft:grass_block");
private static final BlockState DIRT = BlockState.parse("minecraft:dirt");
@Test
void keepsEachBlockOnceAndWhichColumnHoldsIt() {
BlockPattern pattern = BlockPattern.of(2, new BlockState[]{GRASS, DIRT, null, BlockState.parse("minecraft:grass_block")});
assertEquals(List.of(GRASS, DIRT), pattern.blocks());
assertEquals(0, pattern.pickAt(0, 0));
assertEquals(1, pattern.pickAt(1, 0));
assertEquals(BlockPattern.NO_BLOCK, pattern.pickAt(0, 1));
assertEquals(0, pattern.pickAt(1, 1));
}
@Test
void needsAColumnForEverySpotOfTheSquare() {
var short_ = assertThrows(IllegalArgumentException.class, () -> BlockPattern.of(2, new BlockState[3]));
assertTrue(short_.getMessage().contains("holds 4 columns, not 3"), short_.getMessage());
}
@Test
void placesAreaColumnsFromItsCornerAtItsSpacing() {
PatternArea area = new PatternArea(-64, 32, 16, 4);
assertEquals(-56, area.blockX(2));
assertEquals(44, area.blockZ(3));
assertThrows(IllegalArgumentException.class, () -> new PatternArea(0, 0, 16, 0));
}
@Test
void anEntryReachesAtLeastOneBlockDeep() {
assertThrows(IllegalArgumentException.class, () -> new PatternEntry("plains#layer/0", "Layer 1", "Plains", 0, List.of()));
}
}
@@ -41,7 +41,7 @@ public final class FeatureCatalog {
private final Map<Class<?>, List<ServiceRegistration>> registrationsByService;
private final Map<Class<?>, List<?>> providersByService;
private final Optional<PlacementTypeRegistration> placementType;
private final List<SceneViewRegistration> sceneViews;
private final List<StudioViewRegistration> studioViews;
FeatureCatalog(
Map<Class<?>, FeatureDescriptor<?>> settings,
@@ -52,9 +52,9 @@ public final class FeatureCatalog {
List<LifecycleRegistration> lifecycles,
List<ServiceRegistration> services,
PlacementTypeRegistration placementType,
Collection<SceneViewRegistration> sceneViews) {
Collection<StudioViewRegistration> studioViews) {
this.placementType = Optional.ofNullable(placementType);
this.sceneViews = sorted(sceneViews, registration -> registration.view().id());
this.studioViews = sorted(studioViews, registration -> registration.view().id());
this.settings = Map.copyOf(settings);
this.objects = Map.copyOf(objects);
this.mantle = Map.copyOf(mantle);
@@ -93,9 +93,9 @@ public final class FeatureCatalog {
return validators;
}
/** The studio scene views modules registered, by view id. */
public List<SceneViewRegistration> sceneViews() {
return sceneViews;
/** The studio views modules registered, of every kind, by view id. */
public List<StudioViewRegistration> studioViews() {
return studioViews;
}
/** The lifecycle hooks modules registered, by module id, each module's in the order it registered them. */
@@ -11,7 +11,7 @@ import com.volmit.object.ObjectGeneratorFactory;
import com.volmit.resource.ResourceId;
import com.volmit.resource.ResourceKind;
import com.volmit.service.Providers;
import com.volmit.tooling.SceneView;
import com.volmit.tooling.StudioView;
import com.volmit.tooling.ToolingRegistry;
import com.volmit.validation.Validator;
import java.util.ArrayList;
@@ -31,7 +31,7 @@ import java.util.Set;
* settings ids must be the registering module's id or start with it and '/' ({@link com.volmit.ModuleInfo#ownsId}).
* Resource kinds have unique ids, types and folders, and no kind may read a folder the host reserves for its own
* files. Lifecycle hooks and service providers are registered by the module that owns them; a service whose providers
* are {@link Providers#ONE} has at most one. Studio scene views have unique ids the registering module owns. A failed
* are {@link Providers#ONE} has at most one. Studio views have ids unique across their kinds that the registering module owns. A failed
* registration leaves the registry unchanged. A failed module callback invalidates the host's whole build. This does
* not decode JSON, discover modules or instantiate settings.
*/
@@ -47,7 +47,7 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry {
private final List<ValidatorRegistration> validators = new ArrayList<>();
private final List<LifecycleRegistration> lifecycles = new ArrayList<>();
private final List<ServiceRegistration> services = new ArrayList<>();
private final Map<ResourceId, SceneViewRegistration> sceneViews = new HashMap<>();
private final Map<ResourceId, StudioViewRegistration> studioViews = new HashMap<>();
private final Set<String> reservedFolders;
private final List<DiscoveredModule> registered = new ArrayList<>();
private PlacementTypeRegistration placementType;
@@ -199,18 +199,18 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry {
}
@Override
public void registerSceneView(SceneView view) {
public void registerView(StudioView view) {
checkOpen();
Objects.requireNonNull(view, "view");
ResourceId id = Objects.requireNonNull(view.id(), "view id");
requireOwnedId("Scene view", id);
var added = new SceneViewRegistration(view, registering);
var existing = sceneViews.get(id);
requireOwnedId("Studio view", id);
var added = new StudioViewRegistration(view, registering);
var existing = studioViews.get(id);
if (existing != null) {
throw new IllegalArgumentException("Scene view " + id + " is registered by module " + existing.module()
throw new IllegalArgumentException("Studio view " + id + " is registered by module " + existing.module()
+ " already, and module " + added.module() + " registers it too; give one of them another id");
}
sceneViews.put(id, added);
studioViews.put(id, added);
}
/** Catalogs a settings type named by a stage's when(...), whose owner {@link StageGraphBuilder}
@@ -234,7 +234,7 @@ public final class ModuleRegistry implements FeatureRegistry, ToolingRegistry {
public FeatureCatalog freeze() {
if (catalog == null) {
catalog = new FeatureCatalog(settings, objects, mantle, resourceKinds.values(), validators, lifecycles, services,
placementType, sceneViews.values());
placementType, studioViews.values());
}
return catalog;
}
@@ -1,18 +0,0 @@
package com.volmit.host;
import com.volmit.tooling.SceneView;
import java.util.Objects;
/** A studio scene view and the module that registered it. */
public record SceneViewRegistration(SceneView view, DiscoveredModule module) {
public SceneViewRegistration {
Objects.requireNonNull(view, "view");
Objects.requireNonNull(module, "module");
}
@Override
public String toString() {
return "scene view " + view.id() + " of module " + module;
}
}
@@ -0,0 +1,27 @@
package com.volmit.host;
import com.volmit.tooling.PatternView;
import com.volmit.tooling.SceneView;
import com.volmit.tooling.StudioView;
import java.util.Objects;
/** A studio view and the module that registered it. */
public record StudioViewRegistration(StudioView view, DiscoveredModule module) {
public StudioViewRegistration {
Objects.requireNonNull(view, "view");
Objects.requireNonNull(module, "module");
}
@Override
public String toString() {
return kindOf(view) + " " + view.id() + " of module " + module;
}
static String kindOf(StudioView view) {
return switch (view) {
case SceneView ignored -> "scene view";
case PatternView ignored -> "pattern view";
};
}
}
@@ -119,25 +119,41 @@ class ModuleRegistryTest {
}
@Test
void sceneViewsHaveUniqueIdsTheirModuleOwnsAndAreCataloguedById() {
void studioViewsHaveIdsUniqueAcrossKindsTheirModuleOwnsAndAreCataloguedById() {
var registry = new ModuleRegistry();
ModuleSetups.run(DiscoveredModule.builtIn(new IrisModule() {
public ModuleInfo info() { return ModuleInfo.of("iris:objects", "Objects", "1"); }
public void setup(ModuleSetup setup) {
setup.tooling().registerSceneView(sceneView("iris:objects/procedural"));
setup.tooling().registerSceneView(sceneView("iris:objects"));
setup.tooling().registerView(sceneView("iris:objects/procedural"));
setup.tooling().registerView(sceneView("iris:objects"));
setup.tooling().registerView(patternView("iris:objects/footprints"));
var duplicate = assertThrows(IllegalArgumentException.class,
() -> setup.tooling().registerSceneView(sceneView("iris:objects")));
assertTrue(duplicate.getMessage().contains("Scene view iris:objects is registered by module"), duplicate.getMessage());
() -> setup.tooling().registerView(patternView("iris:objects")));
assertTrue(duplicate.getMessage().contains("Studio view iris:objects is registered by module"), duplicate.getMessage());
var foreign = assertThrows(IllegalArgumentException.class,
() -> setup.tooling().registerSceneView(sceneView("iris:terrain/palettes")));
() -> setup.tooling().registerView(sceneView("iris:terrain/palettes")));
assertTrue(foreign.getMessage().contains("must be iris:objects or start with iris:objects/"), foreign.getMessage());
}
}), new StageGraphBuilder(), registry);
assertThrows(IllegalStateException.class, () -> registry.registerSceneView(sceneView("iris:objects/loose")));
assertEquals(List.of("iris:objects", "iris:objects/procedural"),
registry.freeze().sceneViews().stream().map(view -> view.view().id().toString()).toList());
assertThrows(IllegalStateException.class, () -> registry.registerView(sceneView("iris:objects/loose")));
var views = registry.freeze().studioViews();
assertEquals(List.of("iris:objects", "iris:objects/footprints", "iris:objects/procedural"),
views.stream().map(view -> view.view().id().toString()).toList());
assertTrue(views.get(1).toString().startsWith("pattern view iris:objects/footprints of module"), views.get(1).toString());
}
private static com.volmit.tooling.PatternView patternView(String id) {
return new com.volmit.tooling.PatternView() {
public com.volmit.resource.ResourceId id() { return com.volmit.resource.ResourceId.parse(id); }
public String label() { return id; }
public String description() { return id; }
public List<com.volmit.tooling.PatternEntry> entries(com.volmit.world.PackWorld world) { return List.of(); }
public Optional<com.volmit.tooling.BlockPattern> pattern(com.volmit.world.PackWorld world, String key, int depth,
com.volmit.tooling.PatternArea area) {
return Optional.empty();
}
};
}
private static com.volmit.tooling.SceneView sceneView(String id) {