Skip to content

Commit

Permalink
find parts by ID rather than name
Browse files Browse the repository at this point in the history
  • Loading branch information
Algebro7 committed Aug 14, 2024
1 parent cdc6ddb commit a4cc0f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
9 changes: 4 additions & 5 deletions MekHQ/src/mekhq/campaign/parts/PartInUse.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class PartInUse {
private String description;
private String name;
private int id;
private IAcquisitionWork partToBuy;
private int useCount;
private int storeCount;
Expand All @@ -35,14 +35,13 @@ public PartInUse(Part part) {
appendDetails(sb, part);
}
part.setUnit(u);
this.name = part.getName();
this.id = part.getId();
this.description = sb.toString();
this.partToBuy = part.getAcquisitionWork();
this.tonnagePerItem = part.getTonnage();
// AmmoBin are special: They aren't buyable (yet?), but instead buy you the ammo inside
// We redo the description based on that
if (partToBuy instanceof AmmoStorage) {
this.name = ((AmmoStorage) partToBuy).getName();
sb.setLength(0);
sb.append(((AmmoStorage) partToBuy).getName());
appendDetails(sb, (Part) ((AmmoStorage) partToBuy).getAcquisitionWork());
Expand Down Expand Up @@ -77,8 +76,8 @@ public String getDescription() {
return description;
}

public String getName() {
return name;
public int getId() {
return id;
}

public IAcquisitionWork getPartToBuy() {
Expand Down
43 changes: 25 additions & 18 deletions MekHQ/src/mekhq/gui/dialog/PartsReportDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,16 @@ public void actionPerformed(ActionEvent e) {
public void actionPerformed(ActionEvent e) {
int row = Integer.parseInt(e.getActionCommand());

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
PartInUse partInUse = overviewPartsModel.getPartInUse(row);
campaign.getWarehouse().getSpareParts().stream().filter(p ->
Objects.equals(p.getName(), partInUse.getName()))
.findFirst()
.ifPresent(p -> campaign.getQuartermaster().sellPart(p, 1));
Part part = campaign.getWarehouse().getPart(partInUse.getId());
if (part == null) {
return;
}
Part spare = campaign.getWarehouse().checkForExistingSparePart(part);
if (spare != null) {
campaign.getQuartermaster().sellPart(spare, 1);
}
refreshOverviewPartsInUse();

}
};

Expand All @@ -149,20 +154,22 @@ public void actionPerformed(ActionEvent e) {
public void actionPerformed(ActionEvent e) {
int row = Integer.parseInt(e.getActionCommand());

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
PartInUse partInUse = overviewPartsModel.getPartInUse(row);
campaign.getWarehouse().getSpareParts().stream().filter(p ->
Objects.equals(p.getName(), partInUse.getName()))
.findFirst()
.ifPresent(p -> {
int quantity = 1;
PopupValueChoiceDialog popupValueChoiceDialog = new PopupValueChoiceDialog(gui.getFrame(), true,
"Sell how many " + p.getName(), quantity, 1, CampaignGUI.MAX_QUANTITY_SPINNER);
popupValueChoiceDialog.setVisible(true);
quantity = popupValueChoiceDialog.getValue();
if (quantity <= 0) {
return;
}
campaign.getQuartermaster().sellPart(p, quantity);
});
Part part = campaign.getWarehouse().getPart(partInUse.getId());
if (part == null) {
return;
}
Part spare = campaign.getWarehouse().checkForExistingSparePart(part);
if (spare != null) {
int quantity = 1;
PopupValueChoiceDialog popupValueChoiceDialog = new PopupValueChoiceDialog(gui.getFrame(), true,
"Sell how many " + spare.getName(), quantity, 1, CampaignGUI.MAX_QUANTITY_SPINNER);
popupValueChoiceDialog.setVisible(true);
quantity = popupValueChoiceDialog.getValue();
if (quantity <= 0) {
return;
}
campaign.getQuartermaster().sellPart(spare, quantity);
}
refreshOverviewPartsInUse();
}
};
Expand Down

0 comments on commit a4cc0f9

Please sign in to comment.