Java Code Examples for com.nukkitx.protocol.bedrock.data.ItemData

The following examples show how to use com.nukkitx.protocol.bedrock.data.ItemData. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: Geyser   Author: GeyserMC   File: BannerTranslator.java    License: MIT License 6 votes vote down vote up
@Override
public ItemData translateToBedrock(ItemStack itemStack, ItemEntry itemEntry) {
    if (itemStack.getNbt() == null) return super.translateToBedrock(itemStack, itemEntry);

    ItemData itemData = super.translateToBedrock(itemStack, itemEntry);

    CompoundTag blockEntityTag = itemStack.getNbt().get("BlockEntityTag");
    if (blockEntityTag.contains("Patterns")) {
        ListTag patterns = blockEntityTag.get("Patterns");

        CompoundTagBuilder builder = itemData.getTag().toBuilder();
        builder.tag(convertBannerPattern(patterns));

        itemData = ItemData.of(itemData.getId(), itemData.getDamage(), itemData.getCount(), builder.buildRootTag());
    }

    return itemData;
}
 
Example #2
Source Project: Geyser   Author: GeyserMC   File: BannerTranslator.java    License: MIT License 6 votes vote down vote up
@Override
public ItemStack translateToJava(ItemData itemData, ItemEntry itemEntry) {
    if (itemData.getTag() == null) return super.translateToJava(itemData, itemEntry);

    ItemStack itemStack = super.translateToJava(itemData, itemEntry);

    com.nukkitx.nbt.tag.CompoundTag nbtTag = itemData.getTag();
    if (nbtTag.contains("Patterns")) {
        com.nukkitx.nbt.tag.ListTag<?> patterns = nbtTag.get("Patterns");

        CompoundTag blockEntityTag = new CompoundTag("BlockEntityTag");
        blockEntityTag.put(convertBannerPattern(patterns));

        itemStack.getNbt().put(blockEntityTag);
    }

    return itemStack;
}
 
Example #3
Source Project: Geyser   Author: GeyserMC   File: ItemTranslator.java    License: MIT License 6 votes vote down vote up
public static ItemStack translateToJava(ItemData data) {
    if (data == null) {
        return new ItemStack(0);
    }
    ItemEntry javaItem = ItemRegistry.getItem(data);

    ItemStack itemStack;
    ItemTranslator itemStackTranslator = ITEM_STACK_TRANSLATORS.get(javaItem.getJavaId());
    if (itemStackTranslator != null) {
        itemStack = itemStackTranslator.translateToJava(data, javaItem);
    } else {
        itemStack = DEFAULT_TRANSLATOR.translateToJava(data, javaItem);
    }

    if (itemStack != null && itemStack.getNbt() != null) {
        for (NbtItemStackTranslator translator : NBT_TRANSLATORS) {
            if (translator.acceptItem(javaItem)) {
                translator.translateToJava(itemStack.getNbt(), javaItem);
            }
        }
    }
    return itemStack;
}
 
Example #4
Source Project: Geyser   Author: GeyserMC   File: ChestInventoryUpdater.java    License: MIT License 6 votes vote down vote up
@Override
public void updateInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
    super.updateInventory(translator, session, inventory);

    ItemData[] bedrockItems = new ItemData[paddedSize];
    for (int i = 0; i < bedrockItems.length; i++) {
        if (i < translator.size) {
            bedrockItems[i] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
        } else {
            bedrockItems[i] = UNUSUABLE_SPACE_BLOCK;
        }
    }

    InventoryContentPacket contentPacket = new InventoryContentPacket();
    contentPacket.setContainerId(inventory.getId());
    contentPacket.setContents(bedrockItems);
    session.sendUpstreamPacket(contentPacket);
}
 
