Search in sources :

Example 11 with IntTag

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

Example 12 with IntTag

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

the class BlockItemPackets1_13 method invertShieldAndBannerId.

private void invertShieldAndBannerId(Item item, CompoundTag tag) {
    if (item.identifier() != 442 && item.identifier() != 425)
        return;
    Tag blockEntityTag = tag.get("BlockEntityTag");
    if (!(blockEntityTag instanceof CompoundTag))
        return;
    CompoundTag blockEntityCompoundTag = (CompoundTag) blockEntityTag;
    Tag base = blockEntityCompoundTag.get("Base");
    if (base instanceof IntTag) {
        IntTag baseTag = (IntTag) base;
        // invert color id
        baseTag.setValue(15 - baseTag.asInt());
    }
    Tag patterns = blockEntityCompoundTag.get("Patterns");
    if (patterns instanceof ListTag) {
        ListTag patternsTag = (ListTag) patterns;
        for (Tag pattern : patternsTag) {
            if (!(pattern instanceof CompoundTag))
                continue;
            IntTag colorTag = ((CompoundTag) pattern).get("Color");
            // Invert color id
            colorTag.setValue(15 - colorTag.asInt());
        }
    }
}
Also used : 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) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)

Example 13 with IntTag

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

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

Example 15 with IntTag

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

the class EntityPackets1_17 method reduceExtendedHeight.

private void reduceExtendedHeight(CompoundTag tag, boolean warn) {
    IntTag minY = tag.get("min_y");
    IntTag height = tag.get("height");
    IntTag logicalHeight = tag.get("logical_height");
    if (minY.asInt() != 0 || height.asInt() > 256 || logicalHeight.asInt() > 256) {
        if (warn && !warned) {
            ViaBackwards.getPlatform().getLogger().warning("Custom worlds heights are NOT SUPPORTED for 1.16 players and older and may lead to errors!");
            ViaBackwards.getPlatform().getLogger().warning("You have min/max set to " + minY.asInt() + "/" + height.asInt());
            warned = true;
        }
        height.setValue(Math.min(256, height.asInt()));
        logicalHeight.setValue(Math.min(256, logicalHeight.asInt()));
    }
}
Also used : IntTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)

Aggregations

IntTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)16 CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)13 StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)12 Tag (com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag)10 ByteTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag)9 ListTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag)9 NumberTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag)8 ShortTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag)7 ArrayList (java.util.ArrayList)3 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)2 MappedItem (com.viaversion.viabackwards.api.data.MappedItem)1 MappedLegacyBlockItem (com.viaversion.viabackwards.api.data.MappedLegacyBlockItem)1 ItemRewriter (com.viaversion.viabackwards.api.rewriters.ItemRewriter)1 Protocol1_17_1To1_18 (com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.Protocol1_17_1To1_18)1 BlockEntityIds (com.viaversion.viabackwards.protocol.protocol1_17_1to1_18.data.BlockEntityIds)1 Block (com.viaversion.viabackwards.utils.Block)1 ParticleMappings (com.viaversion.viaversion.api.data.ParticleMappings)1 EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)1 Position (com.viaversion.viaversion.api.minecraft.Position)1 BlockEntity (com.viaversion.viaversion.api.minecraft.blockentity.BlockEntity)1