diff --git a/src/main/scala/li/cil/oc/common/tileentity/Adapter.scala b/src/main/scala/li/cil/oc/common/tileentity/Adapter.scala index ea280381d..a64cde2ed 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/Adapter.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/Adapter.scala @@ -81,7 +81,7 @@ class Adapter extends traits.Environment with traits.ComponentInventory with tra if (node != null && node.network != null) { val (x, y, z) = (this.x + d.offsetX, this.y + d.offsetY, this.z + d.offsetZ) world.getTileEntity(x, y, z) match { - case env: traits.Environment => + case _: traits.Environment => // Don't provide adaption for our stuffs. This is mostly to avoid // cables and other non-functional stuff popping up in the adapter // due to having a power interface. Might revisit this at some point, diff --git a/src/main/scala/li/cil/oc/common/tileentity/NetSplitter.scala b/src/main/scala/li/cil/oc/common/tileentity/NetSplitter.scala index 56de3dcad..6b515ec1a 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/NetSplitter.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/NetSplitter.scala @@ -1,45 +1,66 @@ package li.cil.oc.common.tileentity +import java.util + import cpw.mods.fml.relauncher.{Side, SideOnly} -import li.cil.oc.{Settings, api} -import li.cil.oc.api.network.Visibility +import li.cil.oc.api.driver.DeviceInfo +import li.cil.oc.api.driver.DeviceInfo.{DeviceAttribute, DeviceClass} +import li.cil.oc.api.machine.{Arguments, Callback, Context} +import li.cil.oc.{Constants, Settings, api} +import li.cil.oc.api.network.{Node, Visibility} import li.cil.oc.common.EventHandler import li.cil.oc.common.tileentity.traits.RedstoneChangedEventArgs import li.cil.oc.server.{PacketSender => ServerPacketSender} import net.minecraft.nbt.NBTTagCompound import net.minecraftforge.common.util.ForgeDirection -class NetSplitter extends traits.Environment with traits.OpenSides with traits.RedstoneAware with api.network.SidedEnvironment { +import scala.collection.convert.WrapAsJava._ + +class NetSplitter extends traits.Environment with traits.OpenSides with traits.RedstoneAware with api.network.SidedEnvironment with DeviceInfo { + private lazy val deviceInfo: util.Map[String, String] = Map( + DeviceAttribute.Class -> DeviceClass.Network, + DeviceAttribute.Description -> "Ethernet controller", + DeviceAttribute.Vendor -> Constants.DeviceInfo.DefaultVendor, + DeviceAttribute.Product -> "NetSplits", + DeviceAttribute.Version -> "1.0", + DeviceAttribute.Width -> "6" + ) + + override def getDeviceInfo: util.Map[String, String] = deviceInfo _isOutputEnabled = true - val node = api.Network.newNode(this, Visibility.None). + val node: Node = api.Network.newNode(this, Visibility.Network). + withComponent("network_splitter", Visibility.Network). create() var isInverted = false - override def isSideOpen(side: ForgeDirection) = if (isInverted) !super.isSideOpen(side) else super.isSideOpen(side) + override def isSideOpen(side: ForgeDirection): Boolean = if (isInverted) !super.isSideOpen(side) else super.isSideOpen(side) override def setSideOpen(side: ForgeDirection, value: Boolean) { + val previous = isSideOpen(side) super.setSideOpen(side, value) - if (isServer) { - node.remove() - api.Network.joinOrCreateNetwork(this) - ServerPacketSender.sendNetSplitterState(this) - world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, "tile.piston.out", 0.5f, world.rand.nextFloat() * 0.25f + 0.7f) - world.notifyBlocksOfNeighborChange(x, y, z, block) - } - else { - world.markBlockForUpdate(x, y, z) + if (previous != isSideOpen(side)) { + if (isServer) { + node.remove() + api.Network.joinOrCreateNetwork(this) + ServerPacketSender.sendNetSplitterState(this) + world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, "tile.piston.out", 0.5f, world.rand.nextFloat() * 0.25f + 0.7f) + world.notifyBlocksOfNeighborChange(x, y, z, block) + } + else { + world.markBlockForUpdate(x, y, z) + } } } // ----------------------------------------------------------------------- // - override def sidedNode(side: ForgeDirection) = if (isSideOpen(side)) node else null + override def sidedNode(side: ForgeDirection): Node = if (isSideOpen(side)) node else null @SideOnly(Side.CLIENT) - override def canConnect(side: ForgeDirection) = isSideOpen(side) + override def canConnect(side: ForgeDirection): Boolean = isSideOpen(side) // ----------------------------------------------------------------------- // @@ -89,4 +110,52 @@ class NetSplitter extends traits.Environment with traits.OpenSides with traits.R super.writeToNBTForClient(nbt) nbt.setBoolean(Settings.namespace + "isInverted", isInverted) } + + // component api + def currentStatus(): Array[Boolean] = { + val openSidesCopy = Array.fill(ForgeDirection.VALID_DIRECTIONS.length)(false) + for (side <- ForgeDirection.VALID_DIRECTIONS) { + openSidesCopy(side.ordinal()) = isSideOpen(side) + } + openSidesCopy + } + + def setSide(side: ForgeDirection, state: Boolean): Boolean = { + val previous = isSideOpen(side) // isSideOpen uses inverter + setSideOpen(side, if (isInverted) !state else state) // but setSideOpen does not + previous != state + } + + @Callback(doc = "function(settings:table):table -- set open state (true/false) of all sides in an array; index by direction. Returns previous states") + def setSides(context: Context, args: Arguments): Array[AnyRef] = { + val settings = args.checkTable(0) + val previous = currentStatus() + for (side <- ForgeDirection.VALID_DIRECTIONS) { + val ordinal = side.ordinal() + val value = if (settings.containsKey(ordinal)) { + settings.get(ordinal) match { + case v: Boolean => v + case _ => false + } + } else false + setSide(side, value) + } + result(previous) + } + + @Callback(direct = true, doc = "function():table -- Returns current open/close state of all sides in an array, indexed by direction.") + def getSides(context: Context, args: Arguments): Array[AnyRef] = result(currentStatus()) + + def setSideHelper(args: Arguments, value: Boolean): Array[AnyRef] = { + val side = ForgeDirection.getOrientation(args.checkInteger(0)) + if (!ForgeDirection.VALID_DIRECTIONS.contains(side)) + return result(Unit, "invalid direction") + result(setSide(side, value)) + } + + @Callback(doc = "function(side: number):boolean -- Open the side, returns true if it changed to open.") + def open(context: Context, args: Arguments): Array[AnyRef] = setSideHelper(args, value = true) + + @Callback(doc = "function(side: number):boolean -- Close the side, returns true if it changed to close.") + def close(context: Context, args: Arguments): Array[AnyRef] = setSideHelper(args, value = false) } diff --git a/src/main/scala/li/cil/oc/server/network/Node.scala b/src/main/scala/li/cil/oc/server/network/Node.scala index ec598dbf3..2199204e8 100644 --- a/src/main/scala/li/cil/oc/server/network/Node.scala +++ b/src/main/scala/li/cil/oc/server/network/Node.scala @@ -36,10 +36,10 @@ trait Node extends ImmutableNode { if (network == null) Iterable.empty[ImmutableNode].toSeq else network.neighbors(this) - // a node should be added to a network before it can connect to a node + // A node should be added to a network before it can connect to a node // but, sometimes other mods try to create nodes and connect them before - // the network is ready. we dont those things to crash here - // with typical nodes we are talking about components here + // the network is ready. We don't desire those things to crash here. + // With typical nodes we are talking about components here // which will be connected anyways when the network is created def connect(node: ImmutableNode): Unit = if (network != null) network.connect(this, node)