|
| 1 | +package net.crystalgames.scaffolding.instance; |
| 2 | + |
| 3 | +import it.unimi.dsi.fastutil.longs.Long2ObjectMap; |
| 4 | +import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; |
| 5 | +import net.crystalgames.scaffolding.schematic.Schematic; |
| 6 | +import net.minestom.server.instance.Chunk; |
| 7 | +import net.minestom.server.instance.DynamicChunk; |
| 8 | +import net.minestom.server.instance.IChunkLoader; |
| 9 | +import net.minestom.server.instance.Instance; |
| 10 | +import net.minestom.server.instance.batch.ChunkBatch; |
| 11 | +import net.minestom.server.instance.block.Block; |
| 12 | +import net.minestom.server.utils.chunk.ChunkUtils; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +// TODO: Entities? |
| 22 | +@SuppressWarnings("UnstableApiUsage") |
| 23 | +public class SchematicChunkLoader implements IChunkLoader { |
| 24 | + |
| 25 | + private final @NotNull Function<@NotNull Chunk, @NotNull CompletableFuture<Void>> saveHandler; |
| 26 | + private final Long2ObjectMap<ChunkBatch> batches = new Long2ObjectOpenHashMap<>(); |
| 27 | + |
| 28 | + private SchematicChunkLoader( |
| 29 | + @NotNull Function<@NotNull Chunk, @NotNull CompletableFuture<Void>> saveHandler, |
| 30 | + @NotNull List<Schematic> schematics, |
| 31 | + int offsetX, |
| 32 | + int offsetY, |
| 33 | + int offsetZ |
| 34 | + ) { |
| 35 | + this.saveHandler = saveHandler; |
| 36 | + |
| 37 | + // The block setter used for Schematic#apply |
| 38 | + Block.Setter setter = (x, y, z, block) -> { |
| 39 | + int chunkX = ChunkUtils.getChunkCoordinate(x); |
| 40 | + int chunkZ = ChunkUtils.getChunkCoordinate(z); |
| 41 | + long index = ChunkUtils.getChunkIndex(chunkX, chunkZ); |
| 42 | + |
| 43 | + // Get the batch, create it if it doesn't exist |
| 44 | + ChunkBatch batch = batches.computeIfAbsent(index, key -> new ChunkBatch()); |
| 45 | + |
| 46 | + // Add the block to the batch |
| 47 | + batch.setBlock(x + offsetX, y + offsetY, z + offsetZ, block); |
| 48 | + }; |
| 49 | + |
| 50 | + // Apply the schematics |
| 51 | + for (Schematic schematic : schematics) { |
| 52 | + schematic.apply(setter); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates a builder for a {@link SchematicChunkLoader}. |
| 58 | + * @return The builder. |
| 59 | + */ |
| 60 | + public static @NotNull Builder builder() { |
| 61 | + return new Builder(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public @NotNull CompletableFuture<@Nullable Chunk> loadChunk(@NotNull Instance instance, int chunkX, int chunkZ) { |
| 66 | + long index = ChunkUtils.getChunkIndex(chunkX, chunkZ); |
| 67 | + ChunkBatch batch = batches.get(index); |
| 68 | + |
| 69 | + if (batch == null) { |
| 70 | + return CompletableFuture.completedFuture(null); |
| 71 | + } |
| 72 | + |
| 73 | + DynamicChunk chunk = new DynamicChunk(instance, chunkX, chunkZ); |
| 74 | + CompletableFuture<Chunk> future = new CompletableFuture<>(); |
| 75 | + batch.apply(instance, chunk, future::complete); |
| 76 | + |
| 77 | + return future; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public @NotNull CompletableFuture<Void> saveChunk(@NotNull Chunk chunk) { |
| 82 | + return saveHandler.apply(chunk); |
| 83 | + } |
| 84 | + |
| 85 | + public static class Builder { |
| 86 | + |
| 87 | + private final List<Schematic> schematics = new ArrayList<>(); |
| 88 | + private @NotNull Function<@NotNull Chunk, @NotNull CompletableFuture<Void>> handler = chunk -> |
| 89 | + CompletableFuture.completedFuture(null); |
| 90 | + private int xOffset; |
| 91 | + private int yOffset; |
| 92 | + private int zOffset; |
| 93 | + |
| 94 | + private Builder() { |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Adds a schematic to this chunk loader. |
| 99 | + * <br><br> |
| 100 | + * Note that schematics are loaded in the order they are added. |
| 101 | + * <br> |
| 102 | + * This means that the last added schematic is the only schematic that is guaranteed to have all its data. |
| 103 | + * @param schematic The schematic to add. |
| 104 | + * @return This builder. |
| 105 | + */ |
| 106 | + // TODO: Add a way to position schematics within the instance. |
| 107 | + public @NotNull Builder addSchematic(@NotNull Schematic schematic) { |
| 108 | + schematics.add(schematic); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Specifies the offset that applies to all schematics added to this chunk loader. |
| 114 | + * @param x The x offset. |
| 115 | + * @param y The y offset. |
| 116 | + * @param z The z offset. |
| 117 | + * @return This builder. |
| 118 | + */ |
| 119 | + public @NotNull Builder offset(int x, int y, int z) { |
| 120 | + this.xOffset = x; |
| 121 | + this.yOffset = y; |
| 122 | + this.zOffset = z; |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Specifies the handler to use to save the chunks. |
| 128 | + * @param handler The handler. |
| 129 | + * @return This builder. |
| 130 | + */ |
| 131 | + public @NotNull Builder saveChunkHandler(@NotNull Function<@NotNull Chunk, @NotNull CompletableFuture<Void>> handler) { |
| 132 | + this.handler = handler; |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + public @NotNull SchematicChunkLoader build() { |
| 137 | + return new SchematicChunkLoader(handler, List.copyOf(schematics), xOffset, yOffset, zOffset); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments