Add Chunk#getSections

This commit is contained in:
themode 2021-12-22 01:47:28 +01:00 committed by TheMode
parent f3221ff412
commit 573e896a1f
2 changed files with 16 additions and 11 deletions

View File

@ -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<Section> getSections();
public abstract @NotNull Section getSection(int section);
public @NotNull Section getSectionAt(int blockY) {

View File

@ -34,7 +34,7 @@ import java.util.*;
public class DynamicChunk extends Chunk {
private final int minSection, maxSection;
private final Section[] sections;
private List<Section> sections;
// Key = ChunkUtils#getBlockIndex
protected final Int2ObjectOpenHashMap<Block> 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<Section> 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;
}