Skip to content

Commit 58f2f4d

Browse files
committed
refactor importers
1 parent 690a690 commit 58f2f4d

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

importer-anvil/src/main/java/net/roxymc/slime/importer/anvil/SlimeAnvilImporter.java

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
public class SlimeAnvilImporter implements SlimeImporter {
3535
private static final Entity[] EMPTY_ENTITIES = new Entity[0];
36+
private static final int SECTION_COUNT = 1024;
3637
private static final int SECTION_SIZE = 4096;
3738
private static final String LEVEL_DAT = "level.dat";
3839
private static final String REGION_DIR = "region";
@@ -84,12 +85,10 @@ public static Builder builder(SlimeLoader slimeLoader) {
8485
return new Builder(slimeLoader);
8586
}
8687

87-
@Override
8888
public Set<String> preservedWorldTags() {
8989
return preservedWorldTags;
9090
}
9191

92-
@Override
9392
public Set<String> preservedChunkTags() {
9493
return preservedChunkTags;
9594
}
@@ -217,23 +216,23 @@ private Chunk[] readChunks(File root, Map<ChunkPos, EntityChunk> entityChunks) t
217216
);
218217
}
219218

220-
private CompoundBinaryTag readCustomData(CompoundBinaryTag tag, Set<String> tags) {
219+
private CompoundBinaryTag readCustomData(CompoundBinaryTag compoundTag, Set<String> tags) {
221220
if (tags.isEmpty()) {
222221
return CompoundBinaryTag.empty();
223-
} else {
224-
CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder();
222+
}
225223

226-
tags.forEach(tagName -> {
227-
BinaryTag binaryTag = tag.get(tagName);
228-
if (binaryTag == null) {
229-
return;
230-
}
224+
CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder();
231225

232-
builder.put(tagName, binaryTag);
233-
});
226+
tags.forEach(key -> {
227+
BinaryTag tag = compoundTag.get(key);
228+
if (tag == null) {
229+
return;
230+
}
234231

235-
return builder.build();
236-
}
232+
builder.put(key, tag);
233+
});
234+
235+
return builder.build();
237236
}
238237

239238
private <T> T[] readRegionFiles(File regionDir, IntFunction<T[]> generator, Function<CompoundBinaryTag, @Nullable T> function) throws IOException {
@@ -260,8 +259,8 @@ private <T> T[] readRegionFile(File regionFile, IntFunction<T[]> generator, Func
260259

261260
ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
262261

263-
List<T> data = new ArrayList<>(1024);
264-
for (int i = 0; i < 1024; i++) {
262+
List<T> data = new ArrayList<>(SECTION_COUNT);
263+
for (int i = 0; i < SECTION_COUNT; i++) {
265264
int entry;
266265
try {
267266
entry = in.readInt();
@@ -270,22 +269,9 @@ private <T> T[] readRegionFile(File regionFile, IntFunction<T[]> generator, Func
270269
}
271270
if (entry == 0) continue;
272271

273-
int offset = (entry >>> 8) * SECTION_SIZE;
274-
int size = (entry & 0xF) * SECTION_SIZE;
275-
276-
ByteArrayDataInput headerIn = ByteStreams.newDataInput(new ByteArrayInputStream(bytes, offset, size));
277-
int chunkSize = headerIn.readInt() - 1;
278-
int compressionScheme = headerIn.readByte();
279-
280-
InputStream chunkStream = new ByteArrayInputStream(bytes, offset + 5, chunkSize);
281-
InputStream decompressorStream = switch (compressionScheme) {
282-
case 1 -> new GZIPInputStream(chunkStream);
283-
case 2 -> new InflaterInputStream(chunkStream);
284-
case 3 -> chunkStream;
285-
default -> throw new IllegalStateException("Unexpected value: " + compressionScheme);
286-
};
272+
InputStream sectionStream = sectionStream(entry, bytes);
287273

288-
CompoundBinaryTag tag = BinaryTagIO.reader().read(decompressorStream);
274+
CompoundBinaryTag tag = BinaryTagIO.reader().read(sectionStream);
289275
T element = function.apply(tag);
290276
if (element != null) {
291277
data.add(element);
@@ -295,6 +281,23 @@ private <T> T[] readRegionFile(File regionFile, IntFunction<T[]> generator, Func
295281
return data.toArray(generator);
296282
}
297283

284+
private InputStream sectionStream(int entry, byte[] bytes) throws IOException {
285+
int offset = (entry >>> 8) * SECTION_SIZE;
286+
int size = (entry & 0xF) * SECTION_SIZE;
287+
288+
ByteArrayDataInput in = ByteStreams.newDataInput(new ByteArrayInputStream(bytes, offset, size));
289+
int sectionSize = in.readInt() - 1;
290+
int compressionScheme = in.readByte();
291+
292+
InputStream sectionStream = new ByteArrayInputStream(bytes, offset + 5, sectionSize);
293+
return switch (compressionScheme) {
294+
case 1 -> new GZIPInputStream(sectionStream);
295+
case 2 -> new InflaterInputStream(sectionStream);
296+
case 3 -> sectionStream;
297+
default -> throw new IllegalStateException("Unexpected value: " + compressionScheme);
298+
};
299+
}
300+
298301
public static class Builder {
299302
private final SlimeLoader slimeLoader;
300303
private Set<String> preservedWorldTags = Collections.emptySet();

slime-loader/src/main/java/net/roxymc/slime/importer/SlimeImporter.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
import java.io.File;
44
import java.io.IOException;
5-
import java.util.Set;
65

76
public interface SlimeImporter {
8-
Set<String> preservedWorldTags();
9-
10-
Set<String> preservedChunkTags();
11-
127
ImportResult importWorld(File source) throws IOException;
138
}

0 commit comments

Comments
 (0)