blob: 6d3fe7ab1e90a9218e274eed1ebf308ddde06092 [file] [log] [blame] [raw]
package li.cil.oc.integration.computercraft
import dan200.computercraft.api.media.IMedia
import li.cil.oc
import li.cil.oc.Settings
import li.cil.oc.api.driver.EnvironmentHost
import li.cil.oc.api.fs.Label
import li.cil.oc.common.Slot
import li.cil.oc.integration.Mods
import li.cil.oc.integration.opencomputers.Item
import li.cil.oc.integration.util.ComputerCraft
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
object DriverComputerCraftMedia extends Item {
override def worksWith(stack: ItemStack) =
Mods.ComputerCraft.isAvailable && ComputerCraft.isDisk(stack)
override def createEnvironment(stack: ItemStack, host: EnvironmentHost) =
if (Mods.ComputerCraft.isAvailable && ComputerCraft.isDisk(stack) && host != null) {
val address = addressFromTag(dataTag(stack))
val mount = ComputerCraft.createDiskMount(stack, host.world)
Option(oc.api.FileSystem.asManagedEnvironment(mount, new ComputerCraftLabel(stack), host, Settings.resourceDomain + ":floppy_access")) match {
case Some(environment) =>
environment.node.asInstanceOf[oc.server.network.Node].address = address
environment
case _ => null
}
}
else null
override def slot(stack: ItemStack) = Slot.Floppy
private def addressFromTag(tag: NBTTagCompound) =
if (tag.hasKey("node") && tag.getCompoundTag("node").hasKey("address")) {
tag.getCompoundTag("node").getString("address")
}
else java.util.UUID.randomUUID().toString
class ComputerCraftLabel(val stack: ItemStack) extends Label {
val media = stack.getItem.asInstanceOf[IMedia]
override def getLabel = media.getLabel(stack)
override def setLabel(value: String) {
media.setLabel(stack, value)
}
override def load(nbt: NBTTagCompound) {}
override def save(nbt: NBTTagCompound) {}
}
}