|
87 | 87 | import net.minecraft.world.inventory.TransientCraftingContainer;
|
88 | 88 | import net.minecraft.world.item.Item;
|
89 | 89 | import net.minecraft.world.item.MapItem;
|
| 90 | +import net.minecraft.world.item.crafting.CraftingInput; |
90 | 91 | import net.minecraft.world.item.crafting.CraftingRecipe;
|
91 | 92 | import net.minecraft.world.item.crafting.RecipeHolder;
|
92 | 93 | import net.minecraft.world.item.crafting.RecipeType;
|
@@ -1765,25 +1766,34 @@ public ItemCraftResult craftItemResult(ItemStack[] craftingMatrix, World world)
|
1765 | 1766 |
|
1766 | 1767 | private CraftItemCraftResult createItemCraftResult(Optional<RecipeHolder<CraftingRecipe>> recipe, ItemStack itemStack, CraftingContainer inventoryCrafting, ServerLevel worldServer) {
|
1767 | 1768 | CraftItemCraftResult craftItemResult = new CraftItemCraftResult(itemStack);
|
1768 |
| - recipe.map((holder) -> holder.value().getRemainingItems(inventoryCrafting.asCraftInput())).ifPresent((remainingItems) -> { |
1769 |
| - // Set the resulting matrix items and overflow items |
1770 |
| - for (int i = 0; i < remainingItems.size(); ++i) { |
1771 |
| - net.minecraft.world.item.ItemStack itemstack1 = inventoryCrafting.getItem(i); |
1772 |
| - net.minecraft.world.item.ItemStack itemstack2 = (net.minecraft.world.item.ItemStack) remainingItems.get(i); |
| 1769 | + // tl;dr: this is an API adopted implementation of ResultSlot#onTake |
| 1770 | + final CraftingInput.Positioned positionedCraftInput = inventoryCrafting.asPositionedCraftInput(); |
| 1771 | + final CraftingInput craftingInput = positionedCraftInput.input(); |
| 1772 | + recipe.map((holder) -> holder.value().getRemainingItems(craftingInput)).ifPresent((remainingItems) -> { |
1773 | 1773 |
|
1774 |
| - if (!itemstack1.isEmpty()) { |
1775 |
| - inventoryCrafting.removeItem(i, 1); |
1776 |
| - itemstack1 = inventoryCrafting.getItem(i); |
1777 |
| - } |
1778 | 1774 |
|
1779 |
| - if (!itemstack2.isEmpty()) { |
1780 |
| - if (itemstack1.isEmpty()) { |
1781 |
| - inventoryCrafting.setItem(i, itemstack2); |
1782 |
| - } else if (net.minecraft.world.item.ItemStack.isSameItemSameComponents(itemstack1, itemstack2)) { |
1783 |
| - itemstack2.grow(itemstack1.getCount()); |
1784 |
| - inventoryCrafting.setItem(i, itemstack2); |
1785 |
| - } else { |
1786 |
| - craftItemResult.getOverflowItems().add(CraftItemStack.asBukkitCopy(itemstack2)); |
| 1775 | + System.out.println(remainingItems.stream().map(Object::toString).toList()); |
| 1776 | + // Set the resulting matrix items and overflow items |
| 1777 | + for (int h = 0; h < craftingInput.height(); h++) { |
| 1778 | + for (int w = 0; w < craftingInput.width(); w++) { |
| 1779 | + final int inventorySlot = w + positionedCraftInput.left() + (h + positionedCraftInput.top()) * inventoryCrafting.getWidth(); |
| 1780 | + net.minecraft.world.item.ItemStack itemstack1 = inventoryCrafting.getItem(inventorySlot); |
| 1781 | + net.minecraft.world.item.ItemStack itemstack2 = remainingItems.get(w + h * craftingInput.width()); |
| 1782 | + |
| 1783 | + if (!itemstack1.isEmpty()) { |
| 1784 | + inventoryCrafting.removeItem(inventorySlot, 1); |
| 1785 | + itemstack1 = inventoryCrafting.getItem(inventorySlot); |
| 1786 | + } |
| 1787 | + |
| 1788 | + if (!itemstack2.isEmpty()) { |
| 1789 | + if (itemstack1.isEmpty()) { |
| 1790 | + inventoryCrafting.setItem(inventorySlot, itemstack2); |
| 1791 | + } else if (net.minecraft.world.item.ItemStack.isSameItemSameComponents(itemstack1, itemstack2)) { |
| 1792 | + itemstack2.grow(itemstack1.getCount()); |
| 1793 | + inventoryCrafting.setItem(inventorySlot, itemstack2); |
| 1794 | + } else { |
| 1795 | + craftItemResult.getOverflowItems().add(CraftItemStack.asBukkitCopy(itemstack2)); |
| 1796 | + } |
1787 | 1797 | }
|
1788 | 1798 | }
|
1789 | 1799 | }
|
|
0 commit comments