From d17a01741810ba704ebcb5a2a1be35046eb0ad9c Mon Sep 17 00:00:00 2001 From: piexel Date: Sat, 10 Oct 2026 18:02:08 +0200 Subject: [PATCH] PackResources.idOf: the id a pack gives the resource its files name by a key (ResourceId.ofPackKey) --- CONTRACTS.md | 2 +- README.md | 4 +++- .../java/com/volmit/resource/PackResources.java | 9 +++++++++ src/main/java/com/volmit/resource/ResourceId.java | 14 ++++++++++++++ src/test/java/com/volmit/ApiValuesTest.java | 7 +++++++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CONTRACTS.md b/CONTRACTS.md index d86722509..5f448c548 100644 --- a/CONTRACTS.md +++ b/CONTRACTS.md @@ -29,7 +29,7 @@ types that carry them and lists what the Iris adapter still owes. | Noise from pack-described styles | `NoiseStyle`, `StyledRange`, `PackResources.noise()`, `NoiseFactory`, `NoiseSampler` | | Values in settings | `BlockSpec`, `PaletteSpec`, `NoiseStyle`, `StyledRange`, `Range`, `BlockPosition`, `JigsawOverrides`, `Object` as `ValueKind.AnyKind` | | Decisions over and fit of the platform's own structures | service `VanillaStructures` (`rule`, `starts`, `shift`, `reach`, `beforePieces`, `afterPieces`, `setup`), `VanillaStructureRule`, `VanillaStructureContext`, `VanillaStructureStart`, `VanillaStructureStartContext`, `VanillaStructureSetup`, `PlatformStart`, `StartMemo`, `StartReach`, `StructureFit` (`Start`, `Piece`, `ColumnVisitor`), `StructureStep`, `TerrainAdaptation`, `PieceKind`, `TemplateCells`, `world.BlockAccess`, host `FeatureCatalog.registrations(service)` | -| Pack-scoped resolution | `ResourceId`, `ResourceKey`, `PackResources` (`find`, `ids`), `PackBiome`, `PackRegion` | +| Pack-scoped resolution | `ResourceId` (`ofPackKey`), `ResourceKey`, `PackResources` (`find`, `ids`, `idOf`), `PackBiome`, `PackRegion` | | Module-owned pack resources | `ResourceKind`, `FeatureRegistry.registerResourceKind`, `@ResourceRef`, host `ModuleHost.reservingFolders`, `FeatureCatalog.resourceKinds()`, `ResourceKindSchema`, `ValueKind.ResourceReferenceKind` | | Module-owned pack checks | `Validator`, `ValidationContext`, `Problem`, `Severity`, `Subject`, `FeatureRegistry.registerValidator`, host `FeatureCatalog.validators()` | | Per-world module state | `PackWorld` (`state`), `InvocationContext.world()`, host `WorldStates` | diff --git a/README.md b/README.md index 7e5022ef8..170d8e7af 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,9 @@ List all = resources.ids(Keep.class); // every keep of the pack, in ``` - A file's key is its path below the folder without `.json` (`towers/north`), and its id is - `:`, like every pack resource. JSON keys inside the file are the class's field names. + `:`, like every pack resource (`resources.idOf("towers/north")` makes it, in lower case with + characters outside `[a-z0-9_./-]` replaced by `_`: `ResourceId.ofPackKey`). JSON keys inside the file are the class's + field names. - The kind's id follows the module id. The folder is one folder name directly below the pack root; the module chooses it, so a kind that replaces one of Iris's own keeps the folder and file format packs already have. - One folder holds one kind and one class backs one kind: `PackResources` finds resources by type. Two kinds diff --git a/src/main/java/com/volmit/resource/PackResources.java b/src/main/java/com/volmit/resource/PackResources.java index 733ba0319..5795a3eb6 100644 --- a/src/main/java/com/volmit/resource/PackResources.java +++ b/src/main/java/com/volmit/resource/PackResources.java @@ -35,6 +35,15 @@ public interface PackResources { */ List ids(Class type); + /** The id of the resource the pack's files name by {@code key} (the file path inside the kind's folder, without + * {@code .json}), such as {@code overworld:dungeon/chest} for {@code dungeon/chest}; see + * {@link ResourceId#ofPackKey}. Settings that name a resource, such as a {@link com.volmit.config.ResourceRef}, + * hold such a key. + */ + default ResourceId idOf(String key) { + return ResourceId.ofPackKey(packId().namespace(), key); + } + default T require(ResourceKey key) { return find(key).orElseThrow(() -> new NoSuchElementException("Missing resource: " + key.id())); } diff --git a/src/main/java/com/volmit/resource/ResourceId.java b/src/main/java/com/volmit/resource/ResourceId.java index a69525e70..4b1ff3665 100644 --- a/src/main/java/com/volmit/resource/ResourceId.java +++ b/src/main/java/com/volmit/resource/ResourceId.java @@ -1,5 +1,6 @@ package com.volmit.resource; +import java.util.Locale; import java.util.Objects; import java.util.regex.Pattern; @@ -7,6 +8,8 @@ import java.util.regex.Pattern; public record ResourceId(String namespace, String path) implements Comparable { private static final Pattern NAMESPACE = Pattern.compile("[a-z0-9_.-]+"); private static final Pattern PATH = Pattern.compile("[a-z0-9_./-]+"); + private static final Pattern NOT_PATH = Pattern.compile("[^a-z0-9_./-]"); + private static final String PATH_REPLACEMENT = "_"; public ResourceId { Objects.requireNonNull(namespace, "namespace"); @@ -28,6 +31,17 @@ public record ResourceId(String namespace, String path) implements Comparable