Skip to content

Commit

Permalink
Merge pull request #6119 from SJuliez/small-sv-ci-wps-ammo
Browse files Browse the repository at this point in the history
#6101: Fix selected ammo retrieval
  • Loading branch information
HammerGS authored Oct 21, 2024
2 parents 47dceae + f3afac0 commit d23fe81
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion megamek/src/megamek/client/ui/swing/ClientGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3023,7 +3023,7 @@ public Optional<WeaponMounted> getDisplayedWeapon() {
}

public Optional<AmmoMounted> getDisplayedAmmo() {
return Optional.ofNullable(unitDisplay.wPan.getSelectedAmmo());
return unitDisplay.wPan.getSelectedAmmo();
}

@Override
Expand Down
24 changes: 10 additions & 14 deletions megamek/src/megamek/client/ui/swing/unitDisplay/WeaponPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.*;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
Expand Down Expand Up @@ -1348,16 +1344,16 @@ public WeaponMounted getSelectedWeapon() {
}

/**
*
* @return the AmmoMounted currently selected by the ammo selector combo box,
* not the linked ammo per se.
* @return the AmmoMounted currently selected by the ammo selector combo box, if any.
* The returned AmmoMounted may or may not be the ammo that is linked to the weapon.
*/
public AmmoMounted getSelectedAmmo() {
public Optional<AmmoMounted> getSelectedAmmo() {
int selected = m_chAmmo.getSelectedIndex();
if (selected == -1 || vAmmo == null) {
return null;
if ((selected == -1) || (vAmmo == null) || (selected >= vAmmo.size())) {
return Optional.empty();
} else {
return Optional.of(vAmmo.get(selected));
}
return vAmmo.get(m_chAmmo.getSelectedIndex());
}

/**
Expand Down Expand Up @@ -1948,8 +1944,7 @@ private void displaySelected() {

// Update the range display to account for the selected ammo, or the loaded ammo
// if none is selected
int curDisplayed = m_chAmmo.getSelectedIndex();
AmmoMounted mAmmo = (curDisplayed != -1 && vAmmo != null) ? vAmmo.get(curDisplayed) : mounted.getLinkedAmmo();
AmmoMounted mAmmo = getSelectedAmmo().orElse(mounted.getLinkedAmmo());
if (mAmmo != null) {
updateRangeDisplayForAmmo(mAmmo);
}
Expand Down Expand Up @@ -2073,6 +2068,7 @@ private void displaySelected() {

vAmmo.add(mountedAmmo);
m_chAmmo.addItem(formatAmmo(mountedAmmo));
int curDisplayed = m_chAmmo.getSelectedIndex();
if (curDisplayed != -1) {
nCur = curDisplayed;
} else if ((mounted.getLinked() != null) &&
Expand Down

0 comments on commit d23fe81

Please sign in to comment.