Skip to content

Commit 4c0924b

Browse files
committed
Implement region file reading and chunk structure
1 parent 86598e7 commit 4c0924b

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/main/java/org/glavo/nbt/chunk/Chunk.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@
1616
package org.glavo.nbt.chunk;
1717

1818
import org.glavo.nbt.NBTElement;
19+
import org.jetbrains.annotations.Nullable;
1920

2021
public final class Chunk implements NBTElement {
21-
// TODO
22+
private int index = -1;
23+
private @Nullable Region region;
24+
25+
public Chunk() {
26+
}
27+
28+
/// Return the region of this chunk, or `null` if this chunk is not in any region.
29+
public @Nullable Region getRegion() {
30+
return region;
31+
}
2232
}

src/main/java/org/glavo/nbt/chunk/Region.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,41 @@
1515
*/
1616
package org.glavo.nbt.chunk;
1717

18+
import org.glavo.nbt.MinecraftEdition;
1819
import org.glavo.nbt.NBTElement;
20+
import org.glavo.nbt.internal.input.InputContext;
1921

22+
import java.io.IOException;
2023
import java.util.Objects;
2124

2225
/// @see <a href="https://minecraft.wiki/w/Region_file_format">Region file format - Minecraft Wiki</a>
2326
/// @see <a href="https://minecraft.wiki/w/Anvil_file_format">Anvil file format - Minecraft Wiki</a>
2427
public final class Region implements NBTElement {
2528
private static final int CHUNKS_PER_REGION_SIDE = 32;
2629

27-
// TODO
30+
static Region readRegion(InputContext context) throws IOException {
31+
if (context.edition != MinecraftEdition.JAVA_EDITION) {
32+
throw new IllegalArgumentException("Only Java Edition supports region file format");
33+
}
34+
35+
int[] diskInfo = context.rawReader.readIntArray(CHUNKS_PER_REGION_SIDE * CHUNKS_PER_REGION_SIDE);
36+
int[] timestamps = context.rawReader.readIntArray(CHUNKS_PER_REGION_SIDE * CHUNKS_PER_REGION_SIDE);
37+
38+
for (int y = 0; y < CHUNKS_PER_REGION_SIDE; y++) {
39+
for (int x = 0; x < CHUNKS_PER_REGION_SIDE; x++) {
40+
int index = x + y * CHUNKS_PER_REGION_SIDE;
41+
42+
int info = diskInfo[index];
43+
int sectorOffset = info >>> 8;
44+
int sectorCount = info & 0xFF;
45+
int timestamp = timestamps[index];
46+
47+
}
48+
}
49+
50+
throw new AssertionError("Not implemented yet");
51+
}
52+
2853
private final Chunk[] chunks = new Chunk[CHUNKS_PER_REGION_SIDE * CHUNKS_PER_REGION_SIDE];
2954

3055
public Chunk getChunk(int x, int z) {

src/main/java/org/glavo/nbt/internal/input/DataReader.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,20 @@ public byte[] readByteArray() throws IOException {
5252
throw new IOException("Array length too large");
5353
}
5454

55+
return readBYteArray(len);
56+
}
57+
58+
public byte[] readByteArray(int len) throws IOException {
5559
ensureBufferRemaining(len);
5660
return buffer.getByteArray(len);
5761
}
5862

5963
public int[] readIntArray() throws IOException {
6064
int len = readInt();
65+
return readIntArray(len);
66+
}
67+
68+
public int[] readIntArray(int len) throws IOException {
6169
if (len < 0 || len > Integer.MAX_VALUE / Integer.BYTES - 8) {
6270
throw new IOException("Array length too large");
6371
}
@@ -76,6 +84,15 @@ public long[] readLongArray() throws IOException {
7684
return buffer.getLongArray(len);
7785
}
7886

87+
public long[] readLongArray(int len) throws IOException {
88+
if (len < 0 || len > Integer.MAX_VALUE / Long.BYTES - 8) {
89+
throw new IOException("Array length too large");
90+
}
91+
92+
ensureBufferRemaining(len * Long.BYTES);
93+
return buffer.getLongArray(len);
94+
}
95+
7996
/// Read a byte from the input stream.
8097
public byte readByte() throws IOException {
8198
ensureBufferRemaining(Byte.BYTES);

0 commit comments

Comments
 (0)