Search in sources :

Example 11 with ListTag

use of com.github.steveice10.opennbt.tag.builtin.ListTag in project ViaVersion by ViaVersion.

the class Protocol1_18_2To1_18 method registerPackets.

@Override
protected void registerPackets() {
    final TagRewriter tagRewriter = new TagRewriter(this);
    tagRewriter.addEmptyTag(RegistryType.BLOCK, "minecraft:fall_damage_resetting");
    tagRewriter.registerGeneric(ClientboundPackets1_18.TAGS);
    registerClientbound(ClientboundPackets1_18.ENTITY_EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity id
            map(Type.VAR_INT);
            // Effect id
            map(Type.BYTE, Type.VAR_INT);
        }
    });
    registerClientbound(ClientboundPackets1_18.REMOVE_ENTITY_EFFECT, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity id
            map(Type.VAR_INT);
            // Effect id
            map(Type.BYTE, Type.VAR_INT);
        }
    });
    registerClientbound(ClientboundPackets1_18.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Entity ID
            map(Type.INT);
            // Hardcore
            map(Type.BOOLEAN);
            // Gamemode
            map(Type.UNSIGNED_BYTE);
            // Previous Gamemode
            map(Type.BYTE);
            // World List
            map(Type.STRING_ARRAY);
            // Registry
            map(Type.NBT);
            // Current dimension data
            map(Type.NBT);
            handler(wrapper -> {
                final CompoundTag registry = wrapper.get(Type.NBT, 0);
                final CompoundTag dimensionsHolder = registry.get("minecraft:dimension_type");
                final ListTag dimensions = dimensionsHolder.get("value");
                for (final Tag dimension : dimensions) {
                    addTagPrefix(((CompoundTag) dimension).get("element"));
                }
                addTagPrefix(wrapper.get(Type.NBT, 1));
            });
        }
    });
    registerClientbound(ClientboundPackets1_18.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> addTagPrefix(wrapper.passthrough(Type.NBT)));
        }
    });
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) RegistryType(com.viaversion.viaversion.api.minecraft.RegistryType) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ClientboundPackets1_18(com.viaversion.viaversion.protocols.protocol1_18to1_17_1.ClientboundPackets1_18) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) ServerboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) AbstractProtocol(com.viaversion.viaversion.api.protocol.AbstractProtocol) TagRewriter(com.viaversion.viaversion.rewriter.TagRewriter) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 12 with ListTag

use of com.github.steveice10.opennbt.tag.builtin.ListTag in project DragonProxy by DragonetMC.

the class ItemBlockTranslator method translateRawNBT.

