Search in sources :

Example 6 with StringTag

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

the class BlockItemPackets1_13 method handleItemToServer.

@Override
public Item handleItemToServer(Item item) {
    if (item == null)
        return null;
    CompoundTag tag = item.tag();
    // Save original id
    int originalId = (item.identifier() << 16 | item.data() & 0xFFFF);
    int rawId = (item.identifier() << 4 | item.data() & 0xF);
    // NBT Additions
    if (isDamageable(item.identifier())) {
        if (tag == null)
            item.setTag(tag = new CompoundTag());
        tag.put("Damage", new IntTag(item.data()));
    }
    if (item.identifier() == 358) {
        // map
        if (tag == null)
            item.setTag(tag = new CompoundTag());
        tag.put("map", new IntTag(item.data()));
    }
    // NBT Changes
    if (tag != null) {
        // Shield and banner
        invertShieldAndBannerId(item, tag);
        // Display Name now uses JSON
        Tag display = tag.get("display");
        if (display instanceof CompoundTag) {
            CompoundTag displayTag = (CompoundTag) display;
            StringTag name = displayTag.get("Name");
            if (name != null) {
                StringTag via = displayTag.remove(extraNbtTag + "|Name");
                name.setValue(via != null ? via.getValue() : ChatRewriter.legacyTextToJsonString(name.getValue()));
            }
        }
        // ench is now Enchantments and now uses identifiers
        rewriteEnchantmentsToServer(tag, false);
        rewriteEnchantmentsToServer(tag, true);
        rewriteCanPlaceToServer(tag, "CanPlaceOn");
        rewriteCanPlaceToServer(tag, "CanDestroy");
        // Handle SpawnEggs
        if (item.identifier() == 383) {
            CompoundTag entityTag = tag.get("EntityTag");
            StringTag identifier;
            if (entityTag != null && (identifier = entityTag.get("id")) != null) {
                rawId = SpawnEggRewriter.getSpawnEggId(identifier.getValue());
                if (rawId == -1) {
                    // Bat fallback
                    rawId = 25100288;
                } else {
                    entityTag.remove("id");
                    if (entityTag.isEmpty()) {
                        tag.remove("EntityTag");
                    }
                }
            } else {
                // Fallback to bat
                rawId = 25100288;
            }
        }
        if (tag.isEmpty()) {
            item.setTag(tag = null);
        }
    }
    // Handle custom mappings
    int identifier = item.identifier();
    item.setIdentifier(rawId);
    super.handleItemToServer(item);
    // Mapped with original data, we can return here
    if (item.identifier() != rawId && item.identifier() != -1)
        return item;
    // Set to legacy id again
    item.setIdentifier(identifier);
    int newId = -1;
    if (!protocol.getMappingData().getItemMappings().inverse().containsKey(rawId)) {
        if (!isDamageable(item.identifier()) && item.identifier() != 358) {
            // Map
            if (tag == null)
                item.setTag(tag = new CompoundTag());
            // Data will be lost, saving original id
            tag.put(extraNbtTag, new IntTag(originalId));
        }
        if (item.identifier() == 229) {
            // purple shulker box
            // directly set the new id -> base/colorless shulker box
            newId = 362;
        } else if (item.identifier() == 31 && item.data() == 0) {
            // Shrub was removed
            // Dead Bush
            rawId = 32 << 4;
        } else if (protocol.getMappingData().getItemMappings().inverse().containsKey(rawId & ~0xF)) {
            // Remove data
            rawId &= ~0xF;
        } else {
            if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                ViaBackwards.getPlatform().getLogger().warning("Failed to get 1.13 item for " + item.identifier());
            }
            // Stone
            rawId = 16;
        }
    }
    if (newId == -1) {
        newId = protocol.getMappingData().getItemMappings().inverse().get(rawId);
    }
    item.setIdentifier(newId);
    item.setData((short) 0);
    return item;
}
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) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)

Example 7 with StringTag

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

the class BlockItemPackets1_13 method itemIdToRaw.

private int itemIdToRaw(int oldId, Item item, CompoundTag tag) {
    Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId);
    if (eggEntityId.isPresent()) {
        if (tag == null) {
            item.setTag(tag = new CompoundTag());
        }
        if (!tag.contains("EntityTag")) {
            CompoundTag entityTag = new CompoundTag();
            entityTag.put("id", new StringTag(eggEntityId.get()));
            tag.put("EntityTag", entityTag);
        }
        // 383 << 16;
        return 0x17f0000;
    }
    return (oldId >> 4) << 16 | oldId & 0xF;
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 8 with StringTag

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

the class LegacyBlockItemRewriter method getNamedTag.

protected CompoundTag getNamedTag(String text) {
    CompoundTag tag = new CompoundTag();
    tag.put("display", new CompoundTag());
    text = "§r" + text;
    ((CompoundTag) tag.get("display")).put("Name", new StringTag(jsonNameFormat ? ChatRewriter.legacyTextToJsonString(text) : text));
    return tag;
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 9 with StringTag

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

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

Aggregations

StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)28 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)27 Tag (com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag)22 ListTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag)16 IntTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)13 ByteTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag)8 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)5 Type (com.viaversion.viaversion.api.type.Type)4 IntArrayTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.IntArrayTag)4 ArrayList (java.util.ArrayList)4 LongArrayTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.LongArrayTag)3 UUID (java.util.UUID)3 MappedLegacyBlockItem (com.viaversion.viabackwards.api.data.MappedLegacyBlockItem)2 EntityRewriter (com.viaversion.viabackwards.api.rewriters.EntityRewriter)2 Protocol1_17_1To1_18 (com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.Protocol1_17_1To1_18)2 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)2 Entity1_17Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_17Types)2 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)2