diff --git a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java index e2587ee08..4287c24f6 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java @@ -232,6 +232,7 @@ public class GeyserSession implements CommandSender { @Setter private Int2ObjectMap craftingRecipes; private final Set unlockedRecipes; + private AtomicInteger lastRecipeNetId; /** * Saves a list of all stonecutter recipes, for use in a stonecutter inventory. @@ -385,6 +386,7 @@ public class GeyserSession implements CommandSender { this.inventoryFuture = CompletableFuture.completedFuture(null); this.craftingRecipes = new Int2ObjectOpenHashMap<>(); this.unlockedRecipes = new ObjectOpenHashSet<>(); + this.lastRecipeNetId = new AtomicInteger(1); this.spawned = false; this.loggedIn = false; diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/InventoryTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/InventoryTranslator.java index f0f734fcf..4c1c55976 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/inventory/InventoryTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/inventory/InventoryTranslator.java @@ -805,13 +805,12 @@ public abstract class InventoryTranslator { } public boolean checkNetId(GeyserSession session, Inventory inventory, StackRequestSlotInfoData slotInfoData) { - if (slotInfoData.getStackNetworkId() < 0) + int netId = slotInfoData.getStackNetworkId(); + if (netId < 0 || netId == 1) return true; -// if (slotInfoData.getContainer() == ContainerSlotType.CURSOR) //TODO: temporary -// return true; GeyserItemStack currentItem = isCursor(slotInfoData) ? session.getPlayerInventory().getCursor() : inventory.getItem(bedrockSlotToJava(slotInfoData)); - return currentItem.getNetId() == slotInfoData.getStackNetworkId(); + return currentItem.getNetId() == netId; } /** diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaDeclareRecipesTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaDeclareRecipesTranslator.java index 3b4f14d71..7624bf868 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaDeclareRecipesTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/JavaDeclareRecipesTranslator.java @@ -186,10 +186,11 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator { @@ -55,6 +67,56 @@ public class JavaSetSlotTranslator extends PacketTranslator InventoryTranslator translator = session.getInventoryTranslator(); if (translator != null) { + if (packet.getSlot() == 0) { + int gridSize = -1; + if (translator instanceof PlayerInventoryTranslator) { + gridSize = 4; + } + if (translator instanceof CraftingInventoryTranslator) { + gridSize = 9; + } + if (gridSize != -1) { + int offset = gridSize == 4 ? 28 : 32; + int gridWidth = gridSize == 4 ? 2 : 3; + ItemData[] ingredients = new ItemData[gridSize]; + //construct ingredient list and clear slots on client + for (int i = 0; i < gridSize; i++) { + ingredients[i] = inventory.getItem(i + 1).getItemData(session); + + InventorySlotPacket slotPacket = new InventorySlotPacket(); + slotPacket.setContainerId(ContainerId.UI); + slotPacket.setSlot(i + offset); + slotPacket.setItem(ItemData.AIR); + session.sendUpstreamPacket(slotPacket); + } + + CraftingDataPacket craftPacket = new CraftingDataPacket(); + UUID uuid = UUID.fromString("e0a4971a-698c-40fb-95dd-afc8ed16e108"); + craftPacket.getCraftingData().add(CraftingData.fromShaped( + uuid.toString(), + gridWidth, + gridWidth, + Arrays.asList(ingredients), + Collections.singletonList(ItemTranslator.translateToBedrock(session, packet.getItem())), + uuid, + "crafting_table", + 0, + session.getLastRecipeNetId().incrementAndGet() + )); + craftPacket.setCleanRecipes(false); + session.sendUpstreamPacket(craftPacket); + + //restore cleared slots + for (int i = 0; i < gridSize; i++) { + InventorySlotPacket slotPacket = new InventorySlotPacket(); + slotPacket.setContainerId(ContainerId.UI); + slotPacket.setSlot(i + offset); + slotPacket.setItem(ingredients[i]); + session.sendUpstreamPacket(slotPacket); + } + } + } + GeyserItemStack newItem = GeyserItemStack.from(packet.getItem()); inventory.setItem(packet.getSlot(), newItem, session); translator.updateSlot(session, inventory, packet.getSlot());