mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Show decorators, snippets and ground cover in the studio
Decorators get their own pattern view from the decorant module: each biome's decorators from above, placed where their own noise allows. The Palettes and Decorators views list the pack's snippet files in a Snippets group, with the biomes that use each one, and mark entries read from a snippet. PackResources gains snippetOf and snippets so a module can tell where a settings value came from. The 3D world draws what stands on the ground, such as grass and flowers, as the block's own model. A plugin block that is solid on the server but shown to players as a plant no longer counts as ground.
This commit is contained in:
1 parent
67063b02d7
commit
55f6a06f9f
30 files changed
+1172
-71
No files matched your search
@@ -36,6 +36,7 @@ import com.volmit.noise.PackExpression;
|
||||
import com.volmit.noise.PackImageMap;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.resource.PackSnippet;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.resource.ResourceKind;
|
||||
import com.volmit.world.PackBiome;
|
||||
@@ -172,6 +173,16 @@ public final class IrisPackResources implements PackResources {
|
||||
return PackServices.of(data).providers(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<PackSnippet.Source> snippetOf(Object value) {
|
||||
return data.getModules().settings().snippetOf(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<PackSnippet<T>> snippets(Class<T> type) {
|
||||
return data.getModules().settings().snippets(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* The ids of the resources of the kind {@code kindId} names, for Iris's own tools that list a module's resources
|
||||
* without the module's class for them, which the plugin's classes cannot reach when the kind ships in a module jar.
|
||||
|
||||
@@ -42,27 +42,35 @@ import com.volmit.config.Snippet;
|
||||
import com.volmit.host.FeatureCatalog;
|
||||
import com.volmit.host.FeatureDescriptor;
|
||||
import com.volmit.host.OptionsMetadata;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.volmit.resource.PackSnippet;
|
||||
import com.volmit.resource.ResourceId;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.EnumMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.function.UnaryOperator;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Reads the module settings a dimension, region or biome declares: each settings feature of the pack at
|
||||
@@ -87,6 +95,8 @@ public final class ModuleSettingsDecoder {
|
||||
private final Map<Level, List<FeatureDescriptor<?>>> settings = new EnumMap<>(Level.class);
|
||||
private final Map<Level, Set<String>> keys = new EnumMap<>(Level.class);
|
||||
private final Map<String, ModuleSettings> placementSettings = new ConcurrentHashMap<>();
|
||||
// Weak keys compare by identity, and let go of the settings of a pack that was reloaded since.
|
||||
private final Cache<Object, PackSnippet.Source> snippetByValue = Caffeine.newBuilder().weakKeys().build();
|
||||
private final Path packFolder;
|
||||
private final Gson gson;
|
||||
private final Supplier<Gson> irisGson;
|
||||
@@ -373,7 +383,9 @@ public final class ModuleSettingsDecoder {
|
||||
if (entry.isJsonArray()) {
|
||||
throw new JsonParseException("Snippet " + reference + " holds several entries, which only a list can take");
|
||||
}
|
||||
return delegate.fromJsonTree(entry);
|
||||
T value = delegate.fromJsonTree(entry);
|
||||
rememberSnippetOf(value, new PackSnippet.Source(snippetName(snippet.value(), reference), 0));
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -392,12 +404,88 @@ public final class ModuleSettingsDecoder {
|
||||
return delegate.read(in);
|
||||
}
|
||||
JsonArray entries = gson.getAdapter(JsonArray.class).read(in);
|
||||
return delegate.fromJsonTree(withSnippetsInlined(entries, snippet.value()));
|
||||
InlinedEntries inlined = withSnippetsInlined(entries, snippet.value());
|
||||
T list = delegate.fromJsonTree(inlined.entries());
|
||||
rememberSnippetsOf((Collection<?>) list, inlined.snippets());
|
||||
return list;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The entries of a list with its snippet references replaced by what their files hold.
|
||||
*
|
||||
* @param snippets the snippet each entry came from, null for one written in place
|
||||
*/
|
||||
private record InlinedEntries(JsonArray entries, List<PackSnippet.Source> snippets) {
|
||||
}
|
||||
|
||||
// A list type that drops entries while it is read can't be matched to them again.
|
||||
private void rememberSnippetsOf(Collection<?> decoded, List<PackSnippet.Source> snippets) {
|
||||
if (decoded == null || decoded.size() != snippets.size()) {
|
||||
return;
|
||||
}
|
||||
int index = 0;
|
||||
for (Object value : decoded) {
|
||||
rememberSnippetOf(value, snippets.get(index++));
|
||||
}
|
||||
}
|
||||
|
||||
private void rememberSnippetOf(Object value, PackSnippet.Source snippet) {
|
||||
if (value != null && snippet != null) {
|
||||
snippetByValue.put(value, snippet);
|
||||
}
|
||||
}
|
||||
|
||||
/** The snippet file {@code value} was read from and which of its entries it is; empty for a value written in place. */
|
||||
Optional<PackSnippet.Source> snippetOf(Object value) {
|
||||
return value == null ? Optional.empty() : Optional.ofNullable(snippetByValue.getIfPresent(value));
|
||||
}
|
||||
|
||||
/** Every snippet file the pack has for {@code type}, in path order; a file that can't be read is logged and has no entries. */
|
||||
<T> List<PackSnippet<T>> snippets(Class<T> type) {
|
||||
Snippet snippet = type.getAnnotation(Snippet.class);
|
||||
if (snippet == null) {
|
||||
throw new IllegalArgumentException(type.getName() + " has no @Snippet, so packs keep no snippet files of it");
|
||||
}
|
||||
Path folder = packFolder.resolve(SNIPPET_FOLDER).resolve(snippet.value());
|
||||
if (!Files.isDirectory(folder)) {
|
||||
return List.of();
|
||||
}
|
||||
try (Stream<Path> files = Files.walk(folder)) {
|
||||
return files.filter(file -> Files.isRegularFile(file) && file.getFileName().toString().endsWith(JSON_EXTENSION))
|
||||
.map(file -> snippetPath(folder, file))
|
||||
.sorted()
|
||||
.map(path -> new PackSnippet<>(path, entriesOfSnippet(type, snippet.value(), path)))
|
||||
.toList();
|
||||
} catch (IOException failure) {
|
||||
throw new UncheckedIOException("Cannot list the snippets in " + packFolder.relativize(folder), failure);
|
||||
}
|
||||
}
|
||||
|
||||
private static String snippetPath(Path folder, Path file) {
|
||||
String relative = folder.relativize(file).toString().replace(File.separatorChar, '/');
|
||||
return relative.substring(0, relative.length() - JSON_EXTENSION.length());
|
||||
}
|
||||
|
||||
private <T> List<T> entriesOfSnippet(Class<T> type, String snippetFolder, String path) {
|
||||
try {
|
||||
JsonElement file = readSnippet(snippetFolder, SNIPPET_PREFIX + snippetFolder + "/" + path);
|
||||
List<JsonElement> written = file.isJsonArray() ? IrisData.entriesOfSnippet(file.getAsJsonArray()) : List.of(file);
|
||||
List<T> entries = new ArrayList<>();
|
||||
for (JsonElement entry : written) {
|
||||
T value = gson.fromJson(entry, type);
|
||||
rememberSnippetOf(value, new PackSnippet.Source(path, entries.size()));
|
||||
entries.add(value);
|
||||
}
|
||||
return entries;
|
||||
} catch (JsonParseException | UncheckedIOException unreadable) {
|
||||
IrisLogging.warn("Cannot read snippet " + SNIPPET_FOLDER + "/" + snippetFolder + "/" + path + JSON_EXTENSION + ": " + unreadable.getMessage());
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
private static Snippet snippetListedBy(TypeToken<?> type) {
|
||||
if (!Collection.class.isAssignableFrom(type.getRawType()) || !(type.getType() instanceof ParameterizedType listType)) {
|
||||
return null;
|
||||
@@ -409,21 +497,24 @@ public final class ModuleSettingsDecoder {
|
||||
return elementType.getAnnotation(Snippet.class);
|
||||
}
|
||||
|
||||
private JsonArray withSnippetsInlined(JsonArray entries, String snippetFolder) {
|
||||
private InlinedEntries withSnippetsInlined(JsonArray entries, String snippetFolder) {
|
||||
JsonArray inlined = new JsonArray();
|
||||
List<PackSnippet.Source> snippets = new ArrayList<>();
|
||||
for (JsonElement entry : entries) {
|
||||
if (!entry.isJsonPrimitive() || !entry.getAsJsonPrimitive().isString()) {
|
||||
inlined.add(entry);
|
||||
snippets.add(null);
|
||||
continue;
|
||||
}
|
||||
JsonElement snippet = readSnippet(snippetFolder, entry.getAsString());
|
||||
if (snippet.isJsonArray()) {
|
||||
IrisData.entriesOfSnippet(snippet.getAsJsonArray()).forEach(inlined::add);
|
||||
} else {
|
||||
inlined.add(snippet);
|
||||
List<JsonElement> held = snippet.isJsonArray() ? IrisData.entriesOfSnippet(snippet.getAsJsonArray()) : List.of(snippet);
|
||||
String name = snippetName(snippetFolder, entry.getAsString());
|
||||
for (int heldIndex = 0; heldIndex < held.size(); heldIndex++) {
|
||||
inlined.add(held.get(heldIndex));
|
||||
snippets.add(new PackSnippet.Source(name, heldIndex));
|
||||
}
|
||||
}
|
||||
return inlined;
|
||||
return new InlinedEntries(inlined, snippets);
|
||||
}
|
||||
|
||||
/** The file name below the snippet folder: {@code snippet/<name>} and {@code snippet/<snippet folder>/<name>} name the same file. */
|
||||
|
||||
@@ -18,7 +18,10 @@
|
||||
|
||||
package art.arcane.iris.core.studio.map;
|
||||
|
||||
import art.arcane.iris.core.link.CustomBlockSource;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.spi.IrisServices;
|
||||
import art.arcane.iris.util.common.data.B;
|
||||
import art.arcane.iris.engine.framework.GenerationSessionLease;
|
||||
import art.arcane.iris.spi.PlatformBiome;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
@@ -27,6 +30,11 @@ import art.arcane.iris.util.project.hunk.Hunk;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import com.github.benmanes.caffeine.cache.LoadingCache;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
/**
|
||||
* The tops of chunks generated for the studio alone: each chunk runs through the world's own stages into memory, so its
|
||||
* columns hold what the world would, trees and structures included, and nothing is placed in the world. Generating
|
||||
@@ -43,6 +51,7 @@ final class GeneratedChunks implements ColumnTops {
|
||||
|
||||
private final Engine engine;
|
||||
private final LoadingCache<Long, ChunkTop> tops;
|
||||
private final Map<String, PlatformBlockState> seenByKey = new ConcurrentHashMap<>();
|
||||
|
||||
GeneratedChunks(Engine engine) {
|
||||
this.engine = engine;
|
||||
@@ -65,7 +74,18 @@ final class GeneratedChunks implements ColumnTops {
|
||||
IrisContext.Scope ignored = IrisContext.open(engine, lease.sessionId(), null)) {
|
||||
engine.getGeneration().generate(chunkX << CHUNK_SHIFT, chunkZ << CHUNK_SHIFT, blocks, biomes, false, lease.sessionId());
|
||||
}
|
||||
return ChunkTop.of(blocks);
|
||||
return new ChunkTop(topsOf(blocks, this::seenByPlayers));
|
||||
}
|
||||
|
||||
// A plugin's block can be solid on the server where players see grass they walk through.
|
||||
private PlatformBlockState seenByPlayers(PlatformBlockState block) {
|
||||
return seenByKey.computeIfAbsent(block.key(), key -> {
|
||||
CustomBlockSource plugins = IrisServices.getOrNull(CustomBlockSource.class);
|
||||
Optional<PlatformBlockState> shown = plugins == null
|
||||
? Optional.empty()
|
||||
: plugins.lookOf(key).map(look -> B.getStateOrNull(look.shownAs(), false));
|
||||
return shown.orElse(block);
|
||||
});
|
||||
}
|
||||
|
||||
private static long keyOf(int chunkX, int chunkZ) {
|
||||
@@ -73,36 +93,43 @@ final class GeneratedChunks implements ColumnTops {
|
||||
}
|
||||
|
||||
private record ChunkTop(ColumnTop[] columns) {
|
||||
static ChunkTop of(Hunk<PlatformBlockState> blocks) {
|
||||
ColumnTop[] columns = new ColumnTop[BLOCKS_ACROSS * BLOCKS_ACROSS];
|
||||
for (int z = 0; z < BLOCKS_ACROSS; z++) {
|
||||
for (int x = 0; x < BLOCKS_ACROSS; x++) {
|
||||
columns[z * BLOCKS_ACROSS + x] = topOf(blocks, x, z);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The top of each column of a chunk's blocks, row by row from the north west.
|
||||
*
|
||||
* @param seenByPlayers the state players see in place of a block, which decides whether it is ground; the block
|
||||
* itself for all but a plugin's
|
||||
*/
|
||||
static ColumnTop[] topsOf(Hunk<PlatformBlockState> blocks, UnaryOperator<PlatformBlockState> seenByPlayers) {
|
||||
ColumnTop[] columns = new ColumnTop[BLOCKS_ACROSS * BLOCKS_ACROSS];
|
||||
for (int z = 0; z < BLOCKS_ACROSS; z++) {
|
||||
for (int x = 0; x < BLOCKS_ACROSS; x++) {
|
||||
columns[z * BLOCKS_ACROSS + x] = topOf(blocks, x, z, seenByPlayers);
|
||||
}
|
||||
return new ChunkTop(columns);
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
|
||||
// The ground is the highest block something can stand on, so a tree's leaves and a roof count; what stands on it
|
||||
// without being ground, such as grass or a layer of snow, is its cover.
|
||||
private static ColumnTop topOf(Hunk<PlatformBlockState> blocks, int x, int z) {
|
||||
for (int y = blocks.getHeight() - 1; y >= 0; y--) {
|
||||
PlatformBlockState block = blocks.get(x, y, z);
|
||||
if (!isGround(block)) {
|
||||
continue;
|
||||
}
|
||||
PlatformBlockState above = y + 1 < blocks.getHeight() ? blocks.get(x, y + 1, z) : null;
|
||||
return new ColumnTop(y, block.key(), isCover(above) ? above.key() : null);
|
||||
// The ground is the highest block something can stand on, so a tree's leaves and a roof count; what stands on it
|
||||
// without being ground, such as grass or a layer of snow, is its cover.
|
||||
private static ColumnTop topOf(Hunk<PlatformBlockState> blocks, int x, int z, UnaryOperator<PlatformBlockState> seenByPlayers) {
|
||||
for (int y = blocks.getHeight() - 1; y >= 0; y--) {
|
||||
PlatformBlockState block = blocks.get(x, y, z);
|
||||
if (block == null || !isGround(seenByPlayers.apply(block))) {
|
||||
continue;
|
||||
}
|
||||
return ColumnTop.NOTHING;
|
||||
PlatformBlockState above = y + 1 < blocks.getHeight() ? blocks.get(x, y + 1, z) : null;
|
||||
return new ColumnTop(y, block.key(), isCover(above) ? above.key() : null);
|
||||
}
|
||||
return ColumnTop.NOTHING;
|
||||
}
|
||||
|
||||
private static boolean isGround(PlatformBlockState block) {
|
||||
return block != null && !block.isAir() && !block.isFluid() && block.isSolid();
|
||||
}
|
||||
private static boolean isGround(PlatformBlockState block) {
|
||||
return !block.isAir() && !block.isFluid() && block.isSolid();
|
||||
}
|
||||
|
||||
private static boolean isCover(PlatformBlockState block) {
|
||||
return block != null && !block.isAir() && !block.isFluid();
|
||||
}
|
||||
private static boolean isCover(PlatformBlockState block) {
|
||||
return block != null && !block.isAir() && !block.isFluid();
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import com.volmit.config.Level;
|
||||
import com.volmit.block.BlockSpec;
|
||||
import com.volmit.host.LoadedPack;
|
||||
import com.volmit.host.ModuleHost;
|
||||
import com.volmit.resource.PackSnippet;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
@@ -19,6 +20,7 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -112,6 +114,46 @@ public class DecorantSettingsDecodingTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aDecoratorReadFromASnippetKnowsItsFileAndOneWrittenInPlaceHasNone() {
|
||||
List<Decorator> decorators = decorators(BIOME_DECORATORS);
|
||||
|
||||
assertEquals(Optional.of(new PackSnippet.Source("bush", 0)), decoder.snippetOf(decorators.get(2)));
|
||||
assertEquals(Optional.of(new PackSnippet.Source("bush", 0)), decoder.snippetOf(decorators.get(3)));
|
||||
assertEquals(Optional.empty(), decoder.snippetOf(decorators.getFirst()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void everySnippetFileIsListedWhetherABiomeUsesItOrNot() throws IOException {
|
||||
Path snippets = folder.getRoot().toPath().resolve("pack/snippet/iris/decorant/decorator");
|
||||
Files.createDirectories(snippets.resolve("flowers"));
|
||||
Files.writeString(snippets.resolve("flowers/pair.json"),
|
||||
"[{\"palette\": [{\"block\": \"minecraft:poppy\"}]}, {\"palette\": [{\"block\": \"minecraft:dandelion\"}]}]");
|
||||
Files.writeString(snippets.resolve("broken.json"), "{\"palette\": ");
|
||||
|
||||
List<PackSnippet<Decorator>> listed = decoder.snippets(Decorator.class);
|
||||
|
||||
assertEquals(List.of("broken", "bush", "flowers/pair"), listed.stream().map(PackSnippet::path).toList());
|
||||
assertEquals("a file that can't be read is listed without entries", List.of(), listed.getFirst().entries());
|
||||
assertEquals(List.of(BlockSpec.of("minecraft:fern")), listed.get(1).entries().getFirst().palette());
|
||||
assertEquals(2, listed.get(2).entries().size());
|
||||
assertEquals(Optional.of(new PackSnippet.Source("flowers/pair", 1)), decoder.snippetOf(listed.get(2).entries().get(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aListTakingAFileOfSeveralKnowsWhichOfThemEachEntryIs() throws IOException {
|
||||
Path snippets = folder.getRoot().toPath().resolve("pack/snippet/iris/decorant/decorator");
|
||||
Files.writeString(snippets.resolve("pair.json"),
|
||||
"[{\"palette\": [{\"block\": \"minecraft:poppy\"}]}, {\"palette\": [{\"block\": \"minecraft:dandelion\"}]}]");
|
||||
|
||||
List<Decorator> decorators = decorators("{\"decorators\": [{\"chance\": 1}, \"snippet/pair\"]}");
|
||||
|
||||
assertEquals(3, decorators.size());
|
||||
assertEquals(Optional.empty(), decoder.snippetOf(decorators.getFirst()));
|
||||
assertEquals(Optional.of(new PackSnippet.Source("pair", 0)), decoder.snippetOf(decorators.get(1)));
|
||||
assertEquals(Optional.of(new PackSnippet.Source("pair", 1)), decoder.snippetOf(decorators.get(2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void anEmptyDecoratorHasIrisDefaults() {
|
||||
Decorator defaults = decorators(BIOME_DECORATORS).get(4);
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package art.arcane.iris.core.studio.map;
|
||||
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import art.arcane.iris.util.project.hunk.Hunk;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class GeneratedChunksTest {
|
||||
private static final int HEIGHT = 8;
|
||||
private static final PlatformBlockState STONE = block("minecraft:stone", true);
|
||||
private static final PlatformBlockState POPPY = block("minecraft:poppy", false);
|
||||
private static final PlatformBlockState TRIPWIRE = block("minecraft:tripwire", false);
|
||||
// A plugin's grass: solid on the server, a tripwire to players.
|
||||
private static final PlatformBlockState PLUGIN_GRASS = block("craftengine:custom_7", true);
|
||||
|
||||
@Test
|
||||
public void theGroundIsTheHighestSolidBlockAndWhatStandsOnItItsCover() {
|
||||
Hunk<PlatformBlockState> blocks = Hunk.newArrayHunk(16, HEIGHT, 16);
|
||||
blocks.set(0, 3, 0, STONE);
|
||||
blocks.set(0, 4, 0, POPPY);
|
||||
blocks.set(1, 2, 0, STONE);
|
||||
|
||||
ColumnTops.ColumnTop[] tops = GeneratedChunks.topsOf(blocks, UnaryOperator.identity());
|
||||
|
||||
assertEquals(new ColumnTops.ColumnTop(3, "minecraft:stone", "minecraft:poppy"), tops[0]);
|
||||
assertEquals(new ColumnTops.ColumnTop(2, "minecraft:stone", null), tops[1]);
|
||||
assertEquals(ColumnTops.ColumnTop.NOTHING, tops[2]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPluginBlockPlayersWalkThroughIsCoverThoughTheServerHoldsItSolid() {
|
||||
Hunk<PlatformBlockState> blocks = Hunk.newArrayHunk(16, HEIGHT, 16);
|
||||
blocks.set(0, 3, 0, STONE);
|
||||
blocks.set(0, 4, 0, PLUGIN_GRASS);
|
||||
|
||||
ColumnTops.ColumnTop[] tops = GeneratedChunks.topsOf(blocks, block -> block == PLUGIN_GRASS ? TRIPWIRE : block);
|
||||
|
||||
assertEquals(new ColumnTops.ColumnTop(3, "minecraft:stone", "craftengine:custom_7"), tops[0]);
|
||||
}
|
||||
|
||||
private static PlatformBlockState block(String key, boolean solid) {
|
||||
PlatformBlockState block = mock(PlatformBlockState.class);
|
||||
when(block.key()).thenReturn(key);
|
||||
when(block.isSolid()).thenReturn(solid);
|
||||
return block;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package art.arcane.iris.engine.decorator;
|
||||
|
||||
import art.arcane.iris.builtin.decorant.DecorantSettings;
|
||||
import art.arcane.iris.builtin.decorant.Decorator;
|
||||
import art.arcane.iris.engine.modifier.ApiChunk;
|
||||
import com.google.gson.Gson;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.config.FeatureSettings;
|
||||
import com.volmit.host.ModuleLogs;
|
||||
import com.volmit.noise.NoiseSampler;
|
||||
import com.volmit.resource.PackResources;
|
||||
import com.volmit.resource.PackSnippet;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.tooling.BlockPattern;
|
||||
import com.volmit.tooling.PatternArea;
|
||||
import com.volmit.tooling.PatternEntry;
|
||||
import com.volmit.world.PackBiome;
|
||||
import com.volmit.world.PackWorld;
|
||||
import com.volmit.world.RunningWorld;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class DecoratorPatternsTest {
|
||||
private static final ResourceId MEADOW = ResourceId.parse("pack:meadow");
|
||||
private static final double NOISE_EVERYWHERE = 0.3;
|
||||
private static final String POPPIES = "{\"chance\": 0.5, \"palette\": [{\"block\": \"minecraft:poppy\"}]}";
|
||||
private static final String RARE_FERNS = "{\"chance\": 0.1, \"palette\": [{\"block\": \"minecraft:fern\"}]}";
|
||||
private static final String CANE = "{\"chance\": 1, \"stackMin\": 3, \"stackMax\": 3, \"palette\": [{\"block\": \"minecraft:sugar_cane\"}],"
|
||||
+ " \"topPalette\": [{\"block\": \"minecraft:bamboo\"}]}";
|
||||
private static final String UNUSED_TULIPS = "{\"chance\": 1, \"palette\": [{\"block\": \"minecraft:red_tulip\"}]}";
|
||||
|
||||
private final DecoratorPatterns view = new DecoratorPatterns();
|
||||
private final PatternArea area = new PatternArea(0, 0, 4, 1);
|
||||
private PackWorld world;
|
||||
|
||||
@Before
|
||||
public void createWorld() {
|
||||
Gson gson = new Gson();
|
||||
Decorator poppies = gson.fromJson(POPPIES, Decorator.class);
|
||||
Decorator tulips = gson.fromJson(UNUSED_TULIPS, Decorator.class);
|
||||
DecorantSettings meadow = new DecorantSettings(List.of(poppies, gson.fromJson(RARE_FERNS, Decorator.class), gson.fromJson(CANE, Decorator.class)));
|
||||
|
||||
PackResources resources = mock(PackResources.class);
|
||||
when(resources.ids(PackResources.BIOMES)).thenReturn(List.of(MEADOW));
|
||||
when(resources.find(PackResources.BIOMES, MEADOW))
|
||||
.thenReturn(Optional.of(new PackBiome(MEADOW, "Meadow", ResourceId.parse("minecraft:plains"), List.of())));
|
||||
when(resources.blocks()).thenReturn(ApiChunk.blockRegistry());
|
||||
when(resources.noise()).thenReturn((style, seed) -> constant(NOISE_EVERYWHERE));
|
||||
when(resources.snippetOf(any())).thenReturn(Optional.empty());
|
||||
when(resources.snippetOf(poppies)).thenReturn(Optional.of(new PackSnippet.Source("flowers/poppies", 0)));
|
||||
when(resources.snippets(Decorator.class)).thenReturn(List.of(
|
||||
new PackSnippet<>("flowers/broken", List.of()),
|
||||
new PackSnippet<>("flowers/poppies", List.of(poppies)),
|
||||
new PackSnippet<>("flowers/tulips", List.of(gson.fromJson(RARE_FERNS, Decorator.class), tulips))));
|
||||
|
||||
FeatureSettings settings = mock(FeatureSettings.class);
|
||||
when(settings.declaredBy(MEADOW, DecorantSettings.class)).thenReturn(Optional.of(meadow));
|
||||
RunningWorld running = mock(RunningWorld.class);
|
||||
when(running.settings()).thenReturn(settings);
|
||||
|
||||
world = mock(PackWorld.class);
|
||||
when(world.running()).thenReturn(Optional.of(running));
|
||||
when(world.resources()).thenReturn(resources);
|
||||
when(world.log()).thenReturn(ModuleLogs.discarding());
|
||||
when(world.state(eq(BiomeDecorators.class), any())).thenAnswer(asked -> asked.<Function<PackWorld, BiomeDecorators>>getArgument(1).apply(world));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listsEachDecoratorUnderItsBiomeAndEachSnippetFileAfterThem() {
|
||||
List<PatternEntry> entries = view.entries(world);
|
||||
|
||||
assertEquals(List.of("pack:meadow#0", "pack:meadow#1", "pack:meadow#2", "snippet:flowers/broken#0", "snippet:flowers/poppies#0",
|
||||
"snippet:flowers/tulips#0", "snippet:flowers/tulips#1"), entries.stream().map(PatternEntry::key).toList());
|
||||
assertEquals(Optional.of("Meadow"), entries.getFirst().group());
|
||||
assertTrue(entries.getFirst().facts().contains("Snippet flowers/poppies"));
|
||||
assertFalse(entries.get(1).facts().stream().anyMatch(fact -> fact.startsWith("Snippet")));
|
||||
assertEquals("a stack is stepped through block by block", 3, entries.get(2).depths());
|
||||
assertEquals(List.of("Could not be read; the server log says why"), entries.get(3).facts());
|
||||
assertEquals("flowers/poppies", entries.get(4).name());
|
||||
assertTrue(entries.get(4).facts().contains("Meadow · Decorator 1"));
|
||||
assertEquals("a file holding several is listed entry by entry", "flowers/tulips · 2", entries.get(6).name());
|
||||
assertTrue(entries.get(6).facts().contains("Not used by any biome"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void placesOnlyWhereTheDecoratorsNoiseStaysUnderItsChance() {
|
||||
assertEquals(List.of("minecraft:poppy"), blocksOf("pack:meadow#0", 0));
|
||||
assertTrue(placesEverywhere("pack:meadow#0", 0));
|
||||
assertEquals(List.of(), blocksOf("pack:meadow#1", 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aStackHasItsTopPaletteOnTopAndEndsAtItsHeight() {
|
||||
assertEquals(List.of("minecraft:bamboo"), blocksOf("pack:meadow#2", 0));
|
||||
assertEquals(List.of("minecraft:sugar_cane"), blocksOf("pack:meadow#2", 2));
|
||||
assertEquals(List.of(), blocksOf("pack:meadow#2", 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aSnippetFileShowsWhatItsFirstUsePlacesOrItsOwnBlocksWhenUnused() {
|
||||
assertEquals(List.of("minecraft:poppy"), blocksOf("snippet:flowers/poppies#0", 0));
|
||||
assertEquals(List.of("minecraft:red_tulip"), blocksOf("snippet:flowers/tulips#1", 0));
|
||||
assertEquals("a file that can't be read places nothing", List.of(), blocksOf("snippet:flowers/broken#0", 0));
|
||||
assertTrue(view.pattern(world, "snippet:flowers/none#0", 0, area).isEmpty());
|
||||
assertTrue(view.pattern(world, "snippet:flowers/tulips#2", 0, area).isEmpty());
|
||||
assertTrue(view.pattern(world, "pack:meadow#7", 0, area).isEmpty());
|
||||
}
|
||||
|
||||
private List<String> blocksOf(String key, int depth) {
|
||||
return view.pattern(world, key, depth, area).orElseThrow().blocks().stream().map(BlockState::key).toList();
|
||||
}
|
||||
|
||||
private boolean placesEverywhere(String key, int depth) {
|
||||
BlockPattern pattern = view.pattern(world, key, depth, area).orElseThrow();
|
||||
for (int row = 0; row < area.columns(); row++) {
|
||||
for (int column = 0; column < area.columns(); column++) {
|
||||
if (pattern.pickAt(column, row).isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static NoiseSampler constant(double value) {
|
||||
return new NoiseSampler() {
|
||||
@Override
|
||||
public double sample(double worldX, double worldZ) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double sample(double worldX, double y, double worldZ) {
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user