Search in sources :

Example 16 with ListTag

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

the class EnchantedBookTranslator method translateToBedrock.

@Override
public void translateToBedrock(GeyserSession session, CompoundTag itemTag, ItemMapping mapping) {
    if (!itemTag.contains("StoredEnchantments")) {
        return;
    }
    Tag enchTag = itemTag.get("StoredEnchantments");
    if (enchTag instanceof ListTag) {
        enchTag = new ListTag("Enchantments", ((ListTag) enchTag).getValue());
        itemTag.remove("StoredEnchantments");
        itemTag.put(enchTag);
    }
}
Also used : 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)

Example 17 with ListTag

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

the class FireworkRocketTranslator method translateToBedrock.

@Override
public void translateToBedrock(GeyserSession session, CompoundTag itemTag, ItemMapping mapping) {
    CompoundTag fireworks = itemTag.get("Fireworks");
    if (fireworks == null) {
        return;
    }
    if (fireworks.get("Flight") != null) {
        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 = translateExplosionToBedrock(effectData, "");
        explosions.remove(effectData);
        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 18 with ListTag

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

the class LecternInventoryTranslator method updateBook.

/**
 * Translate the data of the book in the lectern into a block entity tag.
 */
private void updateBook(GeyserSession session, Inventory inventory, GeyserItemStack book) {
    LecternContainer lecternContainer = (LecternContainer) inventory;
    if (session.isDroppingLecternBook()) {
        // We have to enter the inventory GUI to eject the book
        ServerboundContainerButtonClickPacket packet = new ServerboundContainerButtonClickPacket(inventory.getId(), 3);
        session.sendDownstreamPacket(packet);
        session.setDroppingLecternBook(false);
        InventoryUtils.closeInventory(session, inventory.getId(), false);
    } else if (lecternContainer.getBlockEntityTag() == null) {
        CompoundTag tag = book.getNbt();
        // Position has to be the last interacted position... right?
        Vector3i position = session.getLastInteractionBlockPosition();
        // If shouldExpectLecternHandled returns true, this is already handled for us
        // shouldRefresh means that we should boot out the client on our side because their lectern GUI isn't updated yet
        boolean shouldRefresh = !session.getGeyser().getWorldManager().shouldExpectLecternHandled() && !session.getLecternCache().contains(position);
        NbtMap blockEntityTag;
        if (tag != null) {
            int pagesSize = ((ListTag) tag.get("pages")).size();
            ItemData itemData = book.getItemData(session);
            NbtMapBuilder lecternTag = getBaseLecternTag(position.getX(), position.getY(), position.getZ(), pagesSize);
            lecternTag.putCompound("book", NbtMap.builder().putByte("Count", (byte) itemData.getCount()).putShort("Damage", (short) 0).putString("Name", "minecraft:written_book").putCompound("tag", itemData.getTag()).build());
            lecternTag.putInt("page", lecternContainer.getCurrentBedrockPage());
            blockEntityTag = lecternTag.build();
        } else {
            // There is *a* book here, but... no NBT.
            NbtMapBuilder lecternTag = getBaseLecternTag(position.getX(), position.getY(), position.getZ(), 1);
            NbtMapBuilder bookTag = NbtMap.builder().putByte("Count", (byte) 1).putShort("Damage", (short) 0).putString("Name", "minecraft:writable_book").putCompound("tag", NbtMap.builder().putList("pages", NbtType.COMPOUND, Collections.singletonList(NbtMap.builder().putString("photoname", "").putString("text", "").build())).build());
            blockEntityTag = lecternTag.putCompound("book", bookTag.build()).build();
        }
        // Even with serverside access to lecterns, we don't easily know which lectern this is, so we need to rebuild
        // the block entity tag
        lecternContainer.setBlockEntityTag(blockEntityTag);
        lecternContainer.setPosition(position);
        if (shouldRefresh) {
            // Update the lectern because it's not updated client-side
            BlockEntityUtils.updateBlockEntity(session, blockEntityTag, position);
            session.getLecternCache().add(position);
            // Close the window - we will reopen it once the client has this data synced
            ServerboundContainerClosePacket closeWindowPacket = new ServerboundContainerClosePacket(lecternContainer.getId());
            session.sendDownstreamPacket(closeWindowPacket);
            InventoryUtils.closeInventory(session, inventory.getId(), false);
        }
    }
}
Also used : ServerboundContainerClosePacket(com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerClosePacket) LecternContainer(org.geysermc.geyser.inventory.LecternContainer) Vector3i(com.nukkitx.math.vector.Vector3i) NbtMap(com.nukkitx.nbt.NbtMap) NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ServerboundContainerButtonClickPacket(com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerButtonClickPacket) ItemData(com.nukkitx.protocol.bedrock.data.inventory.ItemData)

Example 19 with ListTag

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

the class FireworkEntity method setFireworkItem.

public void setFireworkItem(EntityMetadata<ItemStack, ?> entityMetadata) {
    ItemStack item = entityMetadata.getValue();
    if (item == null) {
        return;
    }
    CompoundTag tag = item.getNbt();
    if (tag == null) {
        return;
    }
    // https://bugs.mojang.com/browse/MCPE-89115
    if (session.getClientData().getDeviceOs() == DeviceOs.XBOX || session.getClientData().getDeviceOs() == DeviceOs.PS4) {
        return;
    }
    CompoundTag fireworks = tag.get("Fireworks");
    if (fireworks == null) {
        // Thank you Mineplex very cool
        return;
    }
    NbtMapBuilder fireworksBuilder = NbtMap.builder();
    if (fireworks.get("Flight") != null) {
        fireworksBuilder.putByte("Flight", MathUtils.getNbtByte(fireworks.get("Flight").getValue()));
    }
    List<NbtMap> explosions = new ArrayList<>();
    if (fireworks.get("Explosions") != null) {
        for (Tag effect : ((ListTag) fireworks.get("Explosions")).getValue()) {
            CompoundTag effectData = (CompoundTag) effect;
            NbtMapBuilder effectBuilder = NbtMap.builder();
            if (effectData.get("Type") != null) {
                effectBuilder.putByte("FireworkType", MathUtils.getNbtByte(effectData.get("Type").getValue()));
            }
            if (effectData.get("Colors") != null) {
                int[] oldColors = (int[]) effectData.get("Colors").getValue();
                byte[] colors = new byte[oldColors.length];
                int i = 0;
                for (int color : oldColors) {
                    colors[i++] = FireworkColor.fromJavaRGB(color);
                }
                effectBuilder.putByteArray("FireworkColor", colors);
            }
            if (effectData.get("FadeColors") != null) {
                int[] oldColors = (int[]) effectData.get("FadeColors").getValue();
                byte[] colors = new byte[oldColors.length];
                int i = 0;
                for (int color : oldColors) {
                    colors[i++] = FireworkColor.fromJavaRGB(color);
                }
                effectBuilder.putByteArray("FireworkFade", colors);
            }
            if (effectData.get("Trail") != null) {
                effectBuilder.putByte("FireworkTrail", MathUtils.getNbtByte(effectData.get("Trail").getValue()));
            }
            if (effectData.get("Flicker") != null) {
                effectBuilder.putByte("FireworkFlicker", MathUtils.getNbtByte(effectData.get("Flicker").getValue()));
            }
            explosions.add(effectBuilder.build());
        }
    }
    fireworksBuilder.putList("Explosions", NbtType.COMPOUND, explosions);
    NbtMapBuilder builder = NbtMap.builder();
    builder.put("Fireworks", fireworksBuilder.build());
    dirtyMetadata.put(EntityData.DISPLAY_ITEM, builder.build());
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) ArrayList(java.util.ArrayList) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ItemStack(com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack) NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 20 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;
    Integer rawId = null;
    boolean gotRawIdFromTag = false;
    CompoundTag tag = item.tag();
    // Use tag to get original ID and data
    if (tag != null) {
        // Check for valid tag
        if (tag.get(NBT_TAG_NAME) instanceof IntTag) {
            rawId = ((NumberTag) tag.get(NBT_TAG_NAME)).asInt();
            // Remove the tag
            tag.remove(NBT_TAG_NAME);
            gotRawIdFromTag = true;
        }
    }
    if (rawId == null) {
        int oldId = Protocol1_13To1_12_2.MAPPINGS.getItemMappings().inverse().get(item.identifier());
        if (oldId != -1) {
            // Handle spawn eggs
            Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId);
            if (eggEntityId.isPresent()) {
                rawId = 383 << 16;
                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);
                }
            } else {
                rawId = (oldId >> 4) << 16 | oldId & 0xF;
            }
        }
    }
    if (rawId == null) {
        if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
            Via.getPlatform().getLogger().warning("Failed to get 1.12 item for " + item.identifier());
        }
        // Stone
        rawId = 0x10000;
    }
    item.setIdentifier((short) (rawId >> 16));
    item.setData((short) (rawId & 0xFFFF));
    // NBT changes
    if (tag != null) {
        if (isDamageable(item.identifier())) {
            if (tag.get("Damage") instanceof IntTag) {
                if (!gotRawIdFromTag) {
                    item.setData((short) (int) tag.get("Damage").getValue());
                }
                tag.remove("Damage");
            }
        }
        if (item.identifier() == 358) {
            // map
            if (tag.get("map") instanceof IntTag) {
                if (!gotRawIdFromTag) {
                    item.setData((short) (int) tag.get("map").getValue());
                }
                tag.remove("map");
            }
        }
        if (item.identifier() == 442 || item.identifier() == 425) {
            // shield / banner
            if (tag.get("BlockEntityTag") instanceof CompoundTag) {
                CompoundTag blockEntityTag = tag.get("BlockEntityTag");
                if (blockEntityTag.get("Base") instanceof IntTag) {
                    IntTag base = blockEntityTag.get("Base");
                    // invert color id
                    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");
                StringTag via = display.remove(NBT_TAG_NAME + "|Name");
                name.setValue(via != null ? via.getValue() : ChatRewriter.jsonToLegacyText(name.getValue()));
            }
        }
        // ench is now Enchantments and now uses identifiers
        if (tag.get("Enchantments") instanceof ListTag) {
            ListTag enchantments = tag.get("Enchantments");
            ListTag ench = new ListTag(CompoundTag.class);
            for (Tag enchantmentEntry : enchantments) {
                if (enchantmentEntry instanceof CompoundTag) {
                    CompoundTag enchEntry = new CompoundTag();
                    String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
                    Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId);
                    if (oldId == null && newId.startsWith("viaversion:legacy/")) {
                        oldId = Short.valueOf(newId.substring(18));
                    }
                    if (oldId != null) {
                        enchEntry.put("id", new ShortTag(oldId));
                        enchEntry.put("lvl", new ShortTag(((NumberTag) ((CompoundTag) enchantmentEntry).get("lvl")).asShort()));
                        ench.add(enchEntry);
                    }
                }
            }
            tag.remove("Enchantments");
            tag.put("ench", ench);
        }
        if (tag.get("StoredEnchantments") instanceof ListTag) {
            ListTag storedEnch = tag.get("StoredEnchantments");
            ListTag newStoredEnch = new ListTag(CompoundTag.class);
            for (Tag enchantmentEntry : storedEnch) {
                if (enchantmentEntry instanceof CompoundTag) {
                    CompoundTag enchEntry = new CompoundTag();
                    String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
                    Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId);
                    if (oldId == null && newId.startsWith("viaversion:legacy/")) {
                        oldId = Short.valueOf(newId.substring(18));
                    }
                    if (oldId != null) {
                        enchEntry.put("id", new ShortTag(oldId));
                        enchEntry.put("lvl", new ShortTag(((NumberTag) ((CompoundTag) enchantmentEntry).get("lvl")).asShort()));
                        newStoredEnch.add(enchEntry);
                    }
                }
            }
            tag.remove("StoredEnchantments");
            tag.put("StoredEnchantments", newStoredEnch);
        }
        if (tag.get(NBT_TAG_NAME + "|CanPlaceOn") instanceof ListTag) {
            tag.put("CanPlaceOn", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(tag.get(NBT_TAG_NAME + "|CanPlaceOn"))));
            tag.remove(NBT_TAG_NAME + "|CanPlaceOn");
        } else if (tag.get("CanPlaceOn") instanceof ListTag) {
            ListTag old = tag.get("CanPlaceOn");
            ListTag newCanPlaceOn = new ListTag(StringTag.class);
            for (Tag oldTag : old) {
                Object value = oldTag.getValue();
                String[] newValues = BlockIdData.fallbackReverseMapping.get(value instanceof String ? ((String) value).replace("minecraft:", "") : null);
                if (newValues != null) {
                    for (String newValue : newValues) {
                        newCanPlaceOn.add(new StringTag(newValue));
                    }
                } else {
                    newCanPlaceOn.add(oldTag);
                }
            }
            tag.put("CanPlaceOn", newCanPlaceOn);
        }
        if (tag.get(NBT_TAG_NAME + "|CanDestroy") instanceof ListTag) {
            tag.put("CanDestroy", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(tag.get(NBT_TAG_NAME + "|CanDestroy"))));
            tag.remove(NBT_TAG_NAME + "|CanDestroy");
        } else if (tag.get("CanDestroy") instanceof ListTag) {
            ListTag old = tag.get("CanDestroy");
            ListTag newCanDestroy = new ListTag(StringTag.class);
            for (Tag oldTag : old) {
                Object value = oldTag.getValue();
                String[] newValues = BlockIdData.fallbackReverseMapping.get(value instanceof String ? ((String) value).replace("minecraft:", "") : null);
                if (newValues != null) {
                    for (String newValue : newValues) {
                        newCanDestroy.add(new StringTag(newValue));
                    }
                } else {
                    newCanDestroy.add(oldTag);
                }
            }
            tag.put("CanDestroy", newCanDestroy);
        }
    }
    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)

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