Not all things must tick.

This commit is contained in:
Florian Nücke 2016-05-26 17:34:37 +02:00
parent 726aa36164
commit ea37f4db67
36 changed files with 51 additions and 83 deletions

View File

@ -54,11 +54,7 @@ abstract class RedstoneAware extends SimpleBlock /* with IRedNetOmniNode TODO MF
}
*/
world.getTileEntity(pos) match {
case redstone: tileentity.traits.RedstoneAware =>
if (redstone.canUpdate)
redstone.checkRedstoneInputChanged()
else
EnumFacing.values().foreach(redstone.updateRedstoneInput)
case redstone: tileentity.traits.RedstoneAware => redstone.checkRedstoneInputChanged()
case _ => // Ignore.
}
}

View File

@ -16,7 +16,7 @@ import net.minecraftforge.common.util.Constants.NBT
import scala.collection.mutable
class Adapter extends traits.Environment with traits.ComponentInventory with Analyzable with internal.Adapter {
class Adapter extends traits.Environment with traits.ComponentInventory with traits.Tickable with Analyzable with internal.Adapter {
val node = api.Network.newNode(this, Visibility.Network).create()
private val blocks = Array.fill[Option[(ManagedEnvironment, api.driver.SidedBlock)]](6)(None)
@ -33,11 +33,9 @@ class Adapter extends traits.Environment with traits.ComponentInventory with Ana
// ----------------------------------------------------------------------- //
override def canUpdate = isServer
override def updateEntity() {
super.updateEntity()
if (updatingBlocks.nonEmpty) {
if (isServer && updatingBlocks.nonEmpty) {
for (block <- updatingBlocks) {
block.update()
}

View File

@ -17,7 +17,7 @@ import net.minecraft.util.EnumFacing
import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly
class Assembler extends traits.Environment with traits.PowerAcceptor with traits.Inventory with SidedEnvironment with traits.StateAware {
class Assembler extends traits.Environment with traits.PowerAcceptor with traits.Inventory with SidedEnvironment with traits.StateAware with traits.Tickable {
val node = api.Network.newNode(this, Visibility.Network).
withComponent("assembler").
withConnector(Settings.get.bufferConverter).
@ -106,8 +106,6 @@ class Assembler extends traits.Environment with traits.PowerAcceptor with traits
// ----------------------------------------------------------------------- //
override def canUpdate = isServer
override def updateEntity() {
super.updateEntity()
if (output.isDefined && world.getTotalWorldTime % Settings.get.tickFrequency == 0) {

View File

@ -40,7 +40,5 @@ class Cable extends traits.Environment with traits.NotAnalyzable with traits.Imm
}
}
override def canUpdate = false
override def getRenderBoundingBox = common.block.Cable.bounds(world, getPos).offset(x, y, z)
}

View File

@ -15,8 +15,6 @@ class Capacitor extends traits.Environment {
// ----------------------------------------------------------------------- //
override def canUpdate = false
override def dispose() {
super.dispose()
if (isServer) {

View File

@ -42,8 +42,6 @@ class Case(var tier: Int) extends traits.PowerAcceptor with traits.Computer with
// ----------------------------------------------------------------------- //
override def canUpdate = isServer
override def updateEntity() {
if (isServer && isCreative && world.getTotalWorldTime % Settings.get.tickFrequency == 0) {
// Creative case, make it generate power.

View File

@ -26,7 +26,7 @@ import net.minecraftforge.fml.relauncher.SideOnly
import scala.collection.convert.WrapAsScala._
import scala.collection.mutable
class Charger extends traits.Environment with traits.PowerAcceptor with traits.RedstoneAware with traits.Rotatable with traits.ComponentInventory with Analyzable with traits.StateAware {
class Charger extends traits.Environment with traits.PowerAcceptor with traits.RedstoneAware with traits.Rotatable with traits.ComponentInventory with traits.Tickable with Analyzable with traits.StateAware {
val node = api.Network.newNode(this, Visibility.None).
withConnector(Settings.get.bufferConverter).
create()
@ -64,8 +64,6 @@ class Charger extends traits.Environment with traits.PowerAcceptor with traits.R
// ----------------------------------------------------------------------- //
override def canUpdate = true
override def updateEntity() {
super.updateEntity()

View File

@ -21,7 +21,7 @@ import net.minecraftforge.fml.relauncher.SideOnly
import scala.collection.mutable
class Disassembler extends traits.Environment with traits.PowerAcceptor with traits.Inventory with traits.StateAware with traits.PlayerInputAware {
class Disassembler extends traits.Environment with traits.PowerAcceptor with traits.Inventory with traits.StateAware with traits.PlayerInputAware with traits.Tickable {
val node = api.Network.newNode(this, Visibility.None).
withConnector(Settings.get.bufferConverter).
create()
@ -61,11 +61,9 @@ class Disassembler extends traits.Environment with traits.PowerAcceptor with tra
// ----------------------------------------------------------------------- //
override def canUpdate = isServer
override def updateEntity() {
super.updateEntity()
if (world.getTotalWorldTime % Settings.get.tickFrequency == 0) {
if (isServer && world.getTotalWorldTime % Settings.get.tickFrequency == 0) {
if (queue.isEmpty) {
val instant = disassembleNextInstantly // Is reset via decrStackSize
disassemble(decrStackSize(0, 1), instant)

View File

@ -102,8 +102,6 @@ class DiskDrive extends traits.Environment with traits.ComponentInventory with t
// ----------------------------------------------------------------------- //
// TileEntity
override def canUpdate = false
@SideOnly(Side.CLIENT) override
def readFromNBTForClient(nbt: NBTTagCompound) {
super.readFromNBTForClient(nbt)

View File

@ -8,8 +8,6 @@ class Geolyzer extends traits.Environment {
def node = geolyzer.node
override def canUpdate = false
override def readFromNBTForServer(nbt: NBTTagCompound) {
super.readFromNBTForServer(nbt)
geolyzer.load(nbt)

View File

@ -20,7 +20,7 @@ import net.minecraftforge.fml.relauncher.SideOnly
import scala.collection.mutable
class Hologram(var tier: Int) extends traits.Environment with SidedEnvironment with Analyzable with traits.Rotatable {
class Hologram(var tier: Int) extends traits.Environment with SidedEnvironment with Analyzable with traits.Rotatable with traits.Tickable {
def this() = this(0)
val node = api.Network.newNode(this, Visibility.Network).
@ -367,8 +367,6 @@ class Hologram(var tier: Int) extends traits.Environment with SidedEnvironment w
// ----------------------------------------------------------------------- //
override def canUpdate = isServer
override def updateEntity() {
super.updateEntity()
if (isServer) {

View File

@ -37,8 +37,6 @@ class Keyboard extends traits.Environment with traits.Rotatable with traits.Immi
// ----------------------------------------------------------------------- //
override def canUpdate = false
override def readFromNBTForServer(nbt: NBTTagCompound) {
super.readFromNBTForServer(nbt)
if (isServer) {

View File

@ -111,13 +111,11 @@ class Microcontroller extends traits.PowerAcceptor with traits.Hub with traits.C
// ----------------------------------------------------------------------- //
override def canUpdate = isServer
override def updateEntity() {
super.updateEntity()
// Pump energy into the internal network.
if (world.getTotalWorldTime % Settings.get.tickFrequency == 0) {
if (isServer && world.getTotalWorldTime % Settings.get.tickFrequency == 0) {
for (side <- EnumFacing.values if side != facing) {
sidedNode(side) match {
case connector: Connector =>

View File

@ -15,7 +15,7 @@ import net.minecraft.util.Vec3
import scala.collection.convert.WrapAsScala._
import scala.collection.mutable
class MotionSensor extends traits.Environment {
class MotionSensor extends traits.Environment with traits.Tickable {
val node = api.Network.newNode(this, Visibility.Network).
withComponent("motion_sensor").
withConnector().
@ -29,16 +29,13 @@ class MotionSensor extends traits.Environment {
// ----------------------------------------------------------------------- //
override def canUpdate = isServer
override def updateEntity() {
super.updateEntity()
if (world.getTotalWorldTime % 10 == 0) {
if (isServer && world.getTotalWorldTime % 10 == 0) {
// Get a list of all living entities we could possibly detect, using a rough
// bounding box check, then refining it using the actual distance and an
// actual visibility check.
val entities = world.getEntitiesWithinAABB(classOf[EntityLivingBase], sensorBounds)
.map(_.asInstanceOf[EntityLivingBase])
.filter(entity => entity.isEntityAlive && isInRange(entity) && isVisible(entity))
.toSet
// Get rid of all tracked entities that are no longer visible.

View File

@ -54,8 +54,6 @@ class NetSplitter extends traits.Environment with traits.RedstoneAware with api.
// ----------------------------------------------------------------------- //
override def canUpdate = false
override protected def initialize(): Unit = {
super.initialize()
EventHandler.scheduleServer(this)

View File

@ -19,5 +19,8 @@ class PowerConverter extends traits.PowerAcceptor with traits.Environment with t
override def energyThroughput = Settings.get.powerConverterRate
override def canUpdate = isServer
override def updateEntity(): Unit = {
super[PowerAcceptor].updateEntity()
super[Environment].updateEntity()
}
}

View File

@ -19,8 +19,6 @@ class PowerDistributor extends traits.Environment with traits.PowerBalancer with
override protected def isConnected = nodes.exists(node => node.address != null && node.network != null)
override def canUpdate = isServer
// ----------------------------------------------------------------------- //
@SideOnly(Side.CLIENT)

View File

@ -139,8 +139,6 @@ class Print(val canToggle: Option[() => Boolean], val scheduleUpdate: Option[Int
}
}
override def canUpdate = false
override protected def onRedstoneInputChanged(side: EnumFacing, oldMaxValue: Int, newMaxValue: Int): Unit = {
super.onRedstoneInputChanged(side, oldMaxValue, newMaxValue)
val newState = newMaxValue > 0

View File

@ -19,7 +19,7 @@ import net.minecraft.util.EnumFacing
import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly
class Printer extends traits.Environment with traits.Inventory with traits.Rotatable with SidedEnvironment with traits.StateAware with ISidedInventory {
class Printer extends traits.Environment with traits.Inventory with traits.Rotatable with SidedEnvironment with traits.StateAware with traits.Tickable with ISidedInventory {
val node = api.Network.newNode(this, Visibility.Network).
withComponent("printer3d").
withConnector(Settings.get.bufferConverter).
@ -209,11 +209,13 @@ class Printer extends traits.Environment with traits.Inventory with traits.Rotat
// ----------------------------------------------------------------------- //
override def canUpdate = isServer
override def updateEntity() {
override def updateEntity(): Unit = {
super.updateEntity()
if (isClient) {
return
}
def canMergeOutput = {
val presentStack = getStackInSlot(slotOutput)
val outputStack = data.createItemStack()

View File

@ -36,8 +36,6 @@ class Raid extends traits.Environment with traits.Inventory with traits.Rotatabl
override def onAnalyze(player: EntityPlayer, side: EnumFacing, hitX: Float, hitY: Float, hitZ: Float) = Array(filesystem.map(_.node).orNull)
override def canUpdate = false
// ----------------------------------------------------------------------- //
override def getSizeInventory = 3

View File

@ -9,7 +9,7 @@ import li.cil.oc.util.ExtendedNBT._
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.util.EnumFacing
class Redstone extends traits.Environment with traits.BundledRedstoneAware {
class Redstone extends traits.Environment with traits.BundledRedstoneAware with traits.Tickable {
val instance =
if (BundledRedstone.isAvailable)
new component.Redstone.Bundled(this)
@ -24,10 +24,12 @@ class Redstone extends traits.Environment with traits.BundledRedstoneAware {
}
else null
override def canUpdate = isServer
// ----------------------------------------------------------------------- //
override def updateEntity(): Unit = {
super[Environment].updateEntity()
}
override def readFromNBTForServer(nbt: NBTTagCompound) {
super.readFromNBTForServer(nbt)
instance.load(nbt.getCompoundTag(Settings.namespace + "redstone"))

View File

@ -50,8 +50,6 @@ class Relay extends traits.SwitchLike with traits.ComponentInventory with traits
withComponent("relay").
create())
override def canUpdate = isServer
// ----------------------------------------------------------------------- //
@SideOnly(Side.CLIENT)

View File

@ -178,8 +178,6 @@ class Screen(var tier: Int) extends traits.TextBuffer with SidedEnvironment with
// ----------------------------------------------------------------------- //
override def canUpdate = true
override def updateEntity() {
super.updateEntity()
if (shouldCheckForMultiBlock && ((isClient && isClientReadyForMultiBlockCheck) || (isServer && isConnected))) {

View File

@ -17,8 +17,6 @@ class Switch extends traits.SwitchLike with traits.NotAnalyzable with traits.Com
override def isLinkedEnabled = false
override def canUpdate = isServer
// ----------------------------------------------------------------------- //
protected def queueMessage(source: String, destination: String, port: Int, answerPort: Int, args: Array[AnyRef]) {

View File

@ -11,8 +11,6 @@ class Transposer extends traits.Environment {
// Used on client side to check whether to render activity indicators.
var lastOperation = 0L
override def canUpdate = false
override def readFromNBTForServer(nbt: NBTTagCompound) {
super.readFromNBTForServer(nbt)
transposer.load(nbt)

View File

@ -13,7 +13,7 @@ import net.minecraft.util.EnumParticleTypes
import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly
class Waypoint extends traits.Environment with traits.Rotatable with traits.RedstoneAware {
class Waypoint extends traits.Environment with traits.Rotatable with traits.RedstoneAware with traits.Tickable {
val node = api.Network.newNode(this, Visibility.Network).
withComponent("waypoint").
create()

View File

@ -24,7 +24,7 @@ import net.minecraftforge.fml.relauncher.SideOnly
import scala.collection.convert.WrapAsJava._
import scala.collection.mutable
trait Computer extends Environment with ComponentInventory with Rotatable with BundledRedstoneAware with api.network.Analyzable with api.machine.MachineHost with StateAware {
trait Computer extends Environment with ComponentInventory with Rotatable with BundledRedstoneAware with api.network.Analyzable with api.machine.MachineHost with StateAware with Tickable {
private lazy val _machine = if (isServer) api.Machine.create(this) else null
def machine = _machine

View File

@ -25,7 +25,7 @@ trait Environment extends TileEntity with network.Environment with network.Envir
override def zPosition = z + 0.5
override def markChanged() = if (canUpdate) isChangeScheduled = true else world.markChunkDirty(getPos, this)
override def markChanged() = if (this.isInstanceOf[Tickable]) isChangeScheduled = true else world.markChunkDirty(getPos, this)
protected def isConnected = node.address != null && node.network != null

View File

@ -14,7 +14,7 @@ import net.minecraftforge.fml.relauncher.SideOnly
import scala.collection.mutable
trait Hub extends traits.Environment with SidedEnvironment {
trait Hub extends traits.Environment with SidedEnvironment with Tickable {
override def node: Node = null
override protected def isConnected = plugs.exists(plug => plug.node.address != null && plug.node.network != null)

View File

@ -5,7 +5,7 @@ import li.cil.oc.api.network.Connector
import li.cil.oc.api.network.SidedEnvironment
import net.minecraft.util.EnumFacing
trait PowerBalancer extends PowerInformation with SidedEnvironment {
trait PowerBalancer extends PowerInformation with SidedEnvironment with Tickable {
var globalBuffer, globalBufferSize = 0.0
protected def isConnected: Boolean

View File

@ -69,7 +69,11 @@ trait RedstoneAware extends RotationAware /* with IConnectable with IRedstoneEmi
}
def checkRedstoneInputChanged() {
if (this.isInstanceOf[Tickable]) {
shouldUpdateInput = isServer
} else {
EnumFacing.values().foreach(updateRedstoneInput)
}
}
// ----------------------------------------------------------------------- //
@ -86,7 +90,7 @@ trait RedstoneAware extends RotationAware /* with IConnectable with IRedstoneEmi
override def validate(): Unit = {
super.validate()
if (!canUpdate) {
if (!this.isInstanceOf[Tickable]) {
EventHandler.scheduleServer(() => EnumFacing.values().foreach(updateRedstoneInput))
}
}

View File

@ -7,7 +7,7 @@ import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly
trait TextBuffer extends Environment {
trait TextBuffer extends Environment with Tickable {
lazy val buffer = {
val screenItem = api.Items.get(Constants.BlockName.ScreenTier1).createItemStack(1)
val buffer = api.Driver.driverFor(screenItem, getClass).createEnvironment(screenItem, this).asInstanceOf[api.internal.TextBuffer]

View File

@ -0,0 +1,9 @@
package li.cil.oc.common.tileentity.traits
import net.minecraft.util.ITickable
trait Tickable extends ITickable {
override def update(): Unit = updateEntity()
def updateEntity(): Unit = {}
}

View File

@ -16,8 +16,7 @@ import net.minecraft.world.World
import net.minecraftforge.fml.relauncher.Side
import net.minecraftforge.fml.relauncher.SideOnly
// TODO only implement ticking interface where needed.
trait TileEntity extends net.minecraft.tileentity.TileEntity with ITickable {
trait TileEntity extends net.minecraft.tileentity.TileEntity {
def world = getWorld
def x = getPos.getX
@ -34,12 +33,6 @@ trait TileEntity extends net.minecraft.tileentity.TileEntity with ITickable {
// ----------------------------------------------------------------------- //
def canUpdate = true
override def update(): Unit = {
if (canUpdate) updateEntity()
}
def updateEntity() {
if (Settings.get.periodicallyForceLightUpdate && world.getTotalWorldTime % 40 == 0 && getBlockType.getLightValue(world, getPos) > 0) {
world.markBlockForUpdate(getPos)

View File

@ -5,6 +5,7 @@ import li.cil.oc.OpenComputers
import li.cil.oc.Settings
import li.cil.oc.common.EventHandler
import li.cil.oc.common.asm.Injectable
import li.cil.oc.common.tileentity.traits
import li.cil.oc.integration.Mods
import li.cil.oc.integration.util.Power
import net.minecraft.nbt.NBTTagCompound
@ -14,7 +15,7 @@ import net.minecraftforge.fml.common.Optional
import net.minecraftforge.fml.common.eventhandler.Event
@Injectable.Interface(value = "ic2.api.energy.tile.IEnergySink", modid = Mods.IDs.IndustrialCraft2)
trait IndustrialCraft2Experimental extends Common with IndustrialCraft2Common {
trait IndustrialCraft2Experimental extends Common with IndustrialCraft2Common with traits.Tickable {
private var conversionBuffer = 0.0
private lazy val useIndustrialCraft2Power = isServer && Mods.IndustrialCraft2.isAvailable

View File

@ -440,6 +440,7 @@ class Machine(val host: MachineHost) extends prefab.ManagedEnvironment with mach
// Component overflow check, crash if too many components are connected, to
// avoid confusion on the user's side due to components not showing up.
if (componentCount > maxComponents) {
beep("-..")
crash("gui.Error.ComponentOverflow")
}