Skip to content

Commit

Permalink
Fix pick block (GeyserMC#1753)
Browse files Browse the repository at this point in the history
* Use pick_item mappings

* Update mappings

* Update mappings and fix wording
  • Loading branch information
davchoo authored Dec 29, 2020
1 parent d1c571d commit fe63a7f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -362,4 +369,19 @@ public static BiMap<String, Integer> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion connector/src/main/resources/mappings
Submodule mappings updated 1 files
+1,761 −0 blocks.json

0 comments on commit fe63a7f

Please sign in to comment.