Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Handling for Unsupported Unit Types #5720

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MekHQ/resources/mekhq/resources/CampaignGUI.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ miMHQOptions.text=MekHQ Options...
miMHQOptions.toolTipText=This launches the MekHQ Options Dialog.
menuThemes.text=Themes
menuExit.text=Exit
mekSelectorDialog.unsupported.gunEmplacement=Gun Emplacements are %s<b>not supported</b>%s by MekHQ.

# Marketplace Menu
menuMarket.text=Marketplace
Expand Down
6 changes: 6 additions & 0 deletions MekHQ/resources/mekhq/resources/GUI.properties
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,12 @@ DataLoadingDialog.OutOfMemoryError.text=MekHQ ran out of memory attempting to lo
DataLoadingDialog.ExecutionException.title=Campaign Loading Error
DataLoadingDialog.ExecutionException.text=The campaign file couldn't be loaded. \nPlease check the MekHQ.log file for details.

## Unsupported Units
unsupportedUnits.title=Unsupported Units Detected
unsupportedUnits.body=The following units are not supported by MekHQ and have been sold.\
<br>\
<br>All assigned personnel have been unassigned.<br><br>

### GMToolsDialog Class
GMToolsDialog.title=GM Tools
## General Tab
Expand Down
5 changes: 5 additions & 0 deletions MekHQ/src/mekhq/campaign/ResolveScenarioTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ public void processGame() {
}
}
} else if (wreck.getOwner().isEnemyOf(client.getLocalPlayer())) {
// MekHQ doesn't support gun emplacements, so we don't want the player salvaging them
if (wreck instanceof GunEmplacement) {
continue;
}

if (wreck.isDropShip() && scenario.getBoardType() != Scenario.T_SPACE) {
double dropShipBonusPercentage =
(double) campaign.getCampaignOptions().getDropShipBonusPercentage() / 100;
Expand Down
53 changes: 53 additions & 0 deletions MekHQ/src/mekhq/gui/dialog/DataLoadingDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import megamek.client.generator.RandomCallsignGenerator;
import megamek.client.generator.RandomNameGenerator;
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.Entity;
import megamek.common.GunEmplacement;
import megamek.common.MekSummaryCache;
import megamek.common.annotations.Nullable;
import megamek.common.options.OptionsConstants;
Expand All @@ -44,6 +46,7 @@
import mekhq.campaign.rating.CamOpsReputation.ReputationController;
import mekhq.campaign.storyarc.StoryArc;
import mekhq.campaign.storyarc.StoryArcStub;
import mekhq.campaign.unit.Unit;
import mekhq.campaign.universe.Factions;
import mekhq.campaign.universe.RATManager;
import mekhq.campaign.universe.Systems;
Expand All @@ -58,6 +61,8 @@
import java.io.File;
import java.io.FileInputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;

Expand Down Expand Up @@ -422,12 +427,60 @@ public Campaign doInBackground() throws Exception {
reputationController.initializeReputation(campaign);
campaign.setReputation(reputationController);
}

sellUnsupportedUnits(campaign);
// endregion Progress 7
}
campaign.setApp(getApplication());
return campaign;
}



/**
* Sells unsupported units.
* <p>
* This method checks all units in the specified campaign and determines if they are unsupported.
* Currently, only gun emplacements are identified as unsupported and are sold upon campaign load.
* If unsupported units are sold, a notification is displayed to the user detailing the sold units,
* including their names and IDs. Additionally, personnel assigned to these units are
* automatically unassigned.
* </p>
*
* @param retVal The campaign being checked for unsupported units. This includes the unit list
* and the quartermaster who handles the selling process.
*/
private static void sellUnsupportedUnits(Campaign retVal) {
List<Unit> soldUnits = new ArrayList<>();
for (Unit unit : retVal.getUnits()) {
Entity entity = unit.getEntity();

if (entity == null) {
continue;
}

// We don't support Gun Emplacements in mhq.
// If the user has somehow acquired one, it is sold on load.
if (entity instanceof GunEmplacement) {
soldUnits.add(unit);
}
}

if (!soldUnits.isEmpty()) {
StringBuilder message = new StringBuilder(resources.getString("unsupportedUnits.body"));

for (Unit soldUnit : soldUnits) {
retVal.getQuartermaster().sellUnit(soldUnit);
message.append("- ").append(soldUnit.getName()).append("<br>");
}

JOptionPane.showMessageDialog(null,
String.format("<html>%s</html>", message),
resources.getString("unsupportedUnits.title"),
JOptionPane.WARNING_MESSAGE);
}
Comment on lines +469 to +481
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can't be in a static function because it has reference to the resources, which is in the base instance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was fixed a while ago - if you're referring to what I think you are. If you update your master it should be good.

}

/**
* Executed in event dispatching thread
*/
Expand Down
19 changes: 19 additions & 0 deletions MekHQ/src/mekhq/gui/dialog/MekHQUnitSelectorDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import megamek.client.ui.swing.UnitLoadingDialog;
import megamek.client.ui.swing.dialog.AbstractUnitSelectorDialog;
import megamek.common.*;
import mekhq.MekHQ;
import mekhq.campaign.Campaign;
import mekhq.campaign.parts.enums.PartQuality;
import mekhq.campaign.unit.UnitOrder;
Expand All @@ -32,8 +33,12 @@
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.regex.PatternSyntaxException;

import static mekhq.utilities.ReportingUtilities.CLOSING_SPAN_TAG;
import static mekhq.utilities.ReportingUtilities.spanOpeningWithCustomColor;

public class MekHQUnitSelectorDialog extends AbstractUnitSelectorDialog {
//region Variable Declarations
private Campaign campaign;
Expand Down Expand Up @@ -125,6 +130,20 @@ protected JPanel createButtonsPanel() {
@Override
protected void select(boolean isGM) {
if (getSelectedEntity() != null) {
// Block the purchase if the unit type is unsupported
if (selectedUnit.getEntity() instanceof GunEmplacement) {
final ResourceBundle resources = ResourceBundle.getBundle("mekhq.resources.CampaignGUI",
MekHQ.getMHQOptions().getLocale());

campaign.addReport(String.format(
resources.getString("mekSelectorDialog.unsupported.gunEmplacement"),
spanOpeningWithCustomColor(MekHQ.getMHQOptions().getFontColorNegativeHexColor()),
CLOSING_SPAN_TAG));

dispose();
return;
}

if (isGM) {
PartQuality quality = PartQuality.QUALITY_D;

Expand Down
Loading