Fix pick block (#1753)

* Use pick_item mappings

* Update mappings

* Update mappings and fix wording
This commit is contained in:
David Choo 2020-12-29 18:09:42 -05:00 committed by GitHub
parent d1c571d710
commit fe63a7f7ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 5 deletions

View file

@ -46,7 +46,6 @@ public class BedrockBlockPickRequestTranslator extends PacketTranslator<BlockPic
return;
}
String targetIdentifier = BlockTranslator.getJavaIdBlockMap().inverse().get(blockToPick).split("\\[")[0];
InventoryUtils.findOrCreatePickedBlock(session, targetIdentifier);
InventoryUtils.findOrCreateItem(session, BlockTranslator.getPickItem(blockToPick));
}
}

View file

@ -110,6 +110,6 @@ public class BedrockEntityPickRequestTranslator extends PacketTranslator<EntityP
// Verify it is, indeed, an item
if (entry == null) return;
InventoryUtils.findOrCreatePickedBlock(session, fullItemName);
InventoryUtils.findOrCreateItem(session, fullItemName);
}
}

View file

@ -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<String> 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 class BlockTranslator {
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;
}
}

View file

@ -46,6 +46,7 @@ import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
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 class InventoryUtils {
* @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) {

@ -1 +1 @@
Subproject commit 3f4707c0d26427dfe2ac79eca68e6048732f4412
Subproject commit 143285afb4bdf4d5ef40ef7a7959477dabf4d34c