Example #5
Source Project: Geyser   Author: GeyserMC   File: LlamaEntity.java    License: MIT License 5 votes vote down vote up
@Override
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
    // Strength
    if (entityMetadata.getId() == 19) {
        metadata.put(EntityData.STRENGTH, entityMetadata.getValue());
    }
    // Color equipped on the llama
    if (entityMetadata.getId() == 20) {
        // Bedrock treats llama decoration as armor
        MobArmorEquipmentPacket equipmentPacket = new MobArmorEquipmentPacket();
        equipmentPacket.setRuntimeEntityId(getGeyserId());
        // -1 means no armor
        if ((int) entityMetadata.getValue() != -1) {
            // The damage value is the dye color that Java sends us
            // Always going to be a carpet so we can hardcode 171 in BlockTranslator
            // The int then short conversion is required or we get a ClassCastException
            equipmentPacket.setChestplate(ItemData.of(BlockTranslator.CARPET, (short)((int) entityMetadata.getValue()), 1));
        } else {
            equipmentPacket.setChestplate(ItemData.AIR);
        }
        // Required to fill out the rest of the equipment or Bedrock ignores it, including above else statement if removing armor
        equipmentPacket.setBoots(ItemData.AIR);
        equipmentPacket.setHelmet(ItemData.AIR);
        equipmentPacket.setLeggings(ItemData.AIR);

        session.sendUpstreamPacket(equipmentPacket);
    }
    // Color of the llama
    if (entityMetadata.getId() == 21) {
        metadata.put(EntityData.VARIANT, entityMetadata.getValue());
    }
    super.updateBedrockMetadata(entityMetadata, session);
}
 
Example #6
Source Project: Geyser   Author: GeyserMC   File: JavaTradeListTranslator.java    License: MIT License 5 votes vote down vote up
private CompoundTag getItemTag(GeyserSession session, ItemStack stack, String name, int specialPrice) {
    ItemData itemData = ItemTranslator.translateToBedrock(session, stack);
    ItemEntry itemEntry = ItemRegistry.getItem(stack);
    CompoundTagBuilder builder = CompoundTagBuilder.builder();
    builder.byteTag("Count", (byte) (Math.max(itemData.getCount() + specialPrice, 1)));
    builder.shortTag("Damage", itemData.getDamage());
    builder.shortTag("id", (short) itemEntry.getBedrockId());
    if (itemData.getTag() != null) {
        CompoundTag tag = itemData.getTag().toBuilder().build("tag");
        builder.tag(tag);
    }
    return builder.build(name);
}
 
Example #7
Source Project: Geyser   Author: GeyserMC   File: PotionTranslator.java    License: MIT License 5 votes vote down vote up
@Override
public ItemData translateToBedrock(ItemStack itemStack, ItemEntry itemEntry) {
    if (itemStack.getNbt() == null) return super.translateToBedrock(itemStack, itemEntry);
    Tag potionTag = itemStack.getNbt().get("Potion");
    if (potionTag instanceof StringTag) {
        Potion potion = Potion.getByJavaIdentifier(((StringTag) potionTag).getValue());
        if (potion != null) {
            return ItemData.of(itemEntry.getBedrockId(), potion.getBedrockId(), itemStack.getAmount(), translateNbtToBedrock(itemStack.getNbt()));
        }
        GeyserConnector.getInstance().getLogger().debug("Unknown java potion: " + potionTag.getValue());
    }
    return super.translateToBedrock(itemStack, itemEntry);
}
 
Example #8
Source Project: Geyser   Author: GeyserMC   File: PotionTranslator.java    License: MIT License 5 votes vote down vote up
@Override
public ItemStack translateToJava(ItemData itemData, ItemEntry itemEntry) {
    Potion potion = Potion.getByBedrockId(itemData.getDamage());
    ItemStack itemStack = super.translateToJava(itemData, itemEntry);
    if (potion != null) {
        StringTag potionTag = new StringTag("Potion", potion.getJavaIdentifier());
        itemStack.getNbt().put(potionTag);
    }
    return itemStack;
}
 
Example #9
Source Project: Geyser   Author: GeyserMC   File: ItemTranslator.java    License: MIT License 5 votes vote down vote up
public ItemData translateToBedrock(ItemStack itemStack, ItemEntry itemEntry) {
    if (itemStack == null) {
        return ItemData.AIR;
    }
    if (itemStack.getNbt() == null) {
        return ItemData.of(itemEntry.getBedrockId(), (short) itemEntry.getBedrockData(), itemStack.getAmount());
    }
    return ItemData.of(itemEntry.getBedrockId(), (short) itemEntry.getBedrockData(), itemStack.getAmount(), this.translateNbtToBedrock(itemStack.getNbt()));
}
 
Example #10
Source Project: Geyser   Author: GeyserMC   File: ItemTranslator.java    License: MIT License 5 votes vote down vote up
public ItemStack translateToJava(ItemData itemData, ItemEntry itemEntry) {
    if (itemData == null) return null;
    if (itemData.getTag() == null) {
        return new ItemStack(itemEntry.getJavaId(), itemData.getCount(), new com.github.steveice10.opennbt.tag.builtin.CompoundTag(""));
    }
    return new ItemStack(itemEntry.getJavaId(), itemData.getCount(), this.translateToJavaNBT(itemData.getTag()));
}
 
