Search in sources :

Example 11 with StringTag

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

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

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

the class PistonHandler method getDataFromTag.

// The type hasn't actually been updated in the blockstorage, so we need to construct it
private String getDataFromTag(CompoundTag tag) {
    StringTag name = tag.get("Name");
    if (name == null)
        return null;
    CompoundTag properties = tag.get("Properties");
    if (properties == null)
        return name.getValue();
    StringJoiner joiner = new StringJoiner(",", name.getValue() + "[", "]");
    for (Map.Entry<String, Tag> entry : properties) {
        if (!(entry.getValue() instanceof StringTag))
            continue;
        joiner.add(entry.getKey() + "=" + ((StringTag) entry.getValue()).getValue());
    }
    return joiner.toString();
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag) HashMap(java.util.HashMap) Map(java.util.Map) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) StringJoiner(java.util.StringJoiner)

Example 14 with StringTag

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

the class SpawnerHandler method transform.

@Override
public CompoundTag transform(UserConnection user, int blockId, CompoundTag tag) {
    Tag dataTag = tag.get("SpawnData");
    if (dataTag instanceof CompoundTag) {
        CompoundTag data = (CompoundTag) dataTag;
        Tag idTag = data.get("id");
        if (idTag instanceof StringTag) {
            StringTag s = (StringTag) idTag;
            s.setValue(EntityNameRewrites.rewrite(s.getValue()));
        }
    }
    return tag;
}
Also used : StringTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag) 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) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 15 with StringTag

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

the class BlockItemPackets1_13 method handleItemToClient.

@Override
public Item handleItemToClient(Item item) {
    if (item == null)
        return null;
    // Custom mappings/super call moved down
    int originalId = item.identifier();
    Integer rawId = null;
    boolean gotRawIdFromTag = false;
    CompoundTag tag = item.tag();
    // Use tag to get original ID and data
    Tag originalIdTag;
    if (tag != null && (originalIdTag = tag.remove(extraNbtTag)) != null) {
        rawId = ((NumberTag) originalIdTag).asInt();
        gotRawIdFromTag = true;
    }
    if (rawId == null) {
        // Look for custom mappings
        super.handleItemToClient(item);
        // Handle one-way special case
        if (item.identifier() == -1) {
            if (originalId == 362) {
                // base/colorless shulker box
                // purple shulker box
                rawId = 0xe50000;
            } else {
                if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                    ViaBackwards.getPlatform().getLogger().warning("Failed to get 1.12 item for " + originalId);
                }
                rawId = 0x10000;
            }
        } else {
            // Take the newly added tag
            if (tag == null) {
                tag = item.tag();
            }
            rawId = itemIdToRaw(item.identifier(), item, tag);
        }
    }
    item.setIdentifier(rawId >> 16);
    item.setData((short) (rawId & 0xFFFF));
    // NBT changes
    if (tag != null) {
        if (isDamageable(item.identifier())) {
            Tag damageTag = tag.remove("Damage");
            if (!gotRawIdFromTag && damageTag instanceof IntTag) {
                item.setData((short) (int) damageTag.getValue());
            }
        }
        if (item.identifier() == 358) {
            // map
            Tag mapTag = tag.remove("map");
            if (!gotRawIdFromTag && mapTag instanceof IntTag) {
                item.setData((short) (int) mapTag.getValue());
            }
        }
        // Shield and banner
        invertShieldAndBannerId(item, tag);
        // Display Name now uses JSON
        CompoundTag display = tag.get("display");
        if (display != null) {
            StringTag name = display.get("Name");
            if (name != null) {
                display.put(extraNbtTag + "|Name", new StringTag(name.getValue()));
                name.setValue(ChatRewriter.jsonToLegacyText(name.getValue()));
            }
        }
        // ench is now Enchantments and now uses identifiers
        rewriteEnchantmentsToClient(tag, false);
        rewriteEnchantmentsToClient(tag, true);
        rewriteCanPlaceToClient(tag, "CanPlaceOn");
        rewriteCanPlaceToClient(tag, "CanDestroy");
    }
    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)

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