use of com.github.steveice10.mc.protocol.data.game.chunk.palette.Palette in project HBot-Release by hhhzzzsss.
the class CommandCore method checkCore.
private boolean checkCore(ChunkPos pos) {
ChunkColumn chunkColumn = world.getChunk(pos);
if (chunkColumn == null)
return false;
ChunkSection section = chunkColumn.getChunks()[0];
if (section == null)
return false;
BitStorage storage = section.getChunkData().getStorage();
Palette palette = section.getChunkData().getPalette();
for (int i = 0; i < 256 * targetCoreHeight; i++) {
if (!isCommandBlock(palette.idToState(storage.get(i)))) {
return false;
}
}
return true;
}
use of com.github.steveice10.mc.protocol.data.game.chunk.palette.Palette in project Geyser by GeyserMC.
the class JavaLevelChunkWithLightTranslator method translate.
@Override
public void translate(GeyserSession session, ClientboundLevelChunkWithLightPacket packet) {
if (session.isSpawned()) {
ChunkUtils.updateChunkPosition(session, session.getPlayerEntity().getPosition().toInt());
}
// Ensure that, if the player is using lower world heights, the position is not offset
int yOffset = session.getChunkCache().getChunkMinY();
int chunkSize = session.getChunkCache().getChunkHeightY();
int biomeGlobalPalette = session.getBiomeGlobalPalette();
DataPalette[] javaChunks = new DataPalette[chunkSize];
DataPalette[] javaBiomes = new DataPalette[chunkSize];
final BlockEntityInfo[] blockEntities = packet.getBlockEntities();
final List<NbtMap> bedrockBlockEntities = new ObjectArrayList<>(blockEntities.length);
BitSet waterloggedPaletteIds = new BitSet();
BitSet pistonOrFlowerPaletteIds = new BitSet();
boolean overworld = session.getChunkCache().isExtendedHeight();
int maxBedrockSectionY = ((overworld ? MAXIMUM_ACCEPTED_HEIGHT_OVERWORLD : MAXIMUM_ACCEPTED_HEIGHT) >> 4) - 1;
int sectionCount;
byte[] payload;
ByteBuf byteBuf = null;
GeyserChunkSection[] sections = new GeyserChunkSection[javaChunks.length - (yOffset + ((overworld ? MINIMUM_ACCEPTED_HEIGHT_OVERWORLD : MINIMUM_ACCEPTED_HEIGHT) >> 4))];
try {
NetInput in = new StreamNetInput(new ByteArrayInputStream(packet.getChunkData()));
for (int sectionY = 0; sectionY < chunkSize; sectionY++) {
ChunkSection javaSection = ChunkSection.read(in, biomeGlobalPalette);
javaChunks[sectionY] = javaSection.getChunkData();
javaBiomes[sectionY] = javaSection.getBiomeData();
int bedrockSectionY = sectionY + (yOffset - ((overworld ? MINIMUM_ACCEPTED_HEIGHT_OVERWORLD : MINIMUM_ACCEPTED_HEIGHT) >> 4));
if (bedrockSectionY < 0 || maxBedrockSectionY < bedrockSectionY) {
// Ignore this chunk section since it goes outside the bounds accepted by the Bedrock client
continue;
}
// No need to encode an empty section...
if (javaSection.isBlockCountEmpty()) {
continue;
}
Palette javaPalette = javaSection.getChunkData().getPalette();
BitStorage javaData = javaSection.getChunkData().getStorage();
if (javaPalette instanceof GlobalPalette) {
// As this is the global palette, simply iterate through the whole chunk section once
GeyserChunkSection section = new GeyserChunkSection(session.getBlockMappings().getBedrockAirId());
for (int yzx = 0; yzx < BlockStorage.SIZE; yzx++) {
int javaId = javaData.get(yzx);
int bedrockId = session.getBlockMappings().getBedrockBlockId(javaId);
int xzy = indexYZXtoXZY(yzx);
section.getBlockStorageArray()[0].setFullBlock(xzy, bedrockId);
if (BlockRegistries.WATERLOGGED.get().contains(javaId)) {
section.getBlockStorageArray()[1].setFullBlock(xzy, session.getBlockMappings().getBedrockWaterId());
}
// Check if block is piston or flower to see if we'll need to create additional block entities, as they're only block entities in Bedrock
if (BlockStateValues.getFlowerPotValues().containsKey(javaId) || BlockStateValues.getPistonValues().containsKey(javaId)) {
bedrockBlockEntities.add(BedrockOnlyBlockEntity.getTag(session, Vector3i.from((packet.getX() << 4) + (yzx & 0xF), ((sectionY + yOffset) << 4) + ((yzx >> 8) & 0xF), (packet.getZ() << 4) + ((yzx >> 4) & 0xF)), javaId));
}
}
sections[bedrockSectionY] = section;
continue;
}
if (javaPalette instanceof SingletonPalette) {
// There's only one block here. Very easy!
int javaId = javaPalette.idToState(0);
int bedrockId = session.getBlockMappings().getBedrockBlockId(javaId);
BlockStorage blockStorage = new BlockStorage(SingletonBitArray.INSTANCE, IntLists.singleton(bedrockId));
if (BlockRegistries.WATERLOGGED.get().contains(javaId)) {
BlockStorage waterlogged = new BlockStorage(SingletonBitArray.INSTANCE, IntLists.singleton(session.getBlockMappings().getBedrockWaterId()));
sections[bedrockSectionY] = new GeyserChunkSection(new BlockStorage[] { blockStorage, waterlogged });
} else {
sections[bedrockSectionY] = new GeyserChunkSection(new BlockStorage[] { blockStorage });
}
// If a chunk contains all of the same piston or flower pot then god help us
continue;
}
IntList bedrockPalette = new IntArrayList(javaPalette.size());
waterloggedPaletteIds.clear();
pistonOrFlowerPaletteIds.clear();
// Iterate through palette and convert state IDs to Bedrock, doing some additional checks as we go
for (int i = 0; i < javaPalette.size(); i++) {
int javaId = javaPalette.idToState(i);
bedrockPalette.add(session.getBlockMappings().getBedrockBlockId(javaId));
if (BlockRegistries.WATERLOGGED.get().contains(javaId)) {
waterloggedPaletteIds.set(i);
}
// Check if block is piston or flower to see if we'll need to create additional block entities, as they're only block entities in Bedrock
if (BlockStateValues.getFlowerPotValues().containsKey(javaId) || BlockStateValues.getPistonValues().containsKey(javaId)) {
pistonOrFlowerPaletteIds.set(i);
}
}
// for no reason, as most sections will not contain any pistons or flower pots
if (!pistonOrFlowerPaletteIds.isEmpty()) {
for (int yzx = 0; yzx < BlockStorage.SIZE; yzx++) {
int paletteId = javaData.get(yzx);
if (pistonOrFlowerPaletteIds.get(paletteId)) {
bedrockBlockEntities.add(BedrockOnlyBlockEntity.getTag(session, Vector3i.from((packet.getX() << 4) + (yzx & 0xF), ((sectionY + yOffset) << 4) + ((yzx >> 8) & 0xF), (packet.getZ() << 4) + ((yzx >> 4) & 0xF)), javaPalette.idToState(paletteId)));
}
}
}
BitArray bedrockData = BitArrayVersion.forBitsCeil(javaData.getBitsPerEntry()).createArray(BlockStorage.SIZE);
BlockStorage layer0 = new BlockStorage(bedrockData, bedrockPalette);
BlockStorage[] layers;
// Convert data array from YZX to XZY coordinate order
if (waterloggedPaletteIds.isEmpty()) {
// This could probably be optimized further...
for (int yzx = 0; yzx < BlockStorage.SIZE; yzx++) {
bedrockData.set(indexYZXtoXZY(yzx), javaData.get(yzx));
}
layers = new BlockStorage[] { layer0 };
} else {
// The section contains waterlogged blocks, we need to convert coordinate order AND generate a V1 block storage for
// layer 1 with palette ID 1 indicating water
int[] layer1Data = new int[BlockStorage.SIZE >> 5];
for (int yzx = 0; yzx < BlockStorage.SIZE; yzx++) {
int paletteId = javaData.get(yzx);
int xzy = indexYZXtoXZY(yzx);
bedrockData.set(xzy, paletteId);
if (waterloggedPaletteIds.get(paletteId)) {
layer1Data[xzy >> 5] |= 1 << (xzy & 0x1F);
}
}
// V1 palette
IntList layer1Palette = new IntArrayList(2);
// Air - see BlockStorage's constructor for more information
layer1Palette.add(session.getBlockMappings().getBedrockAirId());
layer1Palette.add(session.getBlockMappings().getBedrockWaterId());
layers = new BlockStorage[] { layer0, new BlockStorage(BitArrayVersion.V1.createArray(BlockStorage.SIZE, layer1Data), layer1Palette) };
}
sections[bedrockSectionY] = new GeyserChunkSection(layers);
}
session.getChunkCache().addToCache(packet.getX(), packet.getZ(), javaChunks);
final int chunkBlockX = packet.getX() << 4;
final int chunkBlockZ = packet.getZ() << 4;
for (BlockEntityInfo blockEntity : blockEntities) {
BlockEntityType type = blockEntity.getType();
if (type == null) {
// Vanilla Minecraft gracefully handles this
continue;
}
CompoundTag tag = blockEntity.getNbt();
// Relative to chunk
int x = blockEntity.getX();
int y = blockEntity.getY();
// Relative to chunk
int z = blockEntity.getZ();
// Get the Java block state ID from block entity position
DataPalette section = javaChunks[(y >> 4) - yOffset];
int blockState = section.get(x, y & 0xF, z);
if (type == BlockEntityType.LECTERN && BlockStateValues.getLecternBookStates().get(blockState)) {
// If getLecternBookStates is false, let's just treat it like a normal block entity
bedrockBlockEntities.add(session.getGeyser().getWorldManager().getLecternDataAt(session, x + chunkBlockX, y, z + chunkBlockZ, true));
continue;
}
BlockEntityTranslator blockEntityTranslator = BlockEntityUtils.getBlockEntityTranslator(type);
bedrockBlockEntities.add(blockEntityTranslator.getBlockEntityTag(type, x + chunkBlockX, y, z + chunkBlockZ, tag, blockState));
// Check for custom skulls
if (session.getPreferencesCache().showCustomSkulls() && type == BlockEntityType.SKULL && tag != null && tag.contains("SkullOwner")) {
SkullBlockEntityTranslator.spawnPlayer(session, tag, x + chunkBlockX, y, z + chunkBlockZ, blockState);
}
}
// Find highest section
sectionCount = sections.length - 1;
while (sectionCount >= 0 && sections[sectionCount] == null) {
sectionCount--;
}
sectionCount++;
// Estimate chunk size
int size = 0;
for (int i = 0; i < sectionCount; i++) {
GeyserChunkSection section = sections[i];
if (section != null) {
size += section.estimateNetworkSize();
} else {
size += SERIALIZED_CHUNK_DATA.length;
}
}
// Consists only of biome data
size += ChunkUtils.EMPTY_CHUNK_DATA.length;
// Border blocks
size += 1;
// Extra data length (always 0)
size += 1;
// Conservative estimate of 64 bytes per tile entity
size += bedrockBlockEntities.size() * 64;
// Allocate output buffer
byteBuf = ByteBufAllocator.DEFAULT.buffer(size);
for (int i = 0; i < sectionCount; i++) {
GeyserChunkSection section = sections[i];
if (section != null) {
section.writeToNetwork(byteBuf);
} else {
byteBuf.writeBytes(SERIALIZED_CHUNK_DATA);
}
}
// As of 1.17.10, Bedrock hardcodes to always read 32 biome sections
// As of 1.18, this hardcode was lowered to 25
boolean isNewVersion = session.getUpstream().getProtocolVersion() >= Bedrock_v475.V475_CODEC.getProtocolVersion();
int biomeCount = isNewVersion ? 25 : 32;
int dimensionOffset = (overworld ? MINIMUM_ACCEPTED_HEIGHT_OVERWORLD : MINIMUM_ACCEPTED_HEIGHT) >> 4;
for (int i = 0; i < biomeCount; i++) {
int biomeYOffset = dimensionOffset + i;
if (biomeYOffset < yOffset) {
// Ignore this biome section since it goes below the height of the Java world
byteBuf.writeBytes(ChunkUtils.EMPTY_BIOME_DATA);
continue;
}
if (biomeYOffset >= (chunkSize + yOffset)) {
// This biome section goes above the height of the Java world
if (isNewVersion) {
// A header that says to carry on the biome data from the previous chunk
// This notably fixes biomes in the End
byteBuf.writeByte((127 << 1) | 1);
} else {
byteBuf.writeBytes(ChunkUtils.EMPTY_BIOME_DATA);
}
continue;
}
BiomeTranslator.toNewBedrockBiome(session, javaBiomes[i + (dimensionOffset - yOffset)]).writeToNetwork(byteBuf);
}
// Border blocks - Edu edition only
byteBuf.writeByte(0);
// extra data length, 0 for now
VarInts.writeUnsignedInt(byteBuf, 0);
// Encode tile entities into buffer
NBTOutputStream nbtStream = NbtUtils.createNetworkWriter(new ByteBufOutputStream(byteBuf));
for (NbtMap blockEntity : bedrockBlockEntities) {
nbtStream.writeTag(blockEntity);
}
// Copy data into byte[], because the protocol lib really likes things that are s l o w
byteBuf.readBytes(payload = new byte[byteBuf.readableBytes()]);
} catch (IOException e) {
session.getGeyser().getLogger().error("IO error while encoding chunk", e);
return;
} finally {
if (byteBuf != null) {
// Release buffer to allow buffer pooling to be useful
byteBuf.release();
}
}
LevelChunkPacket levelChunkPacket = new LevelChunkPacket();
levelChunkPacket.setSubChunksLength(sectionCount);
levelChunkPacket.setCachingEnabled(false);
levelChunkPacket.setChunkX(packet.getX());
levelChunkPacket.setChunkZ(packet.getZ());
levelChunkPacket.setData(payload);
session.sendUpstreamPacket(levelChunkPacket);
for (Map.Entry<Vector3i, ItemFrameEntity> entry : session.getItemFrameCache().entrySet()) {
Vector3i position = entry.getKey();
if ((position.getX() >> 4) == packet.getX() && (position.getZ() >> 4) == packet.getZ()) {
// Update this item frame so it doesn't get lost in the abyss
// TODO optimize
entry.getValue().updateBlock(true);
}
}
}
use of com.github.steveice10.mc.protocol.data.game.chunk.palette.Palette in project Geyser by GeyserMC.
the class ChunkCache method updateBlock.
public void updateBlock(int x, int y, int z, int block) {
if (!cache) {
return;
}
GeyserChunk chunk = this.getChunk(x >> 4, z >> 4);
if (chunk == null) {
return;
}
if (y < minY || ((y - minY) >> 4) > chunk.sections().length - 1) {
// Y likely goes above or below the height limit of this world
return;
}
DataPalette palette = chunk.sections()[(y - minY) >> 4];
if (palette == null) {
if (block != BlockStateValues.JAVA_AIR_ID) {
// A previously empty chunk, which is no longer empty as a block has been added to it
palette = DataPalette.createForChunk();
// Fixes the chunk assuming that all blocks is the `block` variable we are updating. /shrug
palette.getPalette().stateToId(BlockStateValues.JAVA_AIR_ID);
chunk.sections()[(y - minY) >> 4] = palette;
} else {
// Nothing to update
return;
}
}
palette.set(x & 0xF, y & 0xF, z & 0xF, block);
}
use of com.github.steveice10.mc.protocol.data.game.chunk.palette.Palette in project ViaVersion by ViaVersion.
the class WorldPackets method register.
public static void register(final Protocol1_18To1_17_1 protocol) {
protocol.registerClientbound(ClientboundPackets1_17_1.BLOCK_ENTITY_DATA, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14);
handler(wrapper -> {
final short id = wrapper.read(Type.UNSIGNED_BYTE);
final int newId = BlockEntityIds.newId(id);
wrapper.write(Type.VAR_INT, newId);
handleSpawners(newId, wrapper.passthrough(Type.NBT));
});
}
});
protocol.registerClientbound(ClientboundPackets1_17_1.UPDATE_LIGHT, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
final int chunkX = wrapper.passthrough(Type.VAR_INT);
final int chunkZ = wrapper.passthrough(Type.VAR_INT);
if (wrapper.user().get(ChunkLightStorage.class).isLoaded(chunkX, chunkZ)) {
if (!Via.getConfig().cache1_17Light()) {
// Light packets updating already sent chunks are the same as before
return;
}
// Pass through and cache light data
} else {
// Cancel and cache the light data
wrapper.cancel();
}
final boolean trustEdges = wrapper.passthrough(Type.BOOLEAN);
final long[] skyLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
final long[] blockLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
final long[] emptySkyLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
final long[] emptyBlockLightMask = wrapper.passthrough(Type.LONG_ARRAY_PRIMITIVE);
final int skyLightLenght = wrapper.passthrough(Type.VAR_INT);
final byte[][] skyLight = new byte[skyLightLenght][];
for (int i = 0; i < skyLightLenght; i++) {
skyLight[i] = wrapper.passthrough(Type.BYTE_ARRAY_PRIMITIVE);
}
final int blockLightLength = wrapper.passthrough(Type.VAR_INT);
final byte[][] blockLight = new byte[blockLightLength][];
for (int i = 0; i < blockLightLength; i++) {
blockLight[i] = wrapper.passthrough(Type.BYTE_ARRAY_PRIMITIVE);
}
final ChunkLightStorage lightStorage = wrapper.user().get(ChunkLightStorage.class);
lightStorage.storeLight(chunkX, chunkZ, new ChunkLightStorage.ChunkLight(trustEdges, skyLightMask, blockLightMask, emptySkyLightMask, emptyBlockLightMask, skyLight, blockLight));
});
}
});
protocol.registerClientbound(ClientboundPackets1_17_1.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
final EntityTracker tracker = protocol.getEntityRewriter().tracker(wrapper.user());
final Chunk oldChunk = wrapper.read(new Chunk1_17Type(tracker.currentWorldSectionHeight()));
final List<BlockEntity> blockEntities = new ArrayList<>(oldChunk.getBlockEntities().size());
for (final CompoundTag tag : oldChunk.getBlockEntities()) {
final NumberTag xTag = tag.get("x");
final NumberTag yTag = tag.get("y");
final NumberTag zTag = tag.get("z");
final StringTag idTag = tag.get("id");
if (xTag == null || yTag == null || zTag == null || idTag == null) {
continue;
}
final String id = idTag.getValue();
final int typeId = protocol.getMappingData().blockEntityIds().getInt(id.replace("minecraft:", ""));
if (typeId == -1) {
Via.getPlatform().getLogger().warning("Unknown block entity: " + id);
}
handleSpawners(typeId, tag);
final byte packedXZ = (byte) ((xTag.asInt() & 15) << 4 | (zTag.asInt() & 15));
blockEntities.add(new BlockEntityImpl(packedXZ, yTag.asShort(), typeId, tag));
}
final int[] biomeData = oldChunk.getBiomeData();
final ChunkSection[] sections = oldChunk.getSections();
for (int i = 0; i < sections.length; i++) {
ChunkSection section = sections[i];
if (section == null) {
// There's no section mask anymore
section = new ChunkSectionImpl();
sections[i] = section;
section.setNonAirBlocksCount(0);
final DataPaletteImpl blockPalette = new DataPaletteImpl(ChunkSection.SIZE);
blockPalette.addId(0);
section.addPalette(PaletteType.BLOCKS, blockPalette);
}
// Fill biome palette
final DataPaletteImpl biomePalette = new DataPaletteImpl(ChunkSection.BIOME_SIZE);
section.addPalette(PaletteType.BIOMES, biomePalette);
final int offset = i * ChunkSection.BIOME_SIZE;
for (int biomeIndex = 0, biomeArrayIndex = offset; biomeIndex < ChunkSection.BIOME_SIZE; biomeIndex++, biomeArrayIndex++) {
// Also catch invalid biomes with id -1
final int biome = biomeData[biomeArrayIndex];
biomePalette.setIdAt(biomeIndex, biome != -1 ? biome : 0);
}
}
final Chunk chunk = new Chunk1_18(oldChunk.getX(), oldChunk.getZ(), sections, oldChunk.getHeightMap(), blockEntities);
wrapper.write(new Chunk1_18Type(tracker.currentWorldSectionHeight(), MathUtil.ceilLog2(protocol.getMappingData().getBlockStateMappings().mappedSize()), MathUtil.ceilLog2(tracker.biomesSent())), chunk);
final ChunkLightStorage lightStorage = wrapper.user().get(ChunkLightStorage.class);
final boolean alreadyLoaded = !lightStorage.addLoadedChunk(chunk.getX(), chunk.getZ());
// Append light data to chunk packet
final ChunkLightStorage.ChunkLight light = Via.getConfig().cache1_17Light() ? lightStorage.getLight(chunk.getX(), chunk.getZ()) : lightStorage.removeLight(chunk.getX(), chunk.getZ());
if (light == null) {
Via.getPlatform().getLogger().warning("No light data found for chunk at " + chunk.getX() + ", " + chunk.getZ() + ". Chunk was already loaded: " + alreadyLoaded);
final BitSet emptyLightMask = new BitSet();
emptyLightMask.set(0, tracker.currentWorldSectionHeight() + 2);
wrapper.write(Type.BOOLEAN, false);
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, new long[0]);
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, new long[0]);
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, emptyLightMask.toLongArray());
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, emptyLightMask.toLongArray());
wrapper.write(Type.VAR_INT, 0);
wrapper.write(Type.VAR_INT, 0);
} else {
wrapper.write(Type.BOOLEAN, light.trustEdges());
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.skyLightMask());
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.blockLightMask());
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.emptySkyLightMask());
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, light.emptyBlockLightMask());
wrapper.write(Type.VAR_INT, light.skyLight().length);
for (final byte[] skyLight : light.skyLight()) {
wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, skyLight);
}
wrapper.write(Type.VAR_INT, light.blockLight().length);
for (final byte[] blockLight : light.blockLight()) {
wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, blockLight);
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_17_1.UNLOAD_CHUNK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
final int chunkX = wrapper.passthrough(Type.INT);
final int chunkZ = wrapper.passthrough(Type.INT);
wrapper.user().get(ChunkLightStorage.class).clear(chunkX, chunkZ);
});
}
});
}
Aggregations