basic sound framework for disk activity and floppy sounds (thanks to asie for providing the samples!)

This commit is contained in:
Florian Nücke 2014-03-09 17:13:19 +01:00
parent f759a06ee3
commit dd41cb45a4
18 changed files with 173 additions and 62 deletions

View File

@ -6,6 +6,7 @@ import dan200.computer.api.IWritableMount;
import li.cil.oc.api.detail.FileSystemAPI; import li.cil.oc.api.detail.FileSystemAPI;
import li.cil.oc.api.fs.Label; import li.cil.oc.api.fs.Label;
import li.cil.oc.api.network.ManagedEnvironment; import li.cil.oc.api.network.ManagedEnvironment;
import net.minecraft.tileentity.TileEntity;
/** /**
* This class provides factory methods for creating file systems that are * This class provides factory methods for creating file systems that are
@ -46,7 +47,8 @@ public final class FileSystem {
* @return a file system wrapping the specified folder. * @return a file system wrapping the specified folder.
*/ */
public static li.cil.oc.api.fs.FileSystem fromClass(final Class<?> clazz, final String domain, final String root) { public static li.cil.oc.api.fs.FileSystem fromClass(final Class<?> clazz, final String domain, final String root) {
if (instance != null) return instance.fromClass(clazz, domain, root); if (instance != null)
return instance.fromClass(clazz, domain, root);
return null; return null;
} }
@ -74,7 +76,8 @@ public final class FileSystem {
* @return a file system wrapping the specified folder. * @return a file system wrapping the specified folder.
*/ */
public static li.cil.oc.api.fs.FileSystem fromSaveDirectory(final String root, final long capacity, final boolean buffered) { public static li.cil.oc.api.fs.FileSystem fromSaveDirectory(final String root, final long capacity, final boolean buffered) {
if (instance != null) return instance.fromSaveDirectory(root, capacity, buffered); if (instance != null)
return instance.fromSaveDirectory(root, capacity, buffered);
return null; return null;
} }
@ -103,7 +106,8 @@ public final class FileSystem {
* @return a file system residing in memory. * @return a file system residing in memory.
*/ */
public static li.cil.oc.api.fs.FileSystem fromMemory(final long capacity) { public static li.cil.oc.api.fs.FileSystem fromMemory(final long capacity) {
if (instance != null) return instance.fromMemory(capacity); if (instance != null)
return instance.fromMemory(capacity);
return null; return null;
} }
@ -115,7 +119,8 @@ public final class FileSystem {
*/ */
@Optional.Method(modid = "ComputerCraft") @Optional.Method(modid = "ComputerCraft")
public static li.cil.oc.api.fs.FileSystem fromComputerCraft(final IMount mount) { public static li.cil.oc.api.fs.FileSystem fromComputerCraft(final IMount mount) {
if (instance != null) return instance.fromComputerCraft(mount); if (instance != null)
return instance.fromComputerCraft(mount);
return null; return null;
} }
@ -127,7 +132,8 @@ public final class FileSystem {
*/ */
@Optional.Method(modid = "ComputerCraft") @Optional.Method(modid = "ComputerCraft")
public static li.cil.oc.api.fs.FileSystem fromComputerCraft(final IWritableMount mount) { public static li.cil.oc.api.fs.FileSystem fromComputerCraft(final IWritableMount mount) {
if (instance != null) return instance.fromComputerCraft(mount); if (instance != null)
return instance.fromComputerCraft(mount);
return null; return null;
} }
@ -140,13 +146,36 @@ public final class FileSystem {
* more control over the node, implement your own, and connect this one to * more control over the node, implement your own, and connect this one to
* it. In that case you will have to forward any disk driver messages to the * it. In that case you will have to forward any disk driver messages to the
* node, though. * node, though.
* <p/>
* The container parameter is used to give the file system some physical
* relation to the world, for example this is used by hard drives to send
* the disk event notifications to the client that are used to play disk
* access sounds.
* <p/>
* The container may be <tt>null</tt>, if no such context can be provided.
*
* @param fileSystem the file system to wrap.
* @param label the label of the file system.
* @param container the tile entity containing the file system.
* @return the network node wrapping the file system.
*/
public static ManagedEnvironment asManagedEnvironment(final li.cil.oc.api.fs.FileSystem fileSystem, final Label label, final TileEntity container) {
if (instance != null)
return instance.asManagedEnvironment(fileSystem, label, container);
return null;
}
/**
* Like {@link #asManagedEnvironment(li.cil.oc.api.fs.FileSystem, Label, TileEntity)},
* but does not provide a container.
* *
* @param fileSystem the file system to wrap. * @param fileSystem the file system to wrap.
* @param label the label of the file system. * @param label the label of the file system.
* @return the network node wrapping the file system. * @return the network node wrapping the file system.
*/ */
public static ManagedEnvironment asManagedEnvironment(final li.cil.oc.api.fs.FileSystem fileSystem, final Label label) { public static ManagedEnvironment asManagedEnvironment(final li.cil.oc.api.fs.FileSystem fileSystem, final Label label) {
if (instance != null) return instance.asManagedEnvironment(fileSystem, label); if (instance != null)
return instance.asManagedEnvironment(fileSystem, label);
return null; return null;
} }
@ -159,7 +188,8 @@ public final class FileSystem {
* @return the network node wrapping the file system. * @return the network node wrapping the file system.
*/ */
public static ManagedEnvironment asManagedEnvironment(final li.cil.oc.api.fs.FileSystem fileSystem, final String label) { public static ManagedEnvironment asManagedEnvironment(final li.cil.oc.api.fs.FileSystem fileSystem, final String label) {
if (instance != null) return instance.asManagedEnvironment(fileSystem, label); if (instance != null)
return instance.asManagedEnvironment(fileSystem, label);
return null; return null;
} }
@ -172,7 +202,8 @@ public final class FileSystem {
* @return the network node wrapping the file system. * @return the network node wrapping the file system.
*/ */
public static ManagedEnvironment asManagedEnvironment(final li.cil.oc.api.fs.FileSystem fileSystem) { public static ManagedEnvironment asManagedEnvironment(final li.cil.oc.api.fs.FileSystem fileSystem) {
if (instance != null) return instance.asManagedEnvironment(fileSystem); if (instance != null)
return instance.asManagedEnvironment(fileSystem);
return null; return null;
} }

View File

@ -6,6 +6,7 @@ import dan200.computer.api.IWritableMount;
import li.cil.oc.api.fs.FileSystem; import li.cil.oc.api.fs.FileSystem;
import li.cil.oc.api.fs.Label; import li.cil.oc.api.fs.Label;
import li.cil.oc.api.network.ManagedEnvironment; import li.cil.oc.api.network.ManagedEnvironment;
import net.minecraft.tileentity.TileEntity;
public interface FileSystemAPI { public interface FileSystemAPI {
/** /**
@ -96,6 +97,24 @@ public interface FileSystemAPI {
* more control over the node, implement your own, and connect this one to * more control over the node, implement your own, and connect this one to
* it. In that case you will have to forward any disk driver messages to the * it. In that case you will have to forward any disk driver messages to the
* node, though. * node, though.
* <p/>
* The container parameter is used to give the file system some physical
* relation to the world, for example this is used by hard drives to send
* the disk event notifications to the client that are used to play disk
* access sounds.
* <p/>
* The container may be <tt>null</tt>, if no such context can be provided.
*
* @param fileSystem the file system to wrap.
* @param label the label of the file system.
* @param container the tile entity containing the file system.
* @return the network node wrapping the file system.
*/
ManagedEnvironment asManagedEnvironment(FileSystem fileSystem, Label label, TileEntity container);
/**
* Like {@link #asManagedEnvironment(li.cil.oc.api.fs.FileSystem, Label, TileEntity)},
* but does not provide a container.
* *
* @param fileSystem the file system to wrap. * @param fileSystem the file system to wrap.
* @param label the label of the file system. * @param label the label of the file system.

View File

@ -1,7 +1,7 @@
package li.cil.oc.client package li.cil.oc.client
import cpw.mods.fml.client.registry.{RenderingRegistry, ClientRegistry} import cpw.mods.fml.client.registry.{RenderingRegistry, ClientRegistry}
import cpw.mods.fml.common.event.{FMLPostInitializationEvent, FMLInitializationEvent} import cpw.mods.fml.common.event.{FMLPreInitializationEvent, FMLPostInitializationEvent, FMLInitializationEvent}
import cpw.mods.fml.common.network.NetworkRegistry import cpw.mods.fml.common.network.NetworkRegistry
import cpw.mods.fml.common.registry.TickRegistry import cpw.mods.fml.common.registry.TickRegistry
import cpw.mods.fml.relauncher.Side import cpw.mods.fml.relauncher.Side
@ -9,8 +9,7 @@ import li.cil.oc.client.renderer.WirelessNetworkDebugRenderer
import li.cil.oc.client.renderer.block.BlockRenderer import li.cil.oc.client.renderer.block.BlockRenderer
import li.cil.oc.client.renderer.item.UpgradeRenderer import li.cil.oc.client.renderer.item.UpgradeRenderer
import li.cil.oc.client.renderer.tileentity._ import li.cil.oc.client.renderer.tileentity._
import li.cil.oc.common.tileentity import li.cil.oc.common.{Proxy => CommonProxy, Sound, tileentity}
import li.cil.oc.common.{Proxy => CommonProxy}
import li.cil.oc.{Items, Settings, OpenComputers} import li.cil.oc.{Items, Settings, OpenComputers}
import net.minecraft.client.Minecraft import net.minecraft.client.Minecraft
import net.minecraft.client.resources.ReloadableResourceManager import net.minecraft.client.resources.ReloadableResourceManager
@ -18,6 +17,12 @@ import net.minecraftforge.client.MinecraftForgeClient
import net.minecraftforge.common.MinecraftForge import net.minecraftforge.common.MinecraftForge
private[oc] class Proxy extends CommonProxy { private[oc] class Proxy extends CommonProxy {
override def preInit(e: FMLPreInitializationEvent) {
super.preInit(e)
MinecraftForge.EVENT_BUS.register(Sound)
}
override def init(e: FMLInitializationEvent) = { override def init(e: FMLInitializationEvent) = {
super.init(e) super.init(e)

View File

@ -5,11 +5,11 @@ import cpw.mods.fml.common.network.PacketDispatcher
import cpw.mods.fml.common.network.Player import cpw.mods.fml.common.network.Player
import java.io.{OutputStream, ByteArrayOutputStream, DataOutputStream} import java.io.{OutputStream, ByteArrayOutputStream, DataOutputStream}
import java.util.zip.GZIPOutputStream import java.util.zip.GZIPOutputStream
import li.cil.oc.common.tileentity.TileEntity
import net.minecraft.entity.player.EntityPlayerMP import net.minecraft.entity.player.EntityPlayerMP
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
import net.minecraft.nbt.{CompressedStreamTools, NBTTagCompound} import net.minecraft.nbt.{CompressedStreamTools, NBTTagCompound}
import net.minecraft.network.packet.Packet250CustomPayload import net.minecraft.network.packet.Packet250CustomPayload
import net.minecraft.tileentity.TileEntity
import net.minecraft.world.World import net.minecraft.world.World
import net.minecraftforge.common.ForgeDirection import net.minecraftforge.common.ForgeDirection
import scala.collection.convert.WrapAsScala._ import scala.collection.convert.WrapAsScala._
@ -17,10 +17,10 @@ import scala.collection.convert.WrapAsScala._
// Necessary to keep track of the GZIP stream. // Necessary to keep track of the GZIP stream.
abstract class PacketBuilderBase[T <: OutputStream](protected val stream: T) extends DataOutputStream(stream) { abstract class PacketBuilderBase[T <: OutputStream](protected val stream: T) extends DataOutputStream(stream) {
def writeTileEntity(t: TileEntity) = { def writeTileEntity(t: TileEntity) = {
writeInt(t.world.provider.dimensionId) writeInt(t.getWorldObj.provider.dimensionId)
writeInt(t.x) writeInt(t.xCoord)
writeInt(t.y) writeInt(t.yCoord)
writeInt(t.z) writeInt(t.zCoord)
} }
def writeDirection(d: ForgeDirection) = writeInt(d.ordinal) def writeDirection(d: ForgeDirection) = writeInt(d.ordinal)
@ -37,7 +37,7 @@ abstract class PacketBuilderBase[T <: OutputStream](protected val stream: T) ext
def sendToAllPlayers() = PacketDispatcher.sendPacketToAllPlayers(packet) def sendToAllPlayers() = PacketDispatcher.sendPacketToAllPlayers(packet)
def sendToNearbyPlayers(t: TileEntity, range: Double = 1024): Unit = sendToNearbyPlayers(t.world, t.x + 0.5, t.y + 0.5, t.z + 0.5, range) def sendToNearbyPlayers(t: TileEntity, range: Double = 1024): Unit = sendToNearbyPlayers(t.getWorldObj, t.xCoord + 0.5, t.yCoord + 0.5, t.zCoord + 0.5, range)
def sendToNearbyPlayers(world: World, x: Double, y: Double, z: Double, range: Double) { def sendToNearbyPlayers(world: World, x: Double, y: Double, z: Double, range: Double) {
val dimension = world.provider.dimensionId val dimension = world.provider.dimensionId

View File

@ -0,0 +1,41 @@
package li.cil.oc.common
import li.cil.oc.Settings
import net.minecraft.tileentity.TileEntity
import net.minecraftforge.client.event.sound.SoundLoadEvent
import net.minecraftforge.event.ForgeSubscribe
import cpw.mods.fml.relauncher.{Side, SideOnly}
object Sound {
@SideOnly(Side.CLIENT)
@ForgeSubscribe
def onSoundLoad(event: SoundLoadEvent) {
for (i <- 1 to 6) {
event.manager.soundPoolSounds.addSound(Settings.resourceDomain + s":floppy_access$i.ogg")
}
event.manager.soundPoolSounds.addSound(Settings.resourceDomain + ":floppy_insert.ogg")
event.manager.soundPoolSounds.addSound(Settings.resourceDomain + ":floppy_eject.ogg")
}
def play(t: tileentity.TileEntity, name: String) {
t.world.playSoundEffect(t.x + 0.5, t.y + 0.5, t.z + 0.5, Settings.resourceDomain + ":" + name, 1, 1)
}
def playDiskInsert(t: tileentity.DiskDrive) {
play(t, "floppy_insert")
}
def playDiskEject(t: tileentity.DiskDrive) {
play(t, "floppy_eject")
}
def playDiskActivity(t: TileEntity) = this.synchronized {
t match {
case computer: tileentity.Computer => play(computer, "hdd_access")
case rack: tileentity.Rack => play(rack, "hdd_access")
case drive: tileentity.DiskDrive => play(drive, "floppy_access")
case _ => // Huh?
}
}
}

View File

@ -2,8 +2,9 @@ package li.cil.oc.common.tileentity
import li.cil.oc.api.driver.Slot import li.cil.oc.api.driver.Slot
import li.cil.oc.api.network.{Analyzable, Component, Visibility} import li.cil.oc.api.network.{Analyzable, Component, Visibility}
import li.cil.oc.server.TickHandler import li.cil.oc.common.Sound
import li.cil.oc.server.driver.Registry import li.cil.oc.server.driver.Registry
import li.cil.oc.server.TickHandler
import li.cil.oc.{api, Settings} import li.cil.oc.{api, Settings}
import net.minecraft.entity.player.EntityPlayer import net.minecraft.entity.player.EntityPlayer
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
@ -45,5 +46,11 @@ class DiskDrive extends Environment with ComponentInventory with Rotatable with
} }
case _ => case _ =>
} }
Sound.playDiskInsert(this)
}
override protected def onItemRemoved(slot: Int, stack: ItemStack) {
super.onItemRemoved(slot, stack)
Sound.playDiskEject(this)
} }
} }

View File

@ -1,7 +1,7 @@
package li.cil.oc.server package li.cil.oc.server
import li.cil.oc.common import li.cil.oc.common
import li.cil.oc.common.tileentity._ import li.cil.oc.common.tileentity
import li.cil.oc.common.{CompressedPacketBuilder, PacketBuilder, PacketType} import li.cil.oc.common.{CompressedPacketBuilder, PacketBuilder, PacketType}
import li.cil.oc.util.PackedColor import li.cil.oc.util.PackedColor
import net.minecraft.entity.player.EntityPlayerMP import net.minecraft.entity.player.EntityPlayerMP
@ -9,7 +9,7 @@ import net.minecraft.item.ItemStack
import net.minecraftforge.common.ForgeDirection import net.minecraftforge.common.ForgeDirection
object PacketSender { object PacketSender {
def sendAbstractBusState(t: AbstractBusAware) { def sendAbstractBusState(t: tileentity.AbstractBusAware) {
val pb = new PacketBuilder(PacketType.AbstractBusState) val pb = new PacketBuilder(PacketType.AbstractBusState)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -26,7 +26,7 @@ object PacketSender {
pb.sendToPlayer(player) pb.sendToPlayer(player)
} }
def sendChargerState(t: Charger) { def sendChargerState(t: tileentity.Charger) {
val pb = new PacketBuilder(PacketType.ChargerState) val pb = new PacketBuilder(PacketType.ChargerState)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -35,7 +35,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendComputerState(t: Computer) { def sendComputerState(t: tileentity.Computer) {
val pb = new PacketBuilder(PacketType.ComputerState) val pb = new PacketBuilder(PacketType.ComputerState)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -44,7 +44,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendComputerUserList(t: Computer, list: Array[String]) { def sendComputerUserList(t: tileentity.Computer, list: Array[String]) {
val pb = new PacketBuilder(PacketType.ComputerUserList) val pb = new PacketBuilder(PacketType.ComputerUserList)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -54,7 +54,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendHologramClear(t: Hologram) { def sendHologramClear(t: tileentity.Hologram) {
val pb = new PacketBuilder(PacketType.HologramClear) val pb = new PacketBuilder(PacketType.HologramClear)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -62,7 +62,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendHologramPowerChange(t: Hologram) { def sendHologramPowerChange(t: tileentity.Hologram) {
val pb = new PacketBuilder(PacketType.HologramPowerChange) val pb = new PacketBuilder(PacketType.HologramPowerChange)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -71,7 +71,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendHologramScale(t: Hologram) { def sendHologramScale(t: tileentity.Hologram) {
val pb = new PacketBuilder(PacketType.HologramScale) val pb = new PacketBuilder(PacketType.HologramScale)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -80,7 +80,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendHologramSet(t: Hologram) { def sendHologramSet(t: tileentity.Hologram) {
val pb = new CompressedPacketBuilder(PacketType.HologramSet) val pb = new CompressedPacketBuilder(PacketType.HologramSet)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -97,7 +97,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendPowerState(t: PowerInformation) { def sendPowerState(t: tileentity.PowerInformation) {
val pb = new PacketBuilder(PacketType.PowerState) val pb = new PacketBuilder(PacketType.PowerState)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -107,7 +107,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendRedstoneState(t: RedstoneAware) { def sendRedstoneState(t: tileentity.RedstoneAware) {
val pb = new PacketBuilder(PacketType.RedstoneState) val pb = new PacketBuilder(PacketType.RedstoneState)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -119,7 +119,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendRobotMove(t: Robot, ox: Int, oy: Int, oz: Int, direction: ForgeDirection) { def sendRobotMove(t: tileentity.Robot, ox: Int, oy: Int, oz: Int, direction: ForgeDirection) {
val pb = new PacketBuilder(PacketType.RobotMove) val pb = new PacketBuilder(PacketType.RobotMove)
// Custom pb.writeTileEntity() with fake coordinates (valid for the client). // Custom pb.writeTileEntity() with fake coordinates (valid for the client).
@ -132,7 +132,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendRobotAnimateSwing(t: Robot) { def sendRobotAnimateSwing(t: tileentity.Robot) {
val pb = new PacketBuilder(PacketType.RobotAnimateSwing) val pb = new PacketBuilder(PacketType.RobotAnimateSwing)
pb.writeTileEntity(t.proxy) pb.writeTileEntity(t.proxy)
@ -141,7 +141,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t, 64) pb.sendToNearbyPlayers(t, 64)
} }
def sendRobotAnimateTurn(t: Robot) { def sendRobotAnimateTurn(t: tileentity.Robot) {
val pb = new PacketBuilder(PacketType.RobotAnimateTurn) val pb = new PacketBuilder(PacketType.RobotAnimateTurn)
pb.writeTileEntity(t.proxy) pb.writeTileEntity(t.proxy)
@ -151,7 +151,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t, 64) pb.sendToNearbyPlayers(t, 64)
} }
def sendRobotEquippedItemChange(t: Robot, stack: ItemStack) { def sendRobotEquippedItemChange(t: tileentity.Robot, stack: ItemStack) {
val pb = new PacketBuilder(PacketType.RobotEquippedItemChange) val pb = new PacketBuilder(PacketType.RobotEquippedItemChange)
pb.writeTileEntity(t.proxy) pb.writeTileEntity(t.proxy)
@ -160,7 +160,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendRobotEquippedUpgradeChange(t: Robot, stack: ItemStack) { def sendRobotEquippedUpgradeChange(t: tileentity.Robot, stack: ItemStack) {
val pb = new PacketBuilder(PacketType.RobotEquippedUpgradeChange) val pb = new PacketBuilder(PacketType.RobotEquippedUpgradeChange)
pb.writeTileEntity(t.proxy) pb.writeTileEntity(t.proxy)
@ -169,7 +169,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendRobotSelectedSlotChange(t: Robot) { def sendRobotSelectedSlotChange(t: tileentity.Robot) {
val pb = new PacketBuilder(PacketType.RobotSelectedSlotChange) val pb = new PacketBuilder(PacketType.RobotSelectedSlotChange)
pb.writeTileEntity(t.proxy) pb.writeTileEntity(t.proxy)
@ -178,7 +178,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t, 16) pb.sendToNearbyPlayers(t, 16)
} }
def sendRobotXp(t: Robot) { def sendRobotXp(t: tileentity.Robot) {
val pb = new PacketBuilder(PacketType.RobotXp) val pb = new PacketBuilder(PacketType.RobotXp)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -187,7 +187,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendRotatableState(t: Rotatable) { def sendRotatableState(t: tileentity.Rotatable) {
val pb = new PacketBuilder(PacketType.RotatableState) val pb = new PacketBuilder(PacketType.RotatableState)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -201,7 +201,7 @@ object PacketSender {
val pb = new PacketBuilder(PacketType.ScreenColorChange) val pb = new PacketBuilder(PacketType.ScreenColorChange)
val t = b.owner match { val t = b.owner match {
case t: Buffer => case t: tileentity.Buffer =>
pb.writeTileEntity(t) pb.writeTileEntity(t)
t t
case t: common.component.Terminal => case t: common.component.Terminal =>
@ -220,7 +220,7 @@ object PacketSender {
val pb = new PacketBuilder(PacketType.ScreenCopy) val pb = new PacketBuilder(PacketType.ScreenCopy)
val t = b.owner match { val t = b.owner match {
case t: Buffer => case t: tileentity.Buffer =>
pb.writeTileEntity(t) pb.writeTileEntity(t)
t t
case t: common.component.Terminal => case t: common.component.Terminal =>
@ -243,7 +243,7 @@ object PacketSender {
val pb = new PacketBuilder(PacketType.ScreenDepthChange) val pb = new PacketBuilder(PacketType.ScreenDepthChange)
val t = b.owner match { val t = b.owner match {
case t: Buffer => case t: tileentity.Buffer =>
pb.writeTileEntity(t) pb.writeTileEntity(t)
t t
case t: common.component.Terminal => case t: common.component.Terminal =>
@ -261,7 +261,7 @@ object PacketSender {
val pb = new PacketBuilder(PacketType.ScreenFill) val pb = new PacketBuilder(PacketType.ScreenFill)
val t = b.owner match { val t = b.owner match {
case t: Buffer => case t: tileentity.Buffer =>
pb.writeTileEntity(t) pb.writeTileEntity(t)
t t
case t: common.component.Terminal => case t: common.component.Terminal =>
@ -279,7 +279,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendScreenPowerChange(t: Buffer, hasPower: Boolean) { def sendScreenPowerChange(t: tileentity.Buffer, hasPower: Boolean) {
val pb = new PacketBuilder(PacketType.ScreenPowerChange) val pb = new PacketBuilder(PacketType.ScreenPowerChange)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -292,7 +292,7 @@ object PacketSender {
val pb = new PacketBuilder(PacketType.ScreenResolutionChange) val pb = new PacketBuilder(PacketType.ScreenResolutionChange)
val t = b.owner match { val t = b.owner match {
case t: Buffer => case t: tileentity.Buffer =>
pb.writeTileEntity(t) pb.writeTileEntity(t)
t t
case t: common.component.Terminal => case t: common.component.Terminal =>
@ -311,7 +311,7 @@ object PacketSender {
val pb = new PacketBuilder(PacketType.ScreenSet) val pb = new PacketBuilder(PacketType.ScreenSet)
val t = b.owner match { val t = b.owner match {
case t: Buffer => case t: tileentity.Buffer =>
pb.writeTileEntity(t) pb.writeTileEntity(t)
t t
case t: common.component.Terminal => case t: common.component.Terminal =>
@ -327,7 +327,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendServerPresence(t: Rack) { def sendServerPresence(t: tileentity.Rack) {
val pb = new PacketBuilder(PacketType.ServerPresence) val pb = new PacketBuilder(PacketType.ServerPresence)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -342,7 +342,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendServerState(t: Rack) { def sendServerState(t: tileentity.Rack) {
val pb = new PacketBuilder(PacketType.ComputerState) val pb = new PacketBuilder(PacketType.ComputerState)
pb.writeTileEntity(t) pb.writeTileEntity(t)
@ -352,7 +352,7 @@ object PacketSender {
pb.sendToNearbyPlayers(t) pb.sendToNearbyPlayers(t)
} }
def sendServerState(t: Rack, number: Int, player: Option[EntityPlayerMP] = None) { def sendServerState(t: tileentity.Rack, number: Int, player: Option[EntityPlayerMP] = None) {
val pb = new PacketBuilder(PacketType.ComputerState) val pb = new PacketBuilder(PacketType.ComputerState)
pb.writeTileEntity(t) pb.writeTileEntity(t)

View File

@ -1,15 +1,17 @@
package li.cil.oc.server.component package li.cil.oc.server.component
import java.io.{FileNotFoundException, IOException} import java.io.{FileNotFoundException, IOException}
import li.cil.oc.Settings
import li.cil.oc.api.Network
import li.cil.oc.api.fs.{Label, Mode, FileSystem => IFileSystem} import li.cil.oc.api.fs.{Label, Mode, FileSystem => IFileSystem}
import li.cil.oc.api.Network
import li.cil.oc.api.network._ import li.cil.oc.api.network._
import li.cil.oc.common.Sound
import li.cil.oc.Settings
import li.cil.oc.util.ExtendedNBT._ import li.cil.oc.util.ExtendedNBT._
import net.minecraft.nbt.{NBTTagInt, NBTTagList, NBTTagCompound} import net.minecraft.nbt.{NBTTagInt, NBTTagList, NBTTagCompound}
import net.minecraft.tileentity.TileEntity
import scala.collection.mutable import scala.collection.mutable
class FileSystem(val fileSystem: IFileSystem, var label: Label) extends ManagedComponent { class FileSystem(val fileSystem: IFileSystem, var label: Label, val container: Option[TileEntity] = None) extends ManagedComponent {
val node = Network.newNode(this, Visibility.Network). val node = Network.newNode(this, Visibility.Network).
withComponent("filesystem", Visibility.Neighbors). withComponent("filesystem", Visibility.Neighbors).
withConnector(). withConnector().
@ -148,6 +150,7 @@ class FileSystem(val fileSystem: IFileSystem, var label: Label) extends ManagedC
if (!node.tryChangeBuffer(-Settings.get.hddReadCost * bytes.length)) { if (!node.tryChangeBuffer(-Settings.get.hddReadCost * bytes.length)) {
throw new IOException("not enough energy") throw new IOException("not enough energy")
} }
container.foreach(Sound.playDiskActivity)
result(bytes) result(bytes)
} }
else { else {
@ -185,7 +188,10 @@ class FileSystem(val fileSystem: IFileSystem, var label: Label) extends ManagedC
} }
checkOwner(context.node.address, handle) checkOwner(context.node.address, handle)
Option(fileSystem.getHandle(handle)) match { Option(fileSystem.getHandle(handle)) match {
case Some(file) => file.write(value); result(true) case Some(file) =>
file.write(value)
container.foreach(Sound.playDiskActivity)
result(true)
case _ => throw new IOException("bad file descriptor") case _ => throw new IOException("bad file descriptor")
} }
} }

View File

@ -9,12 +9,12 @@ import li.cil.oc.util.mods.ComputerCraft
import li.cil.oc.{Settings, Items} import li.cil.oc.{Settings, Items}
import net.minecraft.item.ItemStack import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound import net.minecraft.nbt.NBTTagCompound
import net.minecraft.tileentity.{TileEntity => MCTileEntity} import net.minecraft.tileentity.TileEntity
object FileSystem extends Item { object FileSystem extends Item {
override def worksWith(stack: ItemStack) = isOneOf(stack, Items.hdd1, Items.hdd2, Items.hdd3, Items.floppyDisk) || ComputerCraft.isDisk(stack) override def worksWith(stack: ItemStack) = isOneOf(stack, Items.hdd1, Items.hdd2, Items.hdd3, Items.floppyDisk) || ComputerCraft.isDisk(stack)
override def createEnvironment(stack: ItemStack, container: MCTileEntity) = override def createEnvironment(stack: ItemStack, container: TileEntity) =
if (ComputerCraft.isDisk(stack) && container != null) { if (ComputerCraft.isDisk(stack) && container != null) {
val address = addressFromTag(dataTag(stack)) val address = addressFromTag(dataTag(stack))
val mount = ComputerCraft.createDiskMount(stack, container.getWorldObj) val mount = ComputerCraft.createDiskMount(stack, container.getWorldObj)
@ -25,8 +25,8 @@ object FileSystem extends Item {
case _ => null case _ => null
} }
} else Items.multi.subItem(stack) match { } else Items.multi.subItem(stack) match {
case Some(hdd: HardDiskDrive) => createEnvironment(stack, hdd.kiloBytes * 1024) case Some(hdd: HardDiskDrive) => createEnvironment(stack, hdd.kiloBytes * 1024, container)
case Some(disk: FloppyDisk) => createEnvironment(stack, Settings.get.floppySize * 1024) case Some(disk: FloppyDisk) => createEnvironment(stack, Settings.get.floppySize * 1024, container)
case _ => null case _ => null
} }
@ -44,18 +44,17 @@ object FileSystem extends Item {
case _ => 0 case _ => 0
} }
private def createEnvironment(stack: ItemStack, capacity: Int) = { private def createEnvironment(stack: ItemStack, capacity: Int, container: TileEntity) = {
// We have a bit of a chicken-egg problem here, because we want to use the // We have a bit of a chicken-egg problem here, because we want to use the
// node's address as the folder name... so we generate the address here, // node's address as the folder name... so we generate the address here,
// if necessary. No one will know, right? Right!? // if necessary. No one will know, right? Right!?
val address = addressFromTag(dataTag(stack)) val address = addressFromTag(dataTag(stack))
Option(oc.api.FileSystem.asManagedEnvironment(oc.api.FileSystem. val fs = oc.api.FileSystem.fromSaveDirectory(address, capacity, Settings.get.bufferChanges)
fromSaveDirectory(address, capacity, Settings.get.bufferChanges), new ItemLabel(stack))) match { val environment = oc.api.FileSystem.asManagedEnvironment(fs, new ItemLabel(stack), container)
case Some(environment) => if (environment != null) {
environment.node.asInstanceOf[oc.server.network.Node].address = address environment.node.asInstanceOf[oc.server.network.Node].address = address
environment
case _ => null
} }
environment
} }
private def addressFromTag(tag: NBTTagCompound) = private def addressFromTag(tag: NBTTagCompound) =

View File

@ -72,6 +72,9 @@ object FileSystem extends api.detail.FileSystemAPI {
@Optional.Method(modid = "ComputerCraft") @Optional.Method(modid = "ComputerCraft")
def fromComputerCraft(mount: IWritableMount) = new ComputerCraftWritableFileSystem(mount) def fromComputerCraft(mount: IWritableMount) = new ComputerCraftWritableFileSystem(mount)
def asManagedEnvironment(fileSystem: api.fs.FileSystem, label: Label, container: net.minecraft.tileentity.TileEntity) =
Option(fileSystem).flatMap(fs => Some(new component.FileSystem(fs, label, Option(container)))).orNull
def asManagedEnvironment(fileSystem: api.fs.FileSystem, label: Label) = def asManagedEnvironment(fileSystem: api.fs.FileSystem, label: Label) =
Option(fileSystem).flatMap(fs => Some(new component.FileSystem(fs, label))).orNull Option(fileSystem).flatMap(fs => Some(new component.FileSystem(fs, label))).orNull