From b34ec529bdfff142530561631d6fa24b6c4fe2e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Tue, 24 Feb 2015 02:30:42 +0100 Subject: [PATCH] Fixed slot offset in crafting upgrade. Reworked code adding stuff to player inventories a bit to ensure it gets properly synced to clients. --- .../scala/li/cil/oc/common/EventHandler.scala | 15 +++----------- .../container/DynamicComponentSlot.scala | 4 ++-- .../scala/li/cil/oc/common/item/Present.scala | 11 ++-------- .../li/cil/oc/server/component/Agent.scala | 6 ++---- .../oc/server/component/UpgradeCrafting.scala | 12 +++++------ .../scala/li/cil/oc/util/InventoryUtils.scala | 20 ++++++++++++++++++- 6 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/main/scala/li/cil/oc/common/EventHandler.scala b/src/main/scala/li/cil/oc/common/EventHandler.scala index 3be2e4b54..5a44d1a8d 100644 --- a/src/main/scala/li/cil/oc/common/EventHandler.scala +++ b/src/main/scala/li/cil/oc/common/EventHandler.scala @@ -221,15 +221,7 @@ object EventHandler { // Presents! val present = api.Items.get("present").createItemStack(1) e.player.worldObj.playSoundAtEntity(e.player, "note.pling", 0.2f, 1f) - if (e.player.inventory.addItemStackToInventory(present)) { - e.player.inventory.markDirty() - if (e.player.openContainer != null) { - e.player.openContainer.detectAndSendChanges() - } - } - else { - e.player.dropPlayerItemWithRandomChoice(present, false) - } + InventoryUtils.addToPlayerInventory(present, e.player) } case _ => // Nope. } @@ -250,9 +242,8 @@ object EventHandler { for (slot <- 0 until e.craftMatrix.getSizeInventory) { val stack = e.craftMatrix.getStackInSlot(slot) if (api.Items.get(stack) == item) { - callback(stack).foreach(extra => if (!e.player.inventory.addItemStackToInventory(extra)) { - e.player.dropPlayerItemWithRandomChoice(extra, false) - }) + callback(stack).foreach(extra => + InventoryUtils.addToPlayerInventory(extra, e.player)) } } true diff --git a/src/main/scala/li/cil/oc/common/container/DynamicComponentSlot.scala b/src/main/scala/li/cil/oc/common/container/DynamicComponentSlot.scala index 81c8e1291..0d296d213 100644 --- a/src/main/scala/li/cil/oc/common/container/DynamicComponentSlot.scala +++ b/src/main/scala/li/cil/oc/common/container/DynamicComponentSlot.scala @@ -3,6 +3,7 @@ package li.cil.oc.common.container import li.cil.oc.client.gui.Icons import li.cil.oc.common import li.cil.oc.common.InventorySlots.InventorySlot +import li.cil.oc.util.InventoryUtils import li.cil.oc.util.SideTracker import net.minecraft.entity.player.EntityPlayer import net.minecraft.inventory.IInventory @@ -36,8 +37,7 @@ class DynamicComponentSlot(val container: Player, inventory: IInventory, index: if (SideTracker.isServer && getHasStack && !isItemValid(getStack)) { val stack = getStack putStack(null) - player.inventory.addItemStackToInventory(stack) - player.dropPlayerItemWithRandomChoice(stack, false) + InventoryUtils.addToPlayerInventory(stack, player) } } } diff --git a/src/main/scala/li/cil/oc/common/item/Present.scala b/src/main/scala/li/cil/oc/common/item/Present.scala index 3548b129d..8b921790d 100644 --- a/src/main/scala/li/cil/oc/common/item/Present.scala +++ b/src/main/scala/li/cil/oc/common/item/Present.scala @@ -4,6 +4,7 @@ import java.util.Random import li.cil.oc.OpenComputers import li.cil.oc.api +import li.cil.oc.util.InventoryUtils import li.cil.oc.util.ItemUtils import net.minecraft.entity.player.EntityPlayer import net.minecraft.item.ItemStack @@ -20,15 +21,7 @@ class Present(val parent: Delegator) extends Delegate { if (!world.isRemote) { world.playSoundAtEntity(player, "random.levelup", 0.2f, 1f) val present = Present.nextPresent() - if (player.inventory.addItemStackToInventory(present)) { - player.inventory.markDirty() - if (player.openContainer != null) { - player.openContainer.detectAndSendChanges() - } - } - else { - player.dropPlayerItemWithRandomChoice(present, false) - } + InventoryUtils.addToPlayerInventory(present, player) } } stack diff --git a/src/main/scala/li/cil/oc/server/component/Agent.scala b/src/main/scala/li/cil/oc/server/component/Agent.scala index 11d8b7a3e..974a07359 100644 --- a/src/main/scala/li/cil/oc/server/component/Agent.scala +++ b/src/main/scala/li/cil/oc/server/component/Agent.scala @@ -11,6 +11,7 @@ import li.cil.oc.server.agent.Player import li.cil.oc.util.BlockPosition import li.cil.oc.util.ExtendedArguments._ import li.cil.oc.util.ExtendedWorld._ +import li.cil.oc.util.InventoryUtils import net.minecraft.entity.Entity import net.minecraft.entity.EntityLivingBase import net.minecraft.entity.item.EntityMinecart @@ -280,10 +281,7 @@ trait Agent extends traits.WorldControl with traits.InventoryControl with traits entity.captureDrops = false for (drop <- entity.capturedDrops) { val stack = drop.getEntityItem - player.inventory.addItemStackToInventory(stack) - if (stack.stackSize > 0) { - player.dropPlayerItemWithRandomChoice(stack, inPlace = false) - } + InventoryUtils.addToPlayerInventory(stack, player) } entity.capturedDrops.clear() } diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeCrafting.scala b/src/main/scala/li/cil/oc/server/component/UpgradeCrafting.scala index a4fa3ab94..0c6020fc2 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeCrafting.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeCrafting.scala @@ -9,6 +9,7 @@ import li.cil.oc.api.machine.Callback import li.cil.oc.api.machine.Context import li.cil.oc.api.network._ import li.cil.oc.api.prefab +import li.cil.oc.util.InventoryUtils import net.minecraft.entity.player.EntityPlayer import net.minecraft.inventory import net.minecraft.item.ItemStack @@ -67,10 +68,9 @@ class UpgradeCrafting(val host: EnvironmentHost with internal.Robot) extends pre } } save() - val inventory = host.player.inventory - inventory.addItemStackToInventory(result) + InventoryUtils.addToPlayerInventory(result, host.player) for (stack <- surplus) { - inventory.addItemStackToInventory(stack) + InventoryUtils.addToPlayerInventory(stack, host.player) } load() } @@ -79,7 +79,7 @@ class UpgradeCrafting(val host: EnvironmentHost with internal.Robot) extends pre } def load() { - val inventory = host.player.inventory + val inventory = host.mainInventory() amountPossible = Int.MaxValue for (slot <- 0 until getSizeInventory) { val stack = inventory.getStackInSlot(toParentSlot(slot)) @@ -91,7 +91,7 @@ class UpgradeCrafting(val host: EnvironmentHost with internal.Robot) extends pre } def save() { - val inventory = host.player.inventory + val inventory = host.mainInventory() for (slot <- 0 until getSizeInventory) { inventory.setInventorySlotContents(toParentSlot(slot), getStackInSlot(slot)) } @@ -100,7 +100,7 @@ class UpgradeCrafting(val host: EnvironmentHost with internal.Robot) extends pre private def toParentSlot(slot: Int) = { val col = slot % 3 val row = slot / 3 - row * 4 + col + 4 // first four are always: tool, card, disk, upgrade + row * 4 + col } } diff --git a/src/main/scala/li/cil/oc/util/InventoryUtils.scala b/src/main/scala/li/cil/oc/util/InventoryUtils.scala index b224504a2..d06862471 100644 --- a/src/main/scala/li/cil/oc/util/InventoryUtils.scala +++ b/src/main/scala/li/cil/oc/util/InventoryUtils.scala @@ -3,6 +3,7 @@ package li.cil.oc.util import li.cil.oc.util.ExtendedWorld._ import net.minecraft.entity.item.EntityItem import net.minecraft.entity.item.EntityMinecartContainer +import net.minecraft.entity.player.EntityPlayer import net.minecraft.inventory.IInventory import net.minecraft.inventory.ISidedInventory import net.minecraft.item.ItemStack @@ -233,7 +234,7 @@ object InventoryUtils { /** * Utility method for dumping all inventory contents into the world. */ - def dropAllSlots(position: BlockPosition, inventory: IInventory) { + def dropAllSlots(position: BlockPosition, inventory: IInventory): Unit = { for (slot <- 0 until inventory.getSizeInventory) { Option(inventory.getStackInSlot(slot)) match { case Some(stack) if stack.stackSize > 0 => @@ -244,6 +245,23 @@ object InventoryUtils { } } + /** + * Try inserting an item stack into a player inventory. If that fails, drop it into the world. + */ + def addToPlayerInventory(stack: ItemStack, player: EntityPlayer): Unit = { + if (stack != null) { + if (player.inventory.addItemStackToInventory(stack)) { + player.inventory.markDirty() + if (player.openContainer != null) { + player.openContainer.detectAndSendChanges() + } + } + if (stack.stackSize > 0) { + player.dropPlayerItemWithRandomChoice(stack, false) + } + } + } + /** * Utility method for spawning an item stack in the world. */