From 0ee8eb7d459c7c9975818e341f64533ee0e3999b Mon Sep 17 00:00:00 2001 From: themode Date: Thu, 5 Nov 2020 22:37:04 +0100 Subject: [PATCH] Removed the instance from Chunk constructor --- .../java/net/minestom/server/instance/Chunk.java | 12 ++++-------- .../minestom/server/instance/DynamicChunk.java | 8 ++++---- .../server/instance/InstanceContainer.java | 6 +++--- .../server/instance/MinestomBasicChunkLoader.java | 2 +- .../net/minestom/server/instance/StaticChunk.java | 8 ++++---- .../server/utils/chunk/ChunkSupplier.java | 15 ++++++++------- 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index dc0816e36..c52170bc5 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -60,8 +60,6 @@ public abstract class Chunk implements Viewable, DataContainer { public static final int BIOME_COUNT = 1024; // 4x4x4 blocks group - @NotNull - protected final Instance instance; @NotNull protected final Biome[] biomes; protected final int chunkX, chunkZ; @@ -84,8 +82,7 @@ public abstract class Chunk implements Viewable, DataContainer { // Data protected Data data; - public Chunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ, boolean shouldGenerate) { - this.instance = instance; + public Chunk(@Nullable Biome[] biomes, int chunkX, int chunkZ, boolean shouldGenerate) { this.chunkX = chunkX; this.chunkZ = chunkZ; this.shouldGenerate = shouldGenerate; @@ -236,13 +233,12 @@ public abstract class Chunk implements Viewable, DataContainer { *

* The instance and chunk position (X/Z) can be modified using the given arguments. * - * @param instance the instance of the new chunk - * @param chunkX the new chunk X - * @param chunkZ the new chunK Z + * @param chunkX the new chunk X + * @param chunkZ the new chunk Z * @return a copy of this chunk with a potentially new instance and position */ @NotNull - public abstract Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ); + public abstract Chunk copy(int chunkX, int chunkZ); /** * Gets the {@link CustomBlock} at a position. diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index f378a3b7f..2bf81c6a9 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -60,8 +60,8 @@ public class DynamicChunk extends Chunk { // Block entities protected final Set blockEntities = new CopyOnWriteArraySet<>(); - public DynamicChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ) { - super(instance, biomes, chunkX, chunkZ, true); + public DynamicChunk(@Nullable Biome[] biomes, int chunkX, int chunkZ) { + super(biomes, chunkX, chunkZ, true); } @Override @@ -407,8 +407,8 @@ public class DynamicChunk extends Chunk { @NotNull @Override - public Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ) { - DynamicChunk dynamicChunk = new DynamicChunk(instance, biomes.clone(), chunkX, chunkZ); + public Chunk copy(int chunkX, int chunkZ) { + DynamicChunk dynamicChunk = new DynamicChunk(biomes.clone(), chunkX, chunkZ); ArrayUtils.copyToDestination(blocksStateId, dynamicChunk.blocksStateId); ArrayUtils.copyToDestination(customBlocksId, dynamicChunk.customBlocksId); dynamicChunk.blocksData.putAll(blocksData); diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 7328e94f0..14bcabf6f 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -533,7 +533,7 @@ public class InstanceContainer extends Instance { chunkGenerator.fillBiomes(biomes, chunkX, chunkZ); } - final Chunk chunk = chunkSupplier.getChunk(this, biomes, chunkX, chunkZ); + final Chunk chunk = chunkSupplier.getChunk(biomes, chunkX, chunkZ); Check.notNull(chunk, "Chunks supplied by a ChunkSupplier cannot be null."); cacheChunk(chunk); @@ -619,7 +619,7 @@ public class InstanceContainer extends Instance { /** * Copies all the chunks of this instance and create a new instance container with all of them. *

- * Chunks are copied with {@link Chunk#copy(Instance, int, int)}, + * Chunks are copied with {@link Chunk#copy(int, int)}, * {@link UUID} is randomized, {@link DimensionType} is passed over and the {@link StorageLocation} is null. * * @return an {@link InstanceContainer} with the exact same chunks as 'this' @@ -635,7 +635,7 @@ public class InstanceContainer extends Instance { final long index = entry.getKey(); final Chunk chunk = entry.getValue(); - final Chunk copiedChunk = chunk.copy(copiedInstance, chunk.getChunkX(), chunk.getChunkZ()); + final Chunk copiedChunk = chunk.copy(chunk.getChunkX(), chunk.getChunkZ()); copiedChunks.put(index, copiedChunk); UPDATE_MANAGER.signalChunkLoad(copiedInstance, chunk.getChunkX(), chunk.getChunkZ()); diff --git a/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java b/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java index f87b33c1c..50ee59561 100644 --- a/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java +++ b/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java @@ -77,7 +77,7 @@ public class MinestomBasicChunkLoader implements IChunkLoader { // Found, load from result bytes BinaryReader reader = new BinaryReader(bytes); // Create the chunk object using the instance's ChunkSupplier to support multiple implementations - Chunk chunk = instanceContainer.getChunkSupplier().getChunk(instance, null, chunkX, chunkZ); + Chunk chunk = instanceContainer.getChunkSupplier().getChunk(null, chunkX, chunkZ); // Execute the callback once all blocks are placed (allow for multithreaded implementations) chunk.readChunk(reader, callback); return true; diff --git a/src/main/java/net/minestom/server/instance/StaticChunk.java b/src/main/java/net/minestom/server/instance/StaticChunk.java index 7fd749d26..7a6fccd43 100644 --- a/src/main/java/net/minestom/server/instance/StaticChunk.java +++ b/src/main/java/net/minestom/server/instance/StaticChunk.java @@ -28,8 +28,8 @@ public class StaticChunk extends Chunk { protected final BlockProvider blockProvider; - public StaticChunk(Instance instance, Biome[] biomes, int chunkX, int chunkZ, BlockProvider blockProvider) { - super(instance, biomes, chunkX, chunkZ, false); + public StaticChunk(Biome[] biomes, int chunkX, int chunkZ, BlockProvider blockProvider) { + super(biomes, chunkX, chunkZ, false); this.blockProvider = blockProvider; setReadOnly(true); } @@ -114,8 +114,8 @@ public class StaticChunk extends Chunk { @NotNull @Override - public Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ) { - StaticChunk staticChunk = new StaticChunk(instance, biomes.clone(), chunkX, chunkZ, blockProvider); + public Chunk copy(int chunkX, int chunkZ) { + StaticChunk staticChunk = new StaticChunk(biomes.clone(), chunkX, chunkZ, blockProvider); // Prevent re-writing the whole packet since it is static anyway final ByteBuf packetBuffer = getFullDataPacket(); if (packetBuffer != null) { diff --git a/src/main/java/net/minestom/server/utils/chunk/ChunkSupplier.java b/src/main/java/net/minestom/server/utils/chunk/ChunkSupplier.java index 78c3ca742..56d11b687 100644 --- a/src/main/java/net/minestom/server/utils/chunk/ChunkSupplier.java +++ b/src/main/java/net/minestom/server/utils/chunk/ChunkSupplier.java @@ -1,8 +1,9 @@ package net.minestom.server.utils.chunk; import net.minestom.server.instance.Chunk; -import net.minestom.server.instance.Instance; import net.minestom.server.world.biomes.Biome; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Used to customize which type of {@link Chunk} an implementation should use. @@ -11,13 +12,13 @@ import net.minestom.server.world.biomes.Biome; public interface ChunkSupplier { /** - * Create a {@link Chunk} object. + * Creates a {@link Chunk} object. * - * @param instance the {@link Instance} assigned to the chunk - * @param biomes the biomes of the chunk, can be null - * @param chunkX the chunk X - * @param chunkZ the chunk Z + * @param biomes the biomes of the chunk, can be null + * @param chunkX the chunk X + * @param chunkZ the chunk Z * @return a newly {@link Chunk} object, cannot be null */ - Chunk getChunk(Instance instance, Biome[] biomes, int chunkX, int chunkZ); + @NotNull + Chunk getChunk(@Nullable Biome[] biomes, int chunkX, int chunkZ); }