Little bit of cleanup in InventoryUtils.

This commit is contained in:
Florian Nücke 2014-12-15 23:21:41 +01:00
parent c5e79df8b5
commit 67b7da78a2
5 changed files with 21 additions and 18 deletions

View File

@ -9,6 +9,7 @@ import li.cil.oc.api.network.Visibility
import li.cil.oc.common.Tier
import li.cil.oc.common.inventory.ServerInventory
import li.cil.oc.server.{PacketSender => ServerPacketSender}
import li.cil.oc.util.BlockPosition
import li.cil.oc.util.ExtendedNBT._
import li.cil.oc.util.InventoryUtils
import li.cil.oc.util.ItemUtils
@ -203,7 +204,7 @@ class Disassembler extends traits.Environment with traits.PowerAcceptor with tra
private def drop(stack: ItemStack) {
if (stack != null) {
for (side <- ForgeDirection.VALID_DIRECTIONS if stack.stackSize > 0) {
InventoryUtils.insertIntoInventoryAt(stack, world, x + side.offsetX, y + side.offsetY, z + side.offsetZ, side.getOpposite)
InventoryUtils.insertIntoInventoryAt(stack, BlockPosition(this).offset(side), side.getOpposite)
}
if (stack.stackSize > 0) {
spawnStackInWorld(stack, ForgeDirection.UP)

View File

@ -16,6 +16,7 @@ import li.cil.oc.api.machine.Callback
import li.cil.oc.api.machine.Context
import li.cil.oc.api.network.Component
import li.cil.oc.integration.ManagedTileEntityEnvironment
import li.cil.oc.util.BlockPosition
import li.cil.oc.util.ExtendedArguments._
import li.cil.oc.util.InventoryUtils
import li.cil.oc.util.ResultWrapper._
@ -85,8 +86,7 @@ object DriverExportBus extends driver.Block {
val side = args.checkSide(0, ForgeDirection.VALID_DIRECTIONS: _*)
host.getPart(side) match {
case export: PartExportBus =>
val location = host.getLocation
InventoryUtils.inventoryAt(location.getWorld, location.x + side.offsetX, location.y + side.offsetY, location.z + side.offsetZ) match {
InventoryUtils.inventoryAt(BlockPosition(host.getLocation).offset(side)) match {
case Some(inventory) =>
val targetSlot = args.checkSlot(inventory, 1)
val config = export.getInventoryByName("config")

View File

@ -37,7 +37,7 @@ trait InventoryWorldControl extends InventoryAware with WorldAware with SideRest
val count = args.optionalItemCount(1)
val stack = inventory.getStackInSlot(selectedSlot)
if (stack != null && stack.stackSize > 0) {
InventoryUtils.inventoryAt(world, x + facing.offsetX, y + facing.offsetY, z + facing.offsetZ) match {
InventoryUtils.inventoryAt(BlockPosition(x, y, z, Option(world)).offset(facing)) match {
case Some(inv) if inv.isUseableByPlayer(fakePlayer) =>
if (!InventoryUtils.insertIntoInventory(stack, inv, Option(facing.getOpposite), count)) {
// Cannot drop into that inventory.
@ -68,7 +68,7 @@ trait InventoryWorldControl extends InventoryAware with WorldAware with SideRest
val facing = checkSideForAction(args, 0)
val count = args.optionalItemCount(1)
if (InventoryUtils.inventoryAt(world, x + facing.offsetX, y + facing.offsetY, z + facing.offsetZ).exists(inventory => {
if (InventoryUtils.inventoryAt(BlockPosition(x, y, z, Option(world)).offset(facing)).exists(inventory => {
inventory.isUseableByPlayer(fakePlayer) && InventoryUtils.extractFromInventory(InventoryUtils.insertIntoInventory(_, this.inventory, slots = Option(insertionSlots)), inventory, facing.getOpposite, count)
})) {
context.pause(Settings.get.suckDelay)

View File

@ -1,6 +1,9 @@
package li.cil.oc.util
import appeng.api.util.DimensionalCoord
import cpw.mods.fml.common.Optional
import li.cil.oc.api.driver.EnvironmentHost
import li.cil.oc.integration.Mods
import net.minecraft.util.AxisAlignedBB
import net.minecraft.util.ChunkCoordinates
import net.minecraft.util.Vec3
@ -45,4 +48,7 @@ object BlockPosition {
def apply(x: Double, y: Double, z: Double) = new BlockPosition(x, y, z, None)
def apply(host: EnvironmentHost) = new BlockPosition(host)
@Optional.Method(modid = Mods.IDs.AppliedEnergistics2)
def apply(coord: DimensionalCoord) = new BlockPosition(coord.x, coord.y, coord.z, Option(coord.getWorld))
}

View File

@ -1,13 +1,12 @@
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.inventory.IInventory
import net.minecraft.inventory.ISidedInventory
import net.minecraft.item.ItemStack
import net.minecraft.tileentity.TileEntityChest
import net.minecraft.util.AxisAlignedBB
import net.minecraft.world.World
import net.minecraftforge.common.util.ForgeDirection
import scala.collection.convert.WrapAsScala._
@ -19,20 +18,17 @@ object InventoryUtils {
* This performs special handling for (double-)chests and also checks for
* mine carts with chests.
*/
def inventoryAt(world: World, x: Int, y: Int, z: Int): Option[IInventory] = {
if (world.blockExists(x, y, z)) world.getTileEntity(x, y, z) match {
def inventoryAt(position: BlockPosition): Option[IInventory] = position.world match {
case Some(world) if world.blockExists(position) => world.getTileEntity(position) match {
case chest: TileEntityChest => Option(net.minecraft.init.Blocks.chest.func_149951_m(world, chest.xCoord, chest.yCoord, chest.zCoord))
case inventory: IInventory => Some(inventory)
case _ => world.getEntitiesWithinAABB(classOf[EntityMinecartContainer],
AxisAlignedBB.getBoundingBox(x, y, z, x + 1, y + 1, z + 1)).
case _ => world.getEntitiesWithinAABB(classOf[EntityMinecartContainer], position.bounds).
map(_.asInstanceOf[EntityMinecartContainer]).
find(!_.isDead)
}
else None
case _ => None
}
def inventoryAt(blockPos: BlockPosition): Option[IInventory] = inventoryAt(blockPos.world.get, blockPos.x, blockPos.y, blockPos.z)
/**
* Inserts a stack into an inventory.
* <p/>
@ -199,15 +195,15 @@ object InventoryUtils {
* Utility method for calling <tt>insertIntoInventory</tt> on an inventory
* in the world.
*/
def insertIntoInventoryAt(stack: ItemStack, world: World, x: Int, y: Int, z: Int, side: ForgeDirection, limit: Int = 64, simulate: Boolean = false): Boolean =
inventoryAt(world, x, y, z).exists(insertIntoInventory(stack, _, Option(side), limit, simulate))
def insertIntoInventoryAt(stack: ItemStack, position: BlockPosition, side: ForgeDirection, limit: Int = 64, simulate: Boolean = false): Boolean =
inventoryAt(position).exists(insertIntoInventory(stack, _, Option(side), limit, simulate))
/**
* Utility method for calling <tt>extractFromInventory</tt> on an inventory
* in the world.
*/
def extractFromInventoryAt(consumer: (ItemStack) => Unit, world: World, x: Int, y: Int, z: Int, side: ForgeDirection, limit: Int = 64) =
inventoryAt(world, x, y, z).exists(extractFromInventory(consumer, _, side, limit))
def extractFromInventoryAt(consumer: (ItemStack) => Unit, position: BlockPosition, side: ForgeDirection, limit: Int = 64) =
inventoryAt(position).exists(extractFromInventory(consumer, _, side, limit))
/**
* Utility method for dropping contents from a single inventory slot into