fix Vial Smasher will not target invalid opponents
@@ -20,13 +20,13 @@ import mage.game.permanent.Permanent;
import mage.game.stack.Spell;
import mage.players.Player;
import mage.target.TargetPermanent;
-import mage.util.RandomUtil;
import mage.watchers.common.SpellsCastWatcher;
-import java.util.Iterator;
+import java.util.Collections;
import java.util.List;
-import java.util.Set;
+import java.util.Objects;
import java.util.UUID;
+import java.util.stream.Collectors;
/**
* @author LevelX2
@@ -123,17 +123,20 @@ class VialSmasherTheFierceEffect extends OneShotEffect {
if (controller != null) {
int damage = (Integer) getValue("VialSmasherTheFierceCMC");
if (damage > 0) {
- Set<UUID> opponents = game.getOpponents(source.getControllerId());
- int random = RandomUtil.nextInt(opponents.size());
- Iterator<UUID> iterator = opponents.iterator();
- for (int i = 0; i < random; i++) {
- iterator.next();
- }
- UUID opponentId = iterator.next();
- Player opponent = game.getPlayer(opponentId);
- if (opponent != null) {
+ List<Player> opponents = game.getOpponents(source.getControllerId())
+ .stream()
+ .map(game::getPlayer)
+ .filter(Objects::nonNull)
+ .filter(o -> !o.hasLost())
+ .filter(o -> !o.hasLeft())
+ .collect(Collectors.toList());
+ if(opponents.isEmpty()){
+ return false;
+ }
+ Collections.shuffle(opponents);
+ Player opponent = opponents.get(0);
game.informPlayers(opponent.getLogName() + " was chosen at random.");
- if (game.getBattlefield().getAllActivePermanents(new FilterPlaneswalkerPermanent(), opponentId, game).size() > 0) {
+ if (game.getBattlefield().getAllActivePermanents(new FilterPlaneswalkerPermanent(), opponent.getId(), game).size() > 0) {
if (controller.chooseUse(Outcome.Damage, "Redirect to a planeswalker controlled by " + opponent.getLogName() + "?", source, game)) {
FilterPlaneswalkerPermanent filter = new FilterPlaneswalkerPermanent("a planeswalker controlled by " + opponent.getLogName());
filter.add(new ControllerIdPredicate(opponent.getId()));
@@ -147,7 +150,7 @@ class VialSmasherTheFierceEffect extends OneShotEffect {
}
}
opponent.damage(damage, source.getSourceId(), source, game);
- }
+
}
return true;
}
