From b0d0fa09a72d47355b965a5f1cc7b42e1780f5bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Wed, 21 Oct 2015 19:48:28 +0200 Subject: [PATCH] Made disk drive expose a component, allows checking for disk presence and programmatic ejection of disks. Closes #1144. --- .../cil/oc/common/tileentity/DiskDrive.scala | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/scala/li/cil/oc/common/tileentity/DiskDrive.scala b/src/main/scala/li/cil/oc/common/tileentity/DiskDrive.scala index 595b5a06a..b991c0629 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/DiskDrive.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/DiskDrive.scala @@ -4,6 +4,9 @@ import cpw.mods.fml.relauncher.Side import cpw.mods.fml.relauncher.SideOnly import li.cil.oc.api import li.cil.oc.api.Driver +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.Analyzable import li.cil.oc.api.network.Component import li.cil.oc.api.network.Node @@ -12,12 +15,15 @@ import li.cil.oc.common.Slot import li.cil.oc.common.Sound import li.cil.oc.server.{PacketSender => ServerPacketSender} import li.cil.oc.util.ExtendedNBT._ +import li.cil.oc.util.InventoryUtils import net.minecraft.entity.player.EntityPlayer import net.minecraft.item.ItemStack import net.minecraft.nbt.NBTTagCompound class DiskDrive extends traits.Environment with traits.ComponentInventory with traits.Rotatable with Analyzable { - val node = api.Network.newNode(this, Visibility.None).create() + val node = api.Network.newNode(this, Visibility.Network). + withComponent("disk_drive"). + create() // Used on client side to check whether to render disk activity indicators. var lastAccess = 0L @@ -29,6 +35,30 @@ class DiskDrive extends traits.Environment with traits.ComponentInventory with t // ----------------------------------------------------------------------- // + @Callback(doc = """function():boolean -- Checks whether some medium is currently in the drive.""") + def isEmpty(context: Context, args: Arguments): Array[AnyRef] = { + result(filesystemNode.isEmpty) + } + + @Callback(doc = """function([velocity:number]):boolean -- Eject the currently present medium from the drive.""") + def eject(context: Context, args: Arguments): Array[AnyRef] = { + val velocity = args.optDouble(0, 0) max 0 min 1 + val ejected = decrStackSize(0, 1) + if (ejected != null && ejected.stackSize > 0) { + val entity = InventoryUtils.spawnStackInWorld(position, ejected, Option(facing)) + if (entity != null) { + val vx = facing.offsetX * velocity + val vy = facing.offsetY * velocity + val vz = facing.offsetZ * velocity + entity.addVelocity(vx, vy, vz) + } + result(true) + } + else result(false) + } + + // ----------------------------------------------------------------------- // + override def onAnalyze(player: EntityPlayer, side: Int, hitX: Float, hitY: Float, hitZ: Float) = filesystemNode.fold(null: Array[Node])(Array(_)) override def canUpdate = false