Search in sources :

Example 6 with ListTag

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

the class ItemRewriter method handleItemToClient.

@Override
@Nullable
public Item handleItemToClient(@Nullable Item item) {
    if (item == null)
        return null;
    CompoundTag display = item.tag() != null ? item.tag().get("display") : null;
    if (protocol.getTranslatableRewriter() != null && display != null) {
        // Handle name and lore components
        StringTag name = display.get("Name");
        if (name != null) {
            String newValue = protocol.getTranslatableRewriter().processText(name.getValue()).toString();
            if (!newValue.equals(name.getValue())) {
                saveStringTag(display, name, "Name");
            }
            name.setValue(newValue);
        }
        ListTag lore = display.get("Lore");
        if (lore != null) {
            boolean changed = false;
            for (Tag loreEntryTag : lore) {
                if (!(loreEntryTag instanceof StringTag))
                    continue;
                StringTag loreEntry = (StringTag) loreEntryTag;
                String newValue = protocol.getTranslatableRewriter().processText(loreEntry.getValue()).toString();
                if (!changed && !newValue.equals(loreEntry.getValue())) {
                    // Backup original lore before doing any modifications
                    changed = true;
                    saveListTag(display, lore, "Lore");
                }
                loreEntry.setValue(newValue);
            }
        }
    }
    MappedItem data = protocol.getMappingData().getMappedItem(item.identifier());
    if (data == null) {
        // Just rewrite the id
        return super.handleItemToClient(item);
    }
    if (item.tag() == null) {
        item.setTag(new CompoundTag());
    }
    // Save original id, set remapped id
    item.tag().put(nbtTagName + "|id", new IntTag(item.identifier()));
    item.setIdentifier(data.getId());
    // Set custom name - only done if there is no original one
    if (display == null) {
        item.tag().put("display", display = new CompoundTag());
    }
    if (!display.contains("Name")) {
        display.put("Name", new StringTag(data.getJsonName()));
        display.put(nbtTagName + "|customName", new ByteTag());
    }
    return item;
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ByteTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag) MappedItem(com.viaversion.viabackwards.api.data.MappedItem) 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) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) ByteTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 7 with ListTag

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

the class LegacyEnchantmentRewriter method rewriteEnchantmentsToClient.

public void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnchant) {
    String key = storedEnchant ? "StoredEnchantments" : "ench";
    ListTag enchantments = tag.get(key);
    ListTag remappedEnchantments = new ListTag(CompoundTag.class);
    List<Tag> lore = new ArrayList<>();
    for (Tag enchantmentEntry : enchantments.clone()) {
        Tag idTag = ((CompoundTag) enchantmentEntry).get("id");
        if (idTag == null)
            continue;
        short newId = ((NumberTag) idTag).asShort();
        String enchantmentName = enchantmentMappings.get(newId);
        if (enchantmentName != null) {
            enchantments.remove(enchantmentEntry);
            short level = ((NumberTag) ((CompoundTag) enchantmentEntry).get("lvl")).asShort();
            if (hideLevelForEnchants != null && hideLevelForEnchants.contains(newId)) {
                lore.add(new StringTag(enchantmentName));
            } else {
                lore.add(new StringTag(enchantmentName + " " + EnchantmentRewriter.getRomanNumber(level)));
            }
            remappedEnchantments.add(enchantmentEntry);
        }
    }
    if (!lore.isEmpty()) {
        if (!storedEnchant && enchantments.size() == 0) {
            CompoundTag dummyEnchantment = new CompoundTag();
            dummyEnchantment.put("id", new ShortTag((short) 0));
            dummyEnchantment.put("lvl", new ShortTag((short) 0));
            enchantments.add(dummyEnchantment);
            tag.put(nbtTagName + "|dummyEnchant", new ByteTag());
            IntTag hideFlags = tag.get("HideFlags");
            if (hideFlags == null) {
                hideFlags = new IntTag();
            } else {
                tag.put(nbtTagName + "|oldHideFlags", new IntTag(hideFlags.asByte()));
            }
            int flags = hideFlags.asByte() | 1;
            hideFlags.setValue(flags);
            tag.put("HideFlags", hideFlags);
        }
        tag.put(nbtTagName + "|" + key, remappedEnchantments);
        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));
        }
        lore.addAll(loreTag.getValue());
        loreTag.setValue(lore);
    }
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ArrayList(java.util.ArrayList) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) ShortTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag) ByteTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag) 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) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) ByteTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)

