From c4135033828879b1ad77eb3e6a793dd64e925592 Mon Sep 17 00:00:00 2001 From: sleet01 Date: Sun, 14 Apr 2024 16:36:30 -0700 Subject: [PATCH 1/2] Safety the SPA lookup check as some entries in defaultspa.xml do not exist in MM OptionsConstants.java --- .../campaign/mission/CrewSkillUpgrader.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/MekHQ/src/mekhq/campaign/mission/CrewSkillUpgrader.java b/MekHQ/src/mekhq/campaign/mission/CrewSkillUpgrader.java index 97e9c2e226..0f1466a41d 100644 --- a/MekHQ/src/mekhq/campaign/mission/CrewSkillUpgrader.java +++ b/MekHQ/src/mekhq/campaign/mission/CrewSkillUpgrader.java @@ -23,6 +23,7 @@ import megamek.common.options.OptionsConstants; import mekhq.campaign.personnel.SpecialAbility; import mekhq.campaign.personnel.enums.PersonnelRole; +import org.apache.logging.log4j.LogManager; import java.util.*; @@ -47,7 +48,7 @@ public class CrewSkillUpgrader { */ public CrewSkillUpgrader(int upgradeIntensity) { this.upgradeIntensity = upgradeIntensity; - + specialAbilitiesByUnitType = new HashMap<>(); for (SpecialAbility spa : SpecialAbility.getWeightedSpecialAbilities()) { @@ -90,7 +91,7 @@ public void upgradeCrew(Entity entity) { double skillAvg = (entity.getCrew().getGunnery() + entity.getCrew().getPiloting()) / 2.0; double xpCap = 0; int spaCap = 0; - + // elite if (skillAvg < 3) { xpCap = maxAbilityXPCost; @@ -165,14 +166,22 @@ private int addSingleSPA(Entity entity, double xpCap) { spaValue = pickRandomWeapon(entity, true); break; default: + // If we can't access the option, try a different one if ((entity.getCrew() == null) || (entity.getCrew().getOptions() == null) || - (entity.getCrew().getOptions(spa.getName()) == null)) { - return 0; + (entity.getCrew().getOptions(spa.getName()) == null) || + (!entity.getCrew().getOptions(spa.getName()).hasMoreElements())) { + continue; } - entity.getCrew().getOptions().getOption(spa.getName()).setValue(true); - return spa.getCost(); + // If the option has a name but isn't defined, try another one + try { + entity.getCrew().getOptions().getOption(spa.getName()).setValue(true); + return spa.getCost(); + } catch (NullPointerException e) { + LogManager.getLogger().warn("Attempted to assign SPA '" + spa.getName() + "' but SPA not found."); + continue; + } } // Assign the SPA, unless it was unable to pick a random weapon/specialization for whatever reason From 27a193a99d85fb6db2f25a0a9a7d294069364278 Mon Sep 17 00:00:00 2001 From: sleet01 Date: Sun, 14 Apr 2024 17:00:57 -0700 Subject: [PATCH 2/2] Separate handling for no-crew state from no-SPA state --- MekHQ/src/mekhq/campaign/mission/CrewSkillUpgrader.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/MekHQ/src/mekhq/campaign/mission/CrewSkillUpgrader.java b/MekHQ/src/mekhq/campaign/mission/CrewSkillUpgrader.java index 0f1466a41d..4162f0555d 100644 --- a/MekHQ/src/mekhq/campaign/mission/CrewSkillUpgrader.java +++ b/MekHQ/src/mekhq/campaign/mission/CrewSkillUpgrader.java @@ -166,9 +166,11 @@ private int addSingleSPA(Entity entity, double xpCap) { spaValue = pickRandomWeapon(entity, true); break; default: - // If we can't access the option, try a different one - if ((entity.getCrew() == null) || - (entity.getCrew().getOptions() == null) || + // If there's no crew, stop looking for SPAs + if (entity.getCrew() == null) { + return 0; + // If we can't access the option, try a different one + } else if ((entity.getCrew().getOptions() == null) || (entity.getCrew().getOptions(spa.getName()) == null) || (!entity.getCrew().getOptions(spa.getName()).hasMoreElements())) { continue;