From 573e896a1fd1baad6a528fb91373cae6fae7befb Mon Sep 17 00:00:00 2001 From: themode Date: Wed, 22 Dec 2021 01:47:28 +0100 Subject: [PATCH] Add Chunk#getSections --- .../net/minestom/server/instance/Chunk.java | 3 +++ .../server/instance/DynamicChunk.java | 24 ++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index 99c313ed8..4736bd415 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -18,6 +18,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.mutable.MutableNBTCompound; +import java.util.List; import java.util.Set; import java.util.UUID; @@ -85,6 +86,8 @@ public abstract class Chunk implements Block.Getter, Block.Setter, Biome.Getter, @Override public abstract void setBlock(int x, int y, int z, @NotNull Block block); + public abstract @NotNull List
getSections(); + public abstract @NotNull Section getSection(int section); public @NotNull Section getSectionAt(int blockY) { diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index 31a798fa2..19f630209 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -34,7 +34,7 @@ import java.util.*; public class DynamicChunk extends Chunk { private final int minSection, maxSection; - private final Section[] sections; + private List
sections; // Key = ChunkUtils#getBlockIndex protected final Int2ObjectOpenHashMap entries = new Int2ObjectOpenHashMap<>(0); @@ -48,8 +48,9 @@ public class DynamicChunk extends Chunk { super(instance, chunkX, chunkZ, true); this.minSection = instance.getDimensionType().getMinY() / CHUNK_SECTION_SIZE; this.maxSection = (instance.getDimensionType().getMinY() + instance.getDimensionType().getHeight()) / CHUNK_SECTION_SIZE; - this.sections = new Section[maxSection - minSection]; - Arrays.setAll(sections, value -> new Section()); + var sectionsTemp = new Section[maxSection - minSection]; + Arrays.setAll(sectionsTemp, value -> new Section()); + this.sections = List.of(sectionsTemp); } @Override @@ -93,10 +94,14 @@ public class DynamicChunk extends Chunk { toChunkRelativeCoordinate(z) / 4, biome.id()); } + @Override + public @NotNull List
getSections() { + return sections; + } + @Override public @NotNull Section getSection(int section) { - final int index = section - minSection; - return sections[index]; + return sections.get(section - minSection); } @Override @@ -123,7 +128,7 @@ public class DynamicChunk extends Chunk { } } // Retrieve the block from state id - final Section section = sections[ChunkUtils.getChunkCoordinate(y) - minSection]; + final Section section = getSectionAt(y); final int blockStateId = section.blockPalette() .get(toChunkRelativeCoordinate(x), y, toChunkRelativeCoordinate(z)); return Objects.requireNonNullElse(Block.fromStateId((short) blockStateId), Block.AIR); @@ -131,7 +136,7 @@ public class DynamicChunk extends Chunk { @Override public @NotNull Biome getBiome(int x, int y, int z) { - final Section section = sections[ChunkUtils.getChunkCoordinate(y) - minSection]; + final Section section = getSectionAt(y); final int id = section.biomePalette() .get(toChunkRelativeCoordinate(x) / 4, y / 4, toChunkRelativeCoordinate(z) / 4); return MinecraftServer.getBiomeManager().getById(id); @@ -158,10 +163,7 @@ public class DynamicChunk extends Chunk { @Override public @NotNull Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ) { DynamicChunk dynamicChunk = new DynamicChunk(instance, chunkX, chunkZ); - Arrays.setAll(dynamicChunk.sections, value -> { - final Section s = sections[value]; - return s != null ? s.clone() : null; - }); + dynamicChunk.sections = sections.stream().map(Section::clone).toList(); dynamicChunk.entries.putAll(entries); return dynamicChunk; }