Refactoring

See github line by line comments in 'File changed'
This commit is contained in:
vraskulin 2016-12-29 16:49:12 +03:00
parent c0cf22bbf7
commit f04ba151f7
52 changed files with 87 additions and 220 deletions

View file

@ -76,10 +76,7 @@ public abstract class TargetAmount extends TargetImpl {
@Override
public boolean doneChosing() {
if (amountWasSet == false) {
return false;
}
return remainingAmount == 0;
return amountWasSet && remainingAmount == 0;
}
@Override

View file

@ -44,7 +44,7 @@ import mage.players.Player;
*/
public class TargetCard extends TargetObject {
protected FilterCard filter;
protected final FilterCard filter;
protected TargetCard(Zone zone) {
this(1, 1, zone, new FilterCard());
@ -214,10 +214,7 @@ public class TargetCard extends TargetObject {
public boolean canTarget(UUID id, Cards cards, Game game) {
Card card = cards.get(id, game);
if (card != null) {
return filter.match(card, game);
}
return false;
return card != null && filter.match(card, game);
}
@Override

View file

@ -34,7 +34,6 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import mage.MageObject;
@ -55,8 +54,8 @@ import mage.util.RandomUtil;
*/
public abstract class TargetImpl implements Target {
protected Map<UUID, Integer> targets = new LinkedHashMap<>();
protected Map<UUID, Integer> zoneChangeCounters = new HashMap<>();
protected final Map<UUID, Integer> targets = new LinkedHashMap<>();
protected final Map<UUID, Integer> zoneChangeCounters = new HashMap<>();
protected String targetName;
protected Zone zone;
@ -199,18 +198,12 @@ public abstract class TargetImpl implements Target {
if (getMaxNumberOfTargets() == 0 && getNumberOfTargets() == 0) {
return true;
}
if (getMaxNumberOfTargets() != 0 && targets.size() == getMaxNumberOfTargets()) {
return true;
}
return chosen;
return getMaxNumberOfTargets() != 0 && targets.size() == getMaxNumberOfTargets() || chosen;
}
@Override
public boolean doneChosing() {
if (getMaxNumberOfTargets() == 0) {
return false;
}
return targets.size() == getMaxNumberOfTargets();
return getMaxNumberOfTargets() != 0 && targets.size() == getMaxNumberOfTargets();
}
@Override

View file

@ -78,10 +78,7 @@ public abstract class TargetObject extends TargetImpl {
@Override
public boolean canTarget(UUID id, Game game) {
MageObject object = game.getObject(id);
if (object != null && game.getState().getZone(id).match(zone)) {
return getFilter().match(object, game);
}
return false;
return object != null && game.getState().getZone(id).match(zone) && getFilter().match(object, game);
}
@Override

View file

@ -101,10 +101,7 @@ public class TargetPermanent extends TargetObject {
public boolean canTarget(UUID controllerId, UUID id, UUID sourceId, Game game, boolean flag) {
Permanent permanent = game.getPermanent(id);
if (permanent != null) {
return filter.match(permanent, sourceId, controllerId, game);
}
return false;
return permanent != null && filter.match(permanent, sourceId, controllerId, game);
}
@Override

View file

@ -42,7 +42,7 @@ import mage.players.Player;
*/
public class TargetPlayer extends TargetImpl {
protected FilterPlayer filter;
protected final FilterPlayer filter;
public TargetPlayer() {
this(1, 1, false);
@ -169,10 +169,7 @@ public class TargetPlayer extends TargetImpl {
@Override
public boolean canTarget(UUID id, Game game) {
Player player = game.getPlayer(id);
if (player != null) {
return filter.match(player, game);
}
return false;
return player != null && filter.match(player, game);
}
@Override

View file

@ -47,7 +47,7 @@ import mage.players.Player;
*/
public class TargetSource extends TargetObject {
protected FilterObject filter;
protected final FilterObject filter;
public TargetSource() {
this(1, 1, new FilterObject("source of your choice"));

View file

@ -43,7 +43,7 @@ import mage.game.stack.StackObject;
*/
public class TargetSpell extends TargetObject {
protected FilterSpell filter;
protected final FilterSpell filter;
public TargetSpell() {
this(1, 1, new FilterSpell());
@ -82,10 +82,7 @@ public class TargetSpell extends TargetObject {
return false;
}
Spell spell = game.getStack().getSpell(id);
if (spell != null) {
return filter.match(spell, source.getSourceId(), source.getControllerId(), game);
}
return false;
return spell != null && filter.match(spell, source.getSourceId(), source.getControllerId(), game);
}
@Override

View file

@ -44,7 +44,7 @@ import java.util.UUID;
*/
public class TargetStackObject extends TargetObject {
protected FilterStackObject filter;
protected final FilterStackObject filter;
public TargetStackObject() {
this(1, 1, new FilterStackObject());
@ -79,10 +79,7 @@ public class TargetStackObject extends TargetObject {
@Override
public boolean canTarget(UUID id, Ability source, Game game) {
StackObject stackObject = game.getStack().getStackObject(id);
if (stackObject != null) {
return filter.match(stackObject, source.getSourceId(), source.getControllerId(), game);
}
return false;
return stackObject != null && filter.match(stackObject, source.getSourceId(), source.getControllerId(), game);
}
@Override

View file

@ -45,8 +45,8 @@ import mage.target.TargetCard;
*/
public class TargetCardInExile extends TargetCard {
private UUID zoneId;
private boolean allExileZones;
private final UUID zoneId;
private final boolean allExileZones;
public TargetCardInExile(FilterCard filter) {
this(1, 1, filter, null);
@ -68,11 +68,7 @@ public class TargetCardInExile extends TargetCard {
public TargetCardInExile(int minNumTargets, int maxNumTargets, FilterCard filter, UUID zoneId, boolean allExileZones) {
super(minNumTargets, maxNumTargets, Zone.EXILED, filter);
this.zoneId = zoneId;
if (zoneId == null) {
this.allExileZones = true;
} else {
this.allExileZones = allExileZones;
}
this.allExileZones = zoneId == null || allExileZones;
}
public TargetCardInExile(final TargetCardInExile target) {

View file

@ -65,10 +65,7 @@ public class TargetCardInGraveyard extends TargetCard {
@Override
public boolean canTarget(UUID id, Ability source, Game game) {
Card card = game.getCard(id);
if (card != null && game.getState().getZone(card.getId()) == Zone.GRAVEYARD) {
return filter.match(card, game);
}
return false;
return card != null && game.getState().getZone(card.getId()) == Zone.GRAVEYARD && filter.match(card, game);
}
@Override

View file

@ -89,10 +89,7 @@ public class TargetCardInGraveyardOrBattlefield extends TargetCard {
return filter.match(permanent, game);
}
Card card = game.getCard(id);
if (card != null && game.getState().getZone(card.getId()) == Zone.GRAVEYARD) {
return filter.match(card, game);
}
return false;
return card != null && game.getState().getZone(card.getId()) == Zone.GRAVEYARD && filter.match(card, game);
}
@Override

View file

@ -69,10 +69,7 @@ public class TargetCardInHand extends TargetCard {
@Override
public boolean canTarget(UUID playerId, UUID id, Ability source, Game game) {
Card card = game.getPlayer(playerId).getHand().get(id, game);
if (card != null) {
return filter.match(card, source.getSourceId(), playerId, game);
}
return false;
return card != null && filter.match(card, source.getSourceId(), playerId, game);
}
@Override

View file

@ -92,7 +92,7 @@ public class TargetCardInLibrary extends TargetCard {
} else {
cards = new ArrayList<>(targetPlayer.getLibrary().getTopCards(game, librarySearchLimit));
}
Collections.sort(cards, new CardNameComparator());
cards.sort(new CardNameComparator());
Cards cardsId = new CardsImpl();
for (Card card : cards) {
cardsId.add(card);
@ -111,10 +111,7 @@ public class TargetCardInLibrary extends TargetCard {
@Override
public boolean canTarget(UUID id, Ability source, Game game) {
Card card = game.getPlayer(source.getControllerId()).getLibrary().getCard(id, game);
if (card != null) {
return filter.match(card, game);
}
return false;
return card != null && filter.match(card, game);
}
@Override

View file

@ -14,7 +14,7 @@ import mage.players.Player;
public class TargetCardInOpponentsGraveyard extends TargetCard {
protected boolean allFromOneOpponent;
protected final boolean allFromOneOpponent;
public TargetCardInOpponentsGraveyard(FilterCard filter) {
this(1, 1, filter, false);

View file

@ -47,7 +47,7 @@
*/
public class TargetCreatureOrPlaneswalkerAmount extends TargetAmount {
protected FilterCreatureOrPlaneswalkerPermanent filter;
protected final FilterCreatureOrPlaneswalkerPermanent filter;
public TargetCreatureOrPlaneswalkerAmount(int amount) {
// 107.1c If a rule or ability instructs a player to choose any number, that player may choose
@ -85,10 +85,7 @@
@Override
public boolean canTarget(UUID objectId, Game game) {
Permanent permanent = game.getPermanent(objectId);
if (permanent != null) {
return filter.match(permanent, game);
}
return false;
return permanent != null && filter.match(permanent, game);
}
@Override

View file

@ -90,10 +90,7 @@ public class TargetCreatureOrPlayer extends TargetImpl {
return filter.match(permanent, game);
}
Player player = game.getPlayer(id);
if (player != null) {
return filter.match(player, game);
}
return false;
return player != null && filter.match(player, game);
}
@Override
@ -119,10 +116,7 @@ public class TargetCreatureOrPlayer extends TargetImpl {
if (permanent != null) {
return filter.match(permanent, game);
}
if (player != null) {
return filter.match(player, game);
}
return false;
return player != null && filter.match(player, game);
}
/**

View file

@ -49,7 +49,7 @@ import mage.target.TargetAmount;
*/
public class TargetCreatureOrPlayerAmount extends TargetAmount {
protected FilterCreatureOrPlayer filter;
protected final FilterCreatureOrPlayer filter;
public TargetCreatureOrPlayerAmount(int amount) {
// 107.1c If a rule or ability instructs a player to choose any number, that player may choose
@ -84,10 +84,7 @@ public class TargetCreatureOrPlayerAmount extends TargetAmount {
return filter.match(permanent, game);
}
Player player = game.getPlayer(objectId);
if (player != null) {
return filter.match(player, game);
}
return false;
return player != null && filter.match(player, game);
}
@Override
@ -108,10 +105,7 @@ public class TargetCreatureOrPlayerAmount extends TargetAmount {
if (permanent != null) {
return filter.match(permanent, game);
}
if (player != null) {
return filter.match(player, game);
}
return false;
return player != null && filter.match(player, game);
}
@Override

View file

@ -49,7 +49,7 @@ import java.util.UUID;
*/
public class TargetCreaturePermanentAmount extends TargetAmount {
protected FilterCreaturePermanent filter;
protected final FilterCreaturePermanent filter;
public TargetCreaturePermanentAmount(int amount) {
this(amount, new FilterCreaturePermanent());
@ -83,10 +83,7 @@ public class TargetCreaturePermanentAmount extends TargetAmount {
@Override
public boolean canTarget(UUID id, Game game) {
Permanent permanent = game.getPermanent(id);
if (permanent != null) {
return filter.match(permanent, game);
}
return false;
return permanent != null && filter.match(permanent, game);
}
@Override
@ -139,7 +136,7 @@ public class TargetCreaturePermanentAmount extends TargetAmount {
@Override
public Set<UUID> possibleTargets(UUID sourceId, UUID sourceControllerId, Game game) {
Set<UUID> possibleTargets = new HashSet<UUID>();
Set<UUID> possibleTargets = new HashSet<>();
MageObject targetSource = game.getObject(sourceId);
for (Permanent permanent : game.getBattlefield().getActivePermanents(new FilterCreaturePermanent(), sourceControllerId, game)) {
if (permanent.canBeTargetedBy(targetSource, sourceControllerId, game) && filter.match(permanent, sourceId, sourceControllerId, game)) {
@ -151,7 +148,7 @@ public class TargetCreaturePermanentAmount extends TargetAmount {
@Override
public Set<UUID> possibleTargets(UUID sourceControllerId, Game game) {
Set<UUID> possibleTargets = new HashSet<UUID>();
Set<UUID> possibleTargets = new HashSet<>();
for (Permanent permanent : game.getBattlefield().getActivePermanents(new FilterCreaturePermanent(), sourceControllerId, game)) {
if (filter.match(permanent, null, sourceControllerId, game)) {
possibleTargets.add(permanent.getId());

View file

@ -49,8 +49,8 @@ import java.util.UUID;
*/
public class TargetDefender extends TargetImpl {
protected FilterPlaneswalkerOrPlayer filter;
protected UUID attackerId;
protected final FilterPlaneswalkerOrPlayer filter;
protected final UUID attackerId;
public TargetDefender(Set<UUID> defenders, UUID attackerId) {
this(1, 1, defenders, attackerId);
@ -187,10 +187,7 @@ public class TargetDefender extends TargetImpl {
return filter.match(player, game);
}
Permanent permanent = game.getPermanent(id);
if (permanent != null) {
return filter.match(permanent, game);
}
return false;
return permanent != null && filter.match(permanent, game);
}
@Override

View file

@ -44,7 +44,7 @@ import mage.filter.predicate.other.OwnerIdPredicate;
*/
public class TargetDiscard extends TargetCard {
private UUID playerId;
private final UUID playerId;
public TargetDiscard(UUID playerId) {
this(1, 1, new FilterCard(), playerId);
@ -73,10 +73,7 @@ public class TargetDiscard extends TargetCard {
@Override
public boolean canTarget(UUID id, Ability source, Game game) {
Card card = game.getPlayer(playerId).getHand().get(id, game);
if (card != null) {
return filter.match(card, source.getControllerId(), game);
}
return false;
return card != null && filter.match(card, source.getControllerId(), game);
}
@Override

View file

@ -40,10 +40,7 @@ public class TargetOpponentsChoicePermanent extends TargetPermanent {
@Override
public boolean canTarget(UUID controllerId, UUID id, UUID sourceId, Game game, boolean flag) {
if (opponentId != null) {
return super.canTarget(opponentId, id, sourceId, game, flag);
}
return false;
return opponentId != null && super.canTarget(opponentId, id, sourceId, game, flag);
}
@Override

View file

@ -97,10 +97,7 @@ public class TargetPermanentOrPlayer extends TargetImpl {
return filter.match(permanent, game);
}
Player player = game.getPlayer(id);
if (player != null) {
return filter.match(player, game);
}
return false;
return player != null && filter.match(player, game);
}
@Override
@ -118,7 +115,7 @@ public class TargetPermanentOrPlayer extends TargetImpl {
if (permanent != null) {
if (!isNotTarget()) {
if (!permanent.canBeTargetedBy(game.getObject(source.getId()), source.getControllerId(), game) ||
!permanent.canBeTargetedBy(game.getObject(source.getSourceId()), source.getControllerId(), game)) {
!permanent.canBeTargetedBy(game.getObject(source.getSourceId()), source.getControllerId(), game)) {
return false;
}
}
@ -137,10 +134,7 @@ public class TargetPermanentOrPlayer extends TargetImpl {
if (permanent != null) {
return filter.match(permanent, game);
}
if (player != null) {
return filter.match(player, game);
}
return false;
return player != null && filter.match(player, game);
}
/**

View file

@ -43,7 +43,7 @@ import mage.filter.predicate.permanent.CounterPredicate;
*/
public class TargetPermanentOrPlayerWithCounter extends TargetPermanentOrPlayer {
protected FilterPermanentOrPlayerWithCounter filter;
protected final FilterPermanentOrPlayerWithCounter filter;
public TargetPermanentOrPlayerWithCounter() {
this(1, 1);

View file

@ -48,7 +48,7 @@ import mage.target.TargetImpl;
*/
public class TargetPermanentOrSuspendedCard extends TargetImpl {
protected FilterPermanentOrSuspendedCard filter;
protected final FilterPermanentOrSuspendedCard filter;
public TargetPermanentOrSuspendedCard() {
this(new FilterPermanentOrSuspendedCard(), false);
@ -118,10 +118,7 @@ public class TargetPermanentOrSuspendedCard extends TargetImpl {
return filter.match(permanent, game);
}
Card card = game.getExile().getCard(id, game);
if (card != null) {
return filter.match(card, game);
}
return false;
return card != null && filter.match(card, game);
}
@Override
@ -137,10 +134,7 @@ public class TargetPermanentOrSuspendedCard extends TargetImpl {
}
}
Card card = game.getExile().getCard(id, game);
if (card != null) {
return filter.match(card, game);
}
return false;
return card != null && filter.match(card, game);
}
@Override

View file

@ -105,10 +105,7 @@ public class TargetSpellOrPermanent extends TargetImpl {
return filter.match(permanent, game);
}
Spell spell = game.getStack().getSpell(id);
if (spell != null) {
return filter.match(spell, game);
}
return false;
return spell != null && filter.match(spell, game);
}
@Override
@ -124,11 +121,8 @@ public class TargetSpellOrPermanent extends TargetImpl {
}
}
Spell spell = game.getStack().getSpell(id);
if (spell != null
&& !source.getSourceId().equals(id)) { // 114.4. A spell or ability on the stack is an illegal target for itself.
return filter.match(spell, game);
}
return false;
// 114.4. A spell or ability on the stack is an illegal target for itself.
return spell != null && !source.getSourceId().equals(id) && filter.match(spell, game);
}
@Override

View file

@ -11,7 +11,7 @@ import mage.game.Game;
public class SecondTargetPointer implements TargetPointer {
private Map<UUID, Integer> zoneChangeCounter = new HashMap<UUID, Integer>();
private Map<UUID, Integer> zoneChangeCounter = new HashMap<>();
public static SecondTargetPointer getInstance() {
return new SecondTargetPointer();
@ -41,7 +41,7 @@ public class SecondTargetPointer implements TargetPointer {
@Override
public List<UUID> getTargets(Game game, Ability source) {
ArrayList<UUID> target = new ArrayList<UUID>();
ArrayList<UUID> target = new ArrayList<>();
if (source.getTargets().size() > 1) {
for (UUID targetId : source.getTargets().get(1).getTargets()) {
Card card = game.getCard(targetId);