Store handler & nbt inside DynamicChunk

This commit is contained in:
TheMode 2021-05-29 00:07:22 +02:00
parent cb8ad02c0d
commit e0d54f5958
2 changed files with 52 additions and 91 deletions

View File

@ -13,6 +13,7 @@ import net.minestom.server.data.SerializableData;
import net.minestom.server.data.SerializableDataImpl; import net.minestom.server.data.SerializableDataImpl;
import net.minestom.server.entity.pathfinding.PFBlockDescription; import net.minestom.server.entity.pathfinding.PFBlockDescription;
import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.instance.palette.PaletteStorage; import net.minestom.server.instance.palette.PaletteStorage;
import net.minestom.server.network.packet.server.play.ChunkDataPacket; import net.minestom.server.network.packet.server.play.ChunkDataPacket;
import net.minestom.server.utils.binary.BinaryReader; import net.minestom.server.utils.binary.BinaryReader;
@ -24,6 +25,7 @@ import net.minestom.server.utils.validate.Check;
import net.minestom.server.world.biomes.Biome; import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.lang.ref.SoftReference; import java.lang.ref.SoftReference;
import java.util.Set; import java.util.Set;
@ -43,44 +45,37 @@ public class DynamicChunk extends Chunk {
// WARNING: not thread-safe and should not be changed // WARNING: not thread-safe and should not be changed
protected PaletteStorage blockPalette; protected PaletteStorage blockPalette;
protected PaletteStorage customBlockPalette;
// Used to get all blocks with data (no null) // Key = ChunkUtils#getBlockIndex
// Key is still chunk coordinates (see #getBlockIndex) protected final Int2ObjectOpenHashMap<BlockHandler> handlerMap = new Int2ObjectOpenHashMap<>();
protected final Int2ObjectOpenHashMap<Data> blocksData = new Int2ObjectOpenHashMap<>(); protected final Int2ObjectOpenHashMap<NBTCompound> nbtMap = new Int2ObjectOpenHashMap<>();
// Contains CustomBlocks' block index which are updatable // Contains CustomBlocks' block index which are updatable
protected final IntOpenHashSet updatableBlocks = new IntOpenHashSet(); protected final IntOpenHashSet updatableBlocks = new IntOpenHashSet();
// (block index)/(last update in ms) // (block index)/(last update in ms)
protected final Int2LongMap updatableBlocksLastUpdate = new Int2LongOpenHashMap(); protected final Int2LongMap updatableBlocksLastUpdate = new Int2LongOpenHashMap();
// Block entities
protected final IntOpenHashSet blockEntities = new IntOpenHashSet();
private long lastChangeTime; private long lastChangeTime;
private SoftReference<ChunkDataPacket> cachedPacket = new SoftReference<>(null); private SoftReference<ChunkDataPacket> cachedPacket = new SoftReference<>(null);
private long cachedPacketTime; private long cachedPacketTime;
public DynamicChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ, public DynamicChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ,
@NotNull PaletteStorage blockPalette, @NotNull PaletteStorage customBlockPalette) { @NotNull PaletteStorage blockPalette) {
super(instance, biomes, chunkX, chunkZ, true); super(instance, biomes, chunkX, chunkZ, true);
this.blockPalette = blockPalette; this.blockPalette = blockPalette;
this.customBlockPalette = customBlockPalette;
} }
public DynamicChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ) { public DynamicChunk(@NotNull Instance instance, @Nullable Biome[] biomes, int chunkX, int chunkZ) {
this(instance, biomes, chunkX, chunkZ, this(instance, biomes, chunkX, chunkZ, new PaletteStorage(8, 2));
new PaletteStorage(8, 2),
new PaletteStorage(8, 2));
} }
@Override @Override
public void UNSAFE_setBlock(int x, int y, int z, @NotNull Block block) { public void UNSAFE_setBlock(int x, int y, int z, @NotNull Block block) {
final short blockStateId = block.getStateId(); final short blockStateId = block.getStateId();
final short customBlockId = 0; // TODO final BlockHandler handler = block.getHandler();
final NBTCompound nbt = null; // TODO
final boolean updatable = false; // TODO final boolean updatable = false; // TODO
final boolean nbt = false; // TODO
{ {
// Update pathfinder // Update pathfinder
if (columnarSpace != null) { if (columnarSpace != null) {
@ -91,45 +86,19 @@ public class DynamicChunk extends Chunk {
} }
final int index = getBlockIndex(x, y, z); final int index = getBlockIndex(x, y, z);
// True if the block is not complete air without any custom block capabilities // Block palette
final boolean hasBlock = blockStateId != 0 || customBlockId != 0;
setBlockAt(blockPalette, x, y, z, blockStateId); setBlockAt(blockPalette, x, y, z, blockStateId);
setBlockAt(customBlockPalette, x, y, z, customBlockId); // Handler
if (handler != null) {
if (!hasBlock) { this.handlerMap.put(index, handler);
// Block has been deleted, clear cache and return
this.blocksData.remove(index);
this.updatableBlocks.remove(index);
this.updatableBlocksLastUpdate.remove(index);
this.blockEntities.remove(index);
return;
}
// Set the new data (or remove from the map if is null)
if (data != null) {
this.blocksData.put(index, data);
} else { } else {
this.blocksData.remove(index); this.handlerMap.remove(index);
} }
// Nbt
// Set update consumer if (nbt != null) {
if (updatable) { this.nbtMap.put(index, nbt);
this.updatableBlocks.add(index);
this.updatableBlocksLastUpdate.put(index, System.currentTimeMillis());
} else { } else {
this.updatableBlocks.remove(index); this.nbtMap.remove(index);
this.updatableBlocksLastUpdate.remove(index);
}
// Set block entity
if (nbt) {
this.blockEntities.add(index);
} else {
this.blockEntities.remove(index);
} }
} }
@ -149,7 +118,7 @@ public class DynamicChunk extends Chunk {
@NotNull @NotNull
@Override @Override
public Set<Integer> getBlockEntities() { public Set<Integer> getBlockEntities() {
return blockEntities; return nbtMap.keySet();
} }
@Override @Override
@ -205,7 +174,7 @@ public class DynamicChunk extends Chunk {
final int index = getBlockIndex(x, y, z); final int index = getBlockIndex(x, y, z);
final short blockStateId = getBlockAt(blockPalette, x, y, z); final short blockStateId = getBlockAt(blockPalette, x, y, z);
final short customBlockId = getBlockAt(customBlockPalette, x, y, z); final short customBlockId = 0;//getBlockAt(customBlockPalette, x, y, z);
// No block at the position // No block at the position
if (blockStateId == 0 && customBlockId == 0) if (blockStateId == 0 && customBlockId == 0)
@ -350,9 +319,8 @@ public class DynamicChunk extends Chunk {
packet.chunkX = chunkX; packet.chunkX = chunkX;
packet.chunkZ = chunkZ; packet.chunkZ = chunkZ;
packet.paletteStorage = blockPalette.clone(); packet.paletteStorage = blockPalette.clone();
packet.customBlockPaletteStorage = customBlockPalette.clone(); packet.handlerMap = handlerMap.clone();
packet.blockEntities = blockEntities.clone(); packet.nbtMap = nbtMap.clone();
packet.blocksData = blocksData.clone();
this.cachedPacketTime = getLastChangeTime(); this.cachedPacketTime = getLastChangeTime();
this.cachedPacket = new SoftReference<>(packet); this.cachedPacket = new SoftReference<>(packet);
@ -364,11 +332,10 @@ public class DynamicChunk extends Chunk {
public Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ) { public Chunk copy(@NotNull Instance instance, int chunkX, int chunkZ) {
DynamicChunk dynamicChunk = new DynamicChunk(instance, biomes.clone(), chunkX, chunkZ); DynamicChunk dynamicChunk = new DynamicChunk(instance, biomes.clone(), chunkX, chunkZ);
dynamicChunk.blockPalette = blockPalette.clone(); dynamicChunk.blockPalette = blockPalette.clone();
dynamicChunk.customBlockPalette = customBlockPalette.clone(); dynamicChunk.handlerMap.putAll(handlerMap);
dynamicChunk.blocksData.putAll(blocksData);
dynamicChunk.updatableBlocks.addAll(updatableBlocks); dynamicChunk.updatableBlocks.addAll(updatableBlocks);
dynamicChunk.updatableBlocksLastUpdate.putAll(updatableBlocksLastUpdate); dynamicChunk.updatableBlocksLastUpdate.putAll(updatableBlocksLastUpdate);
dynamicChunk.blockEntities.addAll(blockEntities); dynamicChunk.nbtMap.putAll(nbtMap);
return dynamicChunk; return dynamicChunk;
} }
@ -376,12 +343,10 @@ public class DynamicChunk extends Chunk {
@Override @Override
public void reset() { public void reset() {
this.blockPalette.clear(); this.blockPalette.clear();
this.customBlockPalette.clear(); this.handlerMap.clear();
this.nbtMap.clear();
this.blocksData.clear();
this.updatableBlocks.clear(); this.updatableBlocks.clear();
this.updatableBlocksLastUpdate.clear(); this.updatableBlocksLastUpdate.clear();
this.blockEntities.clear();
} }
private short getBlockAt(@NotNull PaletteStorage paletteStorage, int x, int y, int z) { private short getBlockAt(@NotNull PaletteStorage paletteStorage, int x, int y, int z) {

View File

@ -3,10 +3,9 @@ package net.minestom.server.network.packet.server.play;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntSet;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.data.Data; import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.instance.block.BlockManager; import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.palette.PaletteStorage; import net.minestom.server.instance.palette.PaletteStorage;
import net.minestom.server.instance.palette.Section; import net.minestom.server.instance.palette.Section;
@ -26,6 +25,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTException; import org.jglrxavpok.hephaistos.nbt.NBTException;
import java.io.IOException; import java.io.IOException;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -39,10 +39,8 @@ public class ChunkDataPacket implements ServerPacket, CacheablePacket {
public int chunkX, chunkZ; public int chunkX, chunkZ;
public PaletteStorage paletteStorage; public PaletteStorage paletteStorage;
public PaletteStorage customBlockPaletteStorage; public Int2ObjectMap<BlockHandler> handlerMap;
public Int2ObjectMap<NBTCompound> nbtMap;
public IntSet blockEntities;
public Int2ObjectMap<Data> blocksData;
public int[] sections = new int[0]; public int[] sections = new int[0];
@ -54,11 +52,6 @@ public class ChunkDataPacket implements ServerPacket, CacheablePacket {
private final UUID identifier; private final UUID identifier;
private final long timestamp; private final long timestamp;
/**
* Block entities NBT, as read from raw packet data.
* Only filled by #read, and unused at the moment.
*/
public NBTCompound[] blockEntitiesNBT = new NBTCompound[0];
/** /**
* Heightmaps NBT, as read from raw packet data. * Heightmaps NBT, as read from raw packet data.
* Only filled by #read, and unused at the moment. * Only filled by #read, and unused at the moment.
@ -131,28 +124,26 @@ public class ChunkDataPacket implements ServerPacket, CacheablePacket {
blocks.release(); blocks.release();
// Block entities // Block entities
if (blockEntities == null) { if (handlerMap == null || handlerMap.isEmpty()) {
writer.writeVarInt(0); writer.writeVarInt(0);
} else { } else {
writer.writeVarInt(blockEntities.size()); writer.writeVarInt(handlerMap.size());
for (int index : blockEntities) { for (var entry : handlerMap.int2ObjectEntrySet()) {
final int index = entry.getIntKey();
final BlockHandler handler = entry.getValue();
final BlockPosition blockPosition = ChunkUtils.getBlockPosition(index, chunkX, chunkZ); final BlockPosition blockPosition = ChunkUtils.getBlockPosition(index, chunkX, chunkZ);
NBTCompound nbt = new NBTCompound() NBTCompound nbt;
if (nbtMap != null) {
nbt = Objects.requireNonNullElseGet(nbtMap.get(index), NBTCompound::new);
} else {
nbt = new NBTCompound();
}
nbt.setString("id", handler.getNamespaceId().asString())
.setInt("x", blockPosition.getX()) .setInt("x", blockPosition.getX())
.setInt("y", blockPosition.getY()) .setInt("y", blockPosition.getY())
.setInt("z", blockPosition.getZ()); .setInt("z", blockPosition.getZ());
// TODO: Handle custom blocks
// if (customBlockPaletteStorage != null) {
// final short customBlockId = customBlockPaletteStorage.getBlockAt(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
// final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
// if (customBlock != null) {
// final Data data = blocksData.get(index);
// customBlock.writeBlockEntity(blockPosition, data, nbt);
// }
// }
writer.writeNBT("", nbt); writer.writeNBT("", nbt);
} }
} }
@ -213,12 +204,17 @@ public class ChunkDataPacket implements ServerPacket, CacheablePacket {
} }
// Block entities // Block entities
int blockEntityCount = reader.readVarInt(); final int blockEntityCount = reader.readVarInt();
blockEntities = new IntOpenHashSet(); handlerMap = new Int2ObjectOpenHashMap<>();
blockEntitiesNBT = new NBTCompound[blockEntityCount]; nbtMap = new Int2ObjectOpenHashMap<>();
for (int i = 0; i < blockEntityCount; i++) { for (int i = 0; i < blockEntityCount; i++) {
NBTCompound tag = (NBTCompound) reader.readTag(); NBTCompound tag = (NBTCompound) reader.readTag();
blockEntitiesNBT[i] = tag; final String id = tag.getString("id");
// TODO retrieve handler by namespace
final int x = tag.getInt("x");
final int y = tag.getInt("y");
final int z = tag.getInt("z");
// TODO add to handlerMap & nbtMap
} }
} catch (IOException | NBTException e) { } catch (IOException | NBTException e) {
MinecraftServer.getExceptionManager().handleException(e); MinecraftServer.getExceptionManager().handleException(e);