Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Add ability for Schematics to load chunks when applying #36

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.batch.BatchOption;
import net.minestom.server.instance.batch.RelativeBlockBatch;
import net.minestom.server.instance.block.Block;
Expand All @@ -10,8 +12,10 @@
import org.jetbrains.annotations.Nullable;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;
import java.util.function.Function;

Expand Down Expand Up @@ -50,6 +54,36 @@ public Point offset(Rotation rotation) {
return rotatePos(offset, rotation);
}

public @NotNull CompletableFuture<Void> applyToInstance(@NotNull Instance instance, @NotNull Point startPoint, boolean loadChunks, @NotNull Rotation rotation, @Nullable Function<Block, Block> blockModifier) {
RelativeBlockBatch batch = build(rotation, blockModifier);
if (loadChunks) {
Point min = startPoint.add(offset());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could also specify the rotation here, by adding a parameter to the function, as offset() can take a rotation parameter.

Point max = min.add(size());
int chunkXStart = min.chunkX();
int chunkXSize = max.chunkX() - min.chunkX() + 1;
int chunkZStart = min.chunkZ();
int chunkZSize = max.chunkZ() - min.chunkZ() + 1;
ArrayList<CompletableFuture<Chunk>> chunksToLoad = new ArrayList<>();
for (int i = chunkXStart; i < chunkXStart + chunkXSize; i++) {
for (int j = chunkZStart; j < chunkZStart + chunkZSize; j++) {
chunksToLoad.add(instance.loadOptionalChunk(i, j));
}
}
return CompletableFuture.allOf(chunksToLoad.toArray(new CompletableFuture[0])).thenRun(() -> batch.apply(instance, startPoint, null)).thenRun(() -> {
for (int i = chunkXStart; i < chunkXStart + chunkXSize; i++) {
for (int j = chunkZStart; j < chunkZStart + chunkZSize; j++) {
Chunk chunk = instance.getChunk(i, j);
if(chunk != null && chunk.isLoaded() && chunk.getViewers().isEmpty()) {
instance.unloadChunk(chunk);
}
}
}
});
} else {
return CompletableFuture.runAsync(() -> batch.apply(instance, startPoint, null));
}
}

/**
* Convert the schematic into a {@link RelativeBlockBatch} which can be applied to an instance.
* The schematic can be rotated around its {@link #offset()} before placement.
Expand Down
39 changes: 39 additions & 0 deletions modules/schem/src/test/java/SchemPasterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import net.hollowcube.util.schem.Rotation;
import net.hollowcube.util.schem.Schematic;
import net.hollowcube.util.schem.SchematicReader;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.event.instance.InstanceChunkLoadEvent;
import net.minestom.server.event.instance.InstanceChunkUnloadEvent;
import net.minestom.server.instance.Instance;
import net.minestom.server.test.Env;
import net.minestom.server.test.EnvTest;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;

@EnvTest
public class SchemPasterTest {

@Test
public void testSchemPaste(Env env) {
AtomicInteger chunksLoaded = new AtomicInteger();
AtomicInteger chunksUnloaded = new AtomicInteger();
Instance instance = env.createFlatInstance();
instance.eventNode().addListener(InstanceChunkLoadEvent.class, event -> chunksLoaded.getAndIncrement());
instance.eventNode().addListener(InstanceChunkUnloadEvent.class, event -> chunksUnloaded.getAndIncrement());
try {
Schematic big = SchematicReader.read(Path.of("src/test/resources/big.schem"));
CompletableFuture<Void> pasteProcess = big.applyToInstance(instance, new Pos(15, 40, 15), true, Rotation.NONE, null);
pasteProcess.join();
} catch (IOException e) {
throw new RuntimeException(e);
}
assertEquals(2, chunksLoaded.get());
assertEquals(chunksLoaded.get(), chunksUnloaded.get());
}
}