diff --git a/Mage/src/main/java/mage/choices/ChoiceCreatureType.java b/Mage/src/main/java/mage/choices/ChoiceCreatureType.java index c17b5ac9e47..e635c26f387 100644 --- a/Mage/src/main/java/mage/choices/ChoiceCreatureType.java +++ b/Mage/src/main/java/mage/choices/ChoiceCreatureType.java @@ -8,7 +8,7 @@ import java.util.stream.Collectors; public class ChoiceCreatureType extends ChoiceImpl { - private static String DEFAULT_MESSAGE = "Choose a creature type"; + private static final String DEFAULT_MESSAGE = "Choose a creature type"; public ChoiceCreatureType() { this(true, DEFAULT_MESSAGE, null); diff --git a/Mage/src/main/java/mage/choices/ChoiceImpl.java b/Mage/src/main/java/mage/choices/ChoiceImpl.java index 12246f9915a..944f1ad5e2a 100644 --- a/Mage/src/main/java/mage/choices/ChoiceImpl.java +++ b/Mage/src/main/java/mage/choices/ChoiceImpl.java @@ -1,6 +1,7 @@ package mage.choices; import mage.util.RandomUtil; +import org.apache.log4j.Logger; import java.util.*; @@ -9,9 +10,11 @@ import java.util.*; */ public class ChoiceImpl implements Choice { + private static final Logger logger = Logger.getLogger(Choice.class); + protected boolean chosenNormal; protected boolean chosenSpecial; - protected final boolean required; + protected boolean required; protected String choice; protected String choiceKey; protected Set choices = new LinkedHashSet<>(); @@ -109,6 +112,7 @@ public class ChoiceImpl implements Choice { @Override public void setChoices(Set choices) { this.choices = choices; + protectFromEmptyChoices(); } @Override @@ -149,6 +153,7 @@ public class ChoiceImpl implements Choice { @Override public void setKeyChoices(Map choices) { keyChoices = choices; + protectFromEmptyChoices(); } @Override @@ -314,4 +319,25 @@ public class ChoiceImpl implements Choice { public ChoiceHintType getHintType() { return this.hintType; } + + private void protectFromEmptyChoices() { + // if there are no choices then required must be disabled to allow user to close a dialog + // example: database error on too low memory, see Brain Pry and 500 Mb server + + // normal situation + if (!this.required) { + return; + } + + // special checkbox can allow empty choices + if (this.specialEnabled && this.specialCanBeEmpty) { + return; + } + + if (this.choices.isEmpty() && this.keyChoices.isEmpty()) { + // it can be a server problems or wrong card code + this.required = false; + logger.error("Empty choice dialog in " + this.getClass().getCanonicalName(), new Throwable()); + } + } } diff --git a/Mage/src/main/java/mage/choices/Choices.java b/Mage/src/main/java/mage/choices/Choices.java deleted file mode 100644 index c934575142f..00000000000 --- a/Mage/src/main/java/mage/choices/Choices.java +++ /dev/null @@ -1,72 +0,0 @@ - -package mage.choices; - -import mage.abilities.Ability; -import mage.constants.Outcome; -import mage.game.Game; -import mage.players.Player; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -/** - * - * @author BetaSteward_at_googlemail.com - */ -public class Choices extends ArrayList { - - protected Outcome outcome; - - public Choices() { - } - - public Choices(final Choices choices) { - this.outcome = choices.outcome; - for (Choice choice : choices) { - this.add(choice.copy()); - } - } - - public Choices copy() { - return new Choices(this); - } - - public List getUnchosen() { - return stream() - .filter(choice -> !choice.isChosen()) - .collect(Collectors.toList()); - } - - public void clearChosen() { - for (Choice choice : this) { - choice.clearChoice(); - } - } - - public boolean isChosen() { - for (Choice choice : this) { - if (!choice.isChosen()) { - return false; - } - } - return true; - } - - public boolean choose(Game game, Ability source) { - if (this.size() > 0) { - Player player = game.getPlayer(source.getControllerId()); - if (player == null) { - return false; - } - while (!isChosen()) { - Choice choice = this.getUnchosen().get(0); - if (!player.choose(outcome, choice, game)) { - return false; - } - } - } - return true; - } - -}