diff --git a/src/main/scala/li/cil/oc/server/component/UpgradeDatabase.scala b/src/main/scala/li/cil/oc/server/component/UpgradeDatabase.scala index 92475e0b7..503de71bf 100644 --- a/src/main/scala/li/cil/oc/server/component/UpgradeDatabase.scala +++ b/src/main/scala/li/cil/oc/server/component/UpgradeDatabase.scala @@ -1,9 +1,14 @@ package li.cil.oc.server.component import com.google.common.hash.Hashing -import li.cil.oc.api.machine.{Arguments, Callback, Context} -import li.cil.oc.api.network.{Component, Visibility} -import li.cil.oc.api.{Network, internal, prefab} +import li.cil.oc.api.Network +import li.cil.oc.api.internal +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.api.network.Component +import li.cil.oc.api.network.Visibility +import li.cil.oc.api.prefab import li.cil.oc.util.ExtendedArguments._ import li.cil.oc.util.ExtendedNBT._ import net.minecraft.inventory.IInventory @@ -37,21 +42,38 @@ class UpgradeDatabase(val data: IInventory) extends prefab.ManagedEnvironment wi @Callback(doc = "function(hash:string):number -- Get the index of an item stack with the specified hash. Returns a negative value if no such stack was found.") def indexOf(context: Context, args: Arguments): Array[AnyRef] = result(indexOf(args.checkString(0), 1)) + @Callback(doc = "function(slot:number):boolean -- Clears the specified slot. Returns true if there was something in the slot before.") + def clearSlot(context: Context, args: Arguments): Array[AnyRef] = { + val slot = args.checkInteger(0) + val nonEmpty = data.getStackInSlot(slot) != null + data.setInventorySlotContents(slot, null) + result(nonEmpty) + } + + @Callback(doc = "function(fromSlot:number, toSlot:number[, address:string]):boolean -- Copies an entry to another slot, optionally to another database. Returns true if something was overwritten.") + def copy(context: Context, args: Arguments): Array[AnyRef] = { + val fromSlot = args.checkSlot(data, 0) + val entry = data.getStackInSlot(fromSlot) + def set(inventory: IInventory) = { + val toSlot = args.checkSlot(inventory, 1) + val nonEmpty = inventory.getStackInSlot(toSlot) != null + inventory.setInventorySlotContents(toSlot, entry) + result(nonEmpty) + } + if (args.count > 2) withDatabase(args.checkString(2), database => set(database.data)) + else set(data) + } + @Callback(doc = "function(address:string):number -- Copies the data stored in this database to another database with the specified address.") def clone(context: Context, args: Arguments): Array[AnyRef] = { - node.network.node(args.checkString(0)) match { - case component: Component => component.host match { - case database: UpgradeDatabase => - val numberToCopy = math.min(data.getSizeInventory, database.data.getSizeInventory) - for (slot <- 0 until numberToCopy) { - database.data.setInventorySlotContents(slot, data.getStackInSlot(slot)) - } - context.pause(0.25) - result(numberToCopy) - case _ => throw new IllegalArgumentException("not a database") + withDatabase(args.checkString(0), database => { + val numberToCopy = math.min(data.getSizeInventory, database.data.getSizeInventory) + for (slot <- 0 until numberToCopy) { + database.data.setInventorySlotContents(slot, data.getStackInSlot(slot)) } - case _ => throw new IllegalArgumentException("no such component") - } + context.pause(0.25) + result(numberToCopy) + }) } private def indexOf(needle: String, offset: Int = 0): Int = { @@ -63,4 +85,14 @@ class UpgradeDatabase(val data: IInventory) extends prefab.ManagedEnvironment wi } -1 } + + private def withDatabase(address: String, f: UpgradeDatabase => Array[AnyRef]): Array[AnyRef] = { + node.network.node(address) match { + case component: Component => component.host match { + case database: UpgradeDatabase => f(database) + case _ => throw new IllegalArgumentException("not a database") + } + case _ => throw new IllegalArgumentException("no such component") + } + } }