Search in sources :

Example 16 with ItemData

use of com.nukkitx.protocol.bedrock.data.inventory.ItemData in project Geyser by GeyserMC.

the class CrossbowTranslator method translateToBedrock.

@Override
public void translateToBedrock(GeyserSession session, CompoundTag itemTag, ItemMapping mapping) {
    if (itemTag.get("ChargedProjectiles") != null) {
        ListTag chargedProjectiles = itemTag.get("ChargedProjectiles");
        if (!chargedProjectiles.getValue().isEmpty()) {
            CompoundTag projectile = (CompoundTag) chargedProjectiles.getValue().get(0);
            ItemMapping projectileMapping = session.getItemMappings().getMapping((String) projectile.get("id").getValue());
            if (projectileMapping == null)
                return;
            CompoundTag tag = projectile.get("tag");
            ItemStack itemStack = new ItemStack(mapping.getJavaId(), (byte) projectile.get("Count").getValue(), tag);
            ItemData itemData = ItemTranslator.translateToBedrock(session, itemStack);
            CompoundTag newProjectile = new CompoundTag("chargedItem");
            newProjectile.put(new ByteTag("Count", (byte) itemData.getCount()));
            newProjectile.put(new StringTag("Name", projectileMapping.getBedrockIdentifier()));
            newProjectile.put(new ShortTag("Damage", (short) itemData.getDamage()));
            itemTag.put(newProjectile);
        }
    }
}
Also used : ItemMapping(org.geysermc.geyser.registry.type.ItemMapping) ItemStack(com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack) ItemData(com.nukkitx.protocol.bedrock.data.inventory.ItemData)

Example 17 with ItemData

use of com.nukkitx.protocol.bedrock.data.inventory.ItemData 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 18 with ItemData

use of com.nukkitx.protocol.bedrock.data.inventory.ItemData in project Geyser by GeyserMC.

the class ItemEntity method setItem.

public void setItem(EntityMetadata<ItemStack, ?> entityMetadata) {
    ItemData item = ItemTranslator.translateToBedrock(session, entityMetadata.getValue());
    if (this.item == null) {
        this.item = item;
        spawnEntity();
    } else if (item.equals(this.item, false, true, true)) {
        // Don't bother respawning the entity if items are equal
        if (this.item.getCount() != item.getCount()) {
            // Just item count updated; let's make this easy
            this.item = item;
            EntityEventPacket packet = new EntityEventPacket();
            packet.setRuntimeEntityId(geyserId);
            packet.setType(EntityEventType.UPDATE_ITEM_STACK_SIZE);
            packet.setData(this.item.getCount());
            session.sendUpstreamPacket(packet);
        }
    } else {
        this.item = item;
        despawnEntity();
        spawnEntity();
    }
}
Also used : EntityEventPacket(com.nukkitx.protocol.bedrock.packet.EntityEventPacket) ItemData(com.nukkitx.protocol.bedrock.data.inventory.ItemData)

Example 19 with ItemData

use of com.nukkitx.protocol.bedrock.data.inventory.ItemData in project Geyser by GeyserMC.

the class ItemRegistryPopulator method populate.

