Java Code Examples for com.nukkitx.protocol.bedrock.data.ItemData#getTag()

The following examples show how to use com.nukkitx.protocol.bedrock.data.ItemData#getTag() . 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 File: BannerTranslator.java    From Geyser with 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 2
Source File: JavaTradeListTranslator.java    From Geyser with 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 3
Source File: ItemTranslator.java    From Geyser with 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 4
Source File: AnvilInventoryTranslator.java    From Geyser with 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 5
Source File: DownstreamPacketHandler.java    From ProxyPass with 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;
}