Search in sources :

Example 1 with ListTag

use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.

the class FireworkRocketTranslator method translateToJava.

@Override
public void translateToJava(CompoundTag itemTag, ItemMapping mapping) {
    CompoundTag fireworks = itemTag.get("Fireworks");
    if (fireworks == null) {
        return;
    }
    if (fireworks.contains("Flight")) {
        fireworks.put(new ByteTag("Flight", MathUtils.getNbtByte(fireworks.get("Flight").getValue())));
    }
    ListTag explosions = fireworks.get("Explosions");
    if (explosions == null) {
        return;
    }
    for (Tag effect : explosions.getValue()) {
        CompoundTag effectData = (CompoundTag) effect;
        CompoundTag newEffectData = translateExplosionToJava(effectData, "");
        explosions.remove(effect);
        explosions.add(newEffectData);
    }
}
Also used : ByteTag(com.github.steveice10.opennbt.tag.builtin.ByteTag) ByteTag(com.github.steveice10.opennbt.tag.builtin.ByteTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 2 with ListTag

use of com.github.steveice10.opennbt.tag.builtin.ListTag in project Geyser by GeyserMC.

the class CampfireBlockEntityTranslator method translateTag.

@Override
public void translateTag(NbtMapBuilder builder, CompoundTag tag, int blockState) {
    ListTag items = tag.get("Items");
    int i = 1;
    for (com.github.steveice10.opennbt.tag.builtin.Tag itemTag : items.getValue()) {
        builder.put("Item" + i, getItem((CompoundTag) itemTag));
        i++;
    }
}
Also used : ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 3 with ListTag

use of com.github.steveice10.opennbt.tag.builtin.ListTag in project ViaVersion by ViaVersion.

the class InventoryPackets method handleItemToClient.

@Override
public Item handleItemToClient(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) {
        // Invert banner/shield color id
        boolean banner = item.identifier() == 425;
        if (banner || item.identifier() == 442) {
            if (tag.get("BlockEntityTag") instanceof CompoundTag) {
                CompoundTag blockEntityTag = tag.get("BlockEntityTag");
                if (blockEntityTag.get("Base") instanceof IntTag) {
                    IntTag base = blockEntityTag.get("Base");
                    // Set banner item id according to nbt
                    if (banner) {
                        rawId = 6800 + base.asInt();
                    }
                    base.setValue(15 - base.asInt());
                }
                if (blockEntityTag.get("Patterns") instanceof ListTag) {
                    for (Tag pattern : (ListTag) blockEntityTag.get("Patterns")) {
                        if (pattern instanceof CompoundTag) {
                            IntTag c = ((CompoundTag) pattern).get("Color");
                            // Invert color id
                            c.setValue(15 - c.asInt());
                        }
                    }
                }
            }
        }
        // Display Name now uses JSON
        if (tag.get("display") instanceof CompoundTag) {
            CompoundTag display = tag.get("display");
            if (display.get("Name") instanceof StringTag) {
                StringTag name = display.get("Name");
                display.put(NBT_TAG_NAME + "|Name", new StringTag(name.getValue()));
                name.setValue(ChatRewriter.legacyTextToJsonString(name.getValue(), true));
            }
        }
        // ench is now Enchantments and now uses identifiers
        if (tag.get("ench") instanceof ListTag) {
            ListTag ench = tag.get("ench");
            ListTag enchantments = new ListTag(CompoundTag.class);
            for (Tag enchEntry : ench) {
                NumberTag idTag;
                if (enchEntry instanceof CompoundTag && (idTag = ((CompoundTag) enchEntry).get("id")) != null) {
                    CompoundTag enchantmentEntry = new CompoundTag();
                    short oldId = idTag.asShort();
                    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(((NumberTag) ((CompoundTag) enchEntry).get("lvl")).asShort()));
                    enchantments.add(enchantmentEntry);
                }
            }
            tag.remove("ench");
            tag.put("Enchantments", enchantments);
        }
        if (tag.get("StoredEnchantments") instanceof ListTag) {
            ListTag storedEnch = tag.get("StoredEnchantments");
            ListTag newStoredEnch = new ListTag(CompoundTag.class);
            for (Tag enchEntry : storedEnch) {
                if (enchEntry instanceof CompoundTag) {
                    CompoundTag enchantmentEntry = new CompoundTag();
                    short oldId = ((NumberTag) ((CompoundTag) enchEntry).get("id")).asShort();
                    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(((NumberTag) ((CompoundTag) enchEntry).get("lvl")).asShort()));
                    newStoredEnch.add(enchantmentEntry);
                }
            }
            tag.remove("StoredEnchantments");
            tag.put("StoredEnchantments", newStoredEnch);
        }
        if (tag.get("CanPlaceOn") instanceof ListTag) {
            ListTag old = tag.get("CanPlaceOn");
            ListTag newCanPlaceOn = new ListTag(StringTag.class);
            // There will be data losing
            tag.put(NBT_TAG_NAME + "|CanPlaceOn", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(old)));
            for (Tag oldTag : old) {
                Object value = oldTag.getValue();
                String oldId = value.toString().replace("minecraft:", "");
                String numberConverted = BlockIdData.numberIdToString.get(Ints.tryParse(oldId));
                if (numberConverted != null) {
                    oldId = numberConverted;
                }
                String[] newValues = BlockIdData.blockIdMapping.get(oldId.toLowerCase(Locale.ROOT));
                if (newValues != null) {
                    for (String newValue : newValues) {
                        newCanPlaceOn.add(new StringTag(newValue));
                    }
                } else {
                    newCanPlaceOn.add(new StringTag(oldId.toLowerCase(Locale.ROOT)));
                }
            }
            tag.put("CanPlaceOn", newCanPlaceOn);
        }
        if (tag.get("CanDestroy") instanceof ListTag) {
            ListTag old = tag.get("CanDestroy");
            ListTag newCanDestroy = new ListTag(StringTag.class);
            // There will be data losing
            tag.put(NBT_TAG_NAME + "|CanDestroy", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(old)));
            for (Tag oldTag : old) {
                Object value = oldTag.getValue();
                String oldId = value.toString().replace("minecraft:", "");
                String numberConverted = BlockIdData.numberIdToString.get(Ints.tryParse(oldId));
                if (numberConverted != null) {
                    oldId = numberConverted;
                }
                String[] newValues = BlockIdData.blockIdMapping.get(oldId.toLowerCase(Locale.ROOT));
                if (newValues != null) {
                    for (String newValue : newValues) {
                        newCanDestroy.add(new StringTag(newValue));
                    }
                } else {
                    newCanDestroy.add(new StringTag(oldId.toLowerCase(Locale.ROOT)));
                }
            }
            tag.put("CanDestroy", newCanDestroy);
        }
        // Handle SpawnEggs
        if (item.identifier() == 383) {
            if (tag.get("EntityTag") instanceof CompoundTag) {
                CompoundTag entityTag = tag.get("EntityTag");
                if (entityTag.get("id") instanceof StringTag) {
                    StringTag identifier = entityTag.get("id");
                    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;
                }
            } else {
                // Fallback to bat
                rawId = 25100288;
            }
        }
        if (tag.isEmpty()) {
            item.setTag(tag = null);
        }
    }
    if (!Protocol1_13To1_12_2.MAPPINGS.getItemMappings().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(NBT_TAG_NAME, new IntTag(originalId));
        }
        if (item.identifier() == 31 && item.data() == 0) {
            // Shrub was removed
            // Dead Bush
            rawId = 32 << 4;
        } else if (Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId & ~0xF)) {
            // Remove data
            rawId &= ~0xF;
        } else {
            if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                Via.getPlatform().getLogger().warning("Failed to get 1.13 item for " + item.identifier());
            }
            // Stone
            rawId = 16;
        }
    }
    item.setIdentifier(Protocol1_13To1_12_2.MAPPINGS.getItemMappings().get(rawId));
    item.setData((short) 0);
    return item;
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) ShortTag(com.github.steveice10.opennbt.tag.builtin.ShortTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) ShortTag(com.github.steveice10.opennbt.tag.builtin.ShortTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag)

