Search in sources :

Example 1 with NbtMap

use of com.nukkitx.nbt.NbtMap in project Geyser by GeyserMC.

the class FlowerPotBlockEntityTranslator method getTag.

/**
 * Get the Nukkit CompoundTag of the flower pot.
 *
 * @param blockState Java block state of flower pot.
 * @param position   Bedrock position of flower pot.
 * @return Bedrock tag of flower pot.
 */
public static NbtMap getTag(GeyserSession session, int blockState, Vector3i position) {
    NbtMapBuilder tagBuilder = NbtMap.builder().putInt("x", position.getX()).putInt("y", position.getY()).putInt("z", position.getZ()).putByte("isMovable", (byte) 1).putString("id", "FlowerPot");
    // Get the Java name of the plant inside. e.g. minecraft:oak_sapling
    String name = BlockStateValues.getFlowerPotValues().get(blockState);
    if (name != null) {
        // Get the Bedrock CompoundTag of the block.
        // This is where we need to store the *Java* name because Bedrock has six minecraft:sapling blocks with different block states.
        NbtMap plant = session.getBlockMappings().getFlowerPotBlocks().get(name);
        if (plant != null) {
            tagBuilder.put("PlantBlock", plant.toBuilder().build());
        }
    }
    return tagBuilder.build();
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder)

Example 2 with NbtMap

use of com.nukkitx.nbt.NbtMap in project Geyser by GeyserMC.

the class JavaHorseScreenOpenTranslator method translate.

@Override
public void translate(GeyserSession session, ClientboundHorseScreenOpenPacket packet) {
    Entity entity = session.getEntityCache().getEntityByJavaId(packet.getEntityId());
    if (entity == null) {
        return;
    }
    UpdateEquipPacket updateEquipPacket = new UpdateEquipPacket();
    updateEquipPacket.setWindowId((short) packet.getContainerId());
    updateEquipPacket.setWindowType((short) ContainerType.HORSE.getId());
    updateEquipPacket.setUniqueEntityId(entity.getGeyserId());
    NbtMapBuilder builder = NbtMap.builder();
    List<NbtMap> slots = new ArrayList<>();
    InventoryTranslator inventoryTranslator;
    if (entity instanceof LlamaEntity) {
        inventoryTranslator = new LlamaInventoryTranslator(packet.getNumberOfSlots());
        slots.add(CARPET_SLOT);
    } else if (entity instanceof ChestedHorseEntity) {
        inventoryTranslator = new DonkeyInventoryTranslator(packet.getNumberOfSlots());
        slots.add(SADDLE_SLOT);
    } else {
        inventoryTranslator = new HorseInventoryTranslator(packet.getNumberOfSlots());
        slots.add(SADDLE_SLOT);
        slots.add(ARMOR_SLOT);
    }
    // Build the NbtMap that sets the icons for Bedrock (e.g. sets the saddle outline on the saddle slot)
    builder.putList("slots", NbtType.COMPOUND, slots);
    updateEquipPacket.setTag(builder.build());
    session.sendUpstreamPacket(updateEquipPacket);
    session.setInventoryTranslator(inventoryTranslator);
    InventoryUtils.openInventory(session, new Container(entity.getNametag(), packet.getContainerId(), packet.getNumberOfSlots(), null, session.getPlayerInventory()));
}
Also used : LlamaEntity(org.geysermc.geyser.entity.type.living.animal.horse.LlamaEntity) Entity(org.geysermc.geyser.entity.type.Entity) ChestedHorseEntity(org.geysermc.geyser.entity.type.living.animal.horse.ChestedHorseEntity) HorseInventoryTranslator(org.geysermc.geyser.translator.inventory.horse.HorseInventoryTranslator) InventoryTranslator(org.geysermc.geyser.translator.inventory.InventoryTranslator) DonkeyInventoryTranslator(org.geysermc.geyser.translator.inventory.horse.DonkeyInventoryTranslator) LlamaInventoryTranslator(org.geysermc.geyser.translator.inventory.horse.LlamaInventoryTranslator) ChestedHorseEntity(org.geysermc.geyser.entity.type.living.animal.horse.ChestedHorseEntity) NbtMap(com.nukkitx.nbt.NbtMap) ArrayList(java.util.ArrayList) NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder) UpdateEquipPacket(com.nukkitx.protocol.bedrock.packet.UpdateEquipPacket) Container(org.geysermc.geyser.inventory.Container) LlamaEntity(org.geysermc.geyser.entity.type.living.animal.horse.LlamaEntity) LlamaInventoryTranslator(org.geysermc.geyser.translator.inventory.horse.LlamaInventoryTranslator) DonkeyInventoryTranslator(org.geysermc.geyser.translator.inventory.horse.DonkeyInventoryTranslator) HorseInventoryTranslator(org.geysermc.geyser.translator.inventory.horse.HorseInventoryTranslator)

