Yet more redstone refactoring, also passing along old and new max signal values for convenience.

This commit is contained in:
Florian Nücke 2015-01-15 19:16:22 +01:00
parent 0db6028ecd
commit 42d5d997a3
10 changed files with 51 additions and 33 deletions

View File

@ -173,7 +173,7 @@ abstract class DynamicGuiContainer(container: Container) extends CustomGuiContai
private def drawNEIHighlights(): Unit = {
val panel = LayoutManager.itemPanel
if (panel == null) return
zLevel += 500
zLevel += 350
for (index <- 0 until ItemPanel.items.size()) {
val rect = panel.getSlotRect(index)
val slot = panel.getSlotMouseOver(rect.x, rect.y)
@ -188,6 +188,6 @@ abstract class DynamicGuiContainer(container: Container) extends CustomGuiContai
case _ =>
}
}
zLevel -= 500
zLevel -= 350
}
}

View File

@ -1,6 +1,7 @@
package li.cil.oc.common.tileentity
import li.cil.oc.Settings
import li.cil.oc.api
import li.cil.oc.api.network.Visibility
import li.cil.oc.common.tileentity.traits.BundledRedstoneAware
import li.cil.oc.common.tileentity.traits.Environment
@ -17,10 +18,12 @@ class Redstone extends Environment with BundledRedstoneAware {
else
new component.Redstone.Vanilla(this)
val node = instance.node
if (node != null) {
val dummyNode = if (node != null) {
node.setVisibility(Visibility.Network)
_isOutputEnabled = true
api.Network.newNode(this, Visibility.None).create()
}
else null
override def canUpdate = isServer
@ -38,8 +41,9 @@ class Redstone extends Environment with BundledRedstoneAware {
// ----------------------------------------------------------------------- //
override protected def onRedstoneInputChanged(side: ForgeDirection) {
super.onRedstoneInputChanged(side)
node.sendToReachable("computer.signal", "redstone_changed", Int.box(side.ordinal()))
override protected def onRedstoneInputChanged(side: ForgeDirection, oldMaxValue: Int, newMaxValue: Int) {
super.onRedstoneInputChanged(side, oldMaxValue, newMaxValue)
node.connect(dummyNode)
dummyNode.sendToNeighbors("redstone.changed", side, int2Integer(oldMaxValue), int2Integer(newMaxValue))
}
}

View File

@ -314,8 +314,8 @@ class Screen(var tier: Int) extends traits.TextBuffer with SidedEnvironment with
override def onAnalyze(player: EntityPlayer, side: Int, hitX: Float, hitY: Float, hitZ: Float) = Array(origin.node)
override protected def onRedstoneInputChanged(side: ForgeDirection) {
super.onRedstoneInputChanged(side)
override protected def onRedstoneInputChanged(side: ForgeDirection, oldMaxValue: Int, newMaxValue: Int) {
super.onRedstoneInputChanged(side, oldMaxValue, newMaxValue)
val hasRedstoneInput = screens.map(_.maxInput).max > 0
if (hasRedstoneInput != hadRedstoneInput) {
hadRedstoneInput = hasRedstoneInput

View File

@ -506,10 +506,10 @@ class ServerRack extends traits.PowerAcceptor with traits.Hub with traits.PowerB
checkRedstoneInputChanged()
}
override protected def onRedstoneInputChanged(side: ForgeDirection) {
super.onRedstoneInputChanged(side)
override protected def onRedstoneInputChanged(side: ForgeDirection, oldMaxValue: Int, newMaxValue: Int) {
super.onRedstoneInputChanged(side, oldMaxValue, newMaxValue)
servers collect {
case Some(server) => server.machine.node.sendToNeighbors("redstone.changed", toLocal(side))
case Some(server) => server.machine.node.sendToNeighbors("redstone.changed", toLocal(side), int2Integer(oldMaxValue), int2Integer(newMaxValue))
}
}

View File

@ -48,13 +48,15 @@ trait BundledRedstoneAware extends RedstoneAware with IBundledEmitter with IBund
def bundledInput(side: ForgeDirection, color: Int) =
math.max(_bundledInput(side.ordinal())(color), _rednetInput(side.ordinal())(color))
def rednetInput(side: ForgeDirection, color: Int, value: Int) =
if (_rednetInput(side.ordinal())(color) != value) {
if (_rednetInput(side.ordinal())(color) != -1) {
onRedstoneInputChanged(side)
def rednetInput(side: ForgeDirection, color: Int, value: Int): Unit = {
val oldValue = _rednetInput(side.ordinal())(color)
if (oldValue != value) {
if (oldValue != -1) {
onRedstoneInputChanged(side, oldValue, value)
}
_rednetInput(side.ordinal())(color) = value
}
}
def bundledOutput(side: ForgeDirection) = _bundledOutput(toLocal(side).ordinal())
@ -78,19 +80,20 @@ trait BundledRedstoneAware extends RedstoneAware with IBundledEmitter with IBund
override protected def updateRedstoneInput(side: ForgeDirection) {
super.updateRedstoneInput(side)
val oldBundledInput = _bundledInput(side.ordinal())
val ownBundledInput = _bundledInput(side.ordinal())
val newBundledInput = computeBundledInput(side)
val oldMaxValue = ownBundledInput.max
var changed = false
if (newBundledInput != null) for (color <- 0 until 16) {
changed = changed || (oldBundledInput(color) >= 0 && oldBundledInput(color) != newBundledInput(color))
oldBundledInput(color) = newBundledInput(color)
changed = changed || (ownBundledInput(color) >= 0 && ownBundledInput(color) != newBundledInput(color))
ownBundledInput(color) = newBundledInput(color)
}
else for (color <- 0 until 16) {
changed = changed || oldBundledInput(color) > 0
oldBundledInput(color) = 0
changed = changed || ownBundledInput(color) > 0
ownBundledInput(color) = 0
}
if (changed) {
onRedstoneInputChanged(side)
onRedstoneInputChanged(side, oldMaxValue, ownBundledInput.max)
}
}

View File

@ -204,9 +204,9 @@ trait Computer extends Environment with ComponentInventory with Rotatable with B
checkRedstoneInputChanged()
}
override protected def onRedstoneInputChanged(side: ForgeDirection) {
super.onRedstoneInputChanged(side)
machine.node.sendToNeighbors("redstone.changed", toLocal(side))
override protected def onRedstoneInputChanged(side: ForgeDirection, oldMaxValue: Int, newMaxValue: Int) {
super.onRedstoneInputChanged(side, oldMaxValue, newMaxValue)
machine.node.sendToNeighbors("redstone.changed", toLocal(side), int2Integer(oldMaxValue), int2Integer(newMaxValue))
}
// ----------------------------------------------------------------------- //

View File

@ -79,8 +79,8 @@ trait RedstoneAware extends RotationAware with IConnectable with IRedstoneEmitte
val oldInput = _input(side.ordinal())
val newInput = computeInput(side)
_input(side.ordinal()) = newInput
if (oldInput >= 0 && input(side) != oldInput) {
onRedstoneInputChanged(side)
if (oldInput >= 0 && newInput != oldInput) {
onRedstoneInputChanged(side, oldInput, newInput)
}
}
@ -140,7 +140,7 @@ trait RedstoneAware extends RotationAware with IConnectable with IRedstoneEmitte
}
}
protected def onRedstoneInputChanged(side: ForgeDirection) {}
protected def onRedstoneInputChanged(side: ForgeDirection, oldMaxValue: Int, newMaxValue: Int) {}
protected def onRedstoneOutputEnabledChanged() {
world.notifyBlocksOfNeighborChange(position, block)

View File

@ -0,0 +1,12 @@
package li.cil.oc.server.component
import li.cil.oc.api.Persistable
import li.cil.oc.api.network.Node
trait RedstoneSignaller extends Persistable {
def node: Node
def onRedstoneChanged(side: AnyRef, oldMaxValue: AnyRef, newMaxValue: AnyRef): Unit = {
node.sendToReachable("computer.signal", "redstone_changed", side, oldMaxValue, newMaxValue)
}
}

View File

@ -7,12 +7,10 @@ import li.cil.oc.api.machine.Callback
import li.cil.oc.api.machine.Context
import li.cil.oc.api.network._
import li.cil.oc.api.prefab
import li.cil.oc.common.tileentity.traits.BundledRedstoneAware
import li.cil.oc.common.tileentity.traits.RedstoneAware
import li.cil.oc.server.component
import net.minecraftforge.common.util.ForgeDirection
trait RedstoneVanilla extends prefab.ManagedEnvironment {
trait RedstoneVanilla extends prefab.ManagedEnvironment with RedstoneSignaller {
override val node = Network.newNode(this, Visibility.Network).
withComponent("redstone", Visibility.Neighbors).
create()
@ -47,7 +45,8 @@ trait RedstoneVanilla extends prefab.ManagedEnvironment {
override def onMessage(message: Message): Unit = {
super.onMessage(message)
if (message.name == "redstone.changed") message.data match {
case Array(side: ForgeDirection) => node.sendToReachable("computer.signal", "redstone_changed", Int.box(side.ordinal()))
case Array(side: ForgeDirection, oldMaxValue: Number, newMaxValue: Number) =>
onRedstoneChanged(int2Integer(side.ordinal()), oldMaxValue, newMaxValue)
case _ =>
}
}

View File

@ -19,7 +19,7 @@ import net.minecraft.nbt.NBTTagCompound
new Optional.Interface(iface = "codechicken.wirelessredstone.core.WirelessReceivingDevice", modid = Mods.IDs.WirelessRedstoneCBE),
new Optional.Interface(iface = "codechicken.wirelessredstone.core.WirelessTransmittingDevice", modid = Mods.IDs.WirelessRedstoneCBE)
))
trait RedstoneWireless extends prefab.ManagedEnvironment with WirelessReceivingDevice with WirelessTransmittingDevice {
trait RedstoneWireless extends prefab.ManagedEnvironment with RedstoneSignaller with WirelessReceivingDevice with WirelessTransmittingDevice {
def redstone: EnvironmentHost
var wirelessFrequency = 0
@ -77,7 +77,7 @@ trait RedstoneWireless extends prefab.ManagedEnvironment with WirelessReceivingD
override def updateDevice(frequency: Int, on: Boolean) {
if (frequency == wirelessFrequency && on != wirelessInput) {
wirelessInput = on
node.sendToReachable("computer.signal", "redstone_changed", "wireless")
onRedstoneChanged("wireless", boolean2Boolean(!on), boolean2Boolean(on))
}
}