mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-18 06:02:47 +01:00
Improve chunk packet reading
This commit is contained in:
parent
8c6778983b
commit
1c59186b61
@ -164,11 +164,7 @@ public class AnvilLoader implements IChunkLoader {
|
||||
|
||||
final String tileEntityID = te.getString("id");
|
||||
if (tileEntityID != null) {
|
||||
var handler = BLOCK_MANAGER.getHandler(tileEntityID);
|
||||
if (handler == null) {
|
||||
LOGGER.warn("Block {} does not have any corresponding handler, default to dummy.", tileEntityID);
|
||||
handler = BlockHandler.Dummy.get(tileEntityID);
|
||||
}
|
||||
final BlockHandler handler = BLOCK_MANAGER.getHandlerOrDummy(tileEntityID);
|
||||
block = block.withHandler(handler);
|
||||
}
|
||||
// Remove anvil tags
|
||||
|
@ -4,14 +4,18 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
import net.minestom.server.instance.block.rule.BlockPlacementRule;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class BlockManager {
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(BlockManager.class);
|
||||
|
||||
// Namespace -> handler supplier
|
||||
private final Map<String, Supplier<BlockHandler>> blockHandlerMap = new ConcurrentHashMap<>();
|
||||
@ -28,6 +32,16 @@ public class BlockManager {
|
||||
return handler != null ? handler.get() : null;
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
public synchronized @Nullable BlockHandler getHandlerOrDummy(@NotNull String namespace) {
|
||||
BlockHandler handler = getHandler(namespace);
|
||||
if (handler == null) {
|
||||
LOGGER.warn("Block {} does not have any corresponding handler, default to dummy.", namespace);
|
||||
handler = BlockHandler.Dummy.get(namespace);
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a {@link BlockPlacementRule}.
|
||||
*
|
||||
|
@ -192,6 +192,10 @@ public final class Palette implements PublicCloneable<Palette> {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
public void setBlocks(long[] blocks) {
|
||||
this.blocks = blocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the amount of non air blocks in this section.
|
||||
*
|
||||
@ -201,6 +205,10 @@ public final class Palette implements PublicCloneable<Palette> {
|
||||
return blockCount;
|
||||
}
|
||||
|
||||
public void setBlockCount(short blockCount) {
|
||||
this.blockCount = blockCount;
|
||||
}
|
||||
|
||||
public Short2ShortLinkedOpenHashMap getPaletteBlockMap() {
|
||||
return paletteBlockMap;
|
||||
}
|
||||
|
@ -147,8 +147,8 @@ public class ChunkDataPacket implements ServerPacket {
|
||||
|
||||
@Override
|
||||
public void read(@NotNull BinaryReader reader) {
|
||||
chunkX = reader.readInt();
|
||||
chunkZ = reader.readInt();
|
||||
this.chunkX = reader.readInt();
|
||||
this.chunkZ = reader.readInt();
|
||||
|
||||
int maskCount = reader.readVarInt();
|
||||
long[] masks = new long[maskCount];
|
||||
@ -158,7 +158,7 @@ public class ChunkDataPacket implements ServerPacket {
|
||||
try {
|
||||
// TODO: Use heightmaps
|
||||
// unused at the moment
|
||||
heightmapsNBT = (NBTCompound) reader.readTag();
|
||||
this.heightmapsNBT = (NBTCompound) reader.readTag();
|
||||
|
||||
// Biomes
|
||||
int[] biomesIds = reader.readVarIntArray();
|
||||
@ -168,23 +168,22 @@ public class ChunkDataPacket implements ServerPacket {
|
||||
}
|
||||
|
||||
// Data
|
||||
this.sections = new HashMap<>();
|
||||
int blockArrayLength = reader.readVarInt();
|
||||
if (maskCount > 0) {
|
||||
final long mask = masks[0]; // TODO support for variable size
|
||||
for (int sectionIndex = 0; sectionIndex < CHUNK_SECTION_COUNT; sectionIndex++) {
|
||||
boolean hasSection = (mask & 1 << sectionIndex) != 0;
|
||||
if (!hasSection)
|
||||
continue;
|
||||
final boolean hasSection = (mask & 1 << sectionIndex) != 0;
|
||||
if (!hasSection) continue;
|
||||
final Section section = sections.computeIfAbsent(sectionIndex, i -> new Section());
|
||||
final Palette palette = section.getPalette();
|
||||
short blockCount = reader.readShort();
|
||||
byte bitsPerEntry = reader.readByte();
|
||||
|
||||
final short blockCount = reader.readShort();
|
||||
palette.setBlockCount(blockCount);
|
||||
final byte bitsPerEntry = reader.readByte();
|
||||
// Resize palette if necessary
|
||||
if (bitsPerEntry > palette.getBitsPerEntry()) {
|
||||
palette.resize(bitsPerEntry);
|
||||
}
|
||||
|
||||
// Retrieve palette values
|
||||
if (bitsPerEntry < 9) {
|
||||
int paletteSize = reader.readVarInt();
|
||||
@ -194,24 +193,18 @@ public class ChunkDataPacket implements ServerPacket {
|
||||
palette.getBlockPaletteMap().put((short) paletteValue, (short) i);
|
||||
}
|
||||
}
|
||||
|
||||
// Read blocks
|
||||
int dataLength = reader.readVarInt();
|
||||
long[] data = palette.getBlocks();
|
||||
for (int i = 0; i < dataLength; i++) {
|
||||
data[i] = reader.readLong();
|
||||
}
|
||||
palette.setBlocks(reader.readLongArray());
|
||||
}
|
||||
}
|
||||
|
||||
// Block entities
|
||||
final int blockEntityCount = reader.readVarInt();
|
||||
|
||||
entries = new Int2ObjectOpenHashMap<>();
|
||||
this.entries = new Int2ObjectOpenHashMap<>(blockEntityCount);
|
||||
for (int i = 0; i < blockEntityCount; i++) {
|
||||
NBTCompound tag = (NBTCompound) reader.readTag();
|
||||
final String id = tag.getString("id");
|
||||
// TODO retrieve handler by namespace
|
||||
final BlockHandler handler = MinecraftServer.getBlockManager().getHandlerOrDummy(id);
|
||||
final int x = tag.getInt("x");
|
||||
final int y = tag.getInt("y");
|
||||
final int z = tag.getInt("z");
|
||||
|
Loading…
Reference in New Issue
Block a user