mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-16 10:51:55 -04:00
new net_splitter component with api
getSides, setSides, open, and close closes #2531
This commit is contained in:
parent
fd3ba6a55e
commit
4142e09671
@ -81,7 +81,7 @@ class Adapter extends traits.Environment with traits.ComponentInventory with tra
|
|||||||
if (node != null && node.network != null) {
|
if (node != null && node.network != null) {
|
||||||
val (x, y, z) = (this.x + d.offsetX, this.y + d.offsetY, this.z + d.offsetZ)
|
val (x, y, z) = (this.x + d.offsetX, this.y + d.offsetY, this.z + d.offsetZ)
|
||||||
world.getTileEntity(x, y, z) match {
|
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
|
// Don't provide adaption for our stuffs. This is mostly to avoid
|
||||||
// cables and other non-functional stuff popping up in the adapter
|
// cables and other non-functional stuff popping up in the adapter
|
||||||
// due to having a power interface. Might revisit this at some point,
|
// due to having a power interface. Might revisit this at some point,
|
||||||
|
@ -1,45 +1,66 @@
|
|||||||
package li.cil.oc.common.tileentity
|
package li.cil.oc.common.tileentity
|
||||||
|
|
||||||
|
import java.util
|
||||||
|
|
||||||
import cpw.mods.fml.relauncher.{Side, SideOnly}
|
import cpw.mods.fml.relauncher.{Side, SideOnly}
|
||||||
import li.cil.oc.{Settings, api}
|
import li.cil.oc.api.driver.DeviceInfo
|
||||||
import li.cil.oc.api.network.Visibility
|
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.EventHandler
|
||||||
import li.cil.oc.common.tileentity.traits.RedstoneChangedEventArgs
|
import li.cil.oc.common.tileentity.traits.RedstoneChangedEventArgs
|
||||||
import li.cil.oc.server.{PacketSender => ServerPacketSender}
|
import li.cil.oc.server.{PacketSender => ServerPacketSender}
|
||||||
import net.minecraft.nbt.NBTTagCompound
|
import net.minecraft.nbt.NBTTagCompound
|
||||||
import net.minecraftforge.common.util.ForgeDirection
|
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
|
_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()
|
create()
|
||||||
|
|
||||||
var isInverted = false
|
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) {
|
override def setSideOpen(side: ForgeDirection, value: Boolean) {
|
||||||
|
val previous = isSideOpen(side)
|
||||||
super.setSideOpen(side, value)
|
super.setSideOpen(side, value)
|
||||||
if (isServer) {
|
if (previous != isSideOpen(side)) {
|
||||||
node.remove()
|
if (isServer) {
|
||||||
api.Network.joinOrCreateNetwork(this)
|
node.remove()
|
||||||
ServerPacketSender.sendNetSplitterState(this)
|
api.Network.joinOrCreateNetwork(this)
|
||||||
world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, "tile.piston.out", 0.5f, world.rand.nextFloat() * 0.25f + 0.7f)
|
ServerPacketSender.sendNetSplitterState(this)
|
||||||
world.notifyBlocksOfNeighborChange(x, y, z, block)
|
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)
|
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)
|
@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)
|
super.writeToNBTForClient(nbt)
|
||||||
nbt.setBoolean(Settings.namespace + "isInverted", isInverted)
|
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)
|
||||||
}
|
}
|
||||||
|
@ -36,10 +36,10 @@ trait Node extends ImmutableNode {
|
|||||||
if (network == null) Iterable.empty[ImmutableNode].toSeq
|
if (network == null) Iterable.empty[ImmutableNode].toSeq
|
||||||
else network.neighbors(this)
|
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
|
// but, sometimes other mods try to create nodes and connect them before
|
||||||
// the network is ready. we dont those things to crash here
|
// the network is ready. We don't desire those things to crash here.
|
||||||
// with typical nodes we are talking about components here
|
// With typical nodes we are talking about components here
|
||||||
// which will be connected anyways when the network is created
|
// which will be connected anyways when the network is created
|
||||||
def connect(node: ImmutableNode): Unit = if (network != null) network.connect(this, node)
|
def connect(node: ImmutableNode): Unit = if (network != null) network.connect(this, node)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user