From b21fe79a71b0b45b4120435a561cc819b17ac649 Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Sat, 24 Aug 2024 15:51:23 -0500 Subject: [PATCH] refactor: Use stream composition for air-to-ground target modifiers --- .../common/actions/WeaponAttackAction.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/megamek/src/megamek/common/actions/WeaponAttackAction.java b/megamek/src/megamek/common/actions/WeaponAttackAction.java index dcf5fe0c577..5b94efd80ca 100644 --- a/megamek/src/megamek/common/actions/WeaponAttackAction.java +++ b/megamek/src/megamek/common/actions/WeaponAttackAction.java @@ -3676,18 +3676,16 @@ private static ToHitData compileAeroAttackerToHitMods(Game game, Entity ae, Targ } // units making air to ground attacks are easier to hit by air-to-air // attacks - if (Compute.isAirToAir(ae, target)) { - for (Enumeration i = game.getActions(); i.hasMoreElements();) { - EntityAction ea = i.nextElement(); - if (!(ea instanceof WeaponAttackAction)) { - continue; - } - WeaponAttackAction prevAttack = (WeaponAttackAction) ea; - if ((te != null && prevAttack.getEntityId() == te.getId()) && prevAttack.isAirToGround(game)) { - toHit.addModifier(-3, Messages.getString("WeaponAttackAction.TeGroundAttack")); - break; - } - } + + // The ID of the entity being targeted. + final Optional targetEntityId = Optional.ofNullable(te).map(Entity::getId); + + if (Compute.isAirToAir(ae, target) && targetEntityId.isPresent() && game.getActionsVector().stream() + .filter(WeaponAttackAction.class::isInstance) + .map(WeaponAttackAction.class::cast) + .anyMatch(prevAttack -> prevAttack.getEntityId() == targetEntityId.orElseThrow() && prevAttack.isAirToGround(game)) + ) { + toHit.addModifier(-3, Messages.getString("WeaponAttackAction.TeGroundAttack")); } // grounded aero if (!ae.isAirborne() && !ae.isSpaceborne()) {