diff --git a/src/main/scala/li/cil/oc/common/tileentity/BundledRedstoneAware.scala b/src/main/scala/li/cil/oc/common/tileentity/BundledRedstoneAware.scala index 0d18c253b..b23c01d12 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/BundledRedstoneAware.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/BundledRedstoneAware.scala @@ -57,31 +57,38 @@ trait BundledRedstoneAware extends RedstoneAware with IBundledEmitter with IBund def bundledOutput(side: ForgeDirection, color: Int, value: Int): Unit = if (value != bundledOutput(side, color)) { _bundledOutput(toLocal(side).ordinal())(color) = value - onRedstoneOutputChanged(side) + + if (Loader.isModLoaded("MineFactoryReloaded")) { + val nx = x + side.offsetX + val ny = y + side.offsetY + val nz = z + side.offsetZ + world.getBlock(nx, ny, nz) match { + case block: IRedNetNetworkContainer => block.updateNetwork(world, nx, ny, nz) + case _ => + } + } + + onRedstoneOutputChanged() } // ----------------------------------------------------------------------- // - override def updateRedstoneInput() { - if (shouldUpdateInput) { - for (side <- ForgeDirection.VALID_DIRECTIONS) { - val oldBundledInput = _bundledInput(side.ordinal()) - val newBundledInput = computeBundledInput(side) - var changed = false - if (newBundledInput != null) for (color <- 0 until 16) { - changed = changed || (oldBundledInput(color) >= 0 && oldBundledInput(color) != newBundledInput(color)) - oldBundledInput(color) = newBundledInput(color) - } - else for (color <- 0 until 16) { - changed = changed || oldBundledInput(color) > 0 - oldBundledInput(color) = 0 - } - if (changed) { - onRedstoneInputChanged(side) - } - } + override protected def updateRedstoneInput(side: ForgeDirection) { + super.updateRedstoneInput(side) + val oldBundledInput = _bundledInput(side.ordinal()) + val newBundledInput = computeBundledInput(side) + var changed = false + if (newBundledInput != null) for (color <- 0 until 16) { + changed = changed || (oldBundledInput(color) >= 0 && oldBundledInput(color) != newBundledInput(color)) + oldBundledInput(color) = newBundledInput(color) + } + else for (color <- 0 until 16) { + changed = changed || oldBundledInput(color) > 0 + oldBundledInput(color) = 0 + } + if (changed) { + onRedstoneInputChanged(side) } - super.updateRedstoneInput() } override def readFromNBT(nbt: NBTTagCompound) { @@ -152,32 +159,19 @@ trait BundledRedstoneAware extends RedstoneAware with IBundledEmitter with IBund } } - override protected def onRedstoneOutputChanged(side: ForgeDirection) { - if (side == ForgeDirection.UNKNOWN) { - if (Loader.isModLoaded("MineFactoryReloaded")) { - for (side <- ForgeDirection.VALID_DIRECTIONS) { - val nx = x + side.offsetX - val ny = y + side.offsetY - val nz = z + side.offsetZ - world.getBlock(nx, ny, nz) match { - case block: IRedNetNetworkContainer => block.updateNetwork(world, x, y, z) - case _ => - } - } - } - } - else { - val nx = x + side.offsetX - val ny = y + side.offsetY - val nz = z + side.offsetZ - if (Loader.isModLoaded("MineFactoryReloaded")) { + override protected def onRedstoneOutputEnabledChanged() { + if (Loader.isModLoaded("MineFactoryReloaded")) { + for (side <- ForgeDirection.VALID_DIRECTIONS) { + val nx = x + side.offsetX + val ny = y + side.offsetY + val nz = z + side.offsetZ world.getBlock(nx, ny, nz) match { case block: IRedNetNetworkContainer => block.updateNetwork(world, x, y, z) case _ => } } } - super.onRedstoneOutputChanged(side) + super.onRedstoneOutputEnabledChanged() } // ----------------------------------------------------------------------- // diff --git a/src/main/scala/li/cil/oc/common/tileentity/Charger.scala b/src/main/scala/li/cil/oc/common/tileentity/Charger.scala index ed1432a63..e982aefc0 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/Charger.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/Charger.scala @@ -30,8 +30,6 @@ class Charger extends Environment with RedstoneAware with Analyzable { override def updateEntity() { super.updateEntity() if (isServer) { - updateRedstoneInput() - val charge = Settings.get.chargeRate * chargeSpeed robots.collect { case Some(proxy) => node.changeBuffer(proxy.robot.bot.node.changeBuffer(charge + node.changeBuffer(-charge))) diff --git a/src/main/scala/li/cil/oc/common/tileentity/Computer.scala b/src/main/scala/li/cil/oc/common/tileentity/Computer.scala index c22e2bbbf..bebbb9615 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/Computer.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/Computer.scala @@ -125,7 +125,6 @@ trait Computer extends Environment with ComponentInventory with Rotatable with B ServerPacketSender.sendComputerState(this) } - updateRedstoneInput() updateComponents() } } diff --git a/src/main/scala/li/cil/oc/common/tileentity/Rack.scala b/src/main/scala/li/cil/oc/common/tileentity/Rack.scala index 69cc11a6c..c55077876 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/Rack.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/Rack.scala @@ -219,7 +219,6 @@ class Rack extends PowerAcceptor with Hub with PowerBalancer with Inventory with isOutputEnabled = hasRedstoneCard isAbstractBusAvailable = hasAbstractBusCard - updateRedstoneInput() servers collect { case Some(server) => server.inventory.updateComponents() } diff --git a/src/main/scala/li/cil/oc/common/tileentity/Redstone.scala b/src/main/scala/li/cil/oc/common/tileentity/Redstone.scala index 8d5213558..997033357 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/Redstone.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/Redstone.scala @@ -16,13 +16,6 @@ class Redstone extends Environment with BundledRedstoneAware { _isOutputEnabled = true } - override def updateEntity() { - super.updateEntity() - if (isServer) { - updateRedstoneInput() - } - } - override def readFromNBT(nbt: NBTTagCompound) { super.readFromNBT(nbt) instance.load(nbt.getCompoundTag(Settings.namespace + "redstone")) diff --git a/src/main/scala/li/cil/oc/common/tileentity/RedstoneAware.scala b/src/main/scala/li/cil/oc/common/tileentity/RedstoneAware.scala index c05606596..6170b8137 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/RedstoneAware.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/RedstoneAware.scala @@ -33,7 +33,7 @@ trait RedstoneAware extends RotationAware with IConnectable with IRedstoneEmitte _output(i) = 0 } } - onRedstoneOutputChanged(ForgeDirection.UNKNOWN) + onRedstoneOutputEnabledChanged() } this } @@ -44,9 +44,16 @@ trait RedstoneAware extends RotationAware with IConnectable with IRedstoneEmitte def output(side: ForgeDirection) = _output(toLocal(side).ordinal()) - def output(side: ForgeDirection, value: Int): Unit = if (value != output(side)) { + def output(side: ForgeDirection, value: Int): Unit = if (value != output(side)) this.synchronized { _output(toLocal(side).ordinal()) = value - onRedstoneOutputChanged(side) + + val nx = x + side.offsetX + val ny = y + side.offsetY + val nz = z + side.offsetZ + world.notifyBlockOfNeighborChange(nx, ny, nz, block) + world.notifyBlocksOfNeighborChange(nx, ny, nz, world.getBlock(nx, ny, nz), side.getOpposite.ordinal) + + onRedstoneOutputChanged() } def checkRedstoneInputChanged() { @@ -55,20 +62,27 @@ trait RedstoneAware extends RotationAware with IConnectable with IRedstoneEmitte // ----------------------------------------------------------------------- // - def updateRedstoneInput() { - if (shouldUpdateInput) { - shouldUpdateInput = false - for (side <- ForgeDirection.VALID_DIRECTIONS) { - val oldInput = _input(side.ordinal()) - val newInput = computeInput(side) - _input(side.ordinal()) = newInput - if (oldInput >= 0 && input(side) != oldInput) { - onRedstoneInputChanged(side) + override def updateEntity() { + super.updateEntity() + if (isServer) { + if (shouldUpdateInput) { + shouldUpdateInput = false + for (side <- ForgeDirection.VALID_DIRECTIONS) { + updateRedstoneInput(side) } } } } + protected def updateRedstoneInput(side: ForgeDirection) { + val oldInput = _input(side.ordinal()) + val newInput = computeInput(side) + _input(side.ordinal()) = newInput + if (oldInput >= 0 && input(side) != oldInput) { + onRedstoneInputChanged(side) + } + } + // ----------------------------------------------------------------------- // override def readFromNBT(nbt: NBTTagCompound) = { @@ -124,17 +138,13 @@ trait RedstoneAware extends RotationAware with IConnectable with IRedstoneEmitte protected def onRedstoneInputChanged(side: ForgeDirection) {} - protected def onRedstoneOutputChanged(side: ForgeDirection) { - if (side == ForgeDirection.UNKNOWN) { - world.notifyBlocksOfNeighborChange(x, y, z, block) - } - else { - val nx = x + side.offsetX - val ny = y + side.offsetY - val nz = z + side.offsetZ - world.notifyBlockOfNeighborChange(nx, ny, nz, block) - world.notifyBlocksOfNeighborChange(nx, ny, nz, world.getBlock(nx, ny, nz)) - } + protected def onRedstoneOutputEnabledChanged() = this.synchronized { + world.notifyBlocksOfNeighborChange(x, y, z, block) + if (isServer) ServerPacketSender.sendRedstoneState(this) + else world.markBlockForUpdate(x, y, z) + } + + protected def onRedstoneOutputChanged() { if (isServer) ServerPacketSender.sendRedstoneState(this) else world.markBlockForUpdate(x, y, z) } diff --git a/src/main/scala/li/cil/oc/common/tileentity/RobotProxy.scala b/src/main/scala/li/cil/oc/common/tileentity/RobotProxy.scala index b0f8f3506..70086e7ac 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/RobotProxy.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/RobotProxy.scala @@ -154,8 +154,6 @@ class RobotProxy(val robot: Robot) extends Computer with ISidedInventory with Bu override def checkRedstoneInputChanged() = robot.checkRedstoneInputChanged() - override def updateRedstoneInput() = robot.updateRedstoneInput() - @Optional.Method(modid = "RedLogic") override def connects(wire: IWire, blockFace: Int, fromDirection: Int) = robot.connects(wire, blockFace, fromDirection) diff --git a/src/main/scala/li/cil/oc/common/tileentity/Screen.scala b/src/main/scala/li/cil/oc/common/tileentity/Screen.scala index 395863f86..32ef37e69 100644 --- a/src/main/scala/li/cil/oc/common/tileentity/Screen.scala +++ b/src/main/scala/li/cil/oc/common/tileentity/Screen.scala @@ -201,9 +201,6 @@ class Screen(var tier: Int) extends Buffer with SidedEnvironment with Rotatable override def updateEntity() { super.updateEntity() - if (isServer) { - updateRedstoneInput() - } if (isServer && isOn && isOrigin && world.getWorldTime % Settings.get.tickFrequency == 0) { if (relativeLitArea < 0) { // The relative lit area is the number of pixels that are not blank diff --git a/src/main/scala/li/cil/oc/server/component/machine/Machine.scala b/src/main/scala/li/cil/oc/server/component/machine/Machine.scala index 8b2d58be7..f1684f248 100644 --- a/src/main/scala/li/cil/oc/server/component/machine/Machine.scala +++ b/src/main/scala/li/cil/oc/server/component/machine/Machine.scala @@ -382,6 +382,11 @@ class Machine(val owner: Owner, val rom: Option[ManagedEnvironment], constructor switchTo(Machine.State.Running) try { architecture.runSynchronized() + // This sleep is used to avoid spammy synchronized calls to increase + // the tick time the computer eats unduly. This is a very... rough + // workaround for that problem, and may have to be addressed with + // more care at some point... aka when I have more time. + pause(0.1) // Check if the callback called pause() or stop(). state.top match { case Machine.State.Running =>