Example #11
Source Project: Geyser   Author: GeyserMC   File: PlayerInventoryTranslator.java    License: MIT License 5 votes vote down vote up
@Override
public void updateInventory(GeyserSession session, Inventory inventory) {
    updateCraftingGrid(session, inventory);

    InventoryContentPacket inventoryContentPacket = new InventoryContentPacket();
    inventoryContentPacket.setContainerId(ContainerId.INVENTORY);
    ItemData[] contents = new ItemData[36];
    // Inventory
    for (int i = 9; i < 36; i++) {
        contents[i] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
    }
    // Hotbar
    for (int i = 36; i < 45; i++) {
        contents[i - 36] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
    }
    inventoryContentPacket.setContents(contents);
    session.sendUpstreamPacket(inventoryContentPacket);

    // Armor
    InventoryContentPacket armorContentPacket = new InventoryContentPacket();
    armorContentPacket.setContainerId(ContainerId.ARMOR);
    contents = new ItemData[4];
    for (int i = 5; i < 9; i++) {
        contents[i - 5] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
    }
    armorContentPacket.setContents(contents);
    session.sendUpstreamPacket(armorContentPacket);

    // Offhand
    InventoryContentPacket offhandPacket = new InventoryContentPacket();
    offhandPacket.setContainerId(ContainerId.OFFHAND);
    offhandPacket.setContents(new ItemData[]{ItemTranslator.translateToBedrock(session, inventory.getItem(45))});
    session.sendUpstreamPacket(offhandPacket);
}
 
Example #12
Source Project: Geyser   Author: GeyserMC   File: PlayerInventoryTranslator.java    License: MIT License 5 votes vote down vote up
@Override
public void updateSlot(GeyserSession session, Inventory inventory, int slot) {
    if (slot >= 1 && slot <= 44) {
        InventorySlotPacket slotPacket = new InventorySlotPacket();
        if (slot >= 9) {
            slotPacket.setContainerId(ContainerId.INVENTORY);
            if (slot >= 36) {
                slotPacket.setSlot(slot - 36);
            } else {
                slotPacket.setSlot(slot);
            }
        } else if (slot >= 5) {
            slotPacket.setContainerId(ContainerId.ARMOR);
            slotPacket.setSlot(slot - 5);
        } else {
            slotPacket.setContainerId(ContainerId.CURSOR);
            slotPacket.setSlot(slot + 27);
        }
        slotPacket.setItem(ItemTranslator.translateToBedrock(session, inventory.getItem(slot)));
        session.sendUpstreamPacket(slotPacket);
    } else if (slot == 45) {
        InventoryContentPacket offhandPacket = new InventoryContentPacket();
        offhandPacket.setContainerId(ContainerId.OFFHAND);
        offhandPacket.setContents(new ItemData[]{ItemTranslator.translateToBedrock(session, inventory.getItem(slot))});
        session.sendUpstreamPacket(offhandPacket);
    }
}
 
Example #13
Source Project: Geyser   Author: GeyserMC   File: InventoryUpdater.java    License: MIT License 5 votes vote down vote up
public void updateInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
    ItemData[] bedrockItems = new ItemData[36];
    for (int i = 0; i < 36; i++) {
        final int offset = i < 9 ? 27 : -9;
        bedrockItems[i] = ItemTranslator.translateToBedrock(session, inventory.getItem(translator.size + i + offset));
    }
    InventoryContentPacket contentPacket = new InventoryContentPacket();
    contentPacket.setContainerId(ContainerId.INVENTORY);
    contentPacket.setContents(bedrockItems);
    session.sendUpstreamPacket(contentPacket);
}
 
Example #14
Source Project: Geyser   Author: GeyserMC   File: ContainerInventoryUpdater.java    License: MIT License 5 votes vote down vote up
@Override
public void updateInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
    super.updateInventory(translator, session, inventory);

    ItemData[] bedrockItems = new ItemData[translator.size];
    for (int i = 0; i < bedrockItems.length; i++) {
        bedrockItems[translator.javaSlotToBedrock(i)] = ItemTranslator.translateToBedrock(session, inventory.getItem(i));
    }

    InventoryContentPacket contentPacket = new InventoryContentPacket();
    contentPacket.setContainerId(inventory.getId());
    contentPacket.setContents(bedrockItems);
    session.sendUpstreamPacket(contentPacket);
}
 
