Fix crash after upgrading a unit (uninitialized lateinit) (#4928)

This commit is contained in:
SomeTroglodyte 2021-08-21 19:56:08 +02:00 committed by GitHub
parent 3b980a24bd
commit 0c2cdcfcff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View File

@ -439,7 +439,7 @@ class MapUnit {
//region state-changing functions
fun setTransients(ruleset: Ruleset) {
promotions.unit = this
promotions.setTransients(this)
baseUnit = ruleset.units[name]
?: throw java.lang.Exception("Unit $name is not found!")
updateUniques()

View File

@ -3,16 +3,27 @@ package com.unciv.logic.map
import com.unciv.models.ruleset.UniqueTriggerActivation
import com.unciv.models.ruleset.unit.Promotion
class UnitPromotions{
@Transient lateinit var unit:MapUnit
class UnitPromotions {
// Having this as mandatory constructor parameter would be safer, but this class is part of a
// saved game and as usual the json deserializer needs a default constructor.
// Initialization occurs in setTransients() - called as part of MapUnit.setTransients,
// or copied in clone() as part of the UnitAction `Upgrade`.
@Transient
private lateinit var unit: MapUnit
@Suppress("PropertyName")
var XP = 0
var promotions = HashSet<String>()
// The number of times this unit has been promoted
// some promotions don't come from being promoted but from other things,
// like from being constructed in a specific city etc.
var numberOfPromotions = 0
fun setTransients(unit: MapUnit) {
this.unit = unit
}
fun xpForNextPromotion() = (numberOfPromotions+1)*10
fun canBePromoted(): Boolean {
if (XP < xpForNextPromotion()) return false
@ -58,6 +69,7 @@ class UnitPromotions{
toReturn.XP = XP
toReturn.promotions.addAll(promotions)
toReturn.numberOfPromotions = numberOfPromotions
toReturn.unit = unit
return toReturn
}
@ -66,5 +78,4 @@ class UnitPromotions{
for(i in 1..numberOfPromotions) sum += 10*i
return sum
}
}
}