blob: b6b2b2ebcba3e50dc2930e43eb93e9000f27dc75 [file] [log] [blame] [raw]
package li.cil.oc.server.component
import li.cil.oc.api.machine.Arguments
import li.cil.oc.api.machine.Callback
import li.cil.oc.api.machine.Context
import li.cil.oc.common.tileentity.traits.BundledRedstoneAware
trait RedstoneBundled extends Redstone[BundledRedstoneAware] {
@Callback(direct = true, doc = """function(side:number, color:number):number -- Get the bundled redstone input on the specified side and with the specified color.""")
def getBundledInput(context: Context, args: Arguments): Array[AnyRef] = {
val side = checkSide(args, 0)
val color = checkColor(args, 1)
result(owner.bundledInput(side, color))
}
@Callback(direct = true, doc = """function(side:number, color:number):number -- Get the bundled redstone output on the specified side and with the specified color.""")
def getBundledOutput(context: Context, args: Arguments): Array[AnyRef] = {
val side = checkSide(args, 0)
val color = checkColor(args, 1)
result(owner.bundledOutput(side, color))
}
@Callback(doc = """function(side:number, color:number, value:number):number -- Set the bundled redstone output on the specified side and with the specified color.""")
def setBundledOutput(context: Context, args: Arguments): Array[AnyRef] = {
val side = checkSide(args, 0)
if (args.isTable(1)) {
val table = args.checkTable(1)
(0 to 15).map(color => (color, table.get(color))).foreach {
case (color, number: Number) => owner.bundledOutput(side, color, number.intValue())
case _ =>
}
context.pause(0.1)
result(true)
}
else {
val color = checkColor(args, 1)
val value = args.checkInteger(2)
owner.bundledOutput(side, color, value)
context.pause(0.1)
result(owner.bundledOutput(side, color))
}
}
// ----------------------------------------------------------------------- //
private def checkColor(args: Arguments, index: Int): Int = {
val color = args.checkInteger(index)
if (color < 0 || color > 15)
throw new IllegalArgumentException("invalid color")
color
}
}