Example #15
Source Project: Geyser   Author: GeyserMC   File: AnvilInventoryTranslator.java    License: MIT License 5 votes vote down vote up
@Override
public void translateActions(GeyserSession session, Inventory inventory, List<InventoryActionData> actions) {
    InventoryActionData anvilResult = null;
    InventoryActionData anvilInput = null;
    for (InventoryActionData action : actions) {
        if (action.getSource().getContainerId() == ContainerId.ANVIL_MATERIAL) {
            //useless packet
            return;
        } else if (action.getSource().getContainerId() == ContainerId.ANVIL_RESULT) {
            anvilResult = action;
        } else if (bedrockSlotToJava(action) == 0) {
            anvilInput = action;
        }
    }
    ItemData itemName = null;
    if (anvilResult != null) {
        itemName = anvilResult.getFromItem();
    } else if (anvilInput != null) {
        itemName = anvilInput.getToItem();
    }
    if (itemName != null) {
        String rename;
        com.nukkitx.nbt.tag.CompoundTag tag = itemName.getTag();
        if (tag != null) {
            rename = tag.getCompound("display").getString("Name");
        } else {
            rename = "";
        }
        ClientRenameItemPacket renameItemPacket = new ClientRenameItemPacket(rename);
        session.sendDownstreamPacket(renameItemPacket);
    }
    if (anvilResult != null) {
        //client will send another packet to grab anvil output
        return;
    }

    super.translateActions(session, inventory, actions);
}
 
Example #16
Source Project: Geyser   Author: GeyserMC   File: InventoryUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Returns a barrier block with custom name and lore to explain why
 * part of the inventory is unusable.
 */
public static ItemData createUnusableSpaceBlock(String description) {
    CompoundTagBuilder root = CompoundTagBuilder.builder();
    CompoundTagBuilder display = CompoundTagBuilder.builder();

    display.stringTag("Name", ChatColor.RESET + "Unusable inventory space");
    display.listTag("Lore", StringTag.class, Collections.singletonList(new StringTag("", ChatColor.RESET + ChatColor.DARK_PURPLE + description)));

    root.tag(display.build("display"));
    return ItemData.of(ItemRegistry.ITEM_ENTRIES.get(ItemRegistry.BARRIER_INDEX).getBedrockId(), (short) 0, 1, root.buildRootTag());
}
 
Example #17
Source Project: ProxyPass   Author: NukkitX   File: DownstreamPacketHandler.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean handle(InventoryContentPacket packet) {
    if (packet.getContainerId() == ContainerId.CREATIVE) {
        List<CreativeItemEntry> entries = new ArrayList<>();
        for (ItemData data : packet.getContents()) {
            int id = data.getId();
            Integer damage = data.getDamage() == 0 ? null : (int) data.getDamage();

            CompoundTag tag = data.getTag();
            String tagData = null;
            if (tag != null) {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                try (NBTOutputStream stream = new NBTOutputStream(new LittleEndianDataOutputStream(byteArrayOutputStream))) {
                    stream.write(tag);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                tagData = Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
            }
            entries.add(new CreativeItemEntry(id, damage, tagData));
        }

        CreativeItems items = new CreativeItems(entries);

        proxy.saveJson("creative_items.json", items);
    }
    return false;
}
 
Example #18
Source Project: Protocol   Author: NukkitX   File: InventoryContentSerializer_v354.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, InventoryContentPacket packet) {
    VarInts.writeUnsignedInt(buffer, packet.getContainerId());

    ItemData[] contents = packet.getContents();
    VarInts.writeUnsignedInt(buffer, contents.length);
    for (ItemData content : contents) {
        BedrockUtils.writeItemData(buffer, content);
    }
}
 
Example #19
Source Project: Protocol   Author: NukkitX   File: InventoryContentSerializer_v354.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, InventoryContentPacket packet) {
    packet.setContainerId(VarInts.readUnsignedInt(buffer));

    ItemData[] contents = new ItemData[VarInts.readUnsignedInt(buffer)];
    for (int i = 0; i < contents.length; i++) {
        contents[i] = BedrockUtils.readItemData(buffer);
    }
    packet.setContents(contents);
}
 
Example #20
Source Project: Protocol   Author: NukkitX   File: InventoryContentSerializer_v388.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, InventoryContentPacket packet) {
    VarInts.writeUnsignedInt(buffer, packet.getContainerId());

    ItemData[] contents = packet.getContents();
    VarInts.writeUnsignedInt(buffer, contents.length);
    for (ItemData content : contents) {
        BedrockUtils.writeItemData(buffer, content);
    }
}
 
