From f4c25f6803d63a75374f913f37a74b8213ea9cd0 Mon Sep 17 00:00:00 2001 From: IllianiCBT Date: Thu, 9 Jan 2025 14:13:11 -0600 Subject: [PATCH] Fix force commander handling and improve XML persistence. Set forceCommanderID to null when no eligible commanders exist, ensuring valid leadership. Added overrideForceCommanderID to XML read/write methods for better persistence. Updated force commander assignment during campaign load to prevent invalid leadership issues. --- MekHQ/src/mekhq/campaign/force/Force.java | 4 ++++ MekHQ/src/mekhq/campaign/io/CampaignXmlParser.java | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/MekHQ/src/mekhq/campaign/force/Force.java b/MekHQ/src/mekhq/campaign/force/Force.java index 8ffc5555ca..c467b56265 100644 --- a/MekHQ/src/mekhq/campaign/force/Force.java +++ b/MekHQ/src/mekhq/campaign/force/Force.java @@ -589,6 +589,7 @@ public void updateCommander(Campaign campaign) { List eligibleCommanders = getEligibleCommanders(campaign); if (eligibleCommanders.isEmpty()) { + forceCommanderID = null; overrideForceCommanderID = null; return; } @@ -711,6 +712,7 @@ public void writeToXML(PrintWriter pw1, int indent) { MHQXMLUtility.writeSimpleXMLTag(pw1, indent, "populateOriginNode", overrideFormationLevel.toString()); MHQXMLUtility.writeSimpleXMLTag(pw1, indent, "scenarioId", scenarioId); MHQXMLUtility.writeSimpleXMLTag(pw1, indent, "techId", techId); + MHQXMLUtility.writeSimpleXMLTag(pw1, indent, "overrideForceCommanderID", overrideForceCommanderID); MHQXMLUtility.writeSimpleXMLTag(pw1, indent, "forceCommanderID", forceCommanderID); if (!units.isEmpty()) { MHQXMLUtility.writeSimpleXMLOpenTag(pw1, indent++, "units"); @@ -766,6 +768,8 @@ public void writeToXML(PrintWriter pw1, int indent) { force.scenarioId = Integer.parseInt(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("techId")) { force.techId = UUID.fromString(wn2.getTextContent()); + } else if (wn2.getNodeName().equalsIgnoreCase("overrideForceCommanderID")) { + force.overrideForceCommanderID = UUID.fromString(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("forceCommanderID")) { force.forceCommanderID = UUID.fromString(wn2.getTextContent()); } else if (wn2.getNodeName().equalsIgnoreCase("units")) { diff --git a/MekHQ/src/mekhq/campaign/io/CampaignXmlParser.java b/MekHQ/src/mekhq/campaign/io/CampaignXmlParser.java index fe783cdf51..8875fbec00 100644 --- a/MekHQ/src/mekhq/campaign/io/CampaignXmlParser.java +++ b/MekHQ/src/mekhq/campaign/io/CampaignXmlParser.java @@ -860,6 +860,11 @@ private static void processForces(Campaign retVal, Node wn, Version version) { } } + // This removes the risk of having forces with invalid leadership getting locked in + for (Force force : retVal.getAllForces()) { + force.updateCommander(retVal); + } + recalculateCombatTeams(retVal); logger.info("Load of Force Organization complete!"); }