From ce511e62e17ba18e043f0248a74355c29486af93 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 3 Apr 2021 10:32:41 -0400 Subject: [PATCH] Fix dupe when shift-clicking crafting results out of terminal --- .../terminal/CraftingTerminalScreenHandler.kt | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/CraftingTerminalScreenHandler.kt b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/CraftingTerminalScreenHandler.kt index 6a506f3..13832d0 100644 --- a/src/main/kotlin/net/shadowfacts/phycon/block/terminal/CraftingTerminalScreenHandler.kt +++ b/src/main/kotlin/net/shadowfacts/phycon/block/terminal/CraftingTerminalScreenHandler.kt @@ -1,6 +1,6 @@ package net.shadowfacts.phycon.block.terminal -import alexiil.mc.lib.attributes.item.ItemStackCollections +import net.minecraft.entity.player.PlayerEntity import net.minecraft.entity.player.PlayerInventory import net.minecraft.inventory.CraftingInventory import net.minecraft.inventory.CraftingResultInventory @@ -94,6 +94,36 @@ class CraftingTerminalScreenHandler( terminal.requestItemsForCrafting(maxAmount) } + override fun transferSlot(player: PlayerEntity, slotId: Int): ItemStack { + if (slotId == resultSlot.id && resultSlot.hasStack()) { + val craftingResult = resultSlot.stack + val originalResult = craftingResult.copy() + + // todo: CraftingScreenHandler calls onCraft, but I don't think that's necessary because onStackChanged should handle it + craftingResult.item.onCraft(craftingResult, player.world, player) + + if (!insertItem(craftingResult, playerSlotsStart, playerSlotsEnd, true)) { + return ItemStack.EMPTY + } + resultSlot.onStackChanged(craftingResult, originalResult) + + if (craftingResult.isEmpty) { + resultSlot.stack = ItemStack.EMPTY + } + + if (craftingResult.count == originalResult.count) { + return ItemStack.EMPTY + } + + val taken = resultSlot.onTakeItem(player, craftingResult) + player.dropItem(taken, false) + + return originalResult + } else { + return super.transferSlot(player, slotId) + } + } + // RecipeType.CRAFTING wants a CraftingInventory, but we can't store a CraftingInventory on the BE without a screen handler, so... class CraftingInv(val handler: CraftingTerminalScreenHandler): CraftingInventory(handler, 3, 3) { private val backing = handler.terminal.craftingInv