Search in sources :

Example 1 with ListTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag in project ViaBackwards by ViaVersion.

the class Protocol1_17To1_17_1 method registerPackets.

@Override
protected void registerPackets() {
    registerClientbound(ClientboundPackets1_17_1.REMOVE_ENTITIES, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int[] entityIds = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
                wrapper.cancel();
                for (int entityId : entityIds) {
                    // Send individual remove packets
                    PacketWrapper newPacket = wrapper.create(ClientboundPackets1_17.REMOVE_ENTITY);
                    newPacket.write(Type.VAR_INT, entityId);
                    newPacket.send(Protocol1_17To1_17_1.class);
                }
            });
        }
    });
    registerClientbound(ClientboundPackets1_17_1.CLOSE_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
                wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
            });
        }
    });
    registerClientbound(ClientboundPackets1_17_1.SET_SLOT, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
                int stateId = wrapper.read(Type.VAR_INT);
                wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId);
            });
        }
    });
    registerClientbound(ClientboundPackets1_17_1.WINDOW_ITEMS, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
                int stateId = wrapper.read(Type.VAR_INT);
                wrapper.user().get(InventoryStateIds.class).setStateId(containerId, stateId);
                // Length is encoded as a var int in 1.17.1
                wrapper.write(Type.FLAT_VAR_INT_ITEM_ARRAY, wrapper.read(Type.FLAT_VAR_INT_ITEM_ARRAY_VAR_INT));
                // Carried item - should work without adding it to the array above
                wrapper.read(Type.FLAT_VAR_INT_ITEM);
            });
        }
    });
    registerServerbound(ServerboundPackets1_17.CLOSE_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
                wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
            });
        }
    });
    registerServerbound(ServerboundPackets1_17.CLICK_WINDOW, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                short containerId = wrapper.passthrough(Type.UNSIGNED_BYTE);
                int stateId = wrapper.user().get(InventoryStateIds.class).removeStateId(containerId);
                wrapper.write(Type.VAR_INT, stateId == Integer.MAX_VALUE ? 0 : stateId);
            });
        }
    });
    registerServerbound(ServerboundPackets1_17.EDIT_BOOK, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                Item item = wrapper.read(Type.FLAT_VAR_INT_ITEM);
                boolean signing = wrapper.read(Type.BOOLEAN);
                // Slot comes first
                wrapper.passthrough(Type.VAR_INT);
                CompoundTag tag = item.tag();
                ListTag pagesTag;
                StringTag titleTag = null;
                // Sanity checks
                if (tag == null || (pagesTag = tag.get("pages")) == null || (signing && (titleTag = tag.get("title")) == null)) {
                    // Pages length
                    wrapper.write(Type.VAR_INT, 0);
                    // Optional title
                    wrapper.write(Type.BOOLEAN, false);
                    return;
                }
                // Write pages - limit them first
                if (pagesTag.size() > MAX_PAGES) {
                    pagesTag = new ListTag(pagesTag.getValue().subList(0, MAX_PAGES));
                }
                wrapper.write(Type.VAR_INT, pagesTag.size());
                for (Tag pageTag : pagesTag) {
                    String page = ((StringTag) pageTag).getValue();
                    // Limit page length
                    if (page.length() > MAX_PAGE_LENGTH) {
                        page = page.substring(0, MAX_PAGE_LENGTH);
                    }
                    wrapper.write(Type.STRING, page);
                }
                // Write optional title
                wrapper.write(Type.BOOLEAN, signing);
                if (signing) {
                    if (titleTag == null) {
                        titleTag = tag.get("title");
                    }
                    // Limit title length
                    String title = titleTag.getValue();
                    if (title.length() > MAX_TITLE_LENGTH) {
                        title = title.substring(0, MAX_TITLE_LENGTH);
                    }
                    wrapper.write(Type.STRING, title);
                }
            });
        }
    });
}
Also used : ClientboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) ServerboundPackets1_17(com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ServerboundPackets1_17) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) Item(com.viaversion.viaversion.api.minecraft.item.Item) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) BackwardsProtocol(com.viaversion.viabackwards.api.BackwardsProtocol) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ClientboundPackets1_17_1(com.viaversion.viaversion.protocols.protocol1_17_1to1_17.ClientboundPackets1_17_1) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) InventoryStateIds(com.viaversion.viabackwards.protocol.protocol1_17to1_17_1.storage.InventoryStateIds) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 2 with ListTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag in project ViaBackwards by ViaVersion.

the class BlockItemPackets1_11 method handleItemToClient.

