diff --git a/Mage/src/main/java/mage/target/common/TargetCreatureOrPlaneswalkerAmount.java b/Mage/src/main/java/mage/target/common/TargetCreatureOrPlaneswalkerAmount.java index d005822bc0..3485646d4d 100644 --- a/Mage/src/main/java/mage/target/common/TargetCreatureOrPlaneswalkerAmount.java +++ b/Mage/src/main/java/mage/target/common/TargetCreatureOrPlaneswalkerAmount.java @@ -1,151 +1,38 @@ package mage.target.common; -import mage.MageObject; -import mage.abilities.Ability; import mage.abilities.dynamicvalue.DynamicValue; -import mage.abilities.dynamicvalue.common.StaticValue; -import mage.constants.Zone; -import mage.filter.Filter; import mage.filter.common.FilterCreatureOrPlaneswalkerPermanent; -import mage.game.Game; -import mage.game.permanent.Permanent; -import mage.target.TargetAmount; - -import java.util.Objects; -import java.util.Set; -import java.util.UUID; -import java.util.stream.Collectors; /** * @author BetaSteward_at_googlemail.com */ -public class TargetCreatureOrPlaneswalkerAmount extends TargetAmount { +public class TargetCreatureOrPlaneswalkerAmount extends TargetPermanentAmount { - protected final FilterCreatureOrPlaneswalkerPermanent filter; private static final FilterCreatureOrPlaneswalkerPermanent defaultFilter = new FilterCreatureOrPlaneswalkerPermanent(); public TargetCreatureOrPlaneswalkerAmount(int amount) { - // 107.1c If a rule or ability instructs a player to choose “any number,” that player may choose - // any positive number or zero, unless something (such as damage or counters) is being divided - // or distributed among “any number” of players and/or objects. In that case, a nonzero number - // of players and/or objects must be chosen if possible. - this(amount, defaultFilter); + super(amount, defaultFilter); } public TargetCreatureOrPlaneswalkerAmount(DynamicValue amount) { - this(amount, defaultFilter); + super(amount, defaultFilter); } public TargetCreatureOrPlaneswalkerAmount(int amount, FilterCreatureOrPlaneswalkerPermanent filter) { - this(new StaticValue(amount), filter); + super(amount, filter); } public TargetCreatureOrPlaneswalkerAmount(DynamicValue amount, FilterCreatureOrPlaneswalkerPermanent filter) { - super(amount); - this.zone = Zone.ALL; - this.filter = filter; - this.targetName = filter.getMessage(); + super(amount, filter); } private TargetCreatureOrPlaneswalkerAmount(final TargetCreatureOrPlaneswalkerAmount target) { super(target); - this.filter = target.filter.copy(); - } - - @Override - public Filter getFilter() { - return this.filter; - } - - @Override - public boolean canTarget(UUID objectId, Game game) { - Permanent permanent = game.getPermanent(objectId); - return permanent != null && filter.match(permanent, game); - } - - @Override - public boolean canTarget(UUID objectId, Ability source, Game game) { - Permanent permanent = game.getPermanent(objectId); - if (permanent != null) { - if (source != null) { - MageObject targetSource = source.getSourceObject(game); - return permanent.canBeTargetedBy(targetSource, source.getControllerId(), game) - && filter.match(permanent, source.getSourceId(), source.getControllerId(), game); - } else { - return filter.match(permanent, game); - } - } - return false; - } - - @Override - public boolean canTarget(UUID playerId, UUID objectId, Ability source, Game game) { - return canTarget(objectId, source, game); - } - - @Override - public boolean canChoose(UUID sourceId, UUID sourceControllerId, Game game) { - int count = 0; - for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, sourceControllerId, sourceId, game)) { - count++; - if (count >= this.minNumberOfTargets) { - return true; - } - } - return false; - } - - @Override - public boolean canChoose(UUID sourceControllerId, Game game) { - int count = 0; - for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, sourceControllerId, game)) { - count++; - if (count >= this.minNumberOfTargets) { - return true; - } - } - return false; - } - - @Override - public Set possibleTargets(UUID sourceId, UUID sourceControllerId, Game game) { - MageObject targetSource = game.getObject(sourceId); - return game - .getBattlefield() - .getActivePermanents(filter, sourceId, sourceControllerId, game) - .stream() - .filter(Objects::nonNull) - .filter(permanent -> permanent.canBeTargetedBy(targetSource, sourceControllerId, game)) - .map(Permanent::getId) - .collect(Collectors.toSet()); - } - - @Override - public Set possibleTargets(UUID sourceControllerId, Game game) { - return game - .getBattlefield() - .getActivePermanents(filter, sourceControllerId, game) - .stream() - .map(Permanent::getId) - .collect(Collectors.toSet()); - } - - @Override - public String getTargetedName(Game game) { - StringBuilder sb = new StringBuilder(); - for (UUID targetId : getTargets()) { - Permanent permanent = game.getPermanent(targetId); - if (permanent != null) { - sb.append(permanent.getLogName()).append('(').append(getTargetAmount(targetId)).append(") "); - } - } - return sb.toString(); } @Override public TargetCreatureOrPlaneswalkerAmount copy() { return new TargetCreatureOrPlaneswalkerAmount(this); } - } diff --git a/Mage/src/main/java/mage/target/common/TargetCreaturePermanentAmount.java b/Mage/src/main/java/mage/target/common/TargetCreaturePermanentAmount.java index fae796583a..855cb08516 100644 --- a/Mage/src/main/java/mage/target/common/TargetCreaturePermanentAmount.java +++ b/Mage/src/main/java/mage/target/common/TargetCreaturePermanentAmount.java @@ -1,145 +1,32 @@ - package mage.target.common; -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; -import mage.MageObject; -import mage.abilities.Ability; import mage.abilities.dynamicvalue.DynamicValue; -import mage.abilities.dynamicvalue.common.StaticValue; -import mage.constants.Zone; -import mage.filter.Filter; import mage.filter.StaticFilters; import mage.filter.common.FilterCreaturePermanent; -import mage.game.Game; -import mage.game.permanent.Permanent; -import mage.target.TargetAmount; /** - * * @author North */ -public class TargetCreaturePermanentAmount extends TargetAmount { - - protected final FilterCreaturePermanent filter; +public class TargetCreaturePermanentAmount extends TargetPermanentAmount { public TargetCreaturePermanentAmount(int amount) { - this(amount, new FilterCreaturePermanent()); + super(amount, StaticFilters.FILTER_PERMANENT_CREATURE); } public TargetCreaturePermanentAmount(DynamicValue amount) { - this(amount, new FilterCreaturePermanent()); + this(amount, StaticFilters.FILTER_PERMANENT_CREATURE); } public TargetCreaturePermanentAmount(int amount, FilterCreaturePermanent filter) { - this(new StaticValue(amount), filter); + super(amount, filter); } public TargetCreaturePermanentAmount(DynamicValue amount, FilterCreaturePermanent filter) { - super(amount); - this.zone = Zone.ALL; - this.filter = filter; - this.targetName = filter.getMessage(); + super(amount, filter); } - public TargetCreaturePermanentAmount(final TargetCreaturePermanentAmount target) { + private TargetCreaturePermanentAmount(final TargetCreaturePermanentAmount target) { super(target); - this.filter = target.filter.copy(); - } - - @Override - public Filter getFilter() { - return this.filter; - } - - @Override - public boolean canTarget(UUID id, Game game) { - Permanent permanent = game.getPermanent(id); - return permanent != null && filter.match(permanent, game); - } - - @Override - public boolean canTarget(UUID id, Ability source, Game game) { - Permanent permanent = game.getPermanent(id); - if (permanent != null) { - if (source != null) { - MageObject targetSource = game.getObject(source.getSourceId()); - return permanent.canBeTargetedBy(targetSource, source.getControllerId(), game) && filter.match(permanent, source.getSourceId(), source.getControllerId(), game); - } else { - return filter.match(permanent, game); - } - } - return false; - } - - @Override - public boolean canTarget(UUID playerId, UUID id, Ability source, Game game) { - return canTarget(id, source, game); - } - - @Override - public boolean canChoose(UUID sourceId, UUID sourceControllerId, Game game) { - int count = 0; - MageObject targetSource = game.getObject(sourceId); - for (Permanent permanent : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, sourceControllerId, game)) { - if (permanent.canBeTargetedBy(targetSource, sourceControllerId, game) && filter.match(permanent, sourceId, sourceControllerId, game)) { - count++; - if (count >= this.minNumberOfTargets) { - return true; - } - } - } - return false; - } - - @Override - public boolean canChoose(UUID sourceControllerId, Game game) { - int count = 0; - for (Permanent permanent : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, sourceControllerId, game)) { - if (filter.match(permanent, null, sourceControllerId, game)) { - count++; - if (count >= this.minNumberOfTargets) { - return true; - } - } - } - return false; - } - - @Override - public Set possibleTargets(UUID sourceId, UUID sourceControllerId, Game game) { - Set possibleTargets = new HashSet<>(); - MageObject targetSource = game.getObject(sourceId); - for (Permanent permanent : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, sourceControllerId, game)) { - if (permanent.canBeTargetedBy(targetSource, sourceControllerId, game) && filter.match(permanent, sourceId, sourceControllerId, game)) { - possibleTargets.add(permanent.getId()); - } - } - return possibleTargets; - } - - @Override - public Set possibleTargets(UUID sourceControllerId, Game game) { - Set possibleTargets = new HashSet<>(); - for (Permanent permanent : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, sourceControllerId, game)) { - if (filter.match(permanent, null, sourceControllerId, game)) { - possibleTargets.add(permanent.getId()); - } - } - return possibleTargets; - } - - @Override - public String getTargetedName(Game game) { - StringBuilder sb = new StringBuilder(); - for (UUID targetId : getTargets()) { - Permanent permanent = game.getPermanent(targetId); - if (permanent != null) { - sb.append(permanent.getLogName()).append('(').append(getTargetAmount(targetId)).append(") "); - } - } - return sb.toString(); } @Override diff --git a/Mage/src/main/java/mage/target/common/TargetPermanentAmount.java b/Mage/src/main/java/mage/target/common/TargetPermanentAmount.java new file mode 100644 index 0000000000..b6c9afedc3 --- /dev/null +++ b/Mage/src/main/java/mage/target/common/TargetPermanentAmount.java @@ -0,0 +1,150 @@ +package mage.target.common; + +import mage.MageObject; +import mage.abilities.Ability; +import mage.abilities.dynamicvalue.DynamicValue; +import mage.abilities.dynamicvalue.common.StaticValue; +import mage.constants.Zone; +import mage.filter.Filter; +import mage.filter.FilterPermanent; +import mage.game.Game; +import mage.game.permanent.Permanent; +import mage.target.TargetAmount; + +import java.util.Objects; +import java.util.Set; +import java.util.UUID; +import java.util.stream.Collectors; + +/** + * @author TheElk801 + */ +public abstract class TargetPermanentAmount extends TargetAmount { + + protected final FilterPermanent filter; + + TargetPermanentAmount(int amount, FilterPermanent filter) { + // 107.1c If a rule or ability instructs a player to choose “any number,” that player may choose + // any positive number or zero, unless something (such as damage or counters) is being divided + // or distributed among “any number” of players and/or objects. In that case, a nonzero number + // of players and/or objects must be chosen if possible. + this(new StaticValue(amount), filter); + } + + TargetPermanentAmount(DynamicValue amount, FilterPermanent filter) { + super(amount); + this.zone = Zone.ALL; + this.filter = filter; + this.targetName = filter.getMessage(); + } + + TargetPermanentAmount(final TargetPermanentAmount target) { + super(target); + this.filter = target.filter.copy(); + } + + @Override + public Filter getFilter() { + return this.filter; + } + + @Override + public boolean canTarget(UUID objectId, Game game) { + Permanent permanent = game.getPermanent(objectId); + return permanent != null && filter.match(permanent, game); + } + + @Override + public boolean canTarget(UUID objectId, Ability source, Game game) { + if (getMaxNumberOfTargets() > 0 && getTargets().size() >= getMaxNumberOfTargets()) { + return getTargets().contains(objectId); + } + Permanent permanent = game.getPermanent(objectId); + if (permanent == null) { + return false; + } + if (source == null) { + return filter.match(permanent, game); + } + MageObject targetSource = source.getSourceObject(game); + return permanent.canBeTargetedBy(targetSource, source.getControllerId(), game) + && filter.match(permanent, source.getSourceId(), source.getControllerId(), game); + } + + @Override + public boolean canTarget(UUID playerId, UUID objectId, Ability source, Game game) { + return canTarget(objectId, source, game); + } + + @Override + public boolean canChoose(UUID sourceId, UUID sourceControllerId, Game game) { + int count = 0; + for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, sourceControllerId, sourceId, game)) { + count++; + if (count >= this.minNumberOfTargets) { + return true; + } + } + return false; + } + + @Override + public boolean canChoose(UUID sourceControllerId, Game game) { + int count = 0; + for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, sourceControllerId, game)) { + count++; + if (count >= this.minNumberOfTargets) { + return true; + } + } + return false; + } + + @Override + public Set possibleTargets(UUID sourceId, UUID sourceControllerId, Game game) { + if (getMaxNumberOfTargets() > 0 && getTargets().size() >= getMaxNumberOfTargets()) { + return getTargets() + .stream() + .collect(Collectors.toSet()); + } + MageObject targetSource = game.getObject(sourceId); + return game + .getBattlefield() + .getActivePermanents(filter, sourceControllerId, sourceId, game) + .stream() + .filter(Objects::nonNull) + .filter(permanent -> permanent.canBeTargetedBy(targetSource, sourceControllerId, game)) + .map(Permanent::getId) + .collect(Collectors.toSet()); + } + + @Override + public Set possibleTargets(UUID sourceControllerId, Game game) { + if (getMaxNumberOfTargets() > 0 && getTargets().size() >= getMaxNumberOfTargets()) { + return getTargets() + .stream() + .collect(Collectors.toSet()); + } + return game + .getBattlefield() + .getActivePermanents(filter, sourceControllerId, game) + .stream() + .map(Permanent::getId) + .collect(Collectors.toSet()); + } + + @Override + public String getTargetedName(Game game) { + StringBuilder sb = new StringBuilder(); + for (UUID targetId : getTargets()) { + Permanent permanent = game.getPermanent(targetId); + if (permanent != null) { + sb.append(permanent.getLogName()).append('(').append(getTargetAmount(targetId)).append(") "); + } + } + return sb.toString(); + } + + @Override + public abstract TargetPermanentAmount copy(); +}