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

#6101: Fix selected ammo retrieval #6119

Merged
merged 1 commit into from
Oct 21, 2024
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
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