From f45da84c16e54ead36feb3b40fe573c0afefd86c Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Wed, 21 Aug 2024 14:24:59 -0500 Subject: [PATCH] refactor: Use stream composition for BA Narc targeting --- .../common/actions/WeaponAttackAction.java | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/megamek/src/megamek/common/actions/WeaponAttackAction.java b/megamek/src/megamek/common/actions/WeaponAttackAction.java index f9c1c1c1b60..655f0b2b603 100644 --- a/megamek/src/megamek/common/actions/WeaponAttackAction.java +++ b/megamek/src/megamek/common/actions/WeaponAttackAction.java @@ -1881,20 +1881,16 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta // BA compact narc: we have one weapon for each trooper, but you // can fire only at one target at a time if (wtype.getName().equals("Compact Narc")) { - for (Enumeration i = game.getActions(); i.hasMoreElements();) { - EntityAction ea = i.nextElement(); - if (!(ea instanceof WeaponAttackAction)) { - continue; - } - final WeaponAttackAction prevAttack = (WeaponAttackAction) ea; - if (prevAttack.getEntityId() == attackerId) { - Mounted prevWeapon = ae.getEquipment(prevAttack.getWeaponId()); - if (prevWeapon.getType().getName().equals("Compact Narc")) { - if (prevAttack.getTargetId() != target.getId()) { - return Messages.getString("WeaponAttackAction.OneTargetForCNarc"); - } - } - } + if (game.getActionsVector().stream() + .filter(WeaponAttackAction.class::isInstance) + .map(WeaponAttackAction.class::cast) + .anyMatch(prevAttack -> prevAttack.getEntityId() == attackerId + && ae.getEquipment(prevAttack.getWeaponId()) + .getType().getName().equals("Compact Narc") + && prevAttack.getTargetId() != target.getId() + ) + ) { + return Messages.getString("WeaponAttackAction.OneTargetForCNarc"); } }