Fixed slot offset in crafting upgrade.

Reworked code adding stuff to player inventories a bit to ensure it gets properly synced to clients.
This commit is contained in:
Florian Nücke 2015-02-24 02:30:42 +01:00
parent 507b882f47
commit b34ec529bd
6 changed files with 34 additions and 34 deletions

View File

@ -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

View File

@ -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)
}
}
}

View File

@ -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

View File

@ -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()
}

View File

@ -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
}
}

View File

@ -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.
*/