Skip to content

Commit

Permalink
Issue 3650: Fixed NPE Issues with TransportedUnitSummary
Browse files Browse the repository at this point in the history
  • Loading branch information
psikomonkie committed Jan 9, 2025
1 parent a250453 commit 89cd99f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AssignForceToTransportMenu.transportSpaceRemaining.text={0} | Space Remaining: {1}
AssignForceToTransportMenu.TACTICAL_TRANSPORT.text=Assign Force to Ship Transport
AssignForceToTransportMenu.SHIP_TRANSPORT.text=Assign Force to Tactical Transport
AssignForceToTransportMenu.TACTICAL_TRANSPORT.text=Assign Force to Tactical Transport
AssignForceToTransportMenu.SHIP_TRANSPORT.text=Assign Force to Ship Transport
AssignForceToTransportMenu.ASFBay.text=ASF Bay
AssignForceToTransportMenu.HeavyVehicleBay.text=Heavy Vehicle Bay
AssignForceToTransportMenu.NavalRepairFacility.text=Naval Repair Facility
Expand Down
5 changes: 3 additions & 2 deletions MekHQ/src/mekhq/campaign/Campaign.java
Original file line number Diff line number Diff line change
Expand Up @@ -1520,8 +1520,9 @@ public Unit addNewUnit(Entity en, boolean allowNewPilots, int days, PartQuality
en.setGame(game);
en.setExternalIdAsString(unit.getId().toString());

unit.getShipTransportedUnitsSummary().initializeTransportCapacity(unit.getEntity().getTransports());
unit.getTacticalTransportedUnitsSummary().initializeTransportCapacity(unit.getEntity().getTransports());
for (CampaignTransportType campaignTransportType : CampaignTransportType.values()) {
unit.initializeTransportSpace(campaignTransportType);
}
// Added to avoid the 'default force bug' when calculating cargo
removeUnitFromForce(unit);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,18 @@ public void clearTransportedUnits() {
protected Set<Entity> clearTransportedEntities() {
Set<Entity> transportedEntities = new HashSet<>();
if (transport.getEntity() != null) {
for (Entity transportedEntity : transport.getEntity().getUnloadableUnits()) {
if (transport.getEntity().hasUnloadedUnitsFromBays()) {
for (Entity transportedEntity : transport.getEntity().getUnitsUnloadableFromBays()) {
transport.getEntity().unload(transportedEntity);
transportedEntities.add(transportedEntity);
}
} // We can't just use Entity::getUnloadableUnits(); getUnloadableFromBays() throws NPE in that flow
for (Entity transportedEntity : transport.getEntity().getUnitsUnloadableFromNonBays()) {
transport.getEntity().unload(transportedEntity);
transportedEntities.add(transportedEntity);
}

transport.getEntity().resetTransporter();
transport.getEntity().resetTransporter();
}
return transportedEntities;
}
Expand Down
15 changes: 10 additions & 5 deletions MekHQ/src/mekhq/campaign/unit/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,12 @@ public TacticalTransportedUnitsSummary getTacticalTransportedUnitsSummary() {
return (TacticalTransportedUnitsSummary) getTransportedUnitsSummary(TACTICAL_TRANSPORT);
}

private void initializeTransportSpace(CampaignTransportType campaignTransportType) {
/**
* Initializes the transport capacity. If the campaign transport capacity type doesn't exist yet,
* try to create it.
* @param campaignTransportType transport type we want to prepare
*/
public void initializeTransportSpace(CampaignTransportType campaignTransportType) {
// Initialize the capacity
if (hasTransportedUnitsType(campaignTransportType)) {
getTransportedUnitsSummary(campaignTransportType).initializeTransportCapacity(getEntity().getTransports());
Expand All @@ -328,13 +333,13 @@ private void initializeTransportSpace(CampaignTransportType campaignTransportTyp
Constructor<? extends AbstractTransportedUnitsSummary> constructor = campaignTransportType.getTransportedUnitsSummaryType().getConstructor(new Class[]{Unit.class});
addTransportedUnitType(constructor.newInstance(this));
} catch (NoSuchMethodException e) {
logger.error(String.format("Could not find constructor to initialize transport space for %s Error: %s", campaignTransportType.getName(), e.toString()));
logger.error(String.format("Could not find constructor to initialize transport space for %s Error: %s Cause: %s", campaignTransportType.getName(), e.toString(), e.getCause()));
} catch (InvocationTargetException e) {
logger.error(String.format("Could not find constructor to initialize transport space for %s Error: %s", campaignTransportType.getName(), e.toString()));
logger.error(String.format("Could not find constructor to initialize transport space for %s Error: %s Cause: %s", campaignTransportType.getName(), e.toString(), e.getCause()));
} catch (InstantiationException e) {
logger.error(String.format("Could not find constructor to initialize transport space for %s Error: %s", campaignTransportType.getName(), e.toString()));
logger.error(String.format("Could not find constructor to initialize transport space for %s Error: %s Cause: %s", campaignTransportType.getName(), e.toString(), e.getCause()));
} catch (IllegalAccessException e) {
logger.error(String.format("Could not find constructor to initialize transport space for %s Error: %s", campaignTransportType.getName(), e.toString()));
logger.error(String.format("Could not find constructor to initialize transport space for %s Error: %s Cause: %s", campaignTransportType.getName(), e.toString(), e.getCause()));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions MekHQ/src/mekhq/gui/menus/AssignForceToTransportMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private void initialize(final Unit... units) {
return;
}

//Assign Unit to {[}campaignTransportTypeName}
//Assign Unit to {campaignTransportTypeName}
setText(Internationalization.getTextAt("AssignForceToTransport", "AssignForceToTransportMenu." + campaignTransportType.getName() + ".text"));
for (JScrollableMenu transporterTypeMenu : transporterTypeMenus) {
add(transporterTypeMenu);
Expand Down Expand Up @@ -98,7 +98,7 @@ private Set<JMenuItem> createTransportMenus(Class<? extends Transporter> transpo
"AssignForceToTransportMenu.transportSpaceRemaining.text",
transport.getName(), transport.getCurrentTransportCapacity(campaignTransportType, transporterType)));

transportMenu.addActionListener(evt -> { transportMenuAction(evt, transporterType, transport, units); });
transportMenu.addActionListener(evt -> transportMenuAction(evt, transporterType, transport, units));
transportMenus.add(transportMenu);
}

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'evt' is never used.
return transportMenus;
Expand Down

0 comments on commit 89cd99f

Please sign in to comment.