From bfa5b6ab5fecf4f1919e2396a92eb9794bdd8ed4 Mon Sep 17 00:00:00 2001 From: Alex Vasile <48962821+Alex-Vasile@users.noreply.github.com> Date: Mon, 5 Sep 2022 00:30:45 -0400 Subject: [PATCH] [NPH] Fix Hex Parasite wrong behaviour when X=0. Closes #9446. --- Mage.Sets/src/mage/cards/h/HexParasite.java | 57 ++++++++++--------- .../cards/single/nph/HexParasiteTest.java | 44 ++++++++++++++ 2 files changed, 74 insertions(+), 27 deletions(-) create mode 100644 Mage.Tests/src/test/java/org/mage/test/cards/single/nph/HexParasiteTest.java diff --git a/Mage.Sets/src/mage/cards/h/HexParasite.java b/Mage.Sets/src/mage/cards/h/HexParasite.java index 2d3c7988bb..cbf0e0bec8 100644 --- a/Mage.Sets/src/mage/cards/h/HexParasite.java +++ b/Mage.Sets/src/mage/cards/h/HexParasite.java @@ -66,34 +66,37 @@ class HexParasiteEffect extends OneShotEffect { TargetPermanent target = (TargetPermanent) source.getTargets().get(0); Permanent permanent = game.getPermanent(target.getFirstTarget()); Player controller = game.getPlayer(source.getControllerId()); - if (permanent != null - && controller != null) { - int toRemove = source.getManaCostsToPay().getX(); - int removed = 0; - String[] counterNames = permanent.getCounters(game).keySet().toArray(new String[0]); - for (String counterName : counterNames) { - if (controller.chooseUse(Outcome.Neutral, "Remove " + counterName + " counters?", source, game)) { - if (permanent.getCounters(game).get(counterName).getCount() == 1 || (toRemove - removed == 1)) { - permanent.removeCounters(counterName, 1, source, game); - removed++; - } else { - int amount = controller.getAmount(1, Math.min(permanent.getCounters(game).get(counterName).getCount(), toRemove - removed), "How many?", game); - if (amount > 0) { - removed += amount; - permanent.removeCounters(counterName, amount, source, game); - } - } - } - if (removed >= toRemove) { - break; - } - } - if (removed > 0) { - game.addEffect(new BoostSourceEffect(removed, 0, Duration.EndOfTurn), source); - } + if (permanent == null || controller == null) { + return false; + } + + int toRemove = source.getManaCostsToPay().getX(); + if (toRemove == 0) { return true; } - return false; - } + int removed = 0; + String[] counterNames = permanent.getCounters(game).keySet().toArray(new String[0]); + for (String counterName : counterNames) { + if (controller.chooseUse(Outcome.Neutral, "Remove " + counterName + " counters?", source, game)) { + if (permanent.getCounters(game).get(counterName).getCount() == 1 || (toRemove - removed == 1)) { + permanent.removeCounters(counterName, 1, source, game); + removed++; + } else { + int amount = controller.getAmount(1, Math.min(permanent.getCounters(game).get(counterName).getCount(), toRemove - removed), "How many?", game); + if (amount > 0) { + removed += amount; + permanent.removeCounters(counterName, amount, source, game); + } + } + } + if (removed >= toRemove) { + break; + } + } + if (removed > 0) { + game.addEffect(new BoostSourceEffect(removed, 0, Duration.EndOfTurn), source); + } + return true; + } } diff --git a/Mage.Tests/src/test/java/org/mage/test/cards/single/nph/HexParasiteTest.java b/Mage.Tests/src/test/java/org/mage/test/cards/single/nph/HexParasiteTest.java new file mode 100644 index 0000000000..b9f7bfb983 --- /dev/null +++ b/Mage.Tests/src/test/java/org/mage/test/cards/single/nph/HexParasiteTest.java @@ -0,0 +1,44 @@ +package org.mage.test.cards.single.nph; + +import mage.constants.PhaseStep; +import mage.constants.Zone; +import mage.counters.CounterType; +import org.junit.Test; +import org.mage.test.serverside.base.CardTestPlayerBase; + +/** + * {@link mage.cards.h.HexParasite Hex Parasite} + * + * {X}{B/P}: Remove up to X counters from target permanent. + * For each counter removed this way, Hex Parasite gets +1/+0 until end of turn. + * + * @author Alex-Vasile + */ +public class HexParasiteTest extends CardTestPlayerBase { + + private static final String hexParasite = "Hex Parasite"; + + /** + * Reported bug: https://github.com/magefree/mage/issues/9446 + * Removing 0 counters results in "Server's error: (minimum = value = maximum) is false" + */ + @Test + public void remove0Counters() { + addCard(Zone.BATTLEFIELD, playerA, hexParasite); + addCard(Zone.HAND, playerA, "Urza's Saga"); + + setStrictChooseMode(true); + + playLand(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Urza's Saga"); + + activateAbility(1, PhaseStep.POSTCOMBAT_MAIN, playerA, "{X}"); + setChoice(playerA, "X=0"); + setChoice(playerA, "Yes"); // Pay 2 life + addTarget(playerA, "Urza's Saga"); + + setStopAt(1, PhaseStep.END_TURN); + execute(); + assertCounterCount(playerA, "Urza's Saga", CounterType.LORE, 1); + assertLife(playerA, 20 - 2); + } +}