Add initial Biome.Setter/Getter interface

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-11-12 18:35:09 +01:00
parent 9198252dc1
commit 647a9cf375
4 changed files with 42 additions and 3 deletions

View File

@ -12,6 +12,8 @@ import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagHandler;
import net.minestom.server.utils.ViewEngine;
import net.minestom.server.utils.chunk.ChunkSupplier;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -32,7 +34,7 @@ import java.util.UUID;
* You generally want to avoid storing references of this object as this could lead to a huge memory leak,
* you should store the chunk coordinates instead.
*/
public abstract class Chunk implements Block.Getter, Block.Setter, Viewable, Tickable, TagHandler {
public abstract class Chunk implements Block.Getter, Block.Setter, Biome.Getter, Biome.Setter, Viewable, Tickable, TagHandler {
public static final int CHUNK_SIZE_X = 16;
public static final int CHUNK_SIZE_Z = 16;
public static final int CHUNK_SECTION_SIZE = 16;
@ -85,6 +87,10 @@ public abstract class Chunk implements Block.Getter, Block.Setter, Viewable, Tic
public abstract @NotNull Section getSection(int section);
public @NotNull Section getSectionAt(int blockY) {
return getSection(ChunkUtils.getSectionAt(blockY));
}
/**
* Executes a chunk tick.
* <p>

View File

@ -3,6 +3,7 @@ package net.minestom.server.instance;
import com.extollit.gaming.ai.path.model.ColumnarOcclusionFieldList;
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Point;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.pathfinding.PFBlock;
@ -19,6 +20,7 @@ import net.minestom.server.utils.MathUtils;
import net.minestom.server.utils.Utils;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -61,7 +63,7 @@ public class DynamicChunk extends Chunk {
final var blockDescription = PFBlock.get(block);
columnarOcclusionFieldList.onBlockChanged(x, y, z, blockDescription, 0);
}
Section section = getSection(ChunkUtils.getSectionAt(y));
Section section = getSectionAt(y);
section.blockPalette().set(toChunkRelativeCoordinate(x), y, toChunkRelativeCoordinate(z), block.stateId());
final int index = ChunkUtils.getBlockIndex(x, y, z);
@ -80,6 +82,13 @@ public class DynamicChunk extends Chunk {
}
}
@Override
public void setBiome(int x, int y, int z, @NotNull Biome biome) {
this.chunkCache.invalidate();
Section section = getSectionAt(y);
section.biomePalette().set(toChunkRelativeCoordinate(x) / 4, y / 4, toChunkRelativeCoordinate(x) / 4, biome.id());
}
@Override
public @NotNull Section getSection(int section) {
final int index = section - minSection;
@ -119,6 +128,14 @@ public class DynamicChunk extends Chunk {
return Objects.requireNonNullElse(Block.fromStateId((short) blockStateId), Block.AIR);
}
@Override
public @NotNull Biome getBiome(int x, int y, int z) {
final Section section = sections[ChunkUtils.getSectionAt(y) + minSection];
if (section == null) return Biome.PLAINS; // Section is unloaded
final int id = section.biomePalette().get(toChunkRelativeCoordinate(x), y, toChunkRelativeCoordinate(z));
return MinecraftServer.getBiomeManager().getById(id);
}
@Override
public long getLastChangeTime() {
return lastChange;

View File

@ -254,5 +254,4 @@ public sealed interface Block extends ProtocolObject, TagReadable, Blocks permit
TYPE
}
}
}

View File

@ -1,5 +1,6 @@
package net.minestom.server.world.biomes;
import net.minestom.server.coordinate.Point;
import net.minestom.server.utils.NamespaceID;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
@ -195,4 +196,20 @@ public final class Biome {
return new Biome(name, depth, temperature, scale, downfall, category, effects, precipitation, temperatureModifier);
}
}
public interface Setter {
void setBiome(int x, int y, int z, @NotNull Biome biome);
default void setBiome(@NotNull Point blockPosition, @NotNull Biome biome) {
setBiome(blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(), biome);
}
}
public interface Getter {
@NotNull Biome getBiome(int x, int y, int z);
default @NotNull Biome getBiome(@NotNull Point point) {
return getBiome(point.blockX(), point.blockY(), point.blockZ());
}
}
}