Example #21
Source Project: Protocol   Author: NukkitX   File: InventoryContentSerializer_v388.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, InventoryContentPacket packet) {
    packet.setContainerId(VarInts.readUnsignedInt(buffer));

    ItemData[] contents = new ItemData[VarInts.readUnsignedInt(buffer)];
    for (int i = 0; i < contents.length; i++) {
        contents[i] = BedrockUtils.readItemData(buffer);
    }
    packet.setContents(contents);
}
 
Example #22
Source Project: Protocol   Author: NukkitX   File: InventoryContentSerializer_v340.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, InventoryContentPacket packet) {
    VarInts.writeUnsignedInt(buffer, packet.getContainerId());

    ItemData[] contents = packet.getContents();
    VarInts.writeUnsignedInt(buffer, contents.length);
    for (ItemData content : contents) {
        BedrockUtils.writeItemData(buffer, content);
    }
}
 
Example #23
Source Project: Protocol   Author: NukkitX   File: InventoryContentSerializer_v340.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, InventoryContentPacket packet) {
    packet.setContainerId(VarInts.readUnsignedInt(buffer));

    ItemData[] contents = new ItemData[VarInts.readUnsignedInt(buffer)];
    for (int i = 0; i < contents.length; i++) {
        contents[i] = BedrockUtils.readItemData(buffer);
    }
    packet.setContents(contents);
}
 
Example #24
Source Project: Protocol   Author: NukkitX   File: CraftingDataSerializer_v340.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, CraftingDataPacket packet) {
    BedrockUtils.writeArray(buffer, packet.getCraftingData(), (buf, craftingData) -> {
        VarInts.writeInt(buf, craftingData.getType().ordinal());
        switch (craftingData.getType()) {
            case SHAPELESS:
            case SHAPELESS_CHEMISTRY:
            case SHULKER_BOX:
                BedrockUtils.writeArray(buf, craftingData.getInputs(), BedrockUtils::writeItemData);
                BedrockUtils.writeArray(buf, craftingData.getOutputs(), BedrockUtils::writeItemData);
                BedrockUtils.writeUuid(buf, craftingData.getUuid());
                break;
            case SHAPED:
            case SHAPED_CHEMISTRY:
                VarInts.writeInt(buf, craftingData.getWidth());
                VarInts.writeInt(buf, craftingData.getHeight());
                int count = craftingData.getWidth() * craftingData.getHeight();
                ItemData[] inputs = craftingData.getInputs();
                for (int i = 0; i < count; i++) {
                    BedrockUtils.writeItemData(buf, inputs[i]);
                }
                BedrockUtils.writeArray(buf, craftingData.getOutputs(), BedrockUtils::writeItemData);
                BedrockUtils.writeUuid(buf, craftingData.getUuid());
                break;
            case FURNACE:
            case FURNACE_DATA:
                VarInts.writeInt(buf, craftingData.getInputId());
                if (craftingData.getType() == CraftingType.FURNACE_DATA) {
                    VarInts.writeInt(buf, craftingData.getInputDamage());
                }
                BedrockUtils.writeItemData(buf, craftingData.getOutputs()[0]);
                break;
            case MULTI:
                BedrockUtils.writeUuid(buf, craftingData.getUuid());
                break;
        }
    });
    buffer.writeBoolean(packet.isCleanRecipes());
}
 
Example #25
Source Project: Protocol   Author: NukkitX   File: CraftingDataSerializer_v313.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, CraftingDataPacket packet) {
    BedrockUtils.writeArray(buffer, packet.getCraftingData(), (buf, craftingData) -> {
        VarInts.writeInt(buf, craftingData.getType().ordinal());
        switch (craftingData.getType()) {
            case SHAPELESS:
            case SHAPELESS_CHEMISTRY:
            case SHULKER_BOX:
                BedrockUtils.writeArray(buf, craftingData.getInputs(), BedrockUtils::writeItemData);
                BedrockUtils.writeArray(buf, craftingData.getOutputs(), BedrockUtils::writeItemData);
                BedrockUtils.writeUuid(buf, craftingData.getUuid());
                break;
            case SHAPED:
            case SHAPED_CHEMISTRY:
                VarInts.writeInt(buf, craftingData.getWidth());
                VarInts.writeInt(buf, craftingData.getHeight());
                int count = craftingData.getWidth() * craftingData.getHeight();
                ItemData[] inputs = craftingData.getInputs();
                for (int i = 0; i < count; i++) {
                    BedrockUtils.writeItemData(buf, inputs[i]);
                }
                BedrockUtils.writeArray(buf, craftingData.getOutputs(), BedrockUtils::writeItemData);
                BedrockUtils.writeUuid(buf, craftingData.getUuid());
                break;
            case FURNACE:
            case FURNACE_DATA:
                VarInts.writeInt(buf, craftingData.getInputId());
                if (craftingData.getType() == CraftingType.FURNACE_DATA) {
                    VarInts.writeInt(buf, craftingData.getInputDamage());
                }
                BedrockUtils.writeItemData(buf, craftingData.getOutputs()[0]);
                break;
            case MULTI:
                BedrockUtils.writeUuid(buf, craftingData.getUuid());
                break;
        }
    });
    buffer.writeBoolean(packet.isCleanRecipes());
}
 
