Pulled together side argument checking a bit.

This commit is contained in:
Florian Nücke 2015-10-04 17:28:51 +02:00
parent 05845fa917
commit 14f54bd845
13 changed files with 34 additions and 32 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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