Example 8 with ListTag

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

the class LegacyEnchantmentRewriter method rewriteEnchantmentsToServer.

public void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnchant) {
    String key = storedEnchant ? "StoredEnchantments" : "ench";
    ListTag remappedEnchantments = tag.remove(nbtTagName + "|" + key);
    ListTag enchantments = tag.get(key);
    if (enchantments == null) {
        enchantments = new ListTag(CompoundTag.class);
    }
    if (!storedEnchant && tag.remove(nbtTagName + "|dummyEnchant") != null) {
        for (Tag enchantment : enchantments.clone()) {
            short id = ((NumberTag) ((CompoundTag) enchantment).get("id")).asShort();
            short level = ((NumberTag) ((CompoundTag) enchantment).get("lvl")).asShort();
            if (id == 0 && level == 0) {
                enchantments.remove(enchantment);
            }
        }
        IntTag hideFlags = tag.remove(nbtTagName + "|oldHideFlags");
        if (hideFlags != null) {
            tag.put("HideFlags", new IntTag(hideFlags.asByte()));
        } else {
            tag.remove("HideFlags");
        }
    }
    CompoundTag display = tag.get("display");
    // A few null checks just to be safe, though they shouldn't actually be
    ListTag lore = display != null ? display.get("Lore") : null;
    for (Tag enchantment : remappedEnchantments.clone()) {
        enchantments.add(enchantment);
        if (lore != null && lore.size() != 0) {
            lore.remove(lore.get(0));
        }
    }
    if (lore != null && lore.size() == 0) {
        display.remove("Lore");
        if (display.isEmpty()) {
            tag.remove("display");
        }
    }
    tag.put(key, enchantments);
}
Also used : 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) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) ByteTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)

Example 9 with ListTag

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

the class BannerHandler method transform.

@Override
public CompoundTag transform(UserConnection user, int blockId, CompoundTag tag) {
    // Normal banners
    if (blockId >= BANNER_START && blockId <= BANNER_STOP) {
        int color = (blockId - BANNER_START) >> 4;
        tag.put("Base", new IntTag((15 - color)));
    } else // Wall banners
    if (blockId >= WALL_BANNER_START && blockId <= WALL_BANNER_STOP) {
        int color = (blockId - WALL_BANNER_START) >> 2;
        tag.put("Base", new IntTag((15 - color)));
    } else {
        ViaBackwards.getPlatform().getLogger().warning("Why does this block have the banner block entity? :(" + tag);
    }
    // Invert colors
    Tag patternsTag = tag.get("Patterns");
    if (patternsTag instanceof ListTag) {
        for (Tag pattern : (ListTag) patternsTag) {
            if (!(pattern instanceof CompoundTag))
                continue;
            IntTag c = ((CompoundTag) pattern).get("Color");
            // Invert color id
            c.setValue(15 - c.asInt());
        }
    }
    return tag;
}
Also used : Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 10 with ListTag

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

the class BlockItemPackets1_13 method rewriteCanPlaceToClient.

private void rewriteCanPlaceToClient(CompoundTag tag, String tagName) {
    // The tag was manually created incorrectly so ignore rewriting it
    if (!(tag.get(tagName) instanceof ListTag))
        return;
    ListTag blockTag = tag.get(tagName);
    if (blockTag == null)
        return;
    ListTag newCanPlaceOn = new ListTag(StringTag.class);
    tag.put(extraNbtTag + "|" + tagName, ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(blockTag)));
    for (Tag oldTag : blockTag) {
        Object value = oldTag.getValue();
        String[] newValues = value instanceof String ? BlockIdData.fallbackReverseMapping.get(((String) value).replace("minecraft:", "")) : null;
        if (newValues != null) {
            for (String newValue : newValues) {
                newCanPlaceOn.add(new StringTag(newValue));
            }
        } else {
            newCanPlaceOn.add(oldTag);
        }
    }
    tag.put(tagName, newCanPlaceOn);
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag) ShortTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) ByteTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ListTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag)

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