diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockBlockPickRequestTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockBlockPickRequestTranslator.java index 3e40ddd6f4c..f7e3fbd9c3f 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockBlockPickRequestTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockBlockPickRequestTranslator.java @@ -46,7 +46,6 @@ public void translate(BlockPickRequestPacket packet, GeyserSession session) { return; } - String targetIdentifier = BlockTranslator.getJavaIdBlockMap().inverse().get(blockToPick).split("\\[")[0]; - InventoryUtils.findOrCreatePickedBlock(session, targetIdentifier); + InventoryUtils.findOrCreateItem(session, BlockTranslator.getPickItem(blockToPick)); } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockEntityPickRequestTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockEntityPickRequestTranslator.java index 4aa044ee452..d03aa7b738b 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockEntityPickRequestTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/bedrock/BedrockEntityPickRequestTranslator.java @@ -110,6 +110,6 @@ public void translate(EntityPickRequestPacket packet, GeyserSession session) { // Verify it is, indeed, an item if (entry == null) return; - InventoryUtils.findOrCreatePickedBlock(session, fullItemName); + InventoryUtils.findOrCreateItem(session, fullItemName); } } diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/world/block/BlockTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/world/block/BlockTranslator.java index 8869199ddd7..8eca822d683 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/world/block/BlockTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/world/block/BlockTranslator.java @@ -75,6 +75,8 @@ public class BlockTranslator { // The index of the collision data in collision.json public static final Int2IntMap JAVA_RUNTIME_ID_TO_COLLISION_INDEX = new Int2IntOpenHashMap(); + private static final Int2ObjectMap JAVA_RUNTIME_ID_TO_PICK_ITEM = new Int2ObjectOpenHashMap<>(); + /** * Java numeric ID to java unique identifier, used for block names in the statistics screen */ @@ -174,6 +176,11 @@ public class BlockTranslator { JAVA_RUNTIME_ID_TO_COLLISION_INDEX.put(javaRuntimeId, collisionIndexNode.intValue()); } + JsonNode pickItemNode = entry.getValue().get("pick_item"); + if (pickItemNode != null) { + JAVA_RUNTIME_ID_TO_PICK_ITEM.put(javaRuntimeId, pickItemNode.textValue()); + } + JAVA_ID_BLOCK_MAP.put(javaId, javaRuntimeId); BlockStateValues.storeBlockStateValues(entry, javaRuntimeId); @@ -362,4 +369,19 @@ public static BiMap getJavaIdBlockMap() { public static int getJavaWaterloggedState(int bedrockId) { return BEDROCK_TO_JAVA_BLOCK_MAP.get(1 << 31 | bedrockId); } + + /** + * Get the item a Java client would receive when pressing + * the Pick Block key on a specific Java block state. + * + * @param javaId The Java runtime id of the block + * @return The Java identifier of the item + */ + public static String getPickItem(int javaId) { + String itemIdentifier = JAVA_RUNTIME_ID_TO_PICK_ITEM.get(javaId); + if (itemIdentifier == null) { + return JAVA_ID_BLOCK_MAP.inverse().get(javaId).split("\\[")[0]; + } + return itemIdentifier; + } } diff --git a/connector/src/main/java/org/geysermc/connector/utils/InventoryUtils.java b/connector/src/main/java/org/geysermc/connector/utils/InventoryUtils.java index e9d7f13a60a..75bd7c94ef7 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/InventoryUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/InventoryUtils.java @@ -46,6 +46,7 @@ import org.geysermc.connector.network.translators.item.ItemEntry; import org.geysermc.connector.network.translators.item.ItemRegistry; import org.geysermc.connector.network.translators.item.ItemTranslator; +import org.geysermc.connector.network.translators.world.block.BlockTranslator; import java.util.Collections; import java.util.Objects; @@ -168,13 +169,17 @@ public static ItemData createUnusableSpaceBlock(String description) { * @param session the Bedrock client's session * @param itemName the Java identifier of the item to search/select */ - public static void findOrCreatePickedBlock(GeyserSession session, String itemName) { + public static void findOrCreateItem(GeyserSession session, String itemName) { // Get the inventory to choose a slot to pick Inventory inventory = session.getInventoryCache().getOpenInventory(); if (inventory == null) { inventory = session.getInventory(); } + if (itemName.equals("minecraft:air")) { + return; + } + // Check hotbar for item for (int i = 36; i < 45; i++) { if (inventory.getItem(i) == null) { diff --git a/connector/src/main/resources/mappings b/connector/src/main/resources/mappings index 3f4707c0d26..143285afb4b 160000 --- a/connector/src/main/resources/mappings +++ b/connector/src/main/resources/mappings @@ -1 +1 @@ -Subproject commit 3f4707c0d26427dfe2ac79eca68e6048732f4412 +Subproject commit 143285afb4bdf4d5ef40ef7a7959477dabf4d34c