Search in sources :

Example 46 with NbtMap

use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.

the class StartGameSerializer_v291 method serialize.

@Override
public void serialize(ByteBuf buffer, BedrockPacketHelper helper, StartGamePacket packet) {
    VarInts.writeLong(buffer, packet.getUniqueEntityId());
    VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
    VarInts.writeInt(buffer, packet.getPlayerGameType().ordinal());
    helper.writeVector3f(buffer, packet.getPlayerPosition());
    helper.writeVector2f(buffer, packet.getRotation());
    this.writeLevelSettings(buffer, helper, packet);
    helper.writeString(buffer, packet.getLevelId());
    helper.writeString(buffer, packet.getLevelName());
    helper.writeString(buffer, packet.getPremiumWorldTemplateId());
    buffer.writeBoolean(packet.isTrial());
    buffer.writeLongLE(packet.getCurrentTick());
    VarInts.writeInt(buffer, packet.getEnchantmentSeed());
    NbtList<NbtMap> palette = packet.getBlockPalette();
    VarInts.writeUnsignedInt(buffer, palette.size());
    for (NbtMap entry : palette) {
        NbtMap blockTag = entry.getCompound("block");
        helper.writeString(buffer, blockTag.getString("name"));
        buffer.writeShortLE(entry.getShort("meta"));
    }
    helper.writeString(buffer, packet.getMultiplayerCorrelationId());
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap)

Example 47 with NbtMap

use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.

the class BedrockPacketHelper_v431 method readItem.

@Override
public ItemData readItem(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);
    boolean hasNetId = buffer.readBoolean();
    int netId = 0;
    if (hasNetId) {
        netId = VarInts.readInt(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).usingNetId(hasNetId).netId(netId).build();
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NBTInputStream(com.nukkitx.nbt.NBTInputStream) LittleEndianByteBufInputStream(com.nukkitx.protocol.bedrock.util.LittleEndianByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Example 48 with NbtMap

use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.

the class BedrockPacketHelper_v361 method writeEntityData.

@Override
public void writeEntityData(ByteBuf buffer, EntityDataMap entityDataMap) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(entityDataMap, "entityDataDictionary");
    VarInts.writeUnsignedInt(buffer, entityDataMap.size());
    for (Map.Entry<EntityData, Object> entry : entityDataMap.entrySet()) {
        int index = buffer.writerIndex();
        VarInts.writeUnsignedInt(buffer, this.entityData.get(entry.getKey()));
        Object object = entry.getValue();
        EntityData.Type type = EntityData.Type.from(object);
        VarInts.writeUnsignedInt(buffer, this.entityDataTypes.get(type));
        switch(type) {
            case BYTE:
                buffer.writeByte((byte) object);
                break;
            case SHORT:
                buffer.writeShortLE((short) object);
                break;
            case INT:
                VarInts.writeInt(buffer, (int) object);
                break;
            case FLOAT:
                buffer.writeFloatLE((float) object);
                break;
            case STRING:
                writeString(buffer, (String) object);
                break;
            case NBT:
                NbtMap tag;
                if (object instanceof NbtMap) {
                    tag = (NbtMap) object;
                } else {
                    ItemData item = (ItemData) object;
                    tag = item.getTag();
                    if (tag == null) {
                        tag = NbtMap.EMPTY;
                    }
                }
                this.writeTag(buffer, tag);
                break;
            case VECTOR3I:
                writeVector3i(buffer, (Vector3i) object);
                break;
            case FLAGS:
                int flagsIndex = entry.getKey() == EntityData.FLAGS_2 ? 1 : 0;
                object = ((EntityFlags) object).get(flagsIndex, this.entityFlags);
            case LONG:
                VarInts.writeLong(buffer, (long) object);
                break;
            case VECTOR3F:
                writeVector3f(buffer, (Vector3f) object);
                break;
            default:
                buffer.writerIndex(index);
                break;
        }
    }
}
Also used : EntityData(com.nukkitx.protocol.bedrock.data.entity.EntityData) NbtMap(com.nukkitx.nbt.NbtMap) NbtMap(com.nukkitx.nbt.NbtMap) EntityDataMap(com.nukkitx.protocol.bedrock.data.entity.EntityDataMap) Map(java.util.Map) ItemData(com.nukkitx.protocol.bedrock.data.inventory.ItemData)

Example 49 with NbtMap

use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.

the class StartGameSerializer_v419 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());
    packet.setAuthoritativeMovementMode(MOVEMENT_MODES[VarInts.readUnsignedInt(buffer)]);
    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());
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap)

Example 50 with NbtMap

use of com.nukkitx.nbt.NbtMap in project Protocol by CloudburstMC.

the class StartGameSerializer_v361 method serialize.

@Override
public void serialize(ByteBuf buffer, BedrockPacketHelper helper, StartGamePacket packet) {
    VarInts.writeLong(buffer, packet.getUniqueEntityId());
    VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
    VarInts.writeInt(buffer, packet.getPlayerGameType().ordinal());
    helper.writeVector3f(buffer, packet.getPlayerPosition());
    helper.writeVector2f(buffer, packet.getRotation());
    this.writeLevelSettings(buffer, helper, packet);
    helper.writeString(buffer, packet.getLevelId());
    helper.writeString(buffer, packet.getLevelName());
    helper.writeString(buffer, packet.getPremiumWorldTemplateId());
    buffer.writeBoolean(packet.isTrial());
    buffer.writeLongLE(packet.getCurrentTick());
    VarInts.writeInt(buffer, packet.getEnchantmentSeed());
    List<NbtMap> palette = packet.getBlockPalette();
    VarInts.writeUnsignedInt(buffer, palette.size());
    for (NbtMap entry : palette) {
        NbtMap blockTag = entry.getCompound("block");
        helper.writeString(buffer, blockTag.getString("name"));
        buffer.writeShortLE(entry.getShort("meta"));
        buffer.writeShortLE(entry.getShort("id"));
    }
    helper.writeArray(buffer, packet.getItemEntries(), (buf, entry) -> {
        helper.writeString(buf, entry.getIdentifier());
        buf.writeShortLE(entry.getId());
    });
    helper.writeString(buffer, packet.getMultiplayerCorrelationId());
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap)

Aggregations

NbtMap (com.nukkitx.nbt.NbtMap)50 Item (org.jukeboxmc.item.Item)16 NbtMapBuilder (com.nukkitx.nbt.NbtMapBuilder)14 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)11 NBTInputStream (com.nukkitx.nbt.NBTInputStream)9 ItemData (com.nukkitx.protocol.bedrock.data.inventory.ItemData)5 ByteBuf (io.netty.buffer.ByteBuf)5 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Map (java.util.Map)4 BlockEntityShulkerBox (org.jukeboxmc.blockentity.BlockEntityShulkerBox)4 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)3 Vector3i (com.nukkitx.math.vector.Vector3i)3 InputStream (java.io.InputStream)3 ItemAir (org.jukeboxmc.item.ItemAir)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ComponentItemData (com.nukkitx.protocol.bedrock.data.inventory.ComponentItemData)2 BlockEntityDataPacket (com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket)2 UpdateBlockPacket (com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)2