make component inventory components array even more lazy

allow a component inventory to define its inventory size from nbt even after the components array has been accessed. The array will appear to be size zero until initialized

closes #2522
This commit is contained in:
payonel 2018-01-02 20:59:09 -08:00
parent 8a4b1fa4f5
commit f8bfbf55b1
2 changed files with 30 additions and 7 deletions

View File

@ -17,7 +17,16 @@ import scala.collection.convert.WrapAsScala._
import scala.collection.mutable
trait ComponentInventory extends Inventory with network.Environment {
lazy val components = Array.fill[Option[ManagedEnvironment]](getSizeInventory)(None)
private var _components: Array[Option[ManagedEnvironment]] = _
protected var isSizeInventoryReady: Boolean = true
def components: Array[Option[ManagedEnvironment]] = {
if (_components == null && isSizeInventoryReady) {
_components = Array.fill[Option[ManagedEnvironment]](getSizeInventory)(None)
}
if (_components == null) Array[Option[ManagedEnvironment]]() else _components
}
protected val updatingComponents = mutable.ArrayBuffer.empty[ManagedEnvironment]
// ----------------------------------------------------------------------- //
@ -99,6 +108,13 @@ trait ComponentInventory extends Inventory with network.Environment {
for (slot <- 0 until getSizeInventory) {
val stack = getStackInSlot(slot)
if (stack != null) {
if (slot >= components.length) {
// isSizeInventoryReady was added to resolve issues where an inventory was used before its
// nbt data had been parsed. See https://github.com/MightyPirates/OpenComputers/issues/2522
// If this error is hit again, perhaps another subtype needs to handle nbt loading like Case does
OpenComputers.log.error(s"ComponentInventory components length ${components.length} does not accommodate inventory size ${getSizeInventory}")
return
} else {
components(slot) match {
case Some(component) =>
// We're guaranteed to have a driver for entries.
@ -108,6 +124,7 @@ trait ComponentInventory extends Inventory with network.Environment {
}
}
}
}
// ----------------------------------------------------------------------- //

View File

@ -25,7 +25,12 @@ import net.minecraftforge.common.util.ForgeDirection
import scala.collection.convert.WrapAsJava._
class Case(var tier: Int) extends traits.PowerAcceptor with traits.Computer with traits.Colored with internal.Case with DeviceInfo {
def this() = this(0)
def this() = {
this(0)
// If no tier was defined when constructing this case, then we don't yet know the inventory size
// this is set back to true when the nbt data is loaded
isSizeInventoryReady = false
}
// Used on client side to check whether to render disk activity/network indicators.
var lastFileSystemAccess = 0L
@ -78,6 +83,7 @@ class Case(var tier: Int) extends traits.PowerAcceptor with traits.Computer with
tier = nbt.getByte(Settings.namespace + "tier") max 0 min 3
color = Color.byTier(tier)
super.readFromNBTForServer(nbt)
isSizeInventoryReady = true
}
override def writeToNBTForServer(nbt: NBTTagCompound) {