Look up and colour plugin blocks in saved worlds by the state they borrow

Plugins such as CraftEngine save their blocks as the vanilla state players see, so saved worlds showed a tripwire where a custom block stands. A saved block now maps back to its plugin name through the providers' looks, counts as ground by the shape of the block it borrows, and its models and textures come from the plugin's resource pack.

No terrain, pin, golden or digest moved; saved worlds are only read.

v3: 002aab06e
This commit is contained in:
piexel committed 2026-11-10 13:51:37 +01:00
1 parent 3c03bba7e7
commit bcbabc8f60
16 files changed
+479 -33

No files matched your search

@@ -48,6 +48,7 @@ import art.arcane.iris.core.runtime.BukkitEnginePlatformHooks;
import art.arcane.iris.core.runtime.WorldDeletionQueue;
import art.arcane.iris.core.runtime.WorldRuntimeControlService;
import art.arcane.iris.api.terrain.IrisTerrainService;
import art.arcane.iris.core.link.CustomBlockSource;
import art.arcane.iris.core.link.IrisPapiInstaller;
import art.arcane.iris.core.link.IrisPapiListener;
import art.arcane.iris.core.link.IrisPapiState;
@@ -670,6 +671,7 @@ public class Iris extends VolmitPlugin implements Listener, ReloadAware {
IrisServices.register(serviceType, i);
}
IrisServices.register(BlockEditAccess.class, services.get(EditSVC.class));
IrisServices.register(CustomBlockSource.class, services.get(ExternalDataSVC.class));
IrisServices.register(PreservationRegistry.class, services.get(PreservationSVC.class));
compat = IrisCompat.configured(getDataFile("compat.json"));
IrisServices.register(IrisCompat.class, compat);
@@ -0,0 +1,29 @@
/*
* Iris is a World Generator for Minecraft Servers
* Copyright (c) 2026 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.core.link;
/**
* How players see a plugin's block.
*
* @param id the block's name in its plugin
* @param shownAs the vanilla block state sent to players and saved to region files, such as
* {@code minecraft:tripwire[attached=true]}, whose model the plugin's resource pack replaces
*/
public record CustomBlockLook(Identifier id, String shownAs) {
}
@@ -0,0 +1,40 @@
/*
* Iris is a World Generator for Minecraft Servers
* Copyright (c) 2026 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.core.link;
import java.nio.file.Path;
import java.util.List;
/**
* What the studio needs to know of the blocks plugins add, without reaching for the server's plugin API.
*/
public interface CustomBlockSource {
/**
* Plugins save their blocks to region files as the vanilla state players see, and keep which block it really is in
* files of their own. This is the way back from such a state.
*
* @return one look for every vanilla state some plugin block is saved as
*/
List<CustomBlockLook> blocks();
/**
* @return the resource packs holding the models of the plugins' blocks, those that have one on disk
*/
List<Path> resourcePacks();
}
@@ -24,10 +24,12 @@ import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.MissingResourceException;
import java.util.Optional;
@Getter
@RequiredArgsConstructor
@@ -133,6 +135,22 @@ public abstract class ExternalDataProvider {
return false;
}
/**
* @return One look for every vanilla block state the provider's blocks are saved to region files as
*/
@NotNull
public List<CustomBlockLook> getCustomBlocks() {
return List.of();
}
/**
* @return The resource pack holding the models of the provider's blocks, or nothing when it has none on disk
*/
@NotNull
public Optional<Path> getResourcePack() {
return Optional.empty();
}
/**
* Spawns a mob in the specified location using the given engine and entity identifier.
*
@@ -2,6 +2,7 @@ package art.arcane.iris.core.link.data;
import art.arcane.iris.platform.bukkit.BukkitBlockResolution;
import art.arcane.iris.core.link.CustomBlockLook;
import art.arcane.iris.core.link.ExternalDataProvider;
import art.arcane.iris.core.link.Identifier;
import art.arcane.iris.core.nms.INMS;
@@ -34,8 +35,11 @@ import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
@@ -45,6 +49,7 @@ import java.util.stream.Stream;
public class CraftEngineDataProvider extends ExternalDataProvider implements Listener {
private static final String PERSISTENT_PROPERTY = "persistent";
private static final String GENERATED_RESOURCE_PACK = "generated/resource_pack.zip";
private static final BlockProperty[] FURNITURE_PROPERTIES = new BlockProperty[]{
BlockProperty.ofBoolean("randomYaw", false),
BlockProperty.ofDouble("yaw", 0, 0, 360f, false, true),
@@ -170,6 +175,27 @@ public class CraftEngineDataProvider extends ExternalDataProvider implements Lis
return state != null && state.settings().tags().contains(Key.of(tag.getNamespace(), tag.getKey()));
}
// A block's states can look alike, like leaves of every distance, and the first of them stands in for the rest.
@Override
public @NotNull List<CustomBlockLook> getCustomBlocks() {
Map<String, CustomBlockLook> looksByShownState = new LinkedHashMap<>();
for (BlockDefinition block : CraftEngineBlocks.loadedBlocks().values()) {
Identifier id = new Identifier(block.id().namespace(), block.id().value());
for (ImmutableBlockState state : block.variantProvider().states()) {
String shownAs = state.visualBlockState().getAsString();
looksByShownState.putIfAbsent(shownAs, new CustomBlockLook(id, shownAs));
}
}
return List.copyOf(looksByShownState.values());
}
// PackManager#resourcePackPath went away in CraftEngine's later versions, which set this path in their workflows.
@Override
public @NotNull Optional<Path> getResourcePack() {
Path pack = getPlugin().getDataFolder().toPath().resolve(GENERATED_RESOURCE_PACK);
return Files.isRegularFile(pack) ? Optional.of(pack) : Optional.empty();
}
private static ImmutableBlockState customStateOf(BlockData data) {
if (data instanceof IrisCustomData) {
return null;
@@ -20,6 +20,8 @@ package art.arcane.iris.core.service;
import art.arcane.iris.spi.IrisLogging;
import art.arcane.iris.platform.bukkit.BukkitPlatform;
import art.arcane.iris.core.link.CustomBlockLook;
import art.arcane.iris.core.link.CustomBlockSource;
import art.arcane.iris.core.link.ExternalDataProvider;
import art.arcane.iris.core.link.Identifier;
import art.arcane.iris.core.link.data.DataType;
@@ -41,6 +43,7 @@ import org.bukkit.event.server.PluginEnableEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.MissingResourceException;
@@ -49,7 +52,7 @@ import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.Collectors;
public class ExternalDataSVC implements IrisService {
public class ExternalDataSVC implements IrisService, CustomBlockSource {
private static final String PROVIDER_PACKAGE = "art.arcane.iris.core.link.data.";
private static final List<ProviderDefinition> BUILT_IN_PROVIDERS = List.of(
new ProviderDefinition("CraftEngine", PROVIDER_PACKAGE + "CraftEngineDataProvider"),
@@ -134,6 +137,21 @@ public class ExternalDataSVC implements IrisService {
}
}
@Override
public List<CustomBlockLook> blocks() {
return activeProviders.stream()
.flatMap(provider -> provider.getCustomBlocks().stream())
.toList();
}
@Override
public List<Path> resourcePacks() {
return activeProviders.stream()
.map(ExternalDataProvider::getResourcePack)
.flatMap(Optional::stream)
.toList();
}
public Optional<List<BlockProperty>> getBlockProperties(final Identifier key) {
Optional<ExternalDataProvider> provider = activeProviders.stream().filter(p -> p.isValidProvider(key, DataType.BLOCK)).findFirst();
if (provider.isEmpty())
@@ -18,12 +18,15 @@
package art.arcane.iris.core.studio.assets;
import art.arcane.iris.core.link.CustomBlockSource;
import art.arcane.iris.spi.IrisPlatforms;
import art.arcane.iris.spi.IrisServices;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.zip.ZipEntry;
@@ -36,6 +39,7 @@ import java.util.zip.ZipFile;
*/
public final class MinecraftAssets {
private static final String ASSET_FOLDER = "assets/";
private static final String MINECRAFT_ASSET_FOLDER = ASSET_FOLDER + "minecraft/";
private static final MinecraftAssets SHARED = new MinecraftAssets();
private CompletableFuture<ZipFile> clientJar;
@@ -48,8 +52,9 @@ public final class MinecraftAssets {
}
/**
* The asset file at {@code path} in the client jar, such as {@code assets/minecraft/textures/block/stone.png};
* empty when the client has no such file or the path leaves its asset folder.
* The asset file at {@code path} in the client jar, such as {@code assets/minecraft/textures/block/stone.png}, or
* for another namespace in the resource packs of the server's plugins; empty when there is no such file or the path
* leaves the asset folder.
*
* @throws NotReadyException while the client jar is still being fetched, or when fetching it failed; the next call
* after a failure tries again
@@ -58,6 +63,9 @@ public final class MinecraftAssets {
if (!isAssetPath(path)) {
return Optional.empty();
}
if (!path.startsWith(MINECRAFT_ASSET_FOLDER)) {
return readFromPacks(pluginResourcePacks(), path);
}
ZipFile jar = openedJar();
ZipEntry entry = jar.getEntry(path);
if (entry == null || entry.isDirectory()) {
@@ -68,6 +76,25 @@ public final class MinecraftAssets {
}
}
private static List<Path> pluginResourcePacks() {
CustomBlockSource plugins = IrisServices.getOrNull(CustomBlockSource.class);
return plugins == null ? List.of() : plugins.resourcePacks();
}
static Optional<byte[]> readFromPacks(List<Path> packs, String path) throws IOException {
for (Path pack : packs) {
try (ZipFile zip = new ZipFile(pack.toFile())) {
ZipEntry entry = zip.getEntry(path);
if (entry != null && !entry.isDirectory()) {
try (InputStream content = zip.getInputStream(entry)) {
return Optional.of(content.readAllBytes());
}
}
}
}
return Optional.empty();
}
static boolean isAssetPath(String path) {
return path.startsWith(ASSET_FOLDER) && !path.contains("..") && !path.contains("\\") && !path.contains("//");
}
@@ -0,0 +1,107 @@
/*
* Iris is a World Generator for Minecraft Servers
* Copyright (c) 2026 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.core.studio.saved;
import art.arcane.iris.core.link.CustomBlockLook;
import art.arcane.iris.core.link.CustomBlockSource;
import art.arcane.iris.spi.IrisServices;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* Blocks added by plugins. They are saved as the vanilla block states they borrow, such as a tripwire for grass, so
* what a saved block really is has to be looked up from the state it is saved as.
*/
final class CustomBlocks {
static final CustomBlocks NONE = new CustomBlocks(List.of());
private static final char PROPERTIES_START = '[';
private static final String PROPERTY_SEPARATOR = ",";
private static final String VALUE_SEPARATOR = "=";
private final Map<String, String> namesByShownState = new HashMap<>();
private final Map<String, String> shownBlocksByName = new HashMap<>();
private final Set<String> shownBlocks = new HashSet<>();
CustomBlocks(List<CustomBlockLook> looks) {
for (CustomBlockLook look : looks) {
String shownBlock = blockOf(look.shownAs());
String name = look.id().toString();
namesByShownState.putIfAbsent(keyOf(shownBlock, propertiesOf(look.shownAs())), name);
shownBlocksByName.putIfAbsent(name, shownBlock);
shownBlocks.add(shownBlock);
}
}
/** The plugin blocks the server has now; empty on a platform that has none. */
static CustomBlocks platform() {
CustomBlockSource source = IrisServices.getOrNull(CustomBlockSource.class);
return source == null ? NONE : new CustomBlocks(source.blocks());
}
/**
* The name of the plugin block saved as this vanilla state, or the state's own block name when none is.
*
* @param properties the state's properties, only asked for when a plugin block borrows {@code blockName}
*/
String nameSavedAs(String blockName, Supplier<Map<String, String>> properties) {
if (!shownBlocks.contains(blockName)) {
return blockName;
}
return namesByShownState.getOrDefault(keyOf(blockName, properties.get()), blockName);
}
/** The vanilla block the plugin block {@code name} borrows its shape from, or empty when {@code name} is not one. */
Optional<String> shownBlockOf(String name) {
return Optional.ofNullable(shownBlocksByName.get(name));
}
private static String blockOf(String state) {
int propertiesStart = state.indexOf(PROPERTIES_START);
return propertiesStart < 0 ? state : state.substring(0, propertiesStart);
}
private static Map<String, String> propertiesOf(String state) {
int propertiesStart = state.indexOf(PROPERTIES_START);
if (propertiesStart < 0) {
return Map.of();
}
Map<String, String> properties = new HashMap<>();
for (String property : state.substring(propertiesStart + 1, state.length() - 1).split(PROPERTY_SEPARATOR)) {
String[] nameAndValue = property.split(VALUE_SEPARATOR, 2);
if (nameAndValue.length == 2) {
properties.put(nameAndValue[0], nameAndValue[1]);
}
}
return properties;
}
private static String keyOf(String block, Map<String, String> properties) {
return block + new TreeMap<>(properties).entrySet().stream()
.map(property -> property.getKey() + VALUE_SEPARATOR + property.getValue())
.collect(Collectors.joining(PROPERTY_SEPARATOR, String.valueOf(PROPERTIES_START), "]"));
}
}
@@ -24,6 +24,7 @@ import art.arcane.volmlib.util.nbt.tag.StringTag;
import art.arcane.volmlib.util.nbt.tag.Tag;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@@ -42,6 +43,7 @@ final class SavedChunk {
private static final int BIOME_CELLS_ACROSS = 4;
private static final int SMALLEST_BLOCK_INDEX_BITS = 4;
private static final String SURFACE_HEIGHTMAP = "WORLD_SURFACE";
private static final List<String> PROPERTY_KEYS = List.of("Properties", "properties");
// Terrain and heightmaps are final from the features stage on; pregenerated chunks no player visited stop short of full.
private static final Set<String> FINISHED_TERRAIN_STATUSES = Stream.of("features", "initialize_light", "light", "spawn",
"heightmaps", "full")
@@ -52,16 +54,18 @@ final class SavedChunk {
private final long[] surfaceHeights;
private final int surfaceBits;
private final Map<Integer, CompoundTag> sectionsByY;
private final CustomBlocks customBlocks;
private SavedChunk(int minY, long[] surfaceHeights, int surfaceBits, Map<Integer, CompoundTag> sectionsByY) {
private SavedChunk(int minY, long[] surfaceHeights, int surfaceBits, Map<Integer, CompoundTag> sectionsByY, CustomBlocks customBlocks) {
this.minY = minY;
this.surfaceHeights = surfaceHeights;
this.surfaceBits = surfaceBits;
this.sectionsByY = sectionsByY;
this.customBlocks = customBlocks;
}
/** The chunk, or empty where its terrain is not finished yet or it has no surface heightmap. */
static Optional<SavedChunk> of(CompoundTag root) {
static Optional<SavedChunk> of(CompoundTag root, CustomBlocks customBlocks) {
if (!FINISHED_TERRAIN_STATUSES.contains(root.getString("Status"))) {
return Optional.empty();
}
@@ -73,7 +77,7 @@ final class SavedChunk {
int lowestSection = root.containsKey("yPos") ? root.getInt("yPos") : lowestKey(sections);
int minY = lowestSection * BLOCKS_ACROSS;
int worldHeight = (highestKey(sections) - lowestSection + 1) * BLOCKS_ACROSS;
return Optional.of(new SavedChunk(minY, surface, heightmapBits(worldHeight, surface.length), sections));
return Optional.of(new SavedChunk(minY, surface, heightmapBits(worldHeight, surface.length), sections, customBlocks));
}
int minY() {
@@ -85,7 +89,7 @@ final class SavedChunk {
return minY + (int) PackedIndices.get(surfaceHeights, surfaceBits, z * BLOCKS_ACROSS + x) - 1;
}
/** The block's name, such as {@code minecraft:stone}, without its properties. */
/** The block's name, such as {@code minecraft:stone}, without its properties; a plugin's block is named as its plugin does. */
String blockAt(int x, int y, int z) {
CompoundTag section = sectionsByY.get(Math.floorDiv(y, BLOCKS_ACROSS));
if (section == null || !section.containsKey("block_states")) {
@@ -123,13 +127,31 @@ final class SavedChunk {
}
// A block with properties is a compound naming it under Name; since 26.1 a block without them may be its bare name.
private static String nameOf(Tag<?> block) {
return switch (block) {
case StringTag name -> name.getValue();
private String nameOf(Tag<?> block) {
String name = switch (block) {
case StringTag text -> text.getValue();
case CompoundTag compound when !compound.getString("Name").isEmpty() -> compound.getString("Name");
case CompoundTag compound when !compound.getString("").isEmpty() -> compound.getString("");
case null, default -> UNKNOWN;
};
return customBlocks.nameSavedAs(name, () -> propertiesOf(block));
}
private static Map<String, String> propertiesOf(Tag<?> block) {
Map<String, String> properties = new HashMap<>();
if (!(block instanceof CompoundTag compound)) {
return properties;
}
for (String key : PROPERTY_KEYS) {
if (compound.containsKey(key)) {
compound.getCompoundTag(key).forEach((name, value) -> {
if (value instanceof StringTag text) {
properties.put(name, text.getValue());
}
});
}
}
return properties;
}
private static Map<Integer, CompoundTag> sectionsByY(CompoundTag root) {
@@ -55,28 +55,35 @@ interface SavedGround {
}
/** Asks the server's block registry; a block it doesn't know is ground, so a column never runs down to the floor. */
static SavedGround platform() {
return new PlatformGround();
static SavedGround platform(CustomBlocks customBlocks) {
return new PlatformGround(customBlocks);
}
final class PlatformGround implements SavedGround {
private static final List<String> TRUNK_SUFFIXES = List.of("_log", "_wood", "_stem", "_hyphae", "_mushroom_block");
private final Map<String, Boolean> groundByBlock = new ConcurrentHashMap<>();
private final CustomBlocks customBlocks;
PlatformGround(CustomBlocks customBlocks) {
this.customBlocks = customBlocks;
}
@Override
public boolean isGround(String block) {
return groundByBlock.computeIfAbsent(block, PlatformGround::looksLikeGround);
return groundByBlock.computeIfAbsent(block, this::looksLikeGround);
}
private static boolean looksLikeGround(String block) {
// A plugin's block has the shape of the block it borrows, like a tripwire for grass, and whole blocks all borrow the
// same few, like mushroom blocks, so only its own name can tell a trunk from the ground.
private boolean looksLikeGround(String block) {
if (isThin(block)) {
return true;
}
if (TRUNK_SUFFIXES.stream().anyMatch(block::endsWith)) {
return false;
}
PlatformBlockState state = B.getStateOrNull(block, false);
PlatformBlockState state = B.getStateOrNull(customBlocks.shownBlockOf(block).orElse(block), false);
return state == null || state.isOccluding();
}
}
@@ -77,8 +77,8 @@ final class SavedRegion {
Arrays.fill(waterSurfaceY, (short) NO_WATER);
}
static SavedRegion of(RegionFile file, SavedGround ground) {
return new SavedRegion((chunkX, chunkZ) -> file.chunk(chunkX, chunkZ).flatMap(SavedChunk::of), ground);
static SavedRegion of(RegionFile file, SavedGround ground, CustomBlocks customBlocks) {
return new SavedRegion((chunkX, chunkZ) -> file.chunk(chunkX, chunkZ).flatMap(root -> SavedChunk.of(root, customBlocks)), ground);
}
/**
@@ -47,13 +47,15 @@ public final class SavedWorld {
private final Path regionFolder;
private final SavedGround ground;
private final CustomBlocks customBlocks;
private final Cache<Long, OpenRegion> regions = Caffeine.newBuilder()
.maximumSize(heapShareCount())
.build();
SavedWorld(Path regionFolder, SavedGround ground) {
SavedWorld(Path regionFolder, SavedGround ground, CustomBlocks customBlocks) {
this.regionFolder = regionFolder;
this.ground = ground;
this.customBlocks = customBlocks;
}
static boolean holdsRegionFiles(Path folder) {
@@ -119,7 +121,7 @@ public final class SavedWorld {
if (written == null) {
return NOTHING_SAVED;
}
return new OpenRegion(SavedRegion.of(new RegionFile(file), ground), written);
return new OpenRegion(SavedRegion.of(new RegionFile(file), ground, customBlocks), written);
}
private static FileTime lastWritten(Path file) {
@@ -45,11 +45,13 @@ public final class SavedWorlds {
private final Path levelRoot;
private final SavedGround ground;
private final CustomBlocks customBlocks;
private final Map<String, SavedWorld> opened = new ConcurrentHashMap<>();
SavedWorlds(Path levelRoot, SavedGround ground) {
SavedWorlds(Path levelRoot, SavedGround ground, CustomBlocks customBlocks) {
this.levelRoot = levelRoot;
this.ground = ground;
this.customBlocks = customBlocks;
}
/** The server's saved worlds; their regions stay in memory between requests. */
@@ -58,7 +60,8 @@ public final class SavedWorlds {
if (current == null) {
synchronized (SavedWorlds.class) {
if (shared == null) {
shared = new SavedWorlds(IrisWorldStorage.levelRoot().toPath(), SavedGround.platform());
CustomBlocks customBlocks = CustomBlocks.platform();
shared = new SavedWorlds(IrisWorldStorage.levelRoot().toPath(), SavedGround.platform(customBlocks), customBlocks);
}
current = shared;
}
@@ -93,7 +96,7 @@ public final class SavedWorlds {
if (list().stream().noneMatch(info -> info.id().equals(id))) {
throw new NoSuchElementException("No saved world " + id);
}
return opened.computeIfAbsent(id, known -> new SavedWorld(levelRoot.resolve(known).resolve(REGION_FOLDER).normalize(), ground));
return opened.computeIfAbsent(id, known -> new SavedWorld(levelRoot.resolve(known).resolve(REGION_FOLDER).normalize(), ground, customBlocks));
}
private List<Info> dimensionFolders() {
@@ -1,11 +1,50 @@
package art.arcane.iris.core.studio.assets;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class MinecraftAssetsTest {
private static final String BLOCKSTATE = "assets/etopia/blockstates/grass_block.json";
@Rule
public TemporaryFolder folders = new TemporaryFolder();
@Test
public void aPluginsAssetsAreReadFromTheFirstResourcePackThatHasThem() throws IOException {
Path withoutIt = pack("other.zip", "assets/other/blockstates/x.json", "{}");
Path withIt = pack("etopia.zip", BLOCKSTATE, "{\"variants\":{}}");
Optional<byte[]> read = MinecraftAssets.readFromPacks(List.of(withoutIt, withIt), BLOCKSTATE);
assertEquals("{\"variants\":{}}", new String(read.orElseThrow(), StandardCharsets.UTF_8));
assertEquals(Optional.empty(), MinecraftAssets.readFromPacks(List.of(withoutIt, withIt), "assets/etopia/models/none.json"));
assertEquals(Optional.empty(), MinecraftAssets.readFromPacks(List.of(), BLOCKSTATE));
}
private Path pack(String name, String entry, String content) throws IOException {
Path file = folders.getRoot().toPath().resolve(name);
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(file))) {
zip.putNextEntry(new ZipEntry(entry));
zip.write(content.getBytes(StandardCharsets.UTF_8));
zip.closeEntry();
}
return file;
}
@Test
public void onlyFilesInTheClientsAssetFolderAreServed() {
assertTrue(MinecraftAssets.isAssetPath("assets/minecraft/textures/block/stone.png"));
@@ -0,0 +1,68 @@
/*
* Iris is a World Generator for Minecraft Servers
* Copyright (c) 2026 Arcane Arts (Volmit Software)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package art.arcane.iris.core.studio.saved;
import art.arcane.iris.core.link.CustomBlockLook;
import art.arcane.iris.core.link.Identifier;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class CustomBlocksTest {
private static final CustomBlocks ETOPIA = new CustomBlocks(List.of(
new CustomBlockLook(new Identifier("etopia", "grass_block"), "minecraft:tripwire[east=true,attached=false]"),
new CustomBlockLook(new Identifier("etopia", "mushroom_cap"), "minecraft:red_mushroom_block")));
@Test
public void aBorrowedStateIsNamedAsThePluginBlockSavedAsIt() {
assertEquals("etopia:grass_block", ETOPIA.nameSavedAs("minecraft:tripwire", () -> Map.of("attached", "false", "east", "true")));
assertEquals("etopia:mushroom_cap", ETOPIA.nameSavedAs("minecraft:red_mushroom_block", Map::of));
}
@Test
public void otherStatesOfABorrowedBlockKeepTheirName() {
assertEquals("minecraft:tripwire", ETOPIA.nameSavedAs("minecraft:tripwire", () -> Map.of("attached", "true", "east", "true")));
assertEquals("minecraft:tripwire", ETOPIA.nameSavedAs("minecraft:tripwire", Map::of));
}
@Test
public void propertiesAreOnlyReadForBlocksAPluginBorrows() {
assertEquals("minecraft:stone", ETOPIA.nameSavedAs("minecraft:stone", () -> {
fail("Stone is not borrowed");
return Map.of();
}));
}
@Test
public void aPluginBlockShowsAsTheVanillaBlockItBorrows() {
assertEquals(Optional.of("minecraft:tripwire"), ETOPIA.shownBlockOf("etopia:grass_block"));
assertEquals(Optional.empty(), ETOPIA.shownBlockOf("minecraft:tripwire"));
}
@Test
public void withoutPluginBlocksNothingIsRenamed() {
assertEquals("minecraft:tripwire", CustomBlocks.NONE.nameSavedAs("minecraft:tripwire", Map::of));
assertEquals(Optional.empty(), CustomBlocks.NONE.shownBlockOf("etopia:grass_block"));
}
}
@@ -18,6 +18,8 @@
package art.arcane.iris.core.studio.saved;
import art.arcane.iris.core.link.CustomBlockLook;
import art.arcane.iris.core.link.Identifier;
import art.arcane.volmlib.util.nbt.io.NBTSerializer;
import art.arcane.volmlib.util.nbt.io.NamedTag;
import art.arcane.volmlib.util.nbt.tag.CompoundTag;
@@ -47,8 +49,10 @@ public class SavedWorldTest {
private static final int SECTIONS = 8;
private static final int MIN_Y = MIN_SECTION * 16;
private static final List<String> PALETTE = List.of("minecraft:air", "minecraft:stone", "minecraft:oak_log",
"minecraft:oak_leaves", "minecraft:water", "minecraft:snow");
"minecraft:oak_leaves", "minecraft:water", "minecraft:snow", "minecraft:tripwire", "minecraft:tripwire");
private static final int AIR = 0;
private static final int ATTACHED_TRIPWIRE = 6;
private static final int DETACHED_TRIPWIRE = 7;
private static final int STONE = 1;
private static final int LOG = 2;
private static final int LEAVES = 3;
@@ -61,13 +65,16 @@ public class SavedWorldTest {
private static final int POND_FLOOR = 0;
private static final int POND_SURFACE = 5;
private static final SavedGround STONE_IS_GROUND = block -> block.equals("minecraft:stone") || block.equals("minecraft:snow");
private static final SavedGround ETOPIA_GRASS_IS_GROUND = block -> STONE_IS_GROUND.isGround(block) || block.equals("etopia:grass_block");
private static final CustomBlocks ETOPIA_GRASS = new CustomBlocks(List.of(
new CustomBlockLook(new Identifier("etopia", "grass_block"), "minecraft:tripwire[attached=true]")));
@Rule
public TemporaryFolder folders = new TemporaryFolder();
@Test
public void groundIsTheTopWholeBlockUnderTreesAndWater() throws IOException {
SavedWorld world = new SavedWorld(regionFolderWithOneChunk(), STONE_IS_GROUND);
SavedWorld world = new SavedWorld(regionFolderWithOneChunk(), STONE_IS_GROUND, CustomBlocks.NONE);
SavedRegion.SavedColumn underTree = world.columnAt(0, 0);
assertEquals(TREE_GROUND_TOP, underTree.groundY());
@@ -84,7 +91,7 @@ public class SavedWorldTest {
@Test
public void snowCountsAsGroundOnTheGroundButNotOnATree() throws IOException {
SavedWorld world = new SavedWorld(regionFolderWithOneChunk(), STONE_IS_GROUND);
SavedWorld world = new SavedWorld(regionFolderWithOneChunk(), STONE_IS_GROUND, CustomBlocks.NONE);
assertEquals(TREE_GROUND_TOP, world.columnAt(0, 0).groundY());
SavedRegion.SavedColumn snowyGround = world.columnAt(2, 0);
@@ -92,9 +99,31 @@ public class SavedWorldTest {
assertEquals("minecraft:snow", snowyGround.block());
}
@Test
public void aPluginBlockIsNamedAsItsPluginNamesItWhereItsBorrowedStateIsSaved() throws IOException {
SavedWorld world = new SavedWorld(regionFolderWithOneChunk(), ETOPIA_GRASS_IS_GROUND, ETOPIA_GRASS);
SavedRegion.SavedColumn plugin = world.columnAt(3, 0);
assertEquals(PLAIN_GROUND_TOP + 1, plugin.groundY());
assertEquals("etopia:grass_block", plugin.block());
SavedRegion.SavedColumn otherState = world.columnAt(5, 0);
assertEquals(PLAIN_GROUND_TOP, otherState.groundY());
assertEquals("minecraft:stone", otherState.block());
}
@Test
public void withoutPluginBlocksTheBorrowedStateIsOnlyItsVanillaBlock() throws IOException {
SavedWorld world = new SavedWorld(regionFolderWithOneChunk(), ETOPIA_GRASS_IS_GROUND, CustomBlocks.NONE);
SavedRegion.SavedColumn borrowed = world.columnAt(3, 0);
assertEquals(PLAIN_GROUND_TOP, borrowed.groundY());
assertEquals("minecraft:stone", borrowed.block());
}
@Test
public void chunksWithUnfinishedTerrainOrNoFileHaveNoGround() throws IOException {
SavedWorld world = new SavedWorld(regionFolderWithOneChunk(), STONE_IS_GROUND);
SavedWorld world = new SavedWorld(regionFolderWithOneChunk(), STONE_IS_GROUND, CustomBlocks.NONE);
assertNull(world.columnAt(16, 0));
assertNull(world.columnAt(-1, -1));
@@ -102,7 +131,7 @@ public class SavedWorldTest {
@Test
public void theViewStartsInTheMiddleOfTheRegionNearestTheOrigin() throws IOException {
SavedWorld.Start start = new SavedWorld(regionFolderWithOneChunk(), STONE_IS_GROUND).start().orElseThrow();
SavedWorld.Start start = new SavedWorld(regionFolderWithOneChunk(), STONE_IS_GROUND, CustomBlocks.NONE).start().orElseThrow();
assertEquals(256, start.centerX());
assertEquals(256, start.centerZ());
@@ -114,7 +143,7 @@ public class SavedWorldTest {
@Test
public void aTileSamplesOneColumnEveryFewBlocksAndNamesEachBlockOnce() throws IOException {
SavedWorld world = new SavedWorld(regionFolderWithOneChunk(), STONE_IS_GROUND);
SavedWorld world = new SavedWorld(regionFolderWithOneChunk(), STONE_IS_GROUND, CustomBlocks.NONE);
SavedTile tile = SavedTile.sample(world, 0, 0, 2, 16);
@@ -132,7 +161,7 @@ public class SavedWorldTest {
writeRegion(level.resolve("region"));
writeRegion(level.resolve("dimensions/iris/demo/region"));
Files.createDirectories(level.resolve("dimensions/iris/empty/region"));
SavedWorlds worlds = new SavedWorlds(level, STONE_IS_GROUND);
SavedWorlds worlds = new SavedWorlds(level, STONE_IS_GROUND, CustomBlocks.NONE);
assertEquals(List.of(new SavedWorlds.Info(".", "overworld", "minecraft"),
new SavedWorlds.Info("dimensions/iris/demo", "demo", "iris")), worlds.list());
@@ -191,8 +220,8 @@ public class SavedWorldTest {
return bytes.toByteArray();
}
// Column (0, 0) is a log on stone with snowy leaves on top, (1, 0) a pond on stone, (2, 0) snow on stone, every
// other column stone to y 5.
// Column (0, 0) is a log on stone with snowy leaves on top, (1, 0) a pond on stone, (2, 0) snow on stone, (3, 0) an
// attached and (5, 0) a detached tripwire on stone, every other column stone to y 5.
private static CompoundTag chunk(String status) {
CompoundTag root = new CompoundTag();
root.putString("Status", status);
@@ -212,7 +241,7 @@ public class SavedWorldTest {
if (x == 0 && z == 0) {
return SNOWY_TREE_TOP;
}
if (x == 2 && z == 0) {
if ((x == 2 || x == 3 || x == 5) && z == 0) {
return PLAIN_GROUND_TOP + 1;
}
return x == 1 && z == 0 ? POND_SURFACE : PLAIN_GROUND_TOP;
@@ -228,6 +257,9 @@ public class SavedWorldTest {
if (x == 2 && z == 0) {
return y <= PLAIN_GROUND_TOP ? STONE : y == PLAIN_GROUND_TOP + 1 ? SNOW : AIR;
}
if ((x == 3 || x == 5) && z == 0) {
return y <= PLAIN_GROUND_TOP ? STONE : y == PLAIN_GROUND_TOP + 1 ? (x == 3 ? ATTACHED_TRIPWIRE : DETACHED_TRIPWIRE) : AIR;
}
return y <= PLAIN_GROUND_TOP ? STONE : AIR;
}
@@ -249,9 +281,15 @@ public class SavedWorldTest {
// Water is written the way 26.1 writes a block without properties among others: its bare name under an empty key.
private static ListTag<CompoundTag> blockPalette() {
ListTag<CompoundTag> palette = new ListTag<>(CompoundTag.class);
for (String block : PALETTE) {
for (int index = 0; index < PALETTE.size(); index++) {
String block = PALETTE.get(index);
CompoundTag entry = new CompoundTag();
entry.putString(block.equals("minecraft:water") ? "" : "Name", block);
if (index == ATTACHED_TRIPWIRE || index == DETACHED_TRIPWIRE) {
CompoundTag properties = new CompoundTag();
properties.putString("attached", index == ATTACHED_TRIPWIRE ? "true" : "false");
entry.put("Properties", properties);
}
palette.add(entry);
}
return palette;