From 14f54bd8457f5fc954841db1bf553095d27ddc8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sun, 4 Oct 2015 17:28:51 +0200 Subject: [PATCH] Pulled together side argument checking a bit. --- .../common/tileentity/Microcontroller.scala | 4 ++-- .../integration/appeng/DriverExportBus.scala | 2 +- .../appeng/PartEnvironmentBase.scala | 4 ++-- .../cil/oc/server/component/DebugCard.scala | 6 +++--- .../li/cil/oc/server/component/Drone.scala | 2 +- .../li/cil/oc/server/component/Geolyzer.scala | 4 ++-- .../cil/oc/server/component/Transposer.scala | 3 +-- .../UpgradeInventoryController.scala | 5 ++--- .../oc/server/component/UpgradeLeash.scala | 3 +-- .../oc/server/component/UpgradePiston.scala | 2 +- .../component/UpgradeSignInAdapter.scala | 5 ++--- .../component/UpgradeTankController.scala | 5 ++--- .../li/cil/oc/util/ExtendedArguments.scala | 21 ++++++++++++------- 13 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/main/scala/li/cil/oc/common/tileentity/Microcontroller.scala b/src/main/scala/li/cil/oc/common/tileentity/Microcontroller.scala index a4237eefc..783c9fe4b 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/Microcontroller.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/Microcontroller.scala @@ -99,13 +99,13 @@ class Microcontroller extends traits.PowerAcceptor with traits.Hub with traits.C @Callback(direct = true, doc = """function(side:number):boolean -- Get whether network messages are sent via the specified side.""") def isSideOpen(context: Context, args: Arguments): Array[AnyRef] = { - val side = args.checkSide(0, ForgeDirection.VALID_DIRECTIONS.filter(_ != facing): _*) + val side = args.checkSideExcept(0, facing) result(outputSides(side.ordinal())) } @Callback(doc = """function(side:number, open:boolean):boolean -- Set whether network messages are sent via the specified side.""") def setSideOpen(context: Context, args: Arguments): Array[AnyRef] = { - val side = args.checkSide(0, ForgeDirection.VALID_DIRECTIONS.filter(_ != facing): _*) + val side = args.checkSideExcept(0, facing) val oldValue = outputSides(side.ordinal()) outputSides(side.ordinal()) = args.checkBoolean(1) result(oldValue) 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 27cf4fc0b..209b9eea8 100644 --- a/src/main/scala/li/cil/oc/integration/appeng/DriverExportBus.scala +++ b/src/main/scala/li/cil/oc/integration/appeng/DriverExportBus.scala @@ -61,7 +61,7 @@ object DriverExportBus extends driver.Block with EnvironmentAware { @Callback(doc = "function(side:number, slot:number):boolean -- Make the export bus facing the specified direction perform a single export operation into the specified slot.") def exportIntoSlot(context: Context, args: Arguments): Array[AnyRef] = { - val side = args.checkSide(0, ForgeDirection.VALID_DIRECTIONS: _*) + val side = args.checkSideAny(0) host.getPart(side) match { case export: PartExportBus => InventoryUtils.inventoryAt(BlockPosition(host.getLocation).offset(side)) match { diff --git a/src/main/scala/li/cil/oc/integration/appeng/PartEnvironmentBase.scala b/src/main/scala/li/cil/oc/integration/appeng/PartEnvironmentBase.scala index fe75b5ffc..06e184150 100644 --- a/src/main/scala/li/cil/oc/integration/appeng/PartEnvironmentBase.scala +++ b/src/main/scala/li/cil/oc/integration/appeng/PartEnvironmentBase.scala @@ -18,7 +18,7 @@ trait PartEnvironmentBase extends ManagedEnvironment { // function(side:number[, slot:number]):table def getPartConfig[PartType <: ISegmentedInventory : ClassTag](context: Context, args: Arguments): Array[AnyRef] = { - val side = args.checkSide(0, ForgeDirection.VALID_DIRECTIONS: _*) + val side = args.checkSideAny(0) host.getPart(side) match { case part: PartType => val config = part.getInventoryByName("config") @@ -31,7 +31,7 @@ trait PartEnvironmentBase extends ManagedEnvironment { // function(side:number[, slot:number][, database:address, entry:number[, size:number]]):boolean def setPartConfig[PartType <: ISegmentedInventory : ClassTag](context: Context, args: Arguments): Array[AnyRef] = { - val side = args.checkSide(0, ForgeDirection.VALID_DIRECTIONS: _*) + val side = args.checkSideAny(0) host.getPart(side) match { case part: PartType => val config = part.getInventoryByName("config") diff --git a/src/main/scala/li/cil/oc/server/component/DebugCard.scala b/src/main/scala/li/cil/oc/server/component/DebugCard.scala index 1fe7d5bb2..5e5cd39c1 100644 --- a/src/main/scala/li/cil/oc/server/component/DebugCard.scala +++ b/src/main/scala/li/cil/oc/server/component/DebugCard.scala @@ -501,7 +501,7 @@ object DebugCard { val tagJson = args.checkString(3) val tag = if (Strings.isNullOrEmpty(tagJson)) null else JsonToNBT.func_150315_a(tagJson).asInstanceOf[NBTTagCompound] val position = BlockPosition(args.checkDouble(4), args.checkDouble(5), args.checkDouble(6), world) - val side = args.checkSide(7, ForgeDirection.VALID_DIRECTIONS: _*) + val side = args.checkSideAny(7) InventoryUtils.inventoryAt(position) match { case Some(inventory) => val stack = new ItemStack(item, count, damage) @@ -535,7 +535,7 @@ object DebugCard { } val amount = args.checkInteger(1) val position = BlockPosition(args.checkDouble(2), args.checkDouble(3), args.checkDouble(4), world) - val side = args.checkSide(5, ForgeDirection.VALID_DIRECTIONS: _*) + val side = args.checkSideAny(5) world.getTileEntity(position) match { case handler: IFluidHandler => result(handler.fill(side, new FluidStack(fluid, amount), true)) case _ => result(Unit, "no tank") @@ -547,7 +547,7 @@ object DebugCard { checkEnabled() val amount = args.checkInteger(0) val position = BlockPosition(args.checkDouble(1), args.checkDouble(2), args.checkDouble(3), world) - val side = args.checkSide(4, ForgeDirection.VALID_DIRECTIONS: _*) + val side = args.checkSideAny(4) world.getTileEntity(position) match { case handler: IFluidHandler => result(handler.drain(side, amount, true)) case _ => result(Unit, "no tank") 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 add7f1fd1..ee0e68207 100644 --- a/src/main/scala/li/cil/oc/server/component/Drone.scala +++ b/src/main/scala/li/cil/oc/server/component/Drone.scala @@ -20,7 +20,7 @@ class Drone(val agent: entity.Drone) extends prefab.ManagedEnvironment with Agen create() override protected def checkSideForAction(args: Arguments, n: Int) = - args.checkSide(n, ForgeDirection.VALID_DIRECTIONS: _*) + args.checkSideAny(n) override protected def suckableItems(side: ForgeDirection) = entitiesInBlock(position) ++ super.suckableItems(side) diff --git a/src/main/scala/li/cil/oc/server/component/Geolyzer.scala b/src/main/scala/li/cil/oc/server/component/Geolyzer.scala index b1b609218..4ea1e0a2c 100644 --- a/src/main/scala/li/cil/oc/server/component/Geolyzer.scala +++ b/src/main/scala/li/cil/oc/server/component/Geolyzer.scala @@ -54,7 +54,7 @@ class Geolyzer(val host: EnvironmentHost) extends prefab.ManagedEnvironment { @Callback(doc = """function(side:number[,options:table]):table -- Get some information on a directly adjacent block.""") def analyze(computer: Context, args: Arguments): Array[AnyRef] = if (Settings.get.allowItemStackInspection) { - val side = args.checkSide(0, ForgeDirection.VALID_DIRECTIONS: _*) + val side = args.checkSideAny(0) val globalSide = host match { case rotatable: internal.Rotatable => rotatable.toGlobal(side) case _ => side @@ -74,7 +74,7 @@ class Geolyzer(val host: EnvironmentHost) extends prefab.ManagedEnvironment { @Callback(doc = """function(side:number, dbAddress:string, dbSlot:number):boolean -- Store an item stack representation of the block on the specified side in a database component.""") def store(computer: Context, args: Arguments): Array[AnyRef] = { - val side = args.checkSide(0, ForgeDirection.VALID_DIRECTIONS: _*) + val side = args.checkSideAny(0) val globalSide = host match { case rotatable: internal.Rotatable => rotatable.toGlobal(side) case _ => side diff --git a/src/main/scala/li/cil/oc/server/component/Transposer.scala b/src/main/scala/li/cil/oc/server/component/Transposer.scala index 8cea9a26b..aa9980d28 100644 --- a/src/main/scala/li/cil/oc/server/component/Transposer.scala +++ b/src/main/scala/li/cil/oc/server/component/Transposer.scala @@ -10,7 +10,6 @@ import li.cil.oc.common.tileentity import li.cil.oc.server.{PacketSender => ServerPacketSender} import li.cil.oc.util.BlockPosition import li.cil.oc.util.ExtendedArguments._ -import net.minecraftforge.common.util.ForgeDirection import scala.language.existentials @@ -23,7 +22,7 @@ object Transposer { create() override protected def checkSideForAction(args: Arguments, n: Int) = - args.checkSide(n, ForgeDirection.VALID_DIRECTIONS: _*) + args.checkSideAny(n) override def onTransferContents(): Option[String] = { if (node.tryChangeBuffer(-Settings.get.transposerCost)) None diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeInventoryController.scala b/src/main/scala/li/cil/oc/server/component/UpgradeInventoryController.scala index a675306bf..b3a2b67a8 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeInventoryController.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeInventoryController.scala @@ -11,7 +11,6 @@ import li.cil.oc.api.prefab import li.cil.oc.common.tileentity import li.cil.oc.util.BlockPosition import li.cil.oc.util.ExtendedArguments._ -import net.minecraftforge.common.util.ForgeDirection object UpgradeInventoryController { @@ -24,7 +23,7 @@ object UpgradeInventoryController { override def position = BlockPosition(host) - override protected def checkSideForAction(args: Arguments, n: Int) = args.checkSide(n, ForgeDirection.VALID_DIRECTIONS: _*) + override protected def checkSideForAction(args: Arguments, n: Int) = args.checkSideAny(n) } class Drone(val host: EnvironmentHost with internal.Agent) extends prefab.ManagedEnvironment with traits.InventoryAnalytics with traits.InventoryWorldControlMk2 with traits.WorldInventoryAnalytics { @@ -42,7 +41,7 @@ object UpgradeInventoryController { override def selectedSlot_=(value: Int) = host.setSelectedSlot(value) - override protected def checkSideForAction(args: Arguments, n: Int) = args.checkSide(n, ForgeDirection.VALID_DIRECTIONS: _*) + override protected def checkSideForAction(args: Arguments, n: Int) = args.checkSideAny(n) } class Robot(val host: EnvironmentHost with tileentity.Robot) extends prefab.ManagedEnvironment with traits.InventoryAnalytics with traits.InventoryWorldControlMk2 with traits.WorldInventoryAnalytics { diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala index 50a0e6f13..69722cdf0 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeLeash.scala @@ -19,7 +19,6 @@ import net.minecraft.entity.EntityLiving import net.minecraft.nbt.NBTTagCompound import net.minecraft.nbt.NBTTagString import net.minecraftforge.common.util.Constants.NBT -import net.minecraftforge.common.util.ForgeDirection import scala.collection.mutable @@ -35,7 +34,7 @@ class UpgradeLeash(val host: Entity) extends prefab.ManagedEnvironment with trai @Callback(doc = """function(side:number):boolean -- Tries to put an entity on the specified side of the device onto a leash.""") def leash(context: Context, args: Arguments): Array[AnyRef] = { if (leashedEntities.size >= 8) return result(Unit, "too many leashed entities") - val side = args.checkSide(0, ForgeDirection.VALID_DIRECTIONS: _*) + val side = args.checkSideAny(0) val nearBounds = position.bounds val farBounds = nearBounds.offset(side.offsetX * 2.0, side.offsetY * 2.0, side.offsetZ * 2.0) val bounds = nearBounds.func_111270_a(farBounds) diff --git a/src/main/scala/li/cil/oc/server/component/UpgradePiston.scala b/src/main/scala/li/cil/oc/server/component/UpgradePiston.scala index bfe12cb8f..71b3118b2 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradePiston.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradePiston.scala @@ -43,7 +43,7 @@ abstract class UpgradePiston(val host: EnvironmentHost) extends prefab.ManagedEn object UpgradePiston { class Drone(drone: internal.Drone) extends UpgradePiston(drone) { - override def pushDirection(args: Arguments, index: Int) = args.optSide(index, ForgeDirection.SOUTH, ForgeDirection.VALID_DIRECTIONS: _*) + override def pushDirection(args: Arguments, index: Int) = args.optSideAny(index, ForgeDirection.SOUTH) } class Tablet(tablet: internal.Tablet) extends Rotatable(tablet) { diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeSignInAdapter.scala b/src/main/scala/li/cil/oc/server/component/UpgradeSignInAdapter.scala index e18ec5f85..164485231 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeSignInAdapter.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeSignInAdapter.scala @@ -7,7 +7,6 @@ import li.cil.oc.api.machine.Callback import li.cil.oc.api.machine.Context import li.cil.oc.api.network._ import li.cil.oc.util.ExtendedArguments._ -import net.minecraftforge.common.util.ForgeDirection class UpgradeSignInAdapter(val host: EnvironmentHost) extends UpgradeSign { override val node = Network.newNode(this, Visibility.Network). @@ -18,8 +17,8 @@ class UpgradeSignInAdapter(val host: EnvironmentHost) extends UpgradeSign { // ----------------------------------------------------------------------- // @Callback(doc = """function(side:number):string -- Get the text on the sign on the specified side of the adapter.""") - def getValue(context: Context, args: Arguments): Array[AnyRef] = super.getValue(findSign(args.checkSide(0, ForgeDirection.VALID_DIRECTIONS: _*))) + def getValue(context: Context, args: Arguments): Array[AnyRef] = super.getValue(findSign(args.checkSideAny(0))) @Callback(doc = """function(side:number, value:string):string -- Set the text on the sign on the specified side of the adapter.""") - def setValue(context: Context, args: Arguments): Array[AnyRef] = super.setValue(findSign(args.checkSide(0, ForgeDirection.VALID_DIRECTIONS: _*)), args.checkString(1)) + def setValue(context: Context, args: Arguments): Array[AnyRef] = super.setValue(findSign(args.checkSideAny(0)), args.checkString(1)) } diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeTankController.scala b/src/main/scala/li/cil/oc/server/component/UpgradeTankController.scala index 7f3e2b1c1..b52741e78 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeTankController.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeTankController.scala @@ -9,7 +9,6 @@ import li.cil.oc.api.prefab import li.cil.oc.common.tileentity import li.cil.oc.util.BlockPosition import li.cil.oc.util.ExtendedArguments._ -import net.minecraftforge.common.util.ForgeDirection object UpgradeTankController { @@ -22,7 +21,7 @@ object UpgradeTankController { override def position = BlockPosition(host) - override protected def checkSideForAction(args: Arguments, n: Int) = args.checkSide(n, ForgeDirection.VALID_DIRECTIONS: _*) + override protected def checkSideForAction(args: Arguments, n: Int) = args.checkSideAny(n) } class Drone(val host: EnvironmentHost with internal.Agent) extends prefab.ManagedEnvironment with traits.TankInventoryControl with traits.WorldTankAnalytics { @@ -44,7 +43,7 @@ object UpgradeTankController { override def selectedTank_=(value: Int) = host.setSelectedTank(value) - override protected def checkSideForAction(args: Arguments, n: Int) = args.checkSide(n, ForgeDirection.VALID_DIRECTIONS: _*) + override protected def checkSideForAction(args: Arguments, n: Int) = args.checkSideAny(n) } class Robot(val host: EnvironmentHost with tileentity.Robot) extends prefab.ManagedEnvironment with traits.TankInventoryControl with traits.WorldTankAnalytics { diff --git a/src/main/scala/li/cil/oc/util/ExtendedArguments.scala b/src/main/scala/li/cil/oc/util/ExtendedArguments.scala index 06bc49d89..c8bc71ff4 100644 --- a/src/main/scala/li/cil/oc/util/ExtendedArguments.scala +++ b/src/main/scala/li/cil/oc/util/ExtendedArguments.scala @@ -42,6 +42,18 @@ object ExtendedArguments { tank } + def checkSideAny(index: Int) = checkSide(index, ForgeDirection.VALID_DIRECTIONS: _*) + + def optSideAny(index: Int, default: ForgeDirection) = + if (!isDefined(index)) default + else checkSideAny(index) + + def checkSideExcept(index: Int, invalid: ForgeDirection*) = checkSide(index, ForgeDirection.VALID_DIRECTIONS.filterNot(invalid.contains): _*) + + def optSideExcept(index: Int, default: ForgeDirection, invalid: ForgeDirection*) = + if (!isDefined(index)) default + else checkSideExcept(index, invalid: _*) + def checkSideForAction(index: Int) = checkSide(index, ForgeDirection.SOUTH, ForgeDirection.UP, ForgeDirection.DOWN) def optSideForAction(index: Int, default: ForgeDirection) = @@ -54,13 +66,13 @@ object ExtendedArguments { if (!isDefined(index)) default else checkSideForMovement(index) - def checkSideForFace(index: Int, facing: ForgeDirection) = checkSide(index, ForgeDirection.VALID_DIRECTIONS.filter(_ != facing.getOpposite): _*) + def checkSideForFace(index: Int, facing: ForgeDirection) = checkSideExcept(index, facing.getOpposite) def optSideForFace(index: Int, default: ForgeDirection) = if (!isDefined(index)) default else checkSideForAction(index) - def checkSide(index: Int, allowed: ForgeDirection*) = { + private def checkSide(index: Int, allowed: ForgeDirection*) = { val side = args.checkInteger(index) if (side < 0 || side > 5) { throw new IllegalArgumentException("invalid side") @@ -70,11 +82,6 @@ object ExtendedArguments { else throw new IllegalArgumentException("unsupported side") } - def optSide(index: Int, default: ForgeDirection, allowed: ForgeDirection*) = { - if (!isDefined(index)) default - else checkSide(index, allowed: _*) - } - private def isDefined(index: Int) = index >= 0 && index < args.count() private def hasValue(index: Int) = args.checkAny(index) != null