Skip to content

Commit f9d1919

Browse files
committed
add an option to optimize chunks in anvil importer
1 parent df81f67 commit f9d1919

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.roxymc.slime.importer.anvil;
2+
3+
import net.kyori.adventure.nbt.ListBinaryTag;
4+
import net.roxymc.slime.world.block.entity.BlockEntity;
5+
import net.roxymc.slime.world.block.state.BlockStates;
6+
import net.roxymc.slime.world.chunk.Section;
7+
import net.roxymc.slime.world.entity.Entity;
8+
9+
import java.util.Arrays;
10+
11+
final class ChunkUtils {
12+
private static final String PALETTE_TAG = "palette";
13+
private static final String NAME_TAG = "Name";
14+
private static final String BLOCK_AIR = "minecraft:air";
15+
16+
private ChunkUtils() {
17+
}
18+
19+
static boolean isEmpty(Section[] sections, BlockEntity[] blockEntities, Entity[] entities) {
20+
if (Arrays.stream(sections).anyMatch(section -> !isOnlyAir(section.blockStates()))) {
21+
return false;
22+
}
23+
24+
return blockEntities.length == 0 && entities.length == 0;
25+
}
26+
27+
static boolean isOnlyAir(BlockStates blockStates) {
28+
ListBinaryTag palette = blockStates.tag().getList(PALETTE_TAG);
29+
if (palette.isEmpty() || palette.size() > 1) {
30+
return palette.isEmpty();
31+
}
32+
33+
return palette.getCompound(0).getString(NAME_TAG).equals(BLOCK_AIR);
34+
}
35+
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ public class SlimeAnvilImporter implements SlimeImporter {
7474
private final SlimeLoader slimeLoader;
7575
private final Set<String> preservedWorldTags;
7676
private final Set<String> preservedChunkTags;
77+
private final boolean optimizeChunks;
7778

7879
private SlimeAnvilImporter(Builder builder) {
7980
this.slimeLoader = builder.slimeLoader;
8081
this.preservedWorldTags = builder.preservedWorldTags;
8182
this.preservedChunkTags = builder.preservedChunkTags;
83+
this.optimizeChunks = builder.optimizeChunks;
8284
}
8385

8486
public static Builder builder(SlimeLoader slimeLoader) {
@@ -191,10 +193,6 @@ private Chunk[] readChunks(File root, Map<ChunkPos, EntityChunk> entityChunks) t
191193
})
192194
.toArray(Section[]::new);
193195

194-
Heightmaps heightmaps = slimeLoader.deserializers().heightmaps().deserialize(
195-
tag.getCompound(HEIGHTMAPS_TAG)
196-
);
197-
198196
BlockEntity[] blockEntities = tag.getList(BLOCK_ENTITIES_TAG, BinaryTagTypes.COMPOUND).stream()
199197
.map(CompoundBinaryTag.class::cast)
200198
.map(slimeLoader.deserializers().blockEntity()::deserialize)
@@ -203,6 +201,14 @@ private Chunk[] readChunks(File root, Map<ChunkPos, EntityChunk> entityChunks) t
203201
ChunkPos chunkPos = new ChunkPos(x, z);
204202
Entity[] entities = entityChunks.containsKey(chunkPos) ? entityChunks.get(chunkPos).entities() : EMPTY_ENTITIES;
205203

204+
if (optimizeChunks && ChunkUtils.isEmpty(sections, blockEntities, entities)) {
205+
return null;
206+
}
207+
208+
Heightmaps heightmaps = slimeLoader.deserializers().heightmaps().deserialize(
209+
tag.getCompound(HEIGHTMAPS_TAG)
210+
);
211+
206212
CompoundBinaryTag customDataTag = readCustomData(tag, preservedChunkTags);
207213

208214
return slimeLoader.deserializers().chunk().deserialize(
@@ -302,6 +308,7 @@ public static class Builder {
302308
private final SlimeLoader slimeLoader;
303309
private Set<String> preservedWorldTags = Collections.emptySet();
304310
private Set<String> preservedChunkTags = Collections.emptySet();
311+
private boolean optimizeChunks;
305312

306313
private Builder(SlimeLoader slimeLoader) {
307314
this.slimeLoader = nonNull(slimeLoader, "slimeLoader");
@@ -317,6 +324,11 @@ public Builder preserveChunkTags(String... tags) {
317324
return this;
318325
}
319326

327+
public Builder optimizeChunks(boolean optimizeChunks) {
328+
this.optimizeChunks = optimizeChunks;
329+
return this;
330+
}
331+
320332
public SlimeAnvilImporter build() {
321333
return new SlimeAnvilImporter(this);
322334
}

0 commit comments

Comments
 (0)