public static void populate() {
    GeyserBootstrap bootstrap = GeyserImpl.getInstance().getBootstrap();
    TypeReference<Map<String, GeyserMappingItem>> mappingItemsType = new TypeReference<>() {
    };
    Map<String, GeyserMappingItem> items;
    try (InputStream stream = bootstrap.getResource("mappings/items.json")) {
        // Load item mappings from Java Edition to Bedrock Edition
        items = GeyserImpl.JSON_MAPPER.readValue(stream, mappingItemsType);
    } catch (Exception e) {
        throw new AssertionError("Unable to load Java runtime item IDs", e);
    }
    // We can reduce some operations as Java information is the same across all palette versions
    boolean firstMappingsPass = true;
    Int2IntMap dyeColors = new FixedInt2IntMap();
    /* Load item palette */
    for (Map.Entry<String, PaletteVersion> palette : PALETTE_VERSIONS.entrySet()) {
        TypeReference<List<PaletteItem>> paletteEntriesType = new TypeReference<>() {
        };
        // Used to get the Bedrock namespaced ID (in instances where there are small differences)
        Object2IntMap<String> bedrockIdentifierToId = new Object2IntOpenHashMap<>();
        bedrockIdentifierToId.defaultReturnValue(Short.MIN_VALUE);
        List<String> itemNames = new ArrayList<>();
        List<PaletteItem> itemEntries;
        try (InputStream stream = bootstrap.getResource(String.format("bedrock/runtime_item_states.%s.json", palette.getKey()))) {
            itemEntries = GeyserImpl.JSON_MAPPER.readValue(stream, paletteEntriesType);
        } catch (Exception e) {
            throw new AssertionError("Unable to load Bedrock runtime item IDs", e);
        }
        Map<String, StartGamePacket.ItemEntry> entries = new Object2ObjectOpenHashMap<>();
        for (PaletteItem entry : itemEntries) {
            entries.put(entry.getName(), new StartGamePacket.ItemEntry(entry.getName(), (short) entry.getId()));
            bedrockIdentifierToId.put(entry.getName(), entry.getId());
        }
        Object2IntMap<String> bedrockBlockIdOverrides = new Object2IntOpenHashMap<>();
        Object2IntMap<String> blacklistedIdentifiers = new Object2IntOpenHashMap<>();
        // Load creative items
        // We load this before item mappings to get overridden block runtime ID mappings
        JsonNode creativeItemEntries;
        try (InputStream stream = bootstrap.getResource(String.format("bedrock/creative_items.%s.json", palette.getKey()))) {
            creativeItemEntries = GeyserImpl.JSON_MAPPER.readTree(stream).get("items");
        } catch (Exception e) {
            throw new AssertionError("Unable to load creative items", e);
        }
        IntList boats = new IntArrayList();
        IntList buckets = new IntArrayList();
        IntList spawnEggs = new IntArrayList();
        List<ItemData> carpets = new ObjectArrayList<>();
        Int2ObjectMap<ItemMapping> mappings = new Int2ObjectOpenHashMap<>();
        // Temporary mapping to create stored items
        Map<String, ItemMapping> identifierToMapping = new Object2ObjectOpenHashMap<>();
        int netId = 1;
        List<ItemData> creativeItems = new ArrayList<>();
        for (JsonNode itemNode : creativeItemEntries) {
            int count = 1;
            int damage = 0;
            int blockRuntimeId = 0;
            NbtMap tag = null;
            JsonNode damageNode = itemNode.get("damage");
            if (damageNode != null) {
                damage = damageNode.asInt();
            }
            JsonNode countNode = itemNode.get("count");
            if (countNode != null) {
                count = countNode.asInt();
            }
            JsonNode blockRuntimeIdNode = itemNode.get("blockRuntimeId");
            if (blockRuntimeIdNode != null) {
                blockRuntimeId = blockRuntimeIdNode.asInt();
            }
            JsonNode nbtNode = itemNode.get("nbt_b64");
            if (nbtNode != null) {
                byte[] bytes = Base64.getDecoder().decode(nbtNode.asText());
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                try {
                    tag = (NbtMap) NbtUtils.createReaderLE(bais).readTag();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String identifier = itemNode.get("id").textValue();
            if (identifier.equals("minecraft:debug_stick")) {
                // Just shows an empty texture; either way it doesn't exist in the creative menu on Java
                continue;
            }
            StartGamePacket.ItemEntry entry = entries.get(identifier);
            int id = -1;
            if (entry != null) {
                id = entry.getId();
            }
            if (id == -1) {
                throw new RuntimeException("Unable to find matching Bedrock item for " + identifier);
            }
            creativeItems.add(ItemData.builder().id(id).damage(damage).count(count).blockRuntimeId(blockRuntimeId).tag(tag).netId(netId++).build());
            if (blockRuntimeId != 0) {
                // Add override for item mapping, unless it already exists... then we know multiple states can exist
                if (!blacklistedIdentifiers.containsKey(identifier)) {
                    if (bedrockBlockIdOverrides.containsKey(identifier)) {
                        bedrockBlockIdOverrides.removeInt(identifier);
                        // Save this as a blacklist, but also as knowledge of what the block state name should be
                        blacklistedIdentifiers.put(identifier, blockRuntimeId);
                    } else {
                        // Unless there's multiple possibilities for this one state, let this be
                        bedrockBlockIdOverrides.put(identifier, blockRuntimeId);
                    }
                }
            }
        }
        BlockMappings blockMappings = BlockRegistries.BLOCKS.forVersion(palette.getValue().protocolVersion());
        int itemIndex = 0;
        int javaFurnaceMinecartId = 0;
        boolean usingFurnaceMinecart = GeyserImpl.getInstance().getConfig().isAddNonBedrockItems();
        Set<String> javaOnlyItems = new ObjectOpenHashSet<>();
        Collections.addAll(javaOnlyItems, "minecraft:spectral_arrow", "minecraft:debug_stick", "minecraft:knowledge_book", "minecraft:tipped_arrow", "minecraft:trader_llama_spawn_egg", "minecraft:bundle");
        if (!usingFurnaceMinecart) {
            javaOnlyItems.add("minecraft:furnace_minecart");
        }
        // Java-only items for this version
        javaOnlyItems.addAll(palette.getValue().additionalTranslatedItems().keySet());
        for (Map.Entry<String, GeyserMappingItem> entry : items.entrySet()) {
            String javaIdentifier = entry.getKey().intern();
            GeyserMappingItem mappingItem;
            String replacementItem = palette.getValue().additionalTranslatedItems().get(javaIdentifier);
            if (replacementItem != null) {
                mappingItem = items.get(replacementItem);
            } else {
                // This items has a mapping specifically for this version of the game
                mappingItem = entry.getValue();
            }
            String bedrockIdentifier;
            if (javaIdentifier.equals("minecraft:music_disc_otherside") && palette.getValue().protocolVersion() <= Bedrock_v471.V471_CODEC.getProtocolVersion()) {
                bedrockIdentifier = "minecraft:music_disc_pigstep";
            } else if (javaIdentifier.equals("minecraft:globe_banner_pattern") && palette.getValue().protocolVersion() < Bedrock_v486.V486_CODEC.getProtocolVersion()) {
                bedrockIdentifier = "minecraft:banner_pattern";
            } else {
                bedrockIdentifier = mappingItem.getBedrockIdentifier();
            }
            if (usingFurnaceMinecart && javaIdentifier.equals("minecraft:furnace_minecart")) {
                javaFurnaceMinecartId = itemIndex;
                itemIndex++;
                continue;
            }
            int bedrockId = bedrockIdentifierToId.getInt(bedrockIdentifier);
            if (bedrockId == Short.MIN_VALUE) {
                throw new RuntimeException("Missing Bedrock ID in mappings: " + bedrockIdentifier);
            }
            int stackSize = mappingItem.getStackSize();
            int bedrockBlockId = -1;
            Integer firstBlockRuntimeId = entry.getValue().getFirstBlockRuntimeId();
            if (firstBlockRuntimeId != null) {
                int blockIdOverride = bedrockBlockIdOverrides.getOrDefault(bedrockIdentifier, -1);
                if (blockIdOverride != -1) {
                    // Straight from BDS is our best chance of getting an item that doesn't run into issues
                    bedrockBlockId = blockIdOverride;
                } else {
                    // Try to get an example block runtime ID from the creative contents packet, for Bedrock identifier obtaining
                    int aValidBedrockBlockId = blacklistedIdentifiers.getOrDefault(bedrockIdentifier, -1);
                    if (aValidBedrockBlockId == -1) {
                        // Fallback
                        bedrockBlockId = blockMappings.getBedrockBlockId(firstBlockRuntimeId);
                    } else {
                        // As of 1.16.220, every item requires a block runtime ID attached to it.
                        // This is mostly for identifying different blocks with the same item ID - wool, slabs, some walls.
                        // However, in order for some visuals and crafting to work, we need to send the first matching block state
                        // as indexed by Bedrock's block palette
                        // There are exceptions! But, ideally, the block ID override should take care of those.
                        NbtMapBuilder requiredBlockStatesBuilder = NbtMap.builder();
                        String correctBedrockIdentifier = blockMappings.getBedrockBlockStates().get(aValidBedrockBlockId).getString("name");
                        boolean firstPass = true;
                        // Block states are all grouped together. In the mappings, we store the first block runtime ID in order,
                        // and the last, if relevant. We then iterate over all those values and get their Bedrock equivalents
                        Integer lastBlockRuntimeId = entry.getValue().getLastBlockRuntimeId() == null ? firstBlockRuntimeId : entry.getValue().getLastBlockRuntimeId();
                        for (int i = firstBlockRuntimeId; i <= lastBlockRuntimeId; i++) {
                            int bedrockBlockRuntimeId = blockMappings.getBedrockBlockId(i);
                            NbtMap blockTag = blockMappings.getBedrockBlockStates().get(bedrockBlockRuntimeId);
                            String bedrockName = blockTag.getString("name");
                            if (!bedrockName.equals(correctBedrockIdentifier)) {
                                continue;
                            }
                            NbtMap states = blockTag.getCompound("states");
                            if (firstPass) {
                                firstPass = false;
                                if (states.size() == 0) {
                                    // No need to iterate and find all block states - this is the one, as there can't be any others
                                    bedrockBlockId = bedrockBlockRuntimeId;
                                    break;
                                }
                                requiredBlockStatesBuilder.putAll(states);
                                continue;
                            }
                            for (Map.Entry<String, Object> nbtEntry : states.entrySet()) {
                                Object value = requiredBlockStatesBuilder.get(nbtEntry.getKey());
                                if (value != null && !nbtEntry.getValue().equals(value)) {
                                    // Null means this value has already been removed/deemed as unneeded
                                    // This state can change between different block states, and therefore is not required
                                    // to build a successful block state of this
                                    requiredBlockStatesBuilder.remove(nbtEntry.getKey());
                                }
                            }
                            if (requiredBlockStatesBuilder.size() == 0) {
                                // (States that are important include color for glass)
                                break;
                            }
                        }
                        NbtMap requiredBlockStates = requiredBlockStatesBuilder.build();
                        if (bedrockBlockId == -1) {
                            int i = -1;
                            // in it's "preferred" block state - I.E. the first matching block state in the list
                            for (NbtMap blockTag : blockMappings.getBedrockBlockStates()) {
                                i++;
                                if (blockTag.getString("name").equals(correctBedrockIdentifier)) {
                                    NbtMap states = blockTag.getCompound("states");
                                    boolean valid = true;
                                    for (Map.Entry<String, Object> nbtEntry : requiredBlockStates.entrySet()) {
                                        if (!states.get(nbtEntry.getKey()).equals(nbtEntry.getValue())) {
                                            // A required block state doesn't match - this one is not valid
                                            valid = false;
                                            break;
                                        }
                                    }
                                    if (valid) {
                                        bedrockBlockId = i;
                                        break;
                                    }
                                }
                            }
                            if (bedrockBlockId == -1) {
                                throw new RuntimeException("Could not find a block match for " + entry.getKey());
                            }
                        }
                        // That way, creative items work correctly for these blocks
                        for (int j = 0; j < creativeItems.size(); j++) {
                            ItemData itemData = creativeItems.get(j);
                            if (itemData.getId() == bedrockId) {
                                if (itemData.getDamage() != 0) {
                                    break;
                                }
                                NbtMap states = blockMappings.getBedrockBlockStates().get(itemData.getBlockRuntimeId()).getCompound("states");
                                boolean valid = true;
                                for (Map.Entry<String, Object> nbtEntry : requiredBlockStates.entrySet()) {
                                    if (!states.get(nbtEntry.getKey()).equals(nbtEntry.getValue())) {
                                        // A required block state doesn't match - this one is not valid
                                        valid = false;
                                        break;
                                    }
                                }
                                if (valid) {
                                    creativeItems.set(j, itemData.toBuilder().blockRuntimeId(bedrockBlockId).build());
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            ItemMapping.ItemMappingBuilder mappingBuilder = ItemMapping.builder().javaIdentifier(javaIdentifier).javaId(itemIndex).bedrockIdentifier(bedrockIdentifier.intern()).bedrockId(bedrockId).bedrockData(mappingItem.getBedrockData()).bedrockBlockId(bedrockBlockId).stackSize(stackSize).maxDamage(mappingItem.getMaxDamage()).hasSuspiciousStewEffect(mappingItem.isHasSuspiciousStewEffect());
            if (mappingItem.getRepairMaterials() != null) {
                mappingBuilder = mappingBuilder.repairMaterials(new ObjectOpenHashSet<>(mappingItem.getRepairMaterials()));
            }
            if (mappingItem.getToolType() != null) {
                if (mappingItem.getToolTier() != null) {
                    mappingBuilder = mappingBuilder.toolType(mappingItem.getToolType().intern()).toolTier(mappingItem.getToolTier().intern());
                } else {
                    mappingBuilder = mappingBuilder.toolType(mappingItem.getToolType().intern()).toolTier("");
                }
            }
            if (javaOnlyItems.contains(javaIdentifier)) {
                // These items don't exist on Bedrock, so set up a variable that indicates they should have custom names
                mappingBuilder = mappingBuilder.translationString((bedrockBlockId != -1 ? "block." : "item.") + entry.getKey().replace(":", "."));
                GeyserImpl.getInstance().getLogger().debug("Adding " + entry.getKey() + " as an item that needs to be translated.");
            }
            ItemMapping mapping = mappingBuilder.build();
            if (javaIdentifier.contains("boat")) {
                boats.add(bedrockId);
            } else if (javaIdentifier.contains("bucket") && !javaIdentifier.contains("milk")) {
                buckets.add(bedrockId);
            } else if (javaIdentifier.contains("_carpet") && !javaIdentifier.contains("moss")) {
                // This should be the numerical order Java sends as an integer value for llamas
                carpets.add(ItemData.builder().id(mapping.getBedrockId()).damage(mapping.getBedrockData()).count(1).blockRuntimeId(mapping.getBedrockBlockId()).build());
            } else if (javaIdentifier.startsWith("minecraft:music_disc_")) {
                // The Java record level event uses the item ID as the "key" to play the record
                Registries.RECORDS.register(itemIndex, SoundEvent.valueOf("RECORD_" + javaIdentifier.replace("minecraft:music_disc_", "").toUpperCase(Locale.ENGLISH)));
            } else if (javaIdentifier.endsWith("_spawn_egg")) {
                spawnEggs.add(mapping.getBedrockId());
            }
            mappings.put(itemIndex, mapping);
            identifierToMapping.put(javaIdentifier, mapping);
            itemNames.add(javaIdentifier);
            if (firstMappingsPass && mappingItem.getDyeColor() != -1) {
                dyeColors.put(itemIndex, mappingItem.getDyeColor());
            }
            itemIndex++;
        }
        itemNames.add("minecraft:furnace_minecart");
        int lodestoneCompassId = entries.get("minecraft:lodestone_compass").getId();
        if (lodestoneCompassId == 0) {
            throw new RuntimeException("Lodestone compass not found in item palette!");
        }
        // Add the lodestone compass since it doesn't exist on java but we need it for item conversion
        ItemMapping lodestoneEntry = ItemMapping.builder().javaIdentifier("minecraft:lodestone_compass").bedrockIdentifier("minecraft:lodestone_compass").javaId(itemIndex).bedrockId(lodestoneCompassId).bedrockData(0).bedrockBlockId(-1).stackSize(1).build();
        mappings.put(itemIndex, lodestoneEntry);
        identifierToMapping.put(lodestoneEntry.getJavaIdentifier(), lodestoneEntry);
        ComponentItemData furnaceMinecartData = null;
        if (usingFurnaceMinecart) {
            // Add the furnace minecart as a custom item
            int furnaceMinecartId = mappings.size() + 1;
            entries.put("geysermc:furnace_minecart", new StartGamePacket.ItemEntry("geysermc:furnace_minecart", (short) furnaceMinecartId, true));
            mappings.put(javaFurnaceMinecartId, ItemMapping.builder().javaIdentifier("minecraft:furnace_minecart").bedrockIdentifier("geysermc:furnace_minecart").javaId(javaFurnaceMinecartId).bedrockId(furnaceMinecartId).bedrockData(0).bedrockBlockId(-1).stackSize(1).build());
            creativeItems.add(ItemData.builder().netId(netId).id(furnaceMinecartId).count(1).build());
            NbtMapBuilder builder = NbtMap.builder();
            builder.putString("name", "geysermc:furnace_minecart").putInt("id", furnaceMinecartId);
            NbtMapBuilder itemProperties = NbtMap.builder();
            NbtMapBuilder componentBuilder = NbtMap.builder();
            // Conveniently, as of 1.16.200, the furnace minecart has a texture AND translation string already.
            itemProperties.putCompound("minecraft:icon", NbtMap.builder().putString("texture", "minecart_furnace").putString("frame", "0.000000").putInt("frame_version", 1).putString("legacy_id", "").build());
            componentBuilder.putCompound("minecraft:display_name", NbtMap.builder().putString("value", "item.minecartFurnace.name").build());
            // Indicate that the arm animation should play on rails
            List<NbtMap> useOnTag = Collections.singletonList(NbtMap.builder().putString("tags", "q.any_tag('rail')").build());
            componentBuilder.putCompound("minecraft:entity_placer", NbtMap.builder().putList("dispense_on", NbtType.COMPOUND, useOnTag).putString("entity", "minecraft:minecart").putList("use_on", NbtType.COMPOUND, useOnTag).build());
            // We always want to allow offhand usage when we can - matches Java Edition
            itemProperties.putBoolean("allow_off_hand", true);
            itemProperties.putBoolean("hand_equipped", false);
            itemProperties.putInt("max_stack_size", 1);
            itemProperties.putString("creative_group", "itemGroup.name.minecart");
            // 4 - "Items"
            itemProperties.putInt("creative_category", 4);
            componentBuilder.putCompound("item_properties", itemProperties.build());
            builder.putCompound("components", componentBuilder.build());
            furnaceMinecartData = new ComponentItemData("geysermc:furnace_minecart", builder.build());
        }
        ItemMappings itemMappings = ItemMappings.builder().items(mappings).creativeItems(creativeItems.toArray(new ItemData[0])).itemEntries(new ArrayList<>(entries.values())).itemNames(itemNames.toArray(new String[0])).storedItems(new StoredItemMappings(identifierToMapping)).javaOnlyItems(javaOnlyItems).bucketIds(buckets).boatIds(boats).spawnEggIds(spawnEggs).carpets(carpets).furnaceMinecartData(furnaceMinecartData).build();
        Registries.ITEMS.register(palette.getValue().protocolVersion(), itemMappings);
        firstMappingsPass = false;
    }
    ItemUtils.setDyeColors(dyeColors);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) FixedInt2IntMap(org.geysermc.geyser.util.collection.FixedInt2IntMap) NbtMap(com.nukkitx.nbt.NbtMap) GeyserBootstrap(org.geysermc.geyser.GeyserBootstrap) ByteArrayInputStream(java.io.ByteArrayInputStream) NbtMap(com.nukkitx.nbt.NbtMap) FixedInt2IntMap(org.geysermc.geyser.util.collection.FixedInt2IntMap) FixedInt2IntMap(org.geysermc.geyser.util.collection.FixedInt2IntMap) ComponentItemData(com.nukkitx.protocol.bedrock.data.inventory.ComponentItemData) StoredItemMappings(org.geysermc.geyser.inventory.item.StoredItemMappings) NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) StartGamePacket(com.nukkitx.protocol.bedrock.packet.StartGamePacket) IOException(java.io.IOException) StoredItemMappings(org.geysermc.geyser.inventory.item.StoredItemMappings) ComponentItemData(com.nukkitx.protocol.bedrock.data.inventory.ComponentItemData) ItemData(com.nukkitx.protocol.bedrock.data.inventory.ItemData)

Example 20 with ItemData

use of com.nukkitx.protocol.bedrock.data.inventory.ItemData in project Geyser by GeyserMC.

the class RecipeRegistryPopulator method getCraftingDataFromJsonNode.

/**
 * Computes a Bedrock crafting recipe from the given JSON data.
 * @param node the JSON data to compute
 * @param recipes a list of all the recipes
 * @return the {@link CraftingData} to send to the Bedrock client.
 */
private static CraftingData getCraftingDataFromJsonNode(JsonNode node, Int2ObjectMap<GeyserRecipe> recipes, ItemMappings mappings) {
    int netId = ++LAST_RECIPE_NET_ID;
    int type = node.get("bedrockRecipeType").asInt();
    JsonNode outputNode = node.get("output");
    ItemMapping outputEntry = mappings.getMapping(outputNode.get("identifier").asText());
    ItemData output = getBedrockItemFromIdentifierJson(outputEntry, outputNode);
    UUID uuid = UUID.randomUUID();
    if (type == 1) {
        // Shaped recipe
        List<String> shape = new ArrayList<>();
        // Get the shape of the recipe
        for (JsonNode chars : node.get("shape")) {
            shape.add(chars.asText());
        }
        // In recipes.json each recipe is mapped by a letter
        Map<String, ItemData> letterToRecipe = new HashMap<>();
        Iterator<Map.Entry<String, JsonNode>> iterator = node.get("inputs").fields();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonNode> entry = iterator.next();
            JsonNode inputNode = entry.getValue();
            ItemMapping inputEntry = mappings.getMapping(inputNode.get("identifier").asText());
            letterToRecipe.put(entry.getKey(), getBedrockItemFromIdentifierJson(inputEntry, inputNode));
        }
        List<ItemData> inputs = new ArrayList<>(shape.size() * shape.get(0).length());
        int i = 0;
        // Create a linear array of items from the "cube" of the shape
        for (int j = 0; i < shape.size() * shape.get(0).length(); j++) {
            for (char c : shape.get(j).toCharArray()) {
                ItemData data = letterToRecipe.getOrDefault(String.valueOf(c), ItemData.AIR);
                inputs.add(data);
                i++;
            }
        }
        /* Convert into a Java recipe class for autocrafting */
        List<Ingredient> ingredients = new ArrayList<>();
        for (ItemData input : inputs) {
            ingredients.add(new Ingredient(new ItemStack[] { ItemTranslator.translateToJava(input, mappings) }));
        }
        GeyserRecipe recipe = new GeyserShapedRecipe(shape.get(0).length(), shape.size(), ingredients.toArray(new Ingredient[0]), ItemTranslator.translateToJava(output, mappings));
        recipes.put(netId, recipe);
        return CraftingData.fromShaped(uuid.toString(), shape.get(0).length(), shape.size(), inputs, Collections.singletonList(output), uuid, "crafting_table", 0, netId);
    }
    List<ItemData> inputs = new ObjectArrayList<>();
    for (JsonNode entry : node.get("inputs")) {
        ItemMapping inputEntry = mappings.getMapping(entry.get("identifier").asText());
        inputs.add(getBedrockItemFromIdentifierJson(inputEntry, entry));
    }
    /* Convert into a Java Recipe class for autocrafting */
    List<Ingredient> ingredients = new ArrayList<>();
    for (ItemData input : inputs) {
        ingredients.add(new Ingredient(new ItemStack[] { ItemTranslator.translateToJava(input, mappings) }));
    }
    GeyserRecipe recipe = new GeyserShapelessRecipe(ingredients.toArray(new Ingredient[0]), ItemTranslator.translateToJava(output, mappings));
    recipes.put(netId, recipe);
    if (type == 5) {
        // Shulker box
        return CraftingData.fromShulkerBox(uuid.toString(), inputs, Collections.singletonList(output), uuid, "crafting_table", 0, netId);
    }
    return CraftingData.fromShapeless(uuid.toString(), inputs, Collections.singletonList(output), uuid, "crafting_table", 0, netId);
}
Also used : GeyserShapelessRecipe(org.geysermc.geyser.inventory.recipe.GeyserShapelessRecipe) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) ObjectArrayList(it.unimi.dsi.fastutil.objects.ObjectArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) GeyserRecipe(org.geysermc.geyser.inventory.recipe.GeyserRecipe) ObjectArrayList(it.unimi.dsi.fastutil.objects.ObjectArrayList) Ingredient(com.github.steveice10.mc.protocol.data.game.recipe.Ingredient) ItemMapping(org.geysermc.geyser.registry.type.ItemMapping) GeyserShapedRecipe(org.geysermc.geyser.inventory.recipe.GeyserShapedRecipe) ItemStack(com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack) NbtMap(com.nukkitx.nbt.NbtMap) Int2ObjectOpenHashMap(it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap) ItemData(com.nukkitx.protocol.bedrock.data.inventory.ItemData)

Aggregations

ItemData (com.nukkitx.protocol.bedrock.data.inventory.ItemData)30 ObjectArrayList (it.unimi.dsi.fastutil.objects.ObjectArrayList)13 CraftingData (com.nukkitx.protocol.bedrock.data.inventory.CraftingData)9 UUID (java.util.UUID)8 NbtMap (com.nukkitx.nbt.NbtMap)6 InventoryContentPacket (com.nukkitx.protocol.bedrock.packet.InventoryContentPacket)6 ArrayList (java.util.ArrayList)6 Item (org.jukeboxmc.item.Item)4 NbtMapBuilder (com.nukkitx.nbt.NbtMapBuilder)3 ItemMapping (org.geysermc.geyser.registry.type.ItemMapping)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ItemStack (com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack)2 Map (java.util.Map)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 Ingredient (com.github.steveice10.mc.protocol.data.game.recipe.Ingredient)1 ServerboundContainerButtonClickPacket (com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerButtonClickPacket)1 ServerboundContainerClosePacket (com.github.steveice10.mc.protocol.packet.ingame.serverbound.inventory.ServerboundContainerClosePacket)1 CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)1 Vector3i (com.nukkitx.math.vector.Vector3i)1 EntityData (com.nukkitx.protocol.bedrock.data.entity.EntityData)1