Example 3 with NbtMap

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

the class BedrockPacketHelper_v332 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 aux = VarInts.readInt(buffer);
    int damage = (short) (aux >> 8);
    if (damage == Short.MAX_VALUE)
        damage = -1;
    int count = aux & 0xff;
    int nbtSize = buffer.readShortLE();
    NbtMap compoundTag = null;
    if (nbtSize > 0) {
        try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    } else if (nbtSize == -1) {
        int tagCount = buffer.readUnsignedByte();
        if (tagCount != 1)
            throw new IllegalArgumentException("Expected 1 tag but got " + tagCount);
        try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    }
    String[] canPlace = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canPlace.length; i++) {
        canPlace[i] = this.readString(buffer);
    }
    String[] canBreak = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canBreak.length; i++) {
        canBreak[i] = this.readString(buffer);
    }
    return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).build();
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NBTInputStream(com.nukkitx.nbt.NBTInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 4 with NbtMap

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

the class BedrockPacketHelper_v340 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 aux = VarInts.readInt(buffer);
    int damage = (short) (aux >> 8);
    if (damage == Short.MAX_VALUE)
        damage = -1;
    int count = aux & 0xff;
    int nbtSize = buffer.readShortLE();
    NbtMap compoundTag = null;
    if (nbtSize > 0) {
        try (NBTInputStream reader = NbtUtils.createReaderLE(new ByteBufInputStream(buffer.readSlice(nbtSize)))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    } else if (nbtSize == -1) {
        int tagCount = buffer.readUnsignedByte();
        if (tagCount != 1)
            throw new IllegalArgumentException("Expected 1 tag but got " + tagCount);
        try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
            compoundTag = (NbtMap) reader.readTag();
        } catch (IOException e) {
            throw new IllegalStateException("Unable to load NBT data", e);
        }
    }
    String[] canPlace = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canPlace.length; i++) {
        canPlace[i] = this.readString(buffer);
    }
    String[] canBreak = new String[VarInts.readInt(buffer)];
    for (int i = 0; i < canBreak.length; i++) {
        canBreak[i] = this.readString(buffer);
    }
    long blockingTicks = 0;
    if (this.isBlockingItem(id, session)) {
        blockingTicks = VarInts.readLong(buffer);
    }
    return ItemData.builder().id(id).damage(damage).count(count).tag(compoundTag).canPlace(canPlace).canBreak(canBreak).blockingTicks(blockingTicks).build();
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NBTInputStream(com.nukkitx.nbt.NBTInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 5 with NbtMap

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

the class BedrockPacketHelper_v291 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:
                ItemData item;
                if (object instanceof NbtMap) {
                    item = ItemData.builder().id(1).damage(0).count(1).tag((NbtMap) object).build();
                } else {
                    item = (ItemData) object;
                }
                this.writeItem(buffer, item, null);
                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 : Type(com.nukkitx.protocol.bedrock.data.entity.EntityData.Type) NbtMap(com.nukkitx.nbt.NbtMap) NbtMap(com.nukkitx.nbt.NbtMap) Map(java.util.Map) ItemData(com.nukkitx.protocol.bedrock.data.inventory.ItemData)

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