use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.
the class BedrockPacketHelper_v291 method readItem.
@Override
public ItemData readItem(ByteBuf buffer, BedrockSession session) {
Preconditions.checkNotNull(buffer, "buffer");
int id = VarInts.readInt(buffer);
if (id == 0) {
// We don't need to read anything extra.
return ItemData.AIR;
}
int aux = VarInts.readInt(buffer);
int damage = (short) (aux >> 8);
if (damage == Short.MAX_VALUE)
damage = -1;
int count = aux & 0xff;
short nbtSize = buffer.readShortLE();
NbtMap compoundTag = null;
if (nbtSize > 0) {
try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
Object tag = reader.readTag();
if (tag instanceof NbtMap) {
compoundTag = (NbtMap) tag;
}
} catch (IOException e) {
throw new IllegalStateException("Unable to load NBT data", e);
}
}
String[] canPlace = readArray(buffer, new String[0], this::readString);
String[] canBreak = readArray(buffer, new String[0], this::readString);
return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).build();
}
use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.
the class BedrockPacketHelper_v431 method readItemInstance.
@Override
public ItemData readItemInstance(ByteBuf buffer, BedrockSession session) {
int id = VarInts.readInt(buffer);
if (id == 0) {
// We don't need to read anything extra.
return ItemData.AIR;
}
int count = buffer.readUnsignedShortLE();
int damage = VarInts.readUnsignedInt(buffer);
int blockRuntimeId = VarInts.readInt(buffer);
NbtMap compoundTag = null;
long blockingTicks = 0;
String[] canPlace;
String[] canBreak;
ByteBuf buf = buffer.readSlice(VarInts.readUnsignedInt(buffer));
try (LittleEndianByteBufInputStream stream = new LittleEndianByteBufInputStream(buf);
NBTInputStream nbtStream = new NBTInputStream(stream)) {
int nbtSize = stream.readShort();
if (nbtSize > 0) {
compoundTag = (NbtMap) nbtStream.readTag();
} else if (nbtSize == -1) {
int tagCount = stream.readUnsignedByte();
if (tagCount != 1)
throw new IllegalArgumentException("Expected 1 tag but got " + tagCount);
compoundTag = (NbtMap) nbtStream.readTag();
}
canPlace = new String[stream.readInt()];
for (int i = 0; i < canPlace.length; i++) {
canPlace[i] = stream.readUTF();
}
canBreak = new String[stream.readInt()];
for (int i = 0; i < canBreak.length; i++) {
canBreak[i] = stream.readUTF();
}
if (this.isBlockingItem(id, session)) {
blockingTicks = stream.readLong();
}
} catch (IOException e) {
throw new IllegalStateException("Unable to read item user data", e);
}
if (buf.isReadable()) {
log.info("Item user data has {} readable bytes left\n{}", buf.readableBytes(), ByteBufUtil.prettyHexDump(buf.readerIndex(0)));
}
return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).blockingTicks(blockingTicks).blockRuntimeId(blockRuntimeId).build();
}
use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.
the class StartGameSerializer_v428 method deserialize.
@Override
public void deserialize(ByteBuf buffer, BedrockPacketHelper helper, StartGamePacket packet, BedrockSession session) {
packet.setUniqueEntityId(VarInts.readLong(buffer));
packet.setRuntimeEntityId(VarInts.readUnsignedLong(buffer));
packet.setPlayerGameType(GameType.from(VarInts.readInt(buffer)));
packet.setPlayerPosition(helper.readVector3f(buffer));
packet.setRotation(helper.readVector2f(buffer));
this.readLevelSettings(buffer, helper, packet);
packet.setLevelId(helper.readString(buffer));
packet.setLevelName(helper.readString(buffer));
packet.setPremiumWorldTemplateId(helper.readString(buffer));
packet.setTrial(buffer.readBoolean());
// new for v428
packet.setPlayerMovementSettings(readSyncedPlayerMovementSettings(buffer, helper));
packet.setCurrentTick(buffer.readLongLE());
packet.setEnchantmentSeed(VarInts.readInt(buffer));
helper.readArray(buffer, packet.getBlockProperties(), (buf, packetHelper) -> {
String name = packetHelper.readString(buf);
NbtMap properties = packetHelper.readTag(buf);
return new BlockPropertyData(name, properties);
});
helper.readArray(buffer, packet.getItemEntries(), session, (buf, packetHelper, aSession) -> {
String identifier = packetHelper.readString(buf);
short id = buf.readShortLE();
boolean componentBased = buf.readBoolean();
if (identifier.equals(packetHelper.getBlockingItemIdentifier())) {
aSession.getHardcodedBlockingId().set(id);
}
return new StartGamePacket.ItemEntry(identifier, id, componentBased);
});
packet.setMultiplayerCorrelationId(helper.readString(buffer));
packet.setInventoriesServerAuthoritative(buffer.readBoolean());
}
use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.
the class ItemComponentSerializer_v419 method deserialize.
@Override
public void deserialize(ByteBuf buffer, BedrockPacketHelper helper, ItemComponentPacket packet) {
helper.readArray(buffer, packet.getItems(), (buf, packetHelper) -> {
String name = packetHelper.readString(buf);
NbtMap data = packetHelper.readTag(buf);
return new ComponentItemData(name, data);
});
}
use of com.nukkitx.nbt.NbtMap 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);
}
}
}
Aggregations