mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user