Skip to content

Commit e066406

Browse files
committed
refactor: Use stream composition when checking for previously-fired solo weapons
1 parent c3b03f6 commit e066406

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

megamek/src/megamek/common/actions/WeaponAttackAction.java

+15-20
Original file line numberDiff line numberDiff line change
@@ -2408,26 +2408,21 @@ private static String toHitIsImpossible(Game game, Entity ae, int attackerId, Ta
24082408
// Some weapons can only be fired by themselves
24092409

24102410
// Check to see if another solo weapon was fired
2411-
boolean hasSoloAttack = false;
2412-
String soloWeaponName = "";
2413-
for (EntityAction ea : game.getActionsVector()) {
2414-
if ((ea.getEntityId() == attackerId) && (ea instanceof WeaponAttackAction)) {
2415-
WeaponAttackAction otherWAA = (WeaponAttackAction) ea;
2416-
final Mounted otherWeapon = ae.getEquipment(otherWAA.getWeaponId());
2417-
2418-
if (!(otherWeapon.getType() instanceof WeaponType)) {
2419-
continue;
2420-
}
2421-
final WeaponType otherWtype = (WeaponType) otherWeapon.getType();
2422-
hasSoloAttack |= (otherWtype.hasFlag(WeaponType.F_SOLO_ATTACK) && otherWAA.getWeaponId() != weaponId);
2423-
if (hasSoloAttack) {
2424-
soloWeaponName = otherWeapon.getName();
2425-
break;
2426-
}
2427-
}
2428-
}
2429-
if (hasSoloAttack) {
2430-
return String.format(Messages.getString("WeaponAttackAction.CantFireWithOtherWeapons"), soloWeaponName);
2411+
2412+
// The name of a solo weapon that has already been fired, if one exists.
2413+
Optional<String> soloWeaponName = game.getActionsVector().stream()
2414+
.filter(prevAttack -> prevAttack.getEntityId() == attackerId)
2415+
.filter(WeaponAttackAction.class::isInstance)
2416+
.map(WeaponAttackAction.class::cast)
2417+
.map(WeaponAttackAction::getWeaponId)
2418+
.filter(otherWAAId -> otherWAAId != weaponId)
2419+
.map(otherWAAId -> ae.getEquipment(otherWAAId))
2420+
.filter(otherWeapon -> otherWeapon.getType().hasFlag(WeaponType.F_SOLO_ATTACK))
2421+
.map(Mounted::getName)
2422+
.findAny();
2423+
2424+
if (soloWeaponName.isPresent()) {
2425+
return String.format(Messages.getString("WeaponAttackAction.CantFireWithOtherWeapons"), soloWeaponName.orElseThrow());
24312426
}
24322427

24332428
// Handle solo attack weapons.

0 commit comments

Comments
 (0)