mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-15 10:21:45 -04:00
immediately flushing abstract bus packets now, avoids having to save the queue and issues that brings with it (remove card, insert somewhere else, continues working through remaining queue...)
This commit is contained in:
parent
8c3e21928d
commit
5fbf3b88a3
@ -3,10 +3,8 @@ package li.cil.oc.server.component
|
||||
import li.cil.oc.api
|
||||
import li.cil.oc.api.network.{Arguments, Context, LuaCallback, Visibility}
|
||||
import li.cil.oc.common.tileentity
|
||||
import li.cil.oc.util.ExtendedNBT._
|
||||
import net.minecraft.nbt.{NBTTagString, NBTTagList, NBTTagCompound}
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import scala.collection.convert.WrapAsScala._
|
||||
import scala.collection.mutable
|
||||
import stargatetech2.api.StargateTechAPI
|
||||
import stargatetech2.api.bus.{BusPacketLIP, BusPacket, IBusDriver, IBusInterface}
|
||||
|
||||
@ -21,7 +19,7 @@ class AbstractBus(val owner: tileentity.Computer) extends ManagedComponent with
|
||||
|
||||
protected var address = 0
|
||||
|
||||
protected val sendQueue = mutable.Queue.empty[QueuedPacket]
|
||||
protected var sendQueue: Option[QueuedPacket] = None
|
||||
|
||||
// ----------------------------------------------------------------------- //
|
||||
|
||||
@ -31,19 +29,22 @@ class AbstractBus(val owner: tileentity.Computer) extends ManagedComponent with
|
||||
val lip = packet.getPlainText
|
||||
val data = Map(lip.getEntryList.map(key => (key, lip.get(key))): _*)
|
||||
// TODO do we want to push metadata, too?
|
||||
owner.signal("bus_message", Int.box(packet.getProtocolID), Int.box(packet.getSender), Int.box(packet.getTarget), data)
|
||||
val metadata = Map("mod" -> "", "device" -> "", "player" -> "")
|
||||
owner.signal("bus_message", Int.box(packet.getProtocolID), Int.box(packet.getSender), Int.box(packet.getTarget), data, metadata)
|
||||
}
|
||||
|
||||
def getNextPacketToSend = if (sendQueue.nonEmpty) {
|
||||
val info = sendQueue.dequeue()
|
||||
def getNextPacketToSend = if (sendQueue.isDefined) {
|
||||
val info = sendQueue.get
|
||||
sendQueue = None
|
||||
val packet = new BusPacketLIP(info.sender, info.target)
|
||||
for ((key, value) <- info.data) {
|
||||
packet.set(key, value)
|
||||
}
|
||||
// TODO generate metadata?
|
||||
packet.setMetadata(new BusPacketLIP.LIPMetadata("OpenComputers", node.address, null))
|
||||
packet.finish()
|
||||
packet
|
||||
} else null
|
||||
}
|
||||
else null
|
||||
|
||||
def isInterfaceEnabled = isEnabled
|
||||
|
||||
@ -73,7 +74,8 @@ class AbstractBus(val owner: tileentity.Computer) extends ManagedComponent with
|
||||
def send(context: Context, args: Arguments): Array[AnyRef] = {
|
||||
val target = args.checkInteger(0) & 0xFFFF
|
||||
val data = args.checkTable(1)
|
||||
sendQueue += new QueuedPacket(address.toShort, target.toShort, Map(data.toSeq.map(entry => (entry._1.toString, entry._2.toString)): _*))
|
||||
sendQueue = Some(new QueuedPacket(address.toShort, target.toShort, Map(data.toSeq.map(entry => (entry._1.toString, entry._2.toString)): _*)))
|
||||
busInterface.sendAllPackets()
|
||||
result(true)
|
||||
}
|
||||
|
||||
@ -84,9 +86,6 @@ class AbstractBus(val owner: tileentity.Computer) extends ManagedComponent with
|
||||
busInterface.readFromNBT(nbt, "bus")
|
||||
isEnabled = nbt.getBoolean("enabled")
|
||||
address = nbt.getInteger("address") & 0xFFFF
|
||||
if (nbt.hasKey("queue")) {
|
||||
sendQueue ++= nbt.getTagList("queue").map(QueuedPacket.readFromNBT)
|
||||
}
|
||||
}
|
||||
|
||||
override def save(nbt: NBTTagCompound) {
|
||||
@ -94,39 +93,8 @@ class AbstractBus(val owner: tileentity.Computer) extends ManagedComponent with
|
||||
busInterface.writeToNBT(nbt, "bus")
|
||||
nbt.setBoolean("enabled", isEnabled)
|
||||
nbt.setInteger("address", address)
|
||||
if (sendQueue.nonEmpty) {
|
||||
nbt.setNewTagList("queue", sendQueue.toIterable.map(packet => packet.writeToNBT(new NBTTagCompound())))
|
||||
}
|
||||
}
|
||||
|
||||
protected class QueuedPacket(val sender: Short, val target: Short, val data: Map[String, String]) {
|
||||
def writeToNBT(nbt: NBTTagCompound) = {
|
||||
nbt.setShort("sender", sender)
|
||||
nbt.setShort("target", target)
|
||||
val list = new NBTTagList()
|
||||
for ((key, value) <- data) {
|
||||
list.append(key)
|
||||
list.append(value)
|
||||
}
|
||||
nbt.setTag("data", list)
|
||||
nbt
|
||||
}
|
||||
}
|
||||
|
||||
protected object QueuedPacket {
|
||||
def readFromNBT(nbt: NBTTagCompound) = {
|
||||
val sender = nbt.getShort("sender")
|
||||
val target = nbt.getShort("target")
|
||||
val list = nbt.getTagList("data")
|
||||
val data = mutable.Map.empty[String, String]
|
||||
for (i <- 0 until list.tagCount by 2) {
|
||||
(list.tagAt(i), list.tagAt(i + 1)) match {
|
||||
case (key: NBTTagString, value: NBTTagString) => data += key.data -> value.data
|
||||
case _ =>
|
||||
}
|
||||
}
|
||||
new QueuedPacket(sender, target, data.toMap)
|
||||
}
|
||||
}
|
||||
protected class QueuedPacket(val sender: Short, val target: Short, val data: Map[String, String])
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user