From db876333644416945882f045ed1ce45f89d0a0b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Mon, 15 Dec 2014 05:52:38 +0100 Subject: [PATCH] Fixed drone bounds, cleaned up and fixed inventory interop some. --- .../scala/li/cil/oc/common/entity/Drone.scala | 10 +++------- .../oc/integration/appeng/DriverExportBus.scala | 4 ++-- .../scala/li/cil/oc/server/component/Drone.scala | 10 +++++++++- .../UpgradeInventoryControllerInRobot.scala | 2 +- .../component/traits/InventoryWorldInterop.scala | 10 ++++++++-- .../scala/li/cil/oc/util/InventoryUtils.scala | 15 ++++++++------- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/main/scala/li/cil/oc/common/entity/Drone.scala b/src/main/scala/li/cil/oc/common/entity/Drone.scala index e31db2cf5..6b6e51361 100644 --- a/src/main/scala/li/cil/oc/common/entity/Drone.scala +++ b/src/main/scala/li/cil/oc/common/entity/Drone.scala @@ -33,9 +33,9 @@ class Drone(val world: World) extends Entity(world) with MachineHost with intern // Some basic constants. val gravity = 0.05f // low for slow fall (float down) val drag = 0.8f - val bounds = AxisAlignedBB.getBoundingBox(-8, -3, -8, 8, 3, 8) val maxAcceleration = 0.1f val maxVelocity = 0.4f + setSize(1, 6/16f) // Rendering stuff, purely eyecandy. val targetFlapAngles = Array.fill(4, 2)(0f) @@ -87,10 +87,6 @@ class Drone(val world: World) extends Entity(world) with MachineHost with intern // ----------------------------------------------------------------------- // - override def getBoundingBox = bounds.copy() - - override def getCollisionBox(entity: Entity) = bounds.copy() - override def canBeCollidedWith = true override def canBePushed = true @@ -156,7 +152,7 @@ class Drone(val world: World) extends Entity(world) with MachineHost with intern dataWatcher.addObject(8, int2Integer(0)) dataWatcher.addObject(9, int2Integer(100)) // Status text. - dataWatcher.addObject(10, "Hello\nWorld!") + dataWatcher.addObject(10, "") } def isRunning = dataWatcher.getWatchableObjectByte(2) != 0 @@ -221,7 +217,7 @@ class Drone(val world: World) extends Entity(world) with MachineHost with intern components.updateComponents() setRunning(machine.isRunning) - if (math.abs(lastEnergyUpdate - globalBuffer) > 100) { + if (math.abs(lastEnergyUpdate - globalBuffer) > 100 || world.getTotalWorldTime % 200 == 0) { globalBuffer = machine.node.asInstanceOf[Connector].globalBuffer.toInt globalBufferSize = machine.node.asInstanceOf[Connector].globalBufferSize.toInt } diff --git a/src/main/scala/li/cil/oc/integration/appeng/DriverExportBus.scala b/src/main/scala/li/cil/oc/integration/appeng/DriverExportBus.scala index 29c6198d5..7f2412d1d 100644 --- a/src/main/scala/li/cil/oc/integration/appeng/DriverExportBus.scala +++ b/src/main/scala/li/cil/oc/integration/appeng/DriverExportBus.scala @@ -111,14 +111,14 @@ object DriverExportBus extends driver.Block { for (ais <- stacks if count > 0 && ais != null) { val is = ais.getItemStack is.stackSize = count - if (InventoryUtils.insertIntoInventorySlot(is, inventory, side.getOpposite, targetSlot, count, simulate = true)) { + if (InventoryUtils.insertIntoInventorySlot(is, inventory, Option(side.getOpposite), targetSlot, count, simulate = true)) { ais.setStackSize(count - is.stackSize) val eais = Platform.poweredExtraction(export.getProxy.getEnergy, itemStorage, ais, source) if (eais != null) { val eis = eais.getItemStack count -= eis.stackSize didSomething = true - InventoryUtils.insertIntoInventorySlot(eis, inventory, side.getOpposite, targetSlot) + InventoryUtils.insertIntoInventorySlot(eis, inventory, Option(side.getOpposite), targetSlot) if (eis.stackSize > 0) { eais.setStackSize(eis.stackSize) itemStorage.injectItems(ais, Actionable.MODULATE, source) diff --git a/src/main/scala/li/cil/oc/server/component/Drone.scala b/src/main/scala/li/cil/oc/server/component/Drone.scala index cc3e84691..42f687672 100644 --- a/src/main/scala/li/cil/oc/server/component/Drone.scala +++ b/src/main/scala/li/cil/oc/server/component/Drone.scala @@ -9,6 +9,8 @@ import li.cil.oc.api.network.Visibility import li.cil.oc.api.prefab import li.cil.oc.common.entity import li.cil.oc.util.ExtendedArguments._ +import li.cil.oc.util.InventoryUtils +import net.minecraft.item.ItemStack import net.minecraft.world.WorldServer import net.minecraftforge.common.util.FakePlayerFactory import net.minecraftforge.common.util.ForgeDirection @@ -32,7 +34,13 @@ class Drone(val host: entity.Drone) extends prefab.ManagedEnvironment with trait override def selectedSlot_=(value: Int) = host.selectedSlot = value - override protected def fakePlayer = FakePlayerFactory.get(world.asInstanceOf[WorldServer], Settings.get.fakePlayerProfile) + override protected def fakePlayer = { + val player = FakePlayerFactory.get(world.asInstanceOf[WorldServer], Settings.get.fakePlayerProfile) + player.posX = host.posX + player.posY = host.posY + player.posZ = host.posZ + player + } override protected def checkSideForAction(args: Arguments, n: Int) = args.checkSide(n, ForgeDirection.VALID_DIRECTIONS: _*) diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeInventoryControllerInRobot.scala b/src/main/scala/li/cil/oc/server/component/UpgradeInventoryControllerInRobot.scala index 61aa02cbb..bee0dcf1d 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeInventoryControllerInRobot.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeInventoryControllerInRobot.scala @@ -121,7 +121,7 @@ class UpgradeInventoryControllerInRobot(val host: EnvironmentHost with Robot) ex InventoryUtils.inventoryAt(BlockPosition(host).offset(facing)) match { case Some(inventory) if inventory.isUseableByPlayer(host.player) => val slot = args.checkSlot(inventory, 1) - if (!InventoryUtils.insertIntoInventorySlot(stack, inventory, facing.getOpposite, slot, count)) { + if (!InventoryUtils.insertIntoInventorySlot(stack, inventory, Option(facing.getOpposite), slot, count)) { // Cannot drop into that inventory. return result(false, "inventory full/invalid slot") } diff --git a/src/main/scala/li/cil/oc/server/component/traits/InventoryWorldInterop.scala b/src/main/scala/li/cil/oc/server/component/traits/InventoryWorldInterop.scala index 9eadafc23..5ebda7cf4 100644 --- a/src/main/scala/li/cil/oc/server/component/traits/InventoryWorldInterop.scala +++ b/src/main/scala/li/cil/oc/server/component/traits/InventoryWorldInterop.scala @@ -11,6 +11,8 @@ import li.cil.oc.util.InventoryUtils import li.cil.oc.util.ResultWrapper.result import net.minecraft.entity.item.EntityItem import net.minecraft.item.ItemBlock +import net.minecraft.item.ItemStack +import net.minecraftforge.common.util.ForgeDirection trait InventoryWorldInterop extends InventoryAware with WorldAware with SideRestricted { @Callback @@ -38,7 +40,7 @@ trait InventoryWorldInterop extends InventoryAware with WorldAware with SideRest if (stack != null && stack.stackSize > 0) { InventoryUtils.inventoryAt(world, x + facing.offsetX, y + facing.offsetY, z + facing.offsetZ) match { case Some(inv) if inv.isUseableByPlayer(fakePlayer) => - if (!InventoryUtils.insertIntoInventory(stack, inv, facing.getOpposite, count)) { + if (!InventoryUtils.insertIntoInventory(stack, inv, Option(facing.getOpposite), count)) { // Cannot drop into that inventory. return result(false, "inventory full") } @@ -66,9 +68,13 @@ trait InventoryWorldInterop extends InventoryAware with WorldAware with SideRest def suck(context: Context, args: Arguments): Array[AnyRef] = { val facing = checkSideForAction(args, 0) val count = args.optionalItemCount(1) + val range = { + val tmp = (0 until inventory.getSizeInventory).toIterable + tmp.drop(selectedSlot) ++ tmp.take(selectedSlot) + } if (InventoryUtils.inventoryAt(world, x + facing.offsetX, y + facing.offsetY, z + facing.offsetZ).exists(inventory => { - inventory.isUseableByPlayer(fakePlayer) && InventoryUtils.extractFromInventory(fakePlayer.inventory.addItemStackToInventory, inventory, facing.getOpposite, count) + inventory.isUseableByPlayer(fakePlayer) && InventoryUtils.extractFromInventory(InventoryUtils.insertIntoInventory(_, this.inventory, slots = Option(range)), inventory, facing.getOpposite, count) })) { context.pause(Settings.get.suckDelay) result(true) diff --git a/src/main/scala/li/cil/oc/util/InventoryUtils.scala b/src/main/scala/li/cil/oc/util/InventoryUtils.scala index 992f76254..1396c07b6 100644 --- a/src/main/scala/li/cil/oc/util/InventoryUtils.scala +++ b/src/main/scala/li/cil/oc/util/InventoryUtils.scala @@ -53,10 +53,10 @@ object InventoryUtils { * The number of items inserted can be limited, to avoid unnecessary * changes to the inventory the stack may come from, for example. */ - def insertIntoInventorySlot(stack: ItemStack, inventory: IInventory, side: ForgeDirection, slot: Int, limit: Int = 64, simulate: Boolean = false) = + def insertIntoInventorySlot(stack: ItemStack, inventory: IInventory, side: Option[ForgeDirection], slot: Int, limit: Int = 64, simulate: Boolean = false) = (stack != null && limit > 0) && { - val isSideValidForSlot = inventory match { - case inventory: ISidedInventory => inventory.canInsertItem(slot, stack, side.ordinal) + val isSideValidForSlot = (inventory, side) match { + case (inventory: ISidedInventory, Some(s)) => inventory.canInsertItem(slot, stack, s.ordinal) case _ => true } (stack.stackSize > 0 && inventory.isItemValidForSlot(slot, stack) && isSideValidForSlot) && { @@ -152,14 +152,15 @@ object InventoryUtils { * item stack will be adjusted to reflect the number items inserted, by * having its size decremented accordingly. */ - def insertIntoInventory(stack: ItemStack, inventory: IInventory, side: ForgeDirection, limit: Int = 64, simulate: Boolean = false) = + def insertIntoInventory(stack: ItemStack, inventory: IInventory, side: Option[ForgeDirection] = None, limit: Int = 64, simulate: Boolean = false, slots: Option[Iterable[Int]] = None) = (stack != null && limit > 0) && { var success = false var remaining = limit + val range = slots.getOrElse(0 until inventory.getSizeInventory) val shouldTryMerge = !stack.isItemStackDamageable && stack.getMaxStackSize > 1 && inventory.getInventoryStackLimit > 1 if (shouldTryMerge) { - for (slot <- 0 until inventory.getSizeInventory) { + for (slot <- range) { val stackSize = stack.stackSize if ((inventory.getStackInSlot(slot) != null) && insertIntoInventorySlot(stack, inventory, side, slot, remaining, simulate)) { remaining -= stackSize - stack.stackSize @@ -168,7 +169,7 @@ object InventoryUtils { } } - for (slot <- 0 until inventory.getSizeInventory) { + for (slot <- range) { val stackSize = stack.stackSize if ((inventory.getStackInSlot(slot) == null) && insertIntoInventorySlot(stack, inventory, side, slot, remaining, simulate)) { remaining -= stackSize - stack.stackSize @@ -198,7 +199,7 @@ object InventoryUtils { * 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, _, side, limit, simulate)) + inventoryAt(world, x, y, z).exists(insertIntoInventory(stack, _, Option(side), limit, simulate)) /** * Utility method for calling extractFromInventory on an inventory