Search in sources :

Example 16 with CompoundTag

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

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

the class BlockItemPackets1_12 method handleNbtToClient.

private boolean handleNbtToClient(CompoundTag compoundTag, CompoundTag backupTag) {
    // Long array tags were introduced in 1.12 - just remove them
    // Only save the removed tags instead of blindly copying the entire nbt again
    Iterator<Map.Entry<String, Tag>> iterator = compoundTag.iterator();
    boolean hasLongArrayTag = false;
    while (iterator.hasNext()) {
        Map.Entry<String, Tag> entry = iterator.next();
        if (entry.getValue() instanceof CompoundTag) {
            CompoundTag nestedBackupTag = new CompoundTag();
            backupTag.put(entry.getKey(), nestedBackupTag);
            hasLongArrayTag |= handleNbtToClient((CompoundTag) entry.getValue(), nestedBackupTag);
        } else if (entry.getValue() instanceof LongArrayTag) {
            backupTag.put(entry.getKey(), fromLongArrayTag((LongArrayTag) entry.getValue()));
            iterator.remove();
            hasLongArrayTag = true;
        }
    }
    return hasLongArrayTag;
}
Also used : LongArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.LongArrayTag) Tag(com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag) LongArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.LongArrayTag) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) IntArrayTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.IntArrayTag) Map(java.util.Map) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 18 with CompoundTag

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

the class EntityPackets1_12 method registerRewrites.

@Override
protected void registerRewrites() {
    mapEntityTypeWithData(Entity1_12Types.EntityType.PARROT, Entity1_12Types.EntityType.BAT).plainName().spawnMetadata(storage -> storage.add(new Metadata(12, MetaType1_12.Byte, (byte) 0x00)));
    mapEntityTypeWithData(Entity1_12Types.EntityType.ILLUSION_ILLAGER, Entity1_12Types.EntityType.EVOCATION_ILLAGER).plainName();
    // Handle Illager
    filter().filterFamily(Entity1_12Types.EntityType.EVOCATION_ILLAGER).cancel(12);
    filter().filterFamily(Entity1_12Types.EntityType.EVOCATION_ILLAGER).index(13).toIndex(12);
    filter().type(Entity1_12Types.EntityType.ILLUSION_ILLAGER).index(0).handler((event, meta) -> {
        byte mask = (byte) meta.getValue();
        if ((mask & 0x20) == 0x20) {
            mask &= ~0x20;
        }
        meta.setValue(mask);
    });
    // Create Parrot storage
    filter().filterFamily(Entity1_12Types.EntityType.PARROT).handler((event, meta) -> {
        StoredEntityData data = storedEntityData(event);
        if (!data.has(ParrotStorage.class)) {
            data.put(new ParrotStorage());
        }
    });
    // Parrot remove animal metadata
    // Is baby
    filter().type(Entity1_12Types.EntityType.PARROT).cancel(12);
    filter().type(Entity1_12Types.EntityType.PARROT).index(13).handler((event, meta) -> {
        StoredEntityData data = storedEntityData(event);
        ParrotStorage storage = data.get(ParrotStorage.class);
        boolean isSitting = (((byte) meta.getValue()) & 0x01) == 0x01;
        boolean isTamed = (((byte) meta.getValue()) & 0x04) == 0x04;
        if (!storage.isTamed() && isTamed) {
        // TODO do something to let the user know it's done
        }
        storage.setTamed(isTamed);
        if (isSitting) {
            event.setIndex(12);
            meta.setValue((byte) 0x01);
            storage.setSitting(true);
        } else if (storage.isSitting()) {
            event.setIndex(12);
            meta.setValue((byte) 0x00);
            storage.setSitting(false);
        } else {
            event.cancel();
        }
    });
    // Flags (Is sitting etc, might be useful in the future
    // Owner
    filter().type(Entity1_12Types.EntityType.PARROT).cancel(14);
    // Variant
    filter().type(Entity1_12Types.EntityType.PARROT).cancel(15);
    // Left shoulder entity data
    filter().type(Entity1_12Types.EntityType.PLAYER).index(15).handler((event, meta) -> {
        CompoundTag tag = (CompoundTag) meta.getValue();
        ShoulderTracker tracker = event.user().get(ShoulderTracker.class);
        if (tag.isEmpty() && tracker.getLeftShoulder() != null) {
            tracker.setLeftShoulder(null);
            tracker.update();
        } else if (tag.contains("id") && event.entityId() == tracker.getEntityId()) {
            String id = (String) tag.get("id").getValue();
            if (tracker.getLeftShoulder() == null || !tracker.getLeftShoulder().equals(id)) {
                tracker.setLeftShoulder(id);
                tracker.update();
            }
        }
        event.cancel();
    });
    // Right shoulder entity data
    filter().type(Entity1_12Types.EntityType.PLAYER).index(16).handler((event, meta) -> {
        CompoundTag tag = (CompoundTag) event.meta().getValue();
        ShoulderTracker tracker = event.user().get(ShoulderTracker.class);
        if (tag.isEmpty() && tracker.getRightShoulder() != null) {
            tracker.setRightShoulder(null);
            tracker.update();
        } else if (tag.contains("id") && event.entityId() == tracker.getEntityId()) {
            String id = (String) tag.get("id").getValue();
            if (tracker.getRightShoulder() == null || !tracker.getRightShoulder().equals(id)) {
                tracker.setRightShoulder(id);
                tracker.update();
            }
        }
        event.cancel();
    });
}
Also used : ShoulderTracker(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ShoulderTracker) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag) ParrotStorage(com.viaversion.viabackwards.protocol.protocol1_11_1to1_12.data.ParrotStorage)

Example 19 with CompoundTag

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

the class ItemPackets1_11_1 method handleItemToServer.

@Override
public Item handleItemToServer(Item item) {
    if (item == null)
        return null;
    super.handleItemToServer(item);
    CompoundTag tag = item.tag();
    if (tag == null)
        return item;
    if (tag.contains(nbtTagName + "|ench")) {
        enchantmentRewriter.rewriteEnchantmentsToServer(tag, false);
    }
    if (tag.contains(nbtTagName + "|StoredEnchantments")) {
        enchantmentRewriter.rewriteEnchantmentsToServer(tag, true);
    }
    return item;
}
Also used : CompoundTag(com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)

Example 20 with CompoundTag

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

CompoundTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.CompoundTag)48 StringTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.StringTag)36 Tag (com.viaversion.viaversion.libs.opennbt.tag.builtin.Tag)34 ListTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ListTag)25 IntTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.IntTag)18 ByteTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ByteTag)12 NumberTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.NumberTag)12 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)11 Type (com.viaversion.viaversion.api.type.Type)10 ShortTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.ShortTag)10 IntArrayTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.IntArrayTag)8 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)7 LongArrayTag (com.viaversion.viaversion.libs.opennbt.tag.builtin.LongArrayTag)7 ArrayList (java.util.ArrayList)7 Chunk (com.viaversion.viaversion.api.minecraft.chunks.Chunk)6 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)6 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)4 BlockChangeRecord (com.viaversion.viaversion.api.minecraft.BlockChangeRecord)4 Item (com.viaversion.viaversion.api.minecraft.item.Item)4 ClientboundPackets1_16_2 (com.viaversion.viaversion.protocols.protocol1_16_2to1_16_1.ClientboundPackets1_16_2)4