Search in sources :

Example 1 with ByteTag

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

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

the class LegacyBlockItemRewriter method handleItemToClient.

@Override
@Nullable
public Item handleItemToClient(@Nullable Item item) {
    if (item == null)
        return null;
    MappedLegacyBlockItem data = replacementData.get(item.identifier());
    if (data == null) {
        // Just rewrite the id
        return super.handleItemToClient(item);
    }
    short originalData = item.data();
    item.setIdentifier(data.getId());
    // Keep original data if mapped data is set to -1
    if (data.getData() != -1) {
        item.setData(data.getData());
    }
    // Set display name
    if (data.getName() != null) {
        if (item.tag() == null) {
            item.setTag(new CompoundTag());
        }
        CompoundTag display = item.tag().get("display");
        if (display == null) {
            item.tag().put("display", display = new CompoundTag());
        }
        StringTag nameTag = display.get("Name");
        if (nameTag == null) {
            display.put("Name", nameTag = new StringTag(data.getName()));
            display.put(nbtTagName + "|customName", new ByteTag());
        }
        // Handle colors
        String value = nameTag.getValue();
        if (value.contains("%vb_color%")) {
            display.put("Name", new StringTag(value.replace("%vb_color%", BlockColors.get(originalData))));
        }
    }
    return item;
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) MappedLegacyBlockItem(com.viaversion.viabackwards.api.data.MappedLegacyBlockItem) ByteTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 3 with ByteTag

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

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

the class SkullHandler method transform.

@Override
public CompoundTag transform(UserConnection user, int blockId, CompoundTag tag) {
    int diff = blockId - SKULL_START;
    int pos = diff % 20;
    byte type = (byte) Math.floor(diff / 20f);
    // Set type
    tag.put("SkullType", new ByteTag(type));
    // Remove wall skulls
    if (pos < 4) {
        return tag;
    }
    // Add rotation for normal skulls
    tag.put("Rot", new ByteTag((byte) ((pos - 4) & 255)));
    return tag;
}
Also used : ByteTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag)

Example 5 with ByteTag

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

ByteTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag)5 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)4 StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)4 IntTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)3 ListTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag)3 Tag (com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag)3 NumberTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag)2 ShortTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag)2 ArrayList (java.util.ArrayList)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 MappedItem (com.viaversion.viabackwards.api.data.MappedItem)1 MappedLegacyBlockItem (com.viaversion.viabackwards.api.data.MappedLegacyBlockItem)1