mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Batch 2 of the API review: settings stacks, chunk helpers, pack context
- SettingsStack<F> replaces List<Entry<F>> wherever settings stack: entries in level order, values(), dimension(), level(Level) and mostSpecific(). - com.volmit.config.Level replaces FeatureAttachment.Target; FeatureAttachment folds into FeatureDescriptor.targets(). - FeatureSettings.declaredInChunk answers every entry any column of a chunk declares, each once. - ChunkContext gives chunkX/chunkZ, centreX/centreZ and area(). - registerSettings takes several types at once. - ModuleLifecycle.packLoaded(PackContext) carries resources, settings and the module's log. - TerrainSampler.surfaceHeight gives the surface in whole blocks. - Stage contexts drop worldSeed() and resources(); world() always answers. The scoped world is kept per world and module. - Javadoc fixes; README, DESIGN and CONTRACTS updated.
This commit is contained in:
1 parent
1677f4414b
commit
7143908d4b
67 files changed
+870
-423
No files matched your search
@@ -4,7 +4,8 @@ import java.lang.annotation.*;
|
||||
|
||||
/** Identity and JSON attachment for an IrisFeature configuration class. The {@code id}, such as
|
||||
* {@code "example:moss"}, is the feature's identity in the stage graph, logs and lookups by class. Packs write
|
||||
* the settings under the short {@code key}, such as {@code "moss": {...}}, at each of the targets.
|
||||
* the settings under the short {@code key}, such as {@code "moss": {...}}, at each of the targets, the {@link Level}s
|
||||
* of a pack's dimension, region, biome or object placement files.
|
||||
*
|
||||
* <p>The key is a plain JSON field name: not blank, no ':'. It is unique per target across all loaded modules
|
||||
* and the JSON fields the host itself uses at that target, so it must not be a field of a biome, region or
|
||||
@@ -22,5 +23,6 @@ public @interface Feature {
|
||||
String name();
|
||||
String key();
|
||||
String root() default "";
|
||||
com.volmit.FeatureAttachment.Target[] targets();
|
||||
/** The levels packs may write the settings at. */
|
||||
Level[] targets();
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.volmit.config;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.generation.ChunkContext;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -12,16 +13,39 @@ import java.util.Optional;
|
||||
* <p>Every level that declares the settings contributes its own entry: the dimension, the region and
|
||||
* the biome of the column, and in a placement the placement itself. Levels stack: nothing is merged or
|
||||
* overridden, and a module decides what several entries mean, such as running a deposit once per
|
||||
* entry. Entries are immutable lists in level order ({@link FeatureAttachment.Target} order). The
|
||||
* biome is the column's surface biome; cave biomes are not a level. Values must be treated as
|
||||
* entry. The biome is the column's surface biome; cave biomes are not a level. Values must be treated as
|
||||
* read-only. Bounds and lifetime match the invocation's sampler.
|
||||
*
|
||||
* <p>Something that spans several columns, such as a chunk-wide pass or a placed object, reads the
|
||||
* settings at one column it treats as its origin; per-chunk behaviour reads the chunk's centre column
|
||||
* (local 7, 7).
|
||||
* ({@link ChunkContext#centreX()}, {@link ChunkContext#centreZ()}).
|
||||
*/
|
||||
public interface FeatureSettings {
|
||||
<F extends IrisFeature> List<Entry<F>> at(Class<F> featureType, int worldX, int worldZ);
|
||||
/** The entries of {@code featureType} at this column, in level order; empty when no level declares it. */
|
||||
<F extends IrisFeature> SettingsStack<F> at(Class<F> featureType, int worldX, int worldZ);
|
||||
|
||||
/** Every distinct entry of {@code featureType} at the 16 by 16 columns of chunk {@code chunkX}, {@code chunkZ}, in
|
||||
* level order; entries of one level in the order their first column was found, columns by X and then Z. Entries are
|
||||
* distinct by level and source. The chunk and mantle runners decide with it whether a conditioned stage runs in a
|
||||
* chunk. The default asks {@link #at} for every column; a host that knows which regions and biomes a chunk holds
|
||||
* answers it from them.
|
||||
*/
|
||||
default <F extends IrisFeature> SettingsStack<F> declaredInChunk(Class<F> featureType, int chunkX, int chunkZ) {
|
||||
var declarations = new HashSet<Declaration>();
|
||||
var distinct = new ArrayList<Entry<F>>();
|
||||
int originX = chunkX * ChunkContext.CHUNK_WIDTH;
|
||||
int originZ = chunkZ * ChunkContext.CHUNK_WIDTH;
|
||||
for (int x = 0; x < ChunkContext.CHUNK_WIDTH; x++) {
|
||||
for (int z = 0; z < ChunkContext.CHUNK_WIDTH; z++) {
|
||||
for (var entry : at(featureType, originX + x, originZ + z)) {
|
||||
if (declarations.add(new Declaration(entry.level(), entry.source()))) {
|
||||
distinct.add(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return SettingsStack.of(distinct);
|
||||
}
|
||||
|
||||
/** The settings {@code biome} declares itself, whichever column it is found at: for a biome that is not a column's
|
||||
* surface biome, such as the cave biome of {@link com.volmit.world.BiomeSample#caveBiome()}, which is no level of
|
||||
@@ -38,7 +62,7 @@ public interface FeatureSettings {
|
||||
* @param source the resource that declares them, such as {@code overworld:biome/desert/dunes}
|
||||
* @param value the decoded settings
|
||||
*/
|
||||
record Entry<F extends IrisFeature>(FeatureAttachment.Target level, ResourceId source, F value) {
|
||||
record Entry<F extends IrisFeature>(Level level, ResourceId source, F value) {
|
||||
public Entry {
|
||||
Objects.requireNonNull(level, "level");
|
||||
Objects.requireNonNull(source, "source");
|
||||
@@ -46,3 +70,6 @@ public interface FeatureSettings {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** What makes two entries of a chunk the same declaration. */
|
||||
record Declaration(Level level, ResourceId source) {}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.volmit.config;
|
||||
|
||||
/** Where a pack declares settings, from the least to the most specific: the dimension, a region, a biome, and an
|
||||
* object placement. {@link Feature#targets()} names the levels a feature may be written at, and every
|
||||
* {@link FeatureSettings.Entry} carries the level that declared it. Settings stacks keep this order.
|
||||
*/
|
||||
public enum Level { DIMENSION, REGION, BIOME, PLACEMENT }
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.volmit.config;
|
||||
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.config.FeatureSettings.Entry;
|
||||
import java.util.List;
|
||||
|
||||
/** Every settings declaration of the pack the host is loading, for what depends on the whole pack rather
|
||||
* than one column, such as how far a mantle stage reaches ({@link com.volmit.stage.PackReach}).
|
||||
@@ -15,5 +13,5 @@ public interface PackSettings {
|
||||
/** Every entry of {@code featureType} the pack declares, dimension first, then regions, then biomes;
|
||||
* empty when nothing declares it.
|
||||
*/
|
||||
<F extends IrisFeature> List<Entry<F>> declared(Class<F> featureType);
|
||||
<F extends IrisFeature> SettingsStack<F> declared(Class<F> featureType);
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.volmit.config;
|
||||
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.config.FeatureSettings.Entry;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/** The entries of one settings type that apply somewhere, in {@link Level} order: what a column stacks
|
||||
* ({@link FeatureSettings#at}), what a chunk or a placement configures ({@link com.volmit.stage.StageContext#configured})
|
||||
* or what a pack declares ({@link PackSettings#declared}). Levels stack and nothing is merged, so a module picks what it
|
||||
* needs: every entry, the dimension's, the most specific one. Several entries may share a level, such as the biomes of
|
||||
* a chunk or of a pack; they keep the order they were found in.
|
||||
*
|
||||
* <p>An immutable value; equal when the entries are equal. Hosts build it with {@link #of}.
|
||||
*/
|
||||
public final class SettingsStack<F extends IrisFeature> implements Iterable<Entry<F>> {
|
||||
private static final SettingsStack<?> EMPTY = new SettingsStack<>(List.of());
|
||||
private static final Comparator<Entry<?>> LEVEL_ORDER = Comparator.comparing(Entry::level);
|
||||
|
||||
private final List<Entry<F>> entries;
|
||||
private final List<F> values;
|
||||
|
||||
private SettingsStack(List<Entry<F>> entries) {
|
||||
this.entries = entries;
|
||||
this.values = entries.stream().map(Entry::value).toList();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // The empty stack holds no entry of any type.
|
||||
public static <F extends IrisFeature> SettingsStack<F> empty() {
|
||||
return (SettingsStack<F>) EMPTY;
|
||||
}
|
||||
|
||||
/** These entries in level order; entries of one level keep the order given. */
|
||||
public static <F extends IrisFeature> SettingsStack<F> of(List<? extends Entry<F>> entries) {
|
||||
Objects.requireNonNull(entries, "entries");
|
||||
if (entries.isEmpty()) {
|
||||
return empty();
|
||||
}
|
||||
var sorted = new ArrayList<Entry<F>>(entries);
|
||||
sorted.forEach(entry -> Objects.requireNonNull(entry, "entry"));
|
||||
sorted.sort(LEVEL_ORDER);
|
||||
return new SettingsStack<>(List.copyOf(sorted));
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return entries.isEmpty();
|
||||
}
|
||||
|
||||
/** Every entry, in level order. */
|
||||
public List<Entry<F>> entries() {
|
||||
return entries;
|
||||
}
|
||||
|
||||
/** Every entry's value, in level order. */
|
||||
public List<F> values() {
|
||||
return values;
|
||||
}
|
||||
|
||||
/** The dimension's value; empty when the dimension declares none. */
|
||||
public Optional<F> dimension() {
|
||||
return level(Level.DIMENSION);
|
||||
}
|
||||
|
||||
/** The first value declared at {@code level}; empty when that level declares none. */
|
||||
public Optional<F> level(Level level) {
|
||||
Objects.requireNonNull(level, "level");
|
||||
for (var entry : entries) {
|
||||
if (entry.level() == level) {
|
||||
return Optional.of(entry.value());
|
||||
}
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/** The value of the last entry, the most specific level's (a placement before a biome before a region before the
|
||||
* dimension); empty when the stack is.
|
||||
*/
|
||||
public Optional<F> mostSpecific() {
|
||||
return entries.isEmpty() ? Optional.empty() : Optional.of(entries.getLast().value());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Entry<F>> iterator() {
|
||||
return entries.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return other instanceof SettingsStack<?> stack && entries.equals(stack.entries);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return entries.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SettingsStack" + entries;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user