Example #26
Source Project: Protocol   Author: NukkitX   File: InventoryContentSerializer_v313.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, InventoryContentPacket packet) {
    VarInts.writeUnsignedInt(buffer, packet.getContainerId());

    ItemData[] contents = packet.getContents();
    VarInts.writeUnsignedInt(buffer, contents.length);
    for (ItemData content : contents) {
        BedrockUtils.writeItemData(buffer, content);
    }
}
 
Example #27
Source Project: Protocol   Author: NukkitX   File: InventoryContentSerializer_v313.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, InventoryContentPacket packet) {
    packet.setContainerId(VarInts.readUnsignedInt(buffer));

    ItemData[] contents = new ItemData[VarInts.readUnsignedInt(buffer)];
    for (int i = 0; i < contents.length; i++) {
        contents[i] = BedrockUtils.readItemData(buffer);
    }
    packet.setContents(contents);
}
 
Example #28
Source Project: Protocol   Author: NukkitX   File: InventoryContentSerializer_v361.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, InventoryContentPacket packet) {
    VarInts.writeUnsignedInt(buffer, packet.getContainerId());

    ItemData[] contents = packet.getContents();
    VarInts.writeUnsignedInt(buffer, contents.length);
    for (ItemData content : contents) {
        BedrockUtils.writeItemData(buffer, content);
    }
}
 
Example #29
Source Project: Protocol   Author: NukkitX   File: InventoryContentSerializer_v361.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, InventoryContentPacket packet) {
    packet.setContainerId(VarInts.readUnsignedInt(buffer));

    ItemData[] contents = new ItemData[VarInts.readUnsignedInt(buffer)];
    for (int i = 0; i < contents.length; i++) {
        contents[i] = BedrockUtils.readItemData(buffer);
    }
    packet.setContents(contents);
}
 
Example #30
Source Project: Protocol   Author: NukkitX   File: CraftingDataSerializer_v332.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, CraftingDataPacket packet) {
    BedrockUtils.writeArray(buffer, packet.getCraftingData(), (buf, craftingData) -> {
        VarInts.writeInt(buf, craftingData.getType().ordinal());
        switch (craftingData.getType()) {
            case SHAPELESS:
            case SHAPELESS_CHEMISTRY:
            case SHULKER_BOX:
                BedrockUtils.writeArray(buf, craftingData.getInputs(), BedrockUtils::writeItemData);
                BedrockUtils.writeArray(buf, craftingData.getOutputs(), BedrockUtils::writeItemData);
                BedrockUtils.writeUuid(buf, craftingData.getUuid());
                break;
            case SHAPED:
            case SHAPED_CHEMISTRY:
                VarInts.writeInt(buf, craftingData.getWidth());
                VarInts.writeInt(buf, craftingData.getHeight());
                int count = craftingData.getWidth() * craftingData.getHeight();
                ItemData[] inputs = craftingData.getInputs();
                for (int i = 0; i < count; i++) {
                    BedrockUtils.writeItemData(buf, inputs[i]);
                }
                BedrockUtils.writeArray(buf, craftingData.getOutputs(), BedrockUtils::writeItemData);
                BedrockUtils.writeUuid(buf, craftingData.getUuid());
                break;
            case FURNACE:
            case FURNACE_DATA:
                VarInts.writeInt(buf, craftingData.getInputId());
                if (craftingData.getType() == CraftingType.FURNACE_DATA) {
                    VarInts.writeInt(buf, craftingData.getInputDamage());
                }
                BedrockUtils.writeItemData(buf, craftingData.getOutputs()[0]);
                break;
            case MULTI:
                BedrockUtils.writeUuid(buf, craftingData.getUuid());
                break;
        }
    });
    buffer.writeBoolean(packet.isCleanRecipes());
}