mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-17 19:25:20 -04:00
moved some more stuff from case to computer superclass
This commit is contained in:
parent
34a927059f
commit
5faedd2525
@ -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.
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
}
|
@ -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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user