Search in sources :

Example 21 with NbtMapBuilder

use of com.nukkitx.nbt.NbtMapBuilder in project JukeboxMC by LucGamesYT.

the class BlockEntityBed method toCompound.

@Override
public NbtMapBuilder toCompound() {
    NbtMapBuilder compound = super.toCompound();
    compound.putByte("color", this.color);
    return compound;
}
Also used : NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder)

Example 22 with NbtMapBuilder

use of com.nukkitx.nbt.NbtMapBuilder in project JukeboxMC by LucGamesYT.

the class BlockEntityChest method toCompound.

@Override
public NbtMapBuilder toCompound() {
    NbtMapBuilder builder = super.toCompound();
    List<NbtMap> itemsCompoundList = new ArrayList<>();
    for (int slot = 0; slot < this.chestInventory.getSize(); slot++) {
        NbtMapBuilder itemCompound = NbtMap.builder();
        Item item = this.chestInventory.getItem(slot);
        itemCompound.putByte("Slot", (byte) slot);
        this.fromItem(item, itemCompound);
        itemsCompoundList.add(itemCompound.build());
    }
    builder.putList("Items", NbtType.COMPOUND, itemsCompoundList);
    builder.putInt("pairx", this.pairX);
    builder.putInt("pairz", this.pairZ);
    builder.putBoolean("Findable", this.findable);
    return builder;
}
Also used : Item(org.jukeboxmc.item.Item) NbtMap(com.nukkitx.nbt.NbtMap) ArrayList(java.util.ArrayList) NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder)

Example 23 with NbtMapBuilder

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

the class CampfireBlockEntityTranslator method getItem.

protected NbtMap getItem(CompoundTag tag) {
    // TODO: Version independent mappings
    ItemMapping mapping = Registries.ITEMS.forVersion(MinecraftProtocol.DEFAULT_BEDROCK_CODEC.getProtocolVersion()).getMapping((String) tag.get("id").getValue());
    NbtMapBuilder tagBuilder = NbtMap.builder().putString("Name", mapping.getBedrockIdentifier()).putByte("Count", (byte) tag.get("Count").getValue()).putShort("Damage", (short) mapping.getBedrockData());
    tagBuilder.put("tag", NbtMap.builder().build());
    return tagBuilder.build();
}
Also used : ItemMapping(org.geysermc.geyser.registry.type.ItemMapping) NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder)

Example 24 with NbtMapBuilder

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

the class DoubleChestBlockEntityTranslator method updateBlock.

@Override
public void updateBlock(GeyserSession session, int blockState, Vector3i position) {
    NbtMapBuilder tagBuilder = getConstantBedrockTag(BlockEntityUtils.getBedrockBlockEntityId(BlockEntityType.CHEST), position.getX(), position.getY(), position.getZ());
    translateTag(tagBuilder, null, blockState);
    BlockEntityUtils.updateBlockEntity(session, tagBuilder.build(), position);
}
Also used : NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder)

Example 25 with NbtMapBuilder

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

the class LecternInventoryTranslator method updateBook.

/**
 * Translate the data of the book in the lectern into a block entity tag.
 */
private void updateBook(GeyserSession session, Inventory inventory, GeyserItemStack book) {
    LecternContainer lecternContainer = (LecternContainer) inventory;
    if (session.isDroppingLecternBook()) {
        // We have to enter the inventory GUI to eject the book
        ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getId(), 3);
        session.sendDownstreamPacket(packet);
        session.setDroppingLecternBook(false);
        InventoryUtils.closeInventory(session, inventory.getId(), false);
    } else if (lecternContainer.getBlockEntityTag() == null) {
        CompoundTag tag = book.getNbt();
        // Position has to be the last interacted position... right?
        Vector3i position = session.getLastInteractionBlockPosition();
        // If shouldExpectLecternHandled returns true, this is already handled for us
        // shouldRefresh means that we should boot out the client on our side because their lectern GUI isn't updated yet
        boolean shouldRefresh = !session.getGeyser().getWorldManager().shouldExpectLecternHandled() && !session.getLecternCache().contains(position);
        NbtMap blockEntityTag;
        if (tag != null) {
            int pagesSize = ((ListTag) tag.get("pages")).size();
            ItemData itemData = book.getItemData(session);
            NbtMapBuilder lecternTag = getBaseLecternTag(position.getX(), position.getY(), position.getZ(), pagesSize);
            lecternTag.putCompound("book", NbtMap.builder().putByte("Count", (byte) itemData.getCount()).putShort("Damage", (short) 0).putString("Name", "minecraft:written_book").putCompound("tag", itemData.getTag()).build());
            lecternTag.putInt("page", lecternContainer.getCurrentBedrockPage());
            blockEntityTag = lecternTag.build();
        } else {
            // There is *a* book here, but... no NBT.
            NbtMapBuilder lecternTag = getBaseLecternTag(position.getX(), position.getY(), position.getZ(), 1);
            NbtMapBuilder bookTag = NbtMap.builder().putByte("Count", (byte) 1).putShort("Damage", (short) 0).putString("Name", "minecraft:writable_book").putCompound("tag", NbtMap.builder().putList("pages", NbtType.COMPOUND, Collections.singletonList(NbtMap.builder().putString("photoname", "").putString("text", "").build())).build());
            blockEntityTag = lecternTag.putCompound("book", bookTag.build()).build();
        }
        // Even with serverside access to lecterns, we don't easily know which lectern this is, so we need to rebuild
        // the block entity tag
        lecternContainer.setBlockEntityTag(blockEntityTag);
        lecternContainer.setPosition(position);
        if (shouldRefresh) {
            // Update the lectern because it's not updated client-side
            BlockEntityUtils.updateBlockEntity(session, blockEntityTag, position);
            session.getLecternCache().add(position);
            // Close the window - we will reopen it once the client has this data synced
            ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(lecternContainer.getId());
            session.sendDownstreamPacket(closeWindowPacket);
            InventoryUtils.closeInventory(session, inventory.getId(), false);
        }
    }
}
Also used : ServerboundContainerClosePacket(com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerClosePacket) LecternContainer(org.geysermc.geyser.inventory.LecternContainer) Vector3i(com.nukkitx.math.vector.Vector3i) NbtMap(com.nukkitx.nbt.NbtMap) NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ServerboundContainerButtonClickPacket(com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerButtonClickPacket) ItemData(com.nukkitx.protocol.bedrock.data.inventory.ItemData)

Aggregations

NbtMapBuilder (com.nukkitx.nbt.NbtMapBuilder)30 NbtMap (com.nukkitx.nbt.NbtMap)15 ArrayList (java.util.ArrayList)11 Item (org.jukeboxmc.item.Item)7 Vector3i (com.nukkitx.math.vector.Vector3i)3 ItemData (com.nukkitx.protocol.bedrock.data.inventory.ItemData)3 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)2 BlockEntityDataPacket (com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Container (org.geysermc.geyser.inventory.Container)2 ItemMapping (org.geysermc.geyser.registry.type.ItemMapping)2 BlockEntityShulkerBox (org.jukeboxmc.blockentity.BlockEntityShulkerBox)2 ShulkerBoxInventory (org.jukeboxmc.inventory.ShulkerBoxInventory)2 ItemAir (org.jukeboxmc.item.ItemAir)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ItemStack (com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack)1 ServerboundContainerButtonClickPacket (com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerButtonClickPacket)1 ServerboundContainerClosePacket (com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerClosePacket)1