@Override
public Item handleItemToClient(Item item) {
    if (item == null)
        return null;
    super.handleItemToClient(item);
    CompoundTag tag = item.tag();
    if (tag == null)
        return item;
    // Rewrite spawn eggs (id checks are done in the method itself)
    EntityIdRewriter.toClientItem(item, true);
    if (tag.get("ench") instanceof ListTag) {
        enchantmentRewriter.rewriteEnchantmentsToClient(tag, false);
    }
    if (tag.get("StoredEnchantments") instanceof ListTag) {
        enchantmentRewriter.rewriteEnchantmentsToClient(tag, true);
    }
    return item;
}
Also used : ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 3 with ListTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag in project ViaBackwards by ViaVersion.

the class ItemPackets1_11_1 method handleItemToClient.

@Override
public Item handleItemToClient(Item item) {
    if (item == null)
        return null;
    super.handleItemToClient(item);
    CompoundTag tag = item.tag();
    if (tag == null)
        return item;
    if (tag.get("ench") instanceof ListTag) {
        enchantmentRewriter.rewriteEnchantmentsToClient(tag, false);
    }
    if (tag.get("StoredEnchantments") instanceof ListTag) {
        enchantmentRewriter.rewriteEnchantmentsToClient(tag, true);
    }
    return item;
}
Also used : ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 4 with ListTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag in project ViaBackwards by ViaVersion.

the class ItemRewriterBase method saveListTag.

protected void saveListTag(CompoundTag displayTag, ListTag original, String name) {
    // Multiple places might try to backup data
    String backupName = nbtTagName + "|o" + name;
    if (!displayTag.contains(backupName)) {
        // Clone all tag entries
        ListTag listTag = new ListTag();
        for (Tag tag : original.getValue()) {
            listTag.add(tag.clone());
        }
        displayTag.put(backupName, listTag);
    }
}
Also used : Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag)

Example 5 with ListTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag in project ViaBackwards by ViaVersion.

the class EnchantmentRewriter method rewriteEnchantmentsToClient.

public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant) {
    String key = storedEnchant ? "StoredEnchantments" : "Enchantments";
    ListTag enchantments = tag.get(key);
    List<Tag> loreToAdd = new ArrayList<>();
    boolean changed = false;
    Iterator<Tag> iterator = enchantments.iterator();
    while (iterator.hasNext()) {
        CompoundTag enchantmentEntry = (CompoundTag) iterator.next();
        Tag idTag = enchantmentEntry.get("id");
        if (!(idTag instanceof StringTag))
            continue;
        String enchantmentId = ((StringTag) idTag).getValue();
        String remappedName = enchantmentMappings.get(enchantmentId);
        if (remappedName != null) {
            if (!changed) {
                // Backup original before doing modifications
                itemRewriter.saveListTag(tag, enchantments, key);
                changed = true;
            }
            iterator.remove();
            int level = ((NumberTag) enchantmentEntry.get("lvl")).asInt();
            String loreValue = remappedName + " " + getRomanNumber(level);
            if (jsonFormat) {
                loreValue = ChatRewriter.legacyTextToJsonString(loreValue);
            }
            loreToAdd.add(new StringTag(loreValue));
        }
    }
    if (!loreToAdd.isEmpty()) {
        // Add dummy enchant for the glow effect if there are no actual enchantments left
        if (!storedEnchant && enchantments.size() == 0) {
            CompoundTag dummyEnchantment = new CompoundTag();
            dummyEnchantment.put("id", new StringTag());
            dummyEnchantment.put("lvl", new ShortTag((short) 0));
            enchantments.add(dummyEnchantment);
        }
        CompoundTag display = tag.get("display");
        if (display == null) {
            tag.put("display", display = new CompoundTag());
        }
        ListTag loreTag = display.get("Lore");
        if (loreTag == null) {
            display.put("Lore", loreTag = new ListTag(StringTag.class));
        } else {
            // Save original lore
            itemRewriter.saveListTag(display, loreTag, "Lore");
        }
        loreToAdd.addAll(loreTag.getValue());
        loreTag.setValue(loreToAdd);
    }
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ArrayList(java.util.ArrayList) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) ShortTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ShortTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag)

Aggregations

CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)19 ListTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag)19 Tag (com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag)17 StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)16 IntTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)9 ByteTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag)7 NumberTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag)7 ShortTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag)7 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)4 Type (com.viaversion.viaversion.api.type.Type)4 ArrayList (java.util.ArrayList)3 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)2 BackwardsProtocol (com.viaversion.viabackwards.api.BackwardsProtocol)2 EntityRewriter (com.viaversion.viabackwards.api.rewriters.EntityRewriter)2 Entity1_17Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types)2 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)2 MetaType (com.viaversion.viaversion.api.minecraft.metadata.MetaType)2 Particle (com.viaversion.viaversion.api.type.types.Particle)2 Types1_17 (com.viaversion.viaversion.api.type.types.version.Types1_17)2 ClientboundPackets1_17 (com.viaversion.viaversion.protocols.protocol1_17to1_16_4.ClientboundPackets1_17)2