Merge stream lowering

- Streams go down with structure cuts
- Recaptures the schema digest for F1 and F3 together
This commit is contained in:
Brian Fopiano committed 2026-11-03 16:39:27 -05:00
commit 1ddda75efb
10 files changed
+853 -90

No files matched your search

@@ -167,6 +167,7 @@ public final class HydrologyProbe {
HydrologyBlockMetrics.Counts blocks = blockMetrics.count(key);
generationErrors += blocks.errors();
HydrologyUnderground.Counts underground = underground(key, plan);
System.out.println("IRIS_HYDROLOGY_LINK wx=" + key.wx() + " wz=" + key.wz() + " unlinkedWater=" + rasterMetrics.unlinkedWater(plan));
HydrologyColdRuns.ColdRun cold = colds.get(key);
measured.add(new HydrologyReport.WindowMetrics(key, cold.millis(), cold.naturalSamples(), cold.baseSamples(),
plans.windowCpu(key), plans.regionCpu(key.region()), cold.depth(), plans.landTiles(key), planMetrics.rivers(key),
@@ -11,7 +11,11 @@ import art.arcane.iris.generation.hydrology.water.PondKind;
import art.arcane.iris.generation.hydrology.water.Reach;
import art.arcane.iris.generation.hydrology.water.ReachStyle;
import art.arcane.iris.generation.hydrology.raster.NaturalHeights;
import art.arcane.volmlib.nativelib.terrain.structure.StructureStreamLowering;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Consumer;
final class HydrologyRasterMetrics {
@@ -22,6 +26,7 @@ final class HydrologyRasterMetrics {
private final Rasters rasters;
private final NaturalHeights naturalHeight;
private final HydrologyWindows.WindowRange range;
private static final int LINK_SEARCH = 32;
private final Consumer<String> examples;
@FunctionalInterface
@@ -89,6 +94,66 @@ final class HydrologyRasterMetrics {
return rising;
}
long unlinkedWater(WindowPlan plan) {
long unlinked = 0L;
for (Reach reach : plan.reaches()) {
int lastX = 0;
int lastZ = 0;
boolean wet = false;
for (int point = 0; point + 1 < reach.xs().length; point++) {
float fromX = reach.xs()[point];
float fromZ = reach.zs()[point];
float toX = reach.xs()[point + 1];
float toZ = reach.zs()[point + 1];
int steps = Math.max(1, (int) Math.ceil(Math.max(Math.abs(toX - fromX), Math.abs(toZ - fromZ))));
for (int step = 0; step <= steps; step++) {
int x = (int) Math.floor(fromX + (toX - fromX) * step / steps);
int z = (int) Math.floor(fromZ + (toZ - fromZ) * step / steps);
if (!wetInRange(x, z)) {
continue;
}
if (wet && !linkedByWater(lastX, lastZ, x, z)) {
unlinked++;
examples.accept("water gap along reach " + reach.id() + " from " + lastX + "," + lastZ + " to " + x + "," + z);
}
lastX = x;
lastZ = z;
wet = true;
}
}
}
return unlinked;
}
private boolean wetInRange(int x, int z) {
return range.containsBlock(x, z) && rasters.get(x >> 4, z >> 4).role(x & 15, z & 15).wet();
}
private boolean linkedByWater(int fromX, int fromZ, int toX, int toZ) {
Set<Long> seen = new HashSet<>();
ArrayDeque<int[]> open = new ArrayDeque<>();
open.add(new int[]{fromX, fromZ});
seen.add(HydrologyVoxels.columnKey(fromX, fromZ));
int limit = StructureStreamLowering.LINK + LINK_SEARCH + Math.max(Math.abs(toX - fromX), Math.abs(toZ - fromZ));
while (!open.isEmpty()) {
int[] cell = open.poll();
if (Math.max(Math.abs(cell[0] - toX), Math.abs(cell[1] - toZ)) <= StructureStreamLowering.LINK) {
return true;
}
for (int dz = -StructureStreamLowering.LINK; dz <= StructureStreamLowering.LINK; dz++) {
for (int dx = -StructureStreamLowering.LINK; dx <= StructureStreamLowering.LINK; dx++) {
int x = cell[0] + dx;
int z = cell[1] + dz;
if (Math.max(Math.abs(x - fromX), Math.abs(z - fromZ)) <= limit && wetInRange(x, z)
&& seen.add(HydrologyVoxels.columnKey(x, z))) {
open.add(new int[]{x, z});
}
}
}
}
return false;
}
long chunkSeams(WindowKey key) {
long mismatches = 0L;
int minX = key.wx() * WindowKey.SIZE;
@@ -0,0 +1,393 @@
package art.arcane.iris.probe;
import art.arcane.volmlib.util.nbt.io.NBTUtil;
import art.arcane.volmlib.util.nbt.tag.CompoundTag;
import art.arcane.volmlib.util.nbt.tag.IntArrayTag;
import art.arcane.volmlib.util.nbt.tag.ListTag;
import art.arcane.volmlib.util.nbt.tag.LongArrayTag;
import art.arcane.volmlib.util.nbt.tag.NumberTag;
import art.arcane.volmlib.util.nbt.tag.StringTag;
import art.arcane.volmlib.util.nbt.tag.Tag;
import net.jpountz.lz4.LZ4BlockInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
public final class WaterPerchCensus {
private static final Pattern REGION_FILE = Pattern.compile("r\\.(-?\\d+)\\.(-?\\d+)\\.mca");
private static final int SECTOR_BYTES = 4096;
private static final int[][] CARDINALS = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
private static final int LISTED_CELLS = 40;
private static final int NOT_WATER = Integer.MIN_VALUE;
private static final int MISSING = Integer.MIN_VALUE;
private static final int FLOWING_BIT = 1;
private static final int SOLID_BED_BIT = 2;
private static final Set<String> NON_SOLID_BED = Set.of("minecraft:air", "minecraft:cave_air", "minecraft:void_air");
private static final List<String> FLATTEN_STRUCTURES = List.of("minecraft:village", "minecraft:pillager_outpost",
"minecraft:mansion", "minecraft:desert_pyramid", "minecraft:jungle_pyramid", "minecraft:igloo",
"minecraft:swamp_hut", "minecraft:shipwreck_beached", "minecraft:ruined_portal");
private WaterPerchCensus() {
}
public static void main(String[] arguments) throws IOException {
if (arguments.length < 1 || arguments.length > 2) {
throw new IllegalArgumentException("Usage: <copied region folder> [blend padding]");
}
int padding = arguments.length == 2 ? Integer.parseInt(arguments[1]) : 24;
Result result = scan(Path.of(arguments[0]), padding);
for (String line : result.lines()) {
System.out.println(line);
}
System.exit(result.insideSources() == 0 ? 0 : 1);
}
static Result scan(Path regionFolder, int padding) throws IOException {
Map<Long, int[]> tops = new HashMap<>();
Map<Long, int[]> waters = new HashMap<>();
List<Start> starts = new ArrayList<>();
try (Stream<Path> files = Files.list(regionFolder)) {
for (Path file : files.sorted().toList()) {
Matcher matcher = REGION_FILE.matcher(file.getFileName().toString());
if (matcher.matches()) {
readRegion(file, Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)), tops, waters, starts);
}
}
}
Map<Long, List<Start>> startsByChunk = new HashMap<>();
for (Start start : starts) {
for (Bounds piece : start.pieces()) {
for (int chunkX = (piece.minX() - padding) >> 4; chunkX <= (piece.maxX() + padding) >> 4; chunkX++) {
for (int chunkZ = (piece.minZ() - padding) >> 4; chunkZ <= (piece.maxZ() + padding) >> 4; chunkZ++) {
List<Start> near = startsByChunk.computeIfAbsent(chunkKey(chunkX, chunkZ), key -> new ArrayList<>());
if (!near.contains(start)) {
near.add(start);
}
}
}
}
}
long waterColumns = 0L;
long insideSources = 0L;
long outsideSources = 0L;
long insideFlowing = 0L;
long outsideFlowing = 0L;
long skippedEdges = 0L;
Map<String, Long> byStructure = new TreeMap<>();
Map<Long, Boolean> insideChunks = new HashMap<>();
List<String> cells = new ArrayList<>();
for (Map.Entry<Long, int[]> chunk : waters.entrySet()) {
int chunkX = (int) (chunk.getKey() >> 32);
int chunkZ = (int) (long) chunk.getKey();
for (int index = 0; index < 256; index++) {
int water = chunk.getValue()[index];
if (water == NOT_WATER) {
continue;
}
waterColumns++;
int x = (chunkX << 4) + (index & 15);
int z = (chunkZ << 4) + (index >> 4);
int surface = waterY(water);
boolean lower = false;
boolean known = true;
for (int[] direction : CARDINALS) {
int neighbourTop = top(tops, x + direction[0], z + direction[1]);
if (neighbourTop == MISSING) {
known = false;
continue;
}
lower |= neighbourTop < surface;
}
if (!known) {
skippedEdges++;
}
if (!lower || !solidBed(water)) {
continue;
}
Start blend = blendAt(startsByChunk.get(chunkKey(chunkX, chunkZ)), x, z, padding);
boolean source = isSource(water);
if (blend != null) {
if (source) {
insideSources++;
insideChunks.put(chunk.getKey(), true);
byStructure.merge(blend.id(), 1L, Long::sum);
if (cells.size() < LISTED_CELLS) {
cells.add(x + "," + surface + "," + z + " " + blend.id());
}
} else {
insideFlowing++;
}
} else if (source) {
outsideSources++;
if (cells.size() < LISTED_CELLS) {
cells.add(x + "," + surface + "," + z + " outside");
}
} else {
outsideFlowing++;
}
}
}
return new Result(tops.size(), waterColumns, skippedEdges, insideSources, outsideSources, insideFlowing, outsideFlowing,
insideChunks.size(), starts.size(), byStructure, cells);
}
private static int encodeWater(int y, boolean source, boolean solidBed) {
return (y << 2) | (source ? 0 : FLOWING_BIT) | (solidBed ? SOLID_BED_BIT : 0);
}
private static int waterY(int water) {
return water >> 2;
}
private static boolean isSource(int water) {
return (water & FLOWING_BIT) == 0;
}
private static boolean solidBed(int water) {
return (water & SOLID_BED_BIT) != 0;
}
private static int top(Map<Long, int[]> tops, int x, int z) {
int[] chunk = tops.get(chunkKey(x >> 4, z >> 4));
return chunk == null ? MISSING : chunk[((z & 15) << 4) | (x & 15)];
}
private static Start blendAt(List<Start> near, int x, int z, int padding) {
if (near == null) {
return null;
}
for (Start start : near) {
for (Bounds piece : start.pieces()) {
if (x >= piece.minX() - padding && x <= piece.maxX() + padding
&& z >= piece.minZ() - padding && z <= piece.maxZ() + padding) {
return start;
}
}
}
return null;
}
private static long chunkKey(int chunkX, int chunkZ) {
return ((long) chunkX << 32) | (chunkZ & 0xFFFFFFFFL);
}
private static void readRegion(Path file, int regionX, int regionZ, Map<Long, int[]> tops, Map<Long, int[]> waters,
List<Start> starts) throws IOException {
byte[] region = Files.readAllBytes(file);
if (region.length < SECTOR_BYTES * 2) {
return;
}
ByteBuffer buffer = ByteBuffer.wrap(region);
for (int index = 0; index < 1024; index++) {
int location = buffer.getInt(index * 4);
int sector = location >>> 8;
if (sector == 0 || (long) sector * SECTOR_BYTES + 5 > region.length) {
continue;
}
int chunkX = (regionX << 5) + (index & 31);
int chunkZ = (regionZ << 5) + (index >> 5);
int length = buffer.getInt(sector * SECTOR_BYTES);
int compression = region[sector * SECTOR_BYTES + 4];
if (length <= 1 || (compression & 0x80) != 0 || (long) sector * SECTOR_BYTES + 4 + length > region.length) {
continue;
}
ByteArrayInputStream raw = new ByteArrayInputStream(region, sector * SECTOR_BYTES + 5, length - 1);
try (InputStream input = switch (compression) {
case 1 -> new GZIPInputStream(raw);
case 2 -> new InflaterInputStream(raw);
case 3 -> raw;
case 4 -> new LZ4BlockInputStream(raw);
default -> throw new IOException("Chunk " + chunkX + "," + chunkZ + " uses unknown compression " + compression);
}) {
Tag<?> parsed = NBTUtil.read(input, false).getTag();
if (!(parsed instanceof CompoundTag root)) {
throw new IOException("Chunk " + chunkX + "," + chunkZ + " is not a compound");
}
readChunk(root, chunkX, chunkZ, tops, waters, starts);
}
}
}
private static void readChunk(CompoundTag root, int chunkX, int chunkZ, Map<Long, int[]> tops, Map<Long, int[]> waters,
List<Start> starts) throws IOException {
if (root.get("Status") instanceof StringTag status
&& !status.getValue().equals("minecraft:full") && !status.getValue().equals("full")) {
return;
}
if (!(root.get("sections") instanceof ListTag<?> sections)) {
return;
}
Map<Integer, Palette> blocks = new TreeMap<>();
for (Tag<?> entry : sections) {
if (entry instanceof CompoundTag section && section.get("block_states") instanceof CompoundTag states
&& section.get("Y") instanceof NumberTag<?> sectionY) {
blocks.put(sectionY.asInt(), Palette.read(states));
}
}
int[] top = new int[256];
int[] water = new int[256];
List<Integer> descending = new ArrayList<>(blocks.keySet());
descending.sort((left, right) -> right - left);
for (int index = 0; index < 256; index++) {
top[index] = Integer.MIN_VALUE + 1;
water[index] = NOT_WATER;
int x = index & 15;
int z = index >> 4;
boolean found = false;
for (int sectionIndex = 0; sectionIndex < descending.size() && !found; sectionIndex++) {
int sectionY = descending.get(sectionIndex);
Palette palette = blocks.get(sectionY);
for (int localY = 15; localY >= 0 && !found; localY--) {
Block block = palette.get(x, localY, z);
if (block.air()) {
continue;
}
found = true;
int y = sectionY * 16 + localY;
top[index] = y;
if (block.water()) {
Block below = blockAt(blocks, x, y - 1, z);
water[index] = encodeWater(y, block.source(), below != null && !below.air() && !below.water());
}
}
}
}
tops.put(chunkKey(chunkX, chunkZ), top);
waters.put(chunkKey(chunkX, chunkZ), water);
if (root.get("structures") instanceof CompoundTag structures && structures.get("starts") instanceof CompoundTag startTags) {
for (Map.Entry<String, Tag<?>> entry : startTags) {
if (entry.getValue() instanceof CompoundTag start) {
readStart(entry.getKey(), start, starts);
}
}
}
}
private static Block blockAt(Map<Integer, Palette> blocks, int x, int y, int z) throws IOException {
Palette palette = blocks.get(Math.floorDiv(y, 16));
return palette == null ? null : palette.get(x, Math.floorMod(y, 16), z);
}
private static void readStart(String id, CompoundTag start, List<Start> starts) {
boolean flatten = false;
for (String prefix : FLATTEN_STRUCTURES) {
flatten |= id.startsWith(prefix);
}
if (!flatten || !(start.get("Children") instanceof ListTag<?> children)) {
return;
}
List<Bounds> pieces = new ArrayList<>();
for (Tag<?> child : children) {
if (child instanceof CompoundTag piece && piece.get("BB") instanceof IntArrayTag box && box.getValue().length == 6) {
int[] value = box.getValue();
pieces.add(new Bounds(value[0], value[2], value[3], value[5]));
}
}
if (!pieces.isEmpty()) {
starts.add(new Start(id, List.copyOf(pieces)));
}
}
record Bounds(int minX, int minZ, int maxX, int maxZ) {
}
record Start(String id, List<Bounds> pieces) {
}
private record Block(boolean air, boolean water, boolean source) {
}
private static final class Palette {
private final Block[] values;
private final long[] data;
private final int bits;
private final int perLong;
private Palette(Block[] values, long[] data) {
this.values = values;
this.data = data;
bits = Math.max(4, 32 - Integer.numberOfLeadingZeros(values.length - 1));
perLong = 64 / bits;
}
private static Palette read(CompoundTag states) throws IOException {
if (!(states.get("palette") instanceof ListTag<?> palette) || palette.size() == 0) {
throw new IOException("Section has no block palette");
}
Block[] values = new Block[palette.size()];
int index = 0;
for (Tag<?> entry : palette) {
values[index++] = block(entry);
}
long[] data = states.get("data") instanceof LongArrayTag longs ? longs.getValue() : new long[0];
return new Palette(values, data);
}
private static Block block(Tag<?> entry) throws IOException {
Tag<?> state = entry;
if (state instanceof CompoundTag wrapped && wrapped.size() == 1 && wrapped.containsKey("")) {
state = wrapped.get("");
}
if (state instanceof StringTag name) {
return describe(name.getValue(), "0");
}
if (state instanceof CompoundTag compound) {
String name = compound.get("Name") instanceof StringTag upper ? upper.getValue()
: compound.get("id") instanceof StringTag lower ? lower.getValue() : null;
if (name == null) {
throw new IOException("Block palette entry has no name");
}
Tag<?> properties = compound.containsKey("Properties") ? compound.get("Properties") : compound.get("properties");
String level = properties instanceof CompoundTag values && values.get("level") instanceof StringTag text
? text.getValue() : "0";
return describe(name, level);
}
throw new IOException("Block palette entry is not a block state");
}
private static Block describe(String name, String level) {
String key = name.toLowerCase(Locale.ROOT);
return new Block(NON_SOLID_BED.contains(key), key.equals("minecraft:water"), level.equals("0"));
}
private Block get(int x, int y, int z) {
if (values.length == 1) {
return values[0];
}
int index = (y << 8) | (z << 4) | x;
int packed = (int) ((data[index / perLong] >>> ((index % perLong) * bits)) & ((1L << bits) - 1L));
return values[packed];
}
}
record Result(int chunks, long waterColumns, long skippedEdges, long insideSources, long outsideSources,
long insideFlowing, long outsideFlowing, int insideChunks, int flattenStarts,
Map<String, Long> byStructure, List<String> cells) {
List<String> lines() {
List<String> lines = new ArrayList<>();
lines.add("[census] chunks=" + chunks + " waterTopped=" + waterColumns + " edgeColumnsWithAMissingNeighbour=" + skippedEdges
+ " flattenStarts=" + flattenStarts);
lines.add("[census] perched sources inside blends=" + insideSources + " (" + insideChunks + " chunks), outside=" + outsideSources);
lines.add("[census] perched flowing inside blends=" + insideFlowing + ", outside=" + outsideFlowing);
byStructure.forEach((String id, Long count) -> lines.add("[census] " + id + " " + count));
cells.forEach((String cell) -> lines.add("[census] cell " + cell));
return lines;
}
}
}
@@ -156,6 +156,29 @@ public class HydrologyMetricsTest {
assertRises(0L, null, List.of(mouth), List.of(), level);
}
@Test
public void waterAlongAReachMustStayWithinTheStreamLinkRadius() {
Reach reach = reach(1L, STREAM, 0L, 1000f, new int[]{60, 60}, 10f, 10f, 30f, 10f);
Map<Long, Column> linked = new HashMap<>();
for (int x = 10; x <= 30; x += 2) {
linked.put(HydrologyVoxels.columnKey(x, 10), new Column(WaterRole.CHANNEL, 60, 58, 1L));
}
assertEquals(0L, unlinked(reach, linked));
Map<Long, Column> gapped = new HashMap<>(linked);
gapped.remove(HydrologyVoxels.columnKey(20, 10));
assertEquals(1L, unlinked(reach, gapped));
}
private static long unlinked(Reach reach, Map<Long, Column> columns) {
WindowPlan plan = plan(WEST, List.of(reach), List.of());
HydrologyPlanMetrics plans = new HydrologyPlanMetrics(SETTINGS, windows(plan, plan(EAST, List.of(), List.of())), List.of(), BOTH, ignored -> {
});
HydrologyRasterMetrics metrics = new HydrologyRasterMetrics(SETTINGS, plans, rasters(columns), (x, z) -> NATURAL, BOTH, ignored -> {
});
return metrics.unlinkedWater(plan);
}
@Test
public void aReachEndingInItsPondAtThePondsSnappedHeadDoesNotRise() {
Pond odd = pond(10L, 51, square(96, 96, 3), 0L, 100, 100);
@@ -0,0 +1,166 @@
package art.arcane.iris.probe;
import art.arcane.volmlib.util.nbt.io.NBTUtil;
import art.arcane.volmlib.util.nbt.tag.CompoundTag;
import art.arcane.volmlib.util.nbt.tag.ListTag;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.zip.DeflaterOutputStream;
import static org.junit.Assert.assertEquals;
public class WaterPerchCensusTest {
private static final int GROUND = 3;
private static final int STONE = 1;
private static final int WATER = 2;
private static final int FLOWING = 3;
private static final int LAKE_FLOOR = 0;
@Rule
public TemporaryFolder folder = new TemporaryFolder();
@Test
public void countsPerchedSourcesInsideAndOutsideBlends() throws IOException {
int[][][] blocks = column(heights(GROUND));
blocks[GROUND + 1][4][4] = WATER;
blocks[GROUND + 1][4][12] = WATER;
blocks[GROUND + 1][12][12] = FLOWING;
Path regions = folder.newFolder("region").toPath();
writeRegion(regions, 0, 0, section(blocks), startAt(0, 0, 0, 8));
WaterPerchCensus.Result result = WaterPerchCensus.scan(regions, 0);
assertEquals(3L, result.waterColumns());
assertEquals("the source within the piece", 1L, result.insideSources());
assertEquals("the source away from the piece", 1L, result.outsideSources());
assertEquals(1L, result.outsideFlowing());
}
@Test
public void waterHeldByEqualOrTallerBanksIsNotPerched() throws IOException {
int[][][] blocks = column(heights(GROUND));
for (int z = 3; z <= 5; z++) {
for (int x = 3; x <= 5; x++) {
blocks[GROUND + 1][z][x] = STONE;
}
}
blocks[GROUND + 1][4][4] = WATER;
Path regions = folder.newFolder("region").toPath();
writeRegion(regions, 0, 0, section(blocks), null);
WaterPerchCensus.Result result = WaterPerchCensus.scan(regions, 24);
assertEquals(1L, result.waterColumns());
assertEquals(0L, result.insideSources());
assertEquals(0L, result.outsideSources());
}
@Test
public void waterWithoutASolidBedIsNotCounted() throws IOException {
int[][][] blocks = column(heights(LAKE_FLOOR));
blocks[GROUND][4][4] = WATER;
Path regions = folder.newFolder("region").toPath();
writeRegion(regions, 0, 0, section(blocks), null);
assertEquals(0L, WaterPerchCensus.scan(regions, 24).outsideSources());
}
private static int[][] heights(int height) {
int[][] heights = new int[16][16];
for (int[] row : heights) {
Arrays.fill(row, height);
}
return heights;
}
private static int[][][] column(int[][] heights) {
int[][][] blocks = new int[16][16][16];
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
for (int y = 0; y <= heights[z][x] && y < 16; y++) {
blocks[y][z][x] = STONE;
}
}
}
return blocks;
}
private static CompoundTag section(int[][][] blocks) {
ListTag<CompoundTag> palette = new ListTag<>(CompoundTag.class);
palette.add(state("minecraft:air", null));
palette.add(state("minecraft:stone", null));
palette.add(state("minecraft:water", "0"));
palette.add(state("minecraft:water", "1"));
long[] data = new long[256];
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
int index = (y << 8) | (z << 4) | x;
data[index / 16] |= (long) blocks[y][z][x] << ((index % 16) * 4);
}
}
}
CompoundTag states = new CompoundTag();
states.put("palette", palette);
states.putLongArray("data", data);
CompoundTag section = new CompoundTag();
section.putByte("Y", (byte) 0);
section.put("block_states", states);
return section;
}
private static CompoundTag state(String name, String level) {
CompoundTag state = new CompoundTag();
state.putString("Name", name);
if (level != null) {
CompoundTag properties = new CompoundTag();
properties.putString("level", level);
state.put("Properties", properties);
}
return state;
}
private static CompoundTag startAt(int minX, int minZ, int maxX, int maxZ) {
CompoundTag piece = new CompoundTag();
piece.putIntArray("BB", new int[]{minX, 0, minZ, 8, 15, maxZ});
ListTag<CompoundTag> children = new ListTag<>(CompoundTag.class);
children.add(piece);
CompoundTag start = new CompoundTag();
start.put("Children", children);
return start;
}
private static void writeRegion(Path folder, int chunkX, int chunkZ, CompoundTag section, CompoundTag start) throws IOException {
CompoundTag root = new CompoundTag();
root.putString("Status", "minecraft:full");
ListTag<CompoundTag> sections = new ListTag<>(CompoundTag.class);
sections.add(section);
root.put("sections", sections);
if (start != null) {
CompoundTag starts = new CompoundTag();
starts.put("minecraft:village_plains", start);
CompoundTag structures = new CompoundTag();
structures.put("starts", starts);
root.put("structures", structures);
}
ByteArrayOutputStream compressed = new ByteArrayOutputStream();
try (DeflaterOutputStream deflater = new DeflaterOutputStream(compressed)) {
NBTUtil.write(root, deflater, false);
}
byte[] payload = compressed.toByteArray();
ByteBuffer region = ByteBuffer.allocate(4096 * (3 + payload.length / 4096));
region.putInt((chunkZ << 5 | chunkX) * 4, 2 << 8 | (1 + payload.length / 4096));
region.putInt(4096 * 2, payload.length + 1);
region.put(4096 * 2 + 4, (byte) 2);
region.put(4096 * 2 + 5, payload);
Files.write(folder.resolve("r.0.0.mca"), region.array());
}
}