@SuppressWarnings("unchecked")
public static org.dragonet.common.data.nbt.tag.CompoundTag translateRawNBT(int id, Tag pcTag, org.dragonet.common.data.nbt.tag.CompoundTag target) {
    if (pcTag != null) {
        String name = pcTag.getName() != null ? pcTag.getName() : "";
        if (target == null)
            target = new org.dragonet.common.data.nbt.tag.CompoundTag(name);
        switch(pcTag.getClass().getSimpleName()) {
            case "ByteArrayTag":
                target.putByteArray(name, (byte[]) pcTag.getValue());
                break;
            case "ByteTag":
                target.putByte(name, (byte) pcTag.getValue());
                break;
            case "DoubleTag":
                target.putDouble(name, (double) pcTag.getValue());
                break;
            case "FloatTag":
                target.putFloat(name, (float) pcTag.getValue());
                break;
            case "IntArrayTag":
                target.putIntArray(name, (int[]) pcTag.getValue());
                break;
            case "IntTag":
                target.putInt(name, (int) pcTag.getValue());
                break;
            case "LongTag":
                target.putLong(name, (long) pcTag.getValue());
                break;
            case "ShortTag":
                target.putShort(name, (short) pcTag.getValue());
                break;
            case "StringTag":
                target.putString(name, (String) pcTag.getValue());
                break;
            case "CompoundTag":
                for (String subName : ((CompoundTag) pcTag).getValue().keySet()) translateRawNBT(0, ((CompoundTag) pcTag).getValue().get(subName), target);
                break;
            case "ListTag":
                ListTag listTag = new ListTag();
                for (Tag subTag : (List<Tag>) pcTag.getValue()) listTag.add(translateRawNBT(0, subTag, new org.dragonet.common.data.nbt.tag.CompoundTag()));
                target.putList(listTag);
                break;
            default:
                System.out.println("TAG not implemented : " + pcTag.getClass().getSimpleName());
                break;
        }
    }
    return target;
}
Also used : List(java.util.List) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ListTag(org.dragonet.common.data.nbt.tag.ListTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(org.dragonet.common.data.nbt.tag.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 13 with ListTag

use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.

the class BedrockBookEditTranslator method translate.

@Override
public void translate(GeyserSession session, BookEditPacket packet) {
    if (packet.getText() != null && !packet.getText().isEmpty() && packet.getText().getBytes(StandardCharsets.UTF_8).length > MAXIMUM_PAGE_LENGTH) {
        session.getGeyser().getLogger().warning("Page length greater than server allowed!");
        return;
    }
    GeyserItemStack itemStack = session.getPlayerInventory().getItemInHand();
    if (itemStack != null) {
        CompoundTag tag = itemStack.getNbt() != null ? itemStack.getNbt() : new CompoundTag("");
        ItemStack bookItem = new ItemStack(itemStack.getJavaId(), itemStack.getAmount(), tag);
        List<Tag> pages = tag.contains("pages") ? new LinkedList<>(((ListTag) tag.get("pages")).getValue()) : new LinkedList<>();
        int page = packet.getPageNumber();
        switch(packet.getAction()) {
            case ADD_PAGE:
                {
                    // Add empty pages in between
                    for (int i = pages.size(); i < page; i++) {
                        pages.add(i, new StringTag("", ""));
                    }
                    pages.add(page, new StringTag("", packet.getText()));
                    break;
                }
            // Called whenever a page is modified
            case REPLACE_PAGE:
                {
                    if (page < pages.size()) {
                        pages.set(page, new StringTag("", packet.getText()));
                    } else {
                        // Add empty pages in between
                        for (int i = pages.size(); i < page; i++) {
                            pages.add(i, new StringTag("", ""));
                        }
                        pages.add(page, new StringTag("", packet.getText()));
                    }
                    break;
                }
            case DELETE_PAGE:
                {
                    if (page < pages.size()) {
                        pages.remove(page);
                    }
                    break;
                }
            case SWAP_PAGES:
                {
                    int page2 = packet.getSecondaryPageNumber();
                    if (page < pages.size() && page2 < pages.size()) {
                        Collections.swap(pages, page, page2);
                    }
                    break;
                }
            case SIGN_BOOK:
                {
                    tag.put(new StringTag("author", packet.getAuthor()));
                    tag.put(new StringTag("title", packet.getTitle()));
                    break;
                }
            default:
                return;
        }
        // Remove empty pages at the end
        while (pages.size() > 0) {
            StringTag currentPage = (StringTag) pages.get(pages.size() - 1);
            if (currentPage.getValue() == null || currentPage.getValue().isEmpty()) {
                pages.remove(pages.size() - 1);
            } else {
                break;
            }
        }
        tag.put(new ListTag("pages", pages));
        // Update local copy
        session.getPlayerInventory().setItem(36 + session.getPlayerInventory().getHeldItemSlot(), GeyserItemStack.from(bookItem), session);
        session.getInventoryTranslator().updateInventory(session, session.getPlayerInventory());
        List<String> networkPages = new ArrayList<>();
        for (Tag pageTag : pages) {
            networkPages.add(((StringTag) pageTag).getValue());
        }
        String title;
        if (packet.getAction() == BookEditPacket.Action.SIGN_BOOK) {
            // Add title to packet so the server knows we're signing
            if (packet.getTitle().getBytes(StandardCharsets.UTF_8).length > MAXIMUM_TITLE_LENGTH) {
                session.getGeyser().getLogger().warning("Book title larger than server allows!");
                return;
            }
            title = packet.getTitle();
        } else {
            title = null;
        }
        session.getBookEditCache().setPacket(new ServerboundEditBookPacket(session.getPlayerInventory().getHeldItemSlot(), networkPages, title));
        // There won't be any more book updates after this, so we can try sending the edit packet immediately
        if (packet.getAction() == BookEditPacket.Action.SIGN_BOOK) {
            session.getBookEditCache().checkForSend();
        }
    }
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ArrayList(java.util.ArrayList) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) ServerboundEditBookPacket(com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundEditBookPacket) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) GeyserItemStack(org.geysermc.geyser.inventory.GeyserItemStack) ItemStack(com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack) GeyserItemStack(org.geysermc.geyser.inventory.GeyserItemStack) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 14 with ListTag

use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.

the class CrossbowTranslator method translateToBedrock.

@Override
public void translateToBedrock(GeyserSession session, CompoundTag itemTag, ItemMapping mapping) {
    if (itemTag.get("ChargedProjectiles") != null) {
        ListTag chargedProjectiles = itemTag.get("ChargedProjectiles");
        if (!chargedProjectiles.getValue().isEmpty()) {
            CompoundTag projectile = (CompoundTag) chargedProjectiles.getValue().get(0);
            ItemMapping projectileMapping = session.getItemMappings().getMapping((String) projectile.get("id").getValue());
            if (projectileMapping == null)
                return;
            CompoundTag tag = projectile.get("tag");
            ItemStack itemStack = new ItemStack(mapping.getJavaId(), (byte) projectile.get("Count").getValue(), tag);
            ItemData itemData = ItemTranslator.translateToBedrock(session, itemStack);
            CompoundTag newProjectile = new CompoundTag("chargedItem");
            newProjectile.put(new ByteTag("Count", (byte) itemData.getCount()));
            newProjectile.put(new StringTag("Name", projectileMapping.getBedrockIdentifier()));
            newProjectile.put(new ShortTag("Damage", (short) itemData.getDamage()));
            itemTag.put(newProjectile);
        }
    }
}
Also used : ItemMapping(org.geysermc.geyser.registry.type.ItemMapping) ItemStack(com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack) ItemData(com.nukkitx.protocol.bedrock.data.inventory.ItemData)

Example 15 with ListTag

use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.

the class EnchantedBookTranslator method translateToJava.

@Override
public void translateToJava(CompoundTag itemTag, ItemMapping mapping) {
    if (!itemTag.contains("Enchantments")) {
        return;
    }
    Tag enchTag = itemTag.get("Enchantments");
    if (enchTag instanceof ListTag) {
        enchTag = new ListTag("StoredEnchantments", ((ListTag) enchTag).getValue());
        itemTag.remove("Enchantments");
        itemTag.put(enchTag);
    }
}
Also used : Tag(com.github.steveice10.opennbt.tag.builtin.Tag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag)

Aggregations

CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)25 ListTag (com.github.steveice10.opennbt.tag.builtin.ListTag)23 Tag (com.github.steveice10.opennbt.tag.builtin.Tag)21 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)17 IntTag (com.github.steveice10.opennbt.tag.builtin.IntTag)7 NumberTag (com.github.steveice10.opennbt.tag.builtin.NumberTag)7 ByteTag (com.github.steveice10.opennbt.tag.builtin.ByteTag)4 DoubleTag (com.github.steveice10.opennbt.tag.builtin.DoubleTag)4 IntArrayTag (com.github.steveice10.opennbt.tag.builtin.IntArrayTag)4 LongTag (com.github.steveice10.opennbt.tag.builtin.LongTag)4 ShortTag (com.github.steveice10.opennbt.tag.builtin.ShortTag)4 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)4 ItemStack (com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack)3 Type (com.viaversion.viaversion.api.type.Type)3 ArrayList (java.util.ArrayList)3 ByteArrayTag (com.github.steveice10.opennbt.tag.builtin.ByteArrayTag)2 FloatTag (com.github.steveice10.opennbt.tag.builtin.FloatTag)2 LongArrayTag (com.github.steveice10.opennbt.tag.builtin.LongArrayTag)2 NbtMap (com.nukkitx.nbt.NbtMap)2 NbtMapBuilder (com.nukkitx.nbt.NbtMapBuilder)2