From 5faedd25251b0e2a2dfcda249535ae42eb6b7c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Fri, 15 Nov 2013 19:57:11 +0100 Subject: [PATCH] moved some more stuff from case to computer superclass --- li/cil/oc/client/PacketHandler.scala | 2 +- li/cil/oc/client/PacketSender.scala | 2 +- li/cil/oc/common/tileentity/Case.scala | 67 +--------------- li/cil/oc/common/tileentity/Computer.scala | 91 +++++++++++++++++----- 4 files changed, 76 insertions(+), 86 deletions(-) diff --git a/li/cil/oc/client/PacketHandler.scala b/li/cil/oc/client/PacketHandler.scala index 960586166..993306329 100644 --- a/li/cil/oc/client/PacketHandler.scala +++ b/li/cil/oc/client/PacketHandler.scala @@ -57,7 +57,7 @@ class PacketHandler extends CommonPacketHandler { } def onComputerStateResponse(p: PacketParser) = - p.readTileEntity[Case]() match { + p.readTileEntity[Computer]() match { case Some(t) => t.isOn = p.readBoolean() case _ => // Invalid packet. } diff --git a/li/cil/oc/client/PacketSender.scala b/li/cil/oc/client/PacketSender.scala index 260380641..d56a933db 100644 --- a/li/cil/oc/client/PacketSender.scala +++ b/li/cil/oc/client/PacketSender.scala @@ -5,7 +5,7 @@ import li.cil.oc.common.PacketType import li.cil.oc.common.tileentity._ object PacketSender { - def sendComputerStateRequest(t: Case) { + def sendComputerStateRequest(t: Computer) { val pb = new PacketBuilder(PacketType.ComputerStateRequest) pb.writeTileEntity(t) diff --git a/li/cil/oc/common/tileentity/Case.scala b/li/cil/oc/common/tileentity/Case.scala index 0e00d8300..340076f8c 100644 --- a/li/cil/oc/common/tileentity/Case.scala +++ b/li/cil/oc/common/tileentity/Case.scala @@ -2,67 +2,22 @@ package li.cil.oc.common.tileentity import li.cil.oc.Config import li.cil.oc.api.driver.Slot -import li.cil.oc.client.{PacketSender => ClientPacketSender} import li.cil.oc.server.component -import li.cil.oc.server.driver import li.cil.oc.server.driver.Registry -import li.cil.oc.server.{PacketSender => ServerPacketSender} import net.minecraft.item.ItemStack -import net.minecraft.nbt.NBTTagCompound import net.minecraftforge.common.ForgeDirection -class Case(isClient: Boolean) extends Computer with ComponentInventory with Rotatable with Redstone { +class Case(isClient: Boolean) extends Computer { def this() = this(false) // ----------------------------------------------------------------------- // val instance = if (isClient) null else new component.Computer(this) - private var isRunning = false - - // ----------------------------------------------------------------------- // - - def isOn = isRunning - - def isOn_=(value: Boolean) = { - isRunning = value - world.markBlockForRenderUpdate(x, y, z) - this - } - // ----------------------------------------------------------------------- // override def updateEntity() { super.updateEntity() - if (isServer) { - if (isRunning != instance.isRunning) { - isOutputEnabled = hasRedstoneCard && instance.isRunning - ServerPacketSender.sendComputerState(this, instance.isRunning) - } - isRunning = instance.isRunning - updateRedstoneInput() - } - - for (component <- components) component match { - case Some(environment) => environment.update() - case _ => // Empty. - } - } - - override def validate() = { - super.validate() - if (isClient) { - ClientPacketSender.sendRotatableStateRequest(this) - ClientPacketSender.sendComputerStateRequest(this) - ClientPacketSender.sendRedstoneStateRequest(this) - } - } - - // ----------------------------------------------------------------------- // - - override def readFromNBT(nbt: NBTTagCompound) { - super.readFromNBT(nbt) - instance.recomputeMemory() } // ----------------------------------------------------------------------- // @@ -80,27 +35,7 @@ class Case(isClient: Boolean) extends Computer with ComponentInventory with Rota case _ => false // Invalid slot. } - override def onInventoryChanged() { - super.onInventoryChanged() - if (isServer) { - instance.recomputeMemory() - isOutputEnabled = hasRedstoneCard && instance.isRunning - } - } - // ----------------------------------------------------------------------- // def canConnectRedstone(side: ForgeDirection) = isOutputEnabled - - override protected def onRedstoneInputChanged(side: ForgeDirection) { - super.onRedstoneInputChanged(side) - if (isServer) { - instance.signal("redstone_changed", side.ordinal()) - } - } - - private def hasRedstoneCard = items.exists { - case Some(item) => driver.item.RedstoneCard.worksWith(item) - case _ => false - } } \ No newline at end of file diff --git a/li/cil/oc/common/tileentity/Computer.scala b/li/cil/oc/common/tileentity/Computer.scala index d3e5548a1..0a4e2de66 100644 --- a/li/cil/oc/common/tileentity/Computer.scala +++ b/li/cil/oc/common/tileentity/Computer.scala @@ -1,13 +1,15 @@ package li.cil.oc.common.tileentity import li.cil.oc.api.network._ -import li.cil.oc.server.component +import li.cil.oc.client.{PacketSender => ClientPacketSender} +import li.cil.oc.server.{PacketSender => ServerPacketSender, driver, component} import li.cil.oc.{Config, api} import net.minecraft.entity.player.EntityPlayer import net.minecraft.nbt.NBTTagCompound +import net.minecraftforge.common.ForgeDirection import scala.Some -abstract class Computer extends Environment with Context with Analyzable { +abstract class Computer extends Environment with ComponentInventory with Rotatable with Redstone with Context with Analyzable { val node = api.Network.newNode(this, Visibility.Network). withComponent("computer", Visibility.Neighbors). withConnector(). @@ -15,42 +17,80 @@ abstract class Computer extends Environment with Context with Analyzable { val instance: component.Computer - def installedMemory: Int - - // ----------------------------------------------------------------------- // + private var isRunning = false private var hasChanged = false + // ----------------------------------------------------------------------- // + + def isOn = isRunning + + def isOn_=(value: Boolean) = { + isRunning = value + world.markBlockForRenderUpdate(x, y, z) + this + } + def markAsChanged() = hasChanged = true + def hasRedstoneCard = items.exists { + case Some(item) => driver.item.RedstoneCard.worksWith(item) + case _ => false + } + // ----------------------------------------------------------------------- // override def updateEntity() { - // If we're not yet in a network we were just loaded from disk. We skip - // the update this round to allow other tile entities to join the network, - // too, avoiding issues of missing nodes (e.g. in the GPU which would - // otherwise loose track of its screen). - if (isServer && node != null && node.network != null) { - if (instance.isRunning && !node.changeBuffer(-Config.computerCost)) { - instance.lastError = "not enough energy" - instance.stop() - } - instance.update() + if (isServer) { + // If we're not yet in a network we were just loaded from disk. We skip + // the update this round to allow other tile entities to join the network, + // too, avoiding issues of missing nodes (e.g. in the GPU which would + // otherwise loose track of its screen). + if (node != null && node.network != null) { + if (instance.isRunning && !node.changeBuffer(-Config.computerCost)) { + instance.lastError = "not enough energy" + instance.stop() + } + instance.update() - if (hasChanged) { - hasChanged = false - worldObj.markTileEntityChunkModified(xCoord, yCoord, zCoord, this) + if (hasChanged) { + hasChanged = false + worldObj.markTileEntityChunkModified(xCoord, yCoord, zCoord, this) + } + } + + if (isRunning != instance.isRunning) { + isOutputEnabled = hasRedstoneCard && instance.isRunning + ServerPacketSender.sendComputerState(this, instance.isRunning) + } + isRunning = instance.isRunning + + updateRedstoneInput() + + for (component <- components) component match { + case Some(environment) => environment.update() + case _ => // Empty. } } super.updateEntity() } + override def validate() = { + super.validate() + if (isClient) { + ClientPacketSender.sendRotatableStateRequest(this) + ClientPacketSender.sendComputerStateRequest(this) + ClientPacketSender.sendRedstoneStateRequest(this) + } + } + // ----------------------------------------------------------------------- // override def readFromNBT(nbt: NBTTagCompound) { super.readFromNBT(nbt) if (instance != null) instance.load(nbt) + // instance.recomputeMemory() } override def writeToNBT(nbt: NBTTagCompound) { @@ -78,6 +118,21 @@ abstract class Computer extends Environment with Context with Analyzable { this } + override def onInventoryChanged() { + super.onInventoryChanged() + if (isServer) { + instance.recomputeMemory() + isOutputEnabled = hasRedstoneCard && instance.isRunning + } + } + + override protected def onRedstoneInputChanged(side: ForgeDirection) { + super.onRedstoneInputChanged(side) + if (isServer) { + instance.signal("redstone_changed", side.ordinal()) + } + } + // ----------------------------------------------------------------------- // @LuaCallback("start")