Search in sources :

Example 1 with ShortTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag 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)

Example 2 with ShortTag

use of com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag 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 3 with ShortTag

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

the class BlockItemPackets1_13 method rewriteEnchantmentsToServer.

private void rewriteEnchantmentsToServer(CompoundTag tag, boolean storedEnch) {
    String key = storedEnch ? "StoredEnchantments" : "Enchantments";
    ListTag enchantments = tag.get(storedEnch ? key : "ench");
    if (enchantments == null)
        return;
    ListTag newEnchantments = new ListTag(CompoundTag.class);
    boolean dummyEnchant = false;
    if (!storedEnch) {
        IntTag hideFlags = tag.remove(extraNbtTag + "|OldHideFlags");
        if (hideFlags != null) {
            tag.put("HideFlags", new IntTag(hideFlags.asByte()));
            dummyEnchant = true;
        } else if (tag.remove(extraNbtTag + "|DummyEnchant") != null) {
            tag.remove("HideFlags");
            dummyEnchant = true;
        }
    }
    for (Tag enchEntry : enchantments) {
        CompoundTag enchantmentEntry = new CompoundTag();
        short oldId = ((NumberTag) ((CompoundTag) enchEntry).get("id")).asShort();
        short level = ((NumberTag) ((CompoundTag) enchEntry).get("lvl")).asShort();
        if (dummyEnchant && oldId == 0 && level == 0) {
            // Skip dummy enchatment
            continue;
        }
        String newId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().get(oldId);
        if (newId == null) {
            newId = "viaversion:legacy/" + oldId;
        }
        enchantmentEntry.put("id", new StringTag(newId));
        enchantmentEntry.put("lvl", new ShortTag(level));
        newEnchantments.add(enchantmentEntry);
    }
    ListTag noMapped = tag.remove(extraNbtTag + "|Enchantments");
    if (noMapped != null) {
        for (Tag value : noMapped) {
            newEnchantments.add(value);
        }
    }
    CompoundTag display = tag.get("display");
    if (display == null) {
        tag.put("display", display = new CompoundTag());
    }
    ListTag oldLore = tag.remove(extraNbtTag + "|OldLore");
    if (oldLore != null) {
        ListTag lore = display.get("Lore");
        if (lore == null) {
            tag.put("Lore", lore = new ListTag());
        }
        lore.setValue(oldLore.getValue());
    } else if (tag.remove(extraNbtTag + "|DummyLore") != null) {
        display.remove("Lore");
        if (display.isEmpty()) {
            tag.remove("display");
        }
    }
    if (!storedEnch) {
        tag.remove("ench");
    }
    tag.put(key, newEnchantments);
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) NumberTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag) 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) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ShortTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag)

Example 4 with ShortTag

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

the class BlockItemPackets1_13 method rewriteEnchantmentsToClient.

// TODO un-ugly all of this
private void rewriteEnchantmentsToClient(CompoundTag tag, boolean storedEnch) {
    String key = storedEnch ? "StoredEnchantments" : "Enchantments";
    ListTag enchantments = tag.get(key);
    if (enchantments == null)
        return;
    ListTag noMapped = new ListTag(CompoundTag.class);
    ListTag newEnchantments = new ListTag(CompoundTag.class);
    List<Tag> lore = new ArrayList<>();
    boolean hasValidEnchants = false;
    for (Tag enchantmentEntryTag : enchantments.clone()) {
        CompoundTag enchantmentEntry = (CompoundTag) enchantmentEntryTag;
        Tag idTag = enchantmentEntry.get("id");
        if (!(idTag instanceof StringTag))
            continue;
        String newId = (String) idTag.getValue();
        int levelValue = ((NumberTag) enchantmentEntry.get("lvl")).asInt();
        short level = levelValue < Short.MAX_VALUE ? (short) levelValue : Short.MAX_VALUE;
        String mappedEnchantmentId = enchantmentMappings.get(newId);
        if (mappedEnchantmentId != null) {
            lore.add(new StringTag(mappedEnchantmentId + " " + EnchantmentRewriter.getRomanNumber(level)));
            noMapped.add(enchantmentEntry);
        } else if (!newId.isEmpty()) {
            Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId);
            if (oldId == null) {
                if (!newId.startsWith("viaversion:legacy/")) {
                    // Custom enchant (?)
                    noMapped.add(enchantmentEntry);
                    // Some custom-enchant plugins write it into the lore manually, which would double its entry
                    if (ViaBackwards.getConfig().addCustomEnchantsToLore()) {
                        String name = newId;
                        int index = name.indexOf(':') + 1;
                        if (index != 0 && index != name.length()) {
                            name = name.substring(index);
                        }
                        name = "ยง7" + Character.toUpperCase(name.charAt(0)) + name.substring(1).toLowerCase(Locale.ENGLISH);
                        lore.add(new StringTag(name + " " + EnchantmentRewriter.getRomanNumber(level)));
                    }
                    if (Via.getManager().isDebug()) {
                        ViaBackwards.getPlatform().getLogger().warning("Found unknown enchant: " + newId);
                    }
                    continue;
                } else {
                    oldId = Short.valueOf(newId.substring(18));
                }
            }
            if (level != 0) {
                hasValidEnchants = true;
            }
            CompoundTag newEntry = new CompoundTag();
            newEntry.put("id", new ShortTag(oldId));
            newEntry.put("lvl", new ShortTag(level));
            newEnchantments.add(newEntry);
        }
    }
    // Put here to hide empty enchantment from 1.14 rewrites
    if (!storedEnch && !hasValidEnchants) {
        IntTag hideFlags = tag.get("HideFlags");
        if (hideFlags == null) {
            hideFlags = new IntTag();
            tag.put(extraNbtTag + "|DummyEnchant", new ByteTag());
        } else {
            tag.put(extraNbtTag + "|OldHideFlags", new IntTag(hideFlags.asByte()));
        }
        if (newEnchantments.size() == 0) {
            CompoundTag enchEntry = new CompoundTag();
            enchEntry.put("id", new ShortTag((short) 0));
            enchEntry.put("lvl", new ShortTag((short) 0));
            newEnchantments.add(enchEntry);
        }
        int value = hideFlags.asByte() | 1;
        hideFlags.setValue(value);
        tag.put("HideFlags", hideFlags);
    }
    if (noMapped.size() != 0) {
        tag.put(extraNbtTag + "|" + key, noMapped);
        if (!lore.isEmpty()) {
            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));
                tag.put(extraNbtTag + "|DummyLore", new ByteTag());
            } else if (loreTag.size() != 0) {
                ListTag oldLore = new ListTag(StringTag.class);
                for (Tag value : loreTag) {
                    oldLore.add(value.clone());
                }
                tag.put(extraNbtTag + "|OldLore", oldLore);
                lore.addAll(loreTag.getValue());
            }
            loreTag.setValue(lore);
        }
    }
    tag.remove("Enchantments");
    tag.put(storedEnch ? key : "ench", newEnchantments);
}
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) 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) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)

Aggregations

CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)4 ListTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag)4 NumberTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag)4 ShortTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag)4 StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)4 Tag (com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag)4 ByteTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag)3 IntTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)3 ArrayList (java.util.ArrayList)3