Example 4 with ListTag

use of com.github.steveice10.opennbt.tag.builtin.ListTag in project ViaVersion by ViaVersion.

the class InventoryPackets method handleItemToServer.

@Override
public Item handleItemToServer(Item item) {
    if (item == null)
        return null;
    item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getOldItemId(item.identifier()));
    if (item.tag() == null)
        return item;
    // Display Name now uses JSON
    Tag displayTag = item.tag().get("display");
    if (displayTag instanceof CompoundTag) {
        CompoundTag display = (CompoundTag) displayTag;
        Tag loreTag = display.get("Lore");
        if (loreTag instanceof ListTag) {
            ListTag lore = (ListTag) loreTag;
            ListTag savedLore = display.remove(NBT_TAG_NAME + "|Lore");
            if (savedLore != null) {
                display.put("Lore", new ListTag(savedLore.getValue()));
            } else {
                for (Tag loreEntry : lore) {
                    if (loreEntry instanceof StringTag) {
                        ((StringTag) loreEntry).setValue(ChatRewriter.jsonToLegacyText(((StringTag) loreEntry).getValue()));
                    }
                }
            }
        }
    }
    return item;
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) DoubleTag(com.github.steveice10.opennbt.tag.builtin.DoubleTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 5 with ListTag

use of com.github.steveice10.opennbt.tag.builtin.ListTag in project ViaVersion by ViaVersion.

the class InventoryPackets method handleItemToClient.

@Override
public Item handleItemToClient(Item item) {
    if (item == null)
        return null;
    item.setIdentifier(Protocol1_14To1_13_2.MAPPINGS.getNewItemId(item.identifier()));
    if (item.tag() == null)
        return item;
    // Display Lore now uses JSON
    Tag displayTag = item.tag().get("display");
    if (displayTag instanceof CompoundTag) {
        CompoundTag display = (CompoundTag) displayTag;
        Tag loreTag = display.get("Lore");
        if (loreTag instanceof ListTag) {
            ListTag lore = (ListTag) loreTag;
            // Save old lore
            display.put(NBT_TAG_NAME + "|Lore", new ListTag(lore.clone().getValue()));
            for (Tag loreEntry : lore) {
                if (loreEntry instanceof StringTag) {
                    String jsonText = ChatRewriter.legacyTextToJsonString(((StringTag) loreEntry).getValue(), true);
                    ((StringTag) loreEntry).setValue(jsonText);
                }
            }
        }
    }
    return item;
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) DoubleTag(com.github.steveice10.opennbt.tag.builtin.DoubleTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Aggregations

CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)25 ListTag (com.github.steveice10.opennbt.tag.builtin.ListTag)23 Tag (com.github.steveice10.opennbt.tag.builtin.Tag)21 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)17 IntTag (com.github.steveice10.opennbt.tag.builtin.IntTag)7 NumberTag (com.github.steveice10.opennbt.tag.builtin.NumberTag)7 ByteTag (com.github.steveice10.opennbt.tag.builtin.ByteTag)4 DoubleTag (com.github.steveice10.opennbt.tag.builtin.DoubleTag)4 IntArrayTag (com.github.steveice10.opennbt.tag.builtin.IntArrayTag)4 LongTag (com.github.steveice10.opennbt.tag.builtin.LongTag)4 ShortTag (com.github.steveice10.opennbt.tag.builtin.ShortTag)4 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)4 ItemStack (com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack)3 Type (com.viaversion.viaversion.api.type.Type)3 ArrayList (java.util.ArrayList)3 ByteArrayTag (com.github.steveice10.opennbt.tag.builtin.ByteArrayTag)2 FloatTag (com.github.steveice10.opennbt.tag.builtin.FloatTag)2 LongArrayTag (com.github.steveice10.opennbt.tag.builtin.LongArrayTag)2 NbtMap (com.nukkitx.nbt.NbtMap)2 NbtMapBuilder (com.nukkitx.nbt.NbtMapBuilder)2