mimic vanilla crafting solution, 1.10

fixes case where craft item is not consumed but damaged, specifically the ic2 hammer

closes #2479
This commit is contained in:
payonel 2018-09-22 03:12:45 -07:00
parent a5c1419635
commit 7f614c507d

View File

@ -14,13 +14,13 @@ import li.cil.oc.api.machine.Callback
import li.cil.oc.api.machine.Context import li.cil.oc.api.machine.Context
import li.cil.oc.api.network._ import li.cil.oc.api.network._
import li.cil.oc.api.prefab import li.cil.oc.api.prefab
import li.cil.oc.server.agent.Player
import li.cil.oc.util.InventoryUtils import li.cil.oc.util.InventoryUtils
import net.minecraft.entity.player.EntityPlayer import net.minecraft.entity.player.EntityPlayer
import net.minecraft.inventory import net.minecraft.inventory
import net.minecraft.inventory.IInventory import net.minecraft.inventory.IInventory
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
import net.minecraft.item.crafting.CraftingManager import net.minecraft.item.crafting.CraftingManager
import net.minecraftforge.common.ForgeHooks
import net.minecraftforge.common.MinecraftForge import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent
import net.minecraftforge.fml.common.FMLCommonHandler import net.minecraftforge.fml.common.FMLCommonHandler
@ -55,7 +55,7 @@ class UpgradeCrafting(val host: EnvironmentHost with internal.Robot) extends pre
var amountPossible = 0 var amountPossible = 0
def craft(wantedCount: Int): Seq[_] = { def craft(wantedCount: Int): Seq[_] = {
var player = host.player val player = host.player
load(player.inventory) load(player.inventory)
val cm = CraftingManager.getInstance val cm = CraftingManager.getInstance
var countCrafted = 0 var countCrafted = 0
@ -71,27 +71,43 @@ class UpgradeCrafting(val host: EnvironmentHost with internal.Robot) extends pre
break() break()
} }
countCrafted += result.stackSize countCrafted += result.stackSize
FMLCommonHandler.instance.firePlayerCraftingEvent(player, result, this) FMLCommonHandler.instance.firePlayerCraftingEvent(player, result, this)
result.onCrafting(host.world, player, result.stackSize)
ForgeHooks.setCraftingPlayer(player)
val aitemstack = cm.getRemainingItems(this, host.world)
ForgeHooks.setCraftingPlayer(null)
val surplus = mutable.ArrayBuffer.empty[ItemStack] val surplus = mutable.ArrayBuffer.empty[ItemStack]
for (slot <- 0 until getSizeInventory) { for (slot <- 0 until aitemstack.size) {
val stack = getStackInSlot(slot) var clean = getStackInSlot(slot)
if (stack != null) { var used = aitemstack(slot)
// use 1 of this item
if (clean != null) {
decrStackSize(slot, 1) decrStackSize(slot, 1)
val item = stack.getItem clean = getStackInSlot(slot)
if (item.hasContainerItem(stack)) {
val container = item.getContainerItem(stack)
if (container.isItemStackDamageable && container.getItemDamage > container.getMaxDamage) {
MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(player, container, null))
} }
else if (getStackInSlot(slot) != null) {
surplus += container // the item remains
if (used != null) {
// but we thought we used it up
if (clean == null) {
// put it back
setInventorySlotContents(slot, used)
}
else if (ItemStack.areItemsEqual(clean, used) && ItemStack.areItemStackTagsEqual(clean, used)) {
// the recipe doesn't actually consume this item
used.stackSize += used.stackSize
setInventorySlotContents(slot, used)
} }
else { else {
setInventorySlotContents(slot, container) // the used item doesn't stack with the clean (could be a container, could be a damaged tool)
} surplus += used
} }
} }
} }
save(player.inventory) save(player.inventory)
InventoryUtils.addToPlayerInventory(result, player) InventoryUtils.addToPlayerInventory(result, player)
for (stack <- surplus) { for (stack <- surplus) {