From 34ecf28aa00fba046bcca80fa391a0193faa62be Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Sun, 18 Aug 2024 14:59:34 -0500 Subject: [PATCH] refactor: Use stream composition to check for usable weapon bays --- .../common/actions/WeaponAttackAction.java | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/megamek/src/megamek/common/actions/WeaponAttackAction.java b/megamek/src/megamek/common/actions/WeaponAttackAction.java index 260e2288713..c4082fcba61 100644 --- a/megamek/src/megamek/common/actions/WeaponAttackAction.java +++ b/megamek/src/megamek/common/actions/WeaponAttackAction.java @@ -1403,27 +1403,17 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta // limit large craft to zero net heat and to heat by arc final int heatCapacity = ae.getHeatCapacity(); - if (ae.usesWeaponBays() && (weapon != null) && !weapon.getBayWeapons().isEmpty()) { + if (ae.usesWeaponBays() && (weapon != null)) { int totalHeat = 0; // first check to see if there are any usable weapons - boolean usable = false; - for (WeaponMounted m : weapon.getBayWeapons()) { - WeaponType bayWType = m.getType(); - boolean bayWUsesAmmo = (bayWType.getAmmoType() != AmmoType.T_NA); - if (m.canFire()) { - if (bayWUsesAmmo) { - if ((m.getLinked() != null) && (m.getLinked().getUsableShotsLeft() > 0)) { - usable = true; - break; - } - } else { - usable = true; - break; - } - } - } - if (!usable) { + if (!weapon.streamBayWeapons() + .filter(WeaponMounted::canFire) + .anyMatch(m -> + m.getType().getAmmoType() == AmmoType.T_NA + || Optional.ofNullable(m.getLinked()).map(a -> a.getUsableShotsLeft() > 0).orElse(false) + ) + ) { return Messages.getString("WeaponAttackAction.BayNotReady"); }