Removed the instance from Chunk constructor

This commit is contained in:
themode 2020-11-05 22:37:04 +01:00
parent 46d008b595
commit 0ee8eb7d45
6 changed files with 24 additions and 27 deletions

View File

@ -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 {
* <p>
* 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.

View File

@ -60,8 +60,8 @@ public class DynamicChunk extends Chunk {
// Block entities
protected final Set<Integer> 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);

View File

@ -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.
* <p>
* 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());

View File

@ -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;

View File

@ -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) {

View File

@ -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);
}