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

@ -206,26 +206,20 @@ public class ConditionalMana extends Mana implements Serializable {
break; break;
case BLUE: case BLUE:
blue += amount; blue += amount;
;
break; break;
case GREEN: case GREEN:
green += amount; green += amount;
;
break; break;
case RED: case RED:
red += amount; red += amount;
;
break; break;
case WHITE: case WHITE:
white += amount; white += amount;
;
break; break;
case COLORLESS: case COLORLESS:
colorless += amount; colorless += amount;
;
case GENERIC: case GENERIC:
generic += amount; generic += amount;
;
break; break;
} }
} }

View file

@ -32,7 +32,7 @@ import mage.util.Copyable;
public class MageInt implements Serializable, Copyable<MageInt> { public class MageInt implements Serializable, Copyable<MageInt> {
public static MageInt EmptyMageInt = new MageInt(Integer.MIN_VALUE, "") { public static final MageInt EmptyMageInt = new MageInt(Integer.MIN_VALUE, "") {
private static final String exceptionMessage = "MageInt.EmptyMageInt can't be modified."; private static final String exceptionMessage = "MageInt.EmptyMageInt can't be modified.";

View file

@ -74,7 +74,7 @@ public class Library implements Serializable {
public void shuffle() { public void shuffle() {
UUID[] shuffled = library.toArray(new UUID[0]); UUID[] shuffled = library.toArray(new UUID[0]);
for (int n = shuffled.length - 1; n > 0; n--) { for (int n = shuffled.length - 1; n > 0; n--) {
int r = RandomUtil.nextInt(n);; int r = RandomUtil.nextInt(n);
UUID temp = shuffled[n]; UUID temp = shuffled[n];
shuffled[n] = shuffled[r]; shuffled[n] = shuffled[r];
shuffled[r] = temp; shuffled[r] = temp;

View file

@ -303,23 +303,18 @@ public class ManaPoolItem implements Serializable {
break; break;
case BLUE: case BLUE:
blue += amount; blue += amount;
;
break; break;
case GREEN: case GREEN:
green += amount; green += amount;
;
break; break;
case RED: case RED:
red += amount; red += amount;
;
break; break;
case WHITE: case WHITE:
white += amount; white += amount;
;
break; break;
case COLORLESS: case COLORLESS:
colorless += amount; colorless += amount;
;
break; break;
} }
} }

View file

@ -141,7 +141,7 @@ public abstract class PlayerImpl implements Player, Serializable {
private static final Logger logger = Logger.getLogger(PlayerImpl.class); private static final Logger logger = Logger.getLogger(PlayerImpl.class);
private static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); private static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
/** /**
* Used to cancel waiting requests send to the player * Used to cancel waiting requests send to the player
@ -987,7 +987,7 @@ public abstract class PlayerImpl implements Player, Serializable {
} else { } else {
result = cast(card.getSpellAbility(), game, noMana); result = cast(card.getSpellAbility(), game, noMana);
} }
if (result == false) { if (!result) {
game.informPlayer(this, "You can't play " + card.getIdName() + "."); game.informPlayer(this, "You can't play " + card.getIdName() + ".");
} }
return result; return result;
@ -2026,7 +2026,7 @@ public abstract class PlayerImpl implements Player, Serializable {
quit = true; quit = true;
idleTimeout = true; idleTimeout = true;
this.concede(game); this.concede(game);
game.informPlayers(new StringBuilder(getLogName()).append(" was idle for too long, losing the Match.").toString()); game.informPlayers(getLogName() + " was idle for too long, losing the Match.");
} }
@Override @Override
@ -2241,11 +2241,7 @@ public abstract class PlayerImpl implements Player, Serializable {
@Override @Override
public boolean hasWon() { public boolean hasWon() {
if (!this.loses) { return !this.loses && this.wins;
return this.wins;
} else {
return false;
}
} }
@Override @Override
@ -2364,20 +2360,14 @@ public abstract class PlayerImpl implements Player, Serializable {
public List<Permanent> getAvailableAttackers(UUID defenderId, Game game) { public List<Permanent> getAvailableAttackers(UUID defenderId, Game game) {
FilterCreatureForCombat filter = new FilterCreatureForCombat(); FilterCreatureForCombat filter = new FilterCreatureForCombat();
List<Permanent> attackers = game.getBattlefield().getAllActivePermanents(filter, playerId, game); List<Permanent> attackers = game.getBattlefield().getAllActivePermanents(filter, playerId, game);
for (Iterator<Permanent> i = attackers.iterator(); i.hasNext();) { attackers.removeIf(entry -> !entry.canAttack(defenderId, game));
Permanent entry = i.next();
if (!entry.canAttack(defenderId, game)) {
i.remove();
}
}
return attackers; return attackers;
} }
@Override @Override
public List<Permanent> getAvailableBlockers(Game game) { public List<Permanent> getAvailableBlockers(Game game) {
FilterCreatureForCombatBlock blockFilter = new FilterCreatureForCombatBlock(); FilterCreatureForCombatBlock blockFilter = new FilterCreatureForCombatBlock();
List<Permanent> blockers = game.getBattlefield().getAllActivePermanents(blockFilter, playerId, game); return game.getBattlefield().getAllActivePermanents(blockFilter, playerId, game);
return blockers;
} }
@Override @Override
@ -2766,8 +2756,7 @@ public abstract class PlayerImpl implements Player, Serializable {
// activated abilities from stack objects // activated abilities from stack objects
for (StackObject stackObject : game.getState().getStack()) { for (StackObject stackObject : game.getState().getStack()) {
for (ActivatedAbility ability : stackObject.getAbilities().getActivatedAbilities(Zone.STACK)) { for (ActivatedAbility ability : stackObject.getAbilities().getActivatedAbilities(Zone.STACK)) {
if (ability instanceof ActivatedAbility if (ability != null && canPlay(ability, availableMana, game.getObject(ability.getSourceId()), game)) {
&& canPlay(ability, availableMana, game.getObject(ability.getSourceId()), game)) {
playableActivated.put(ability.toString(), ability); playableActivated.put(ability.toString(), ability);
} }
@ -2776,9 +2765,7 @@ public abstract class PlayerImpl implements Player, Serializable {
// activated abilities from objects in the command zone (emblems or commanders) // activated abilities from objects in the command zone (emblems or commanders)
for (CommandObject commandObject : game.getState().getCommand()) { for (CommandObject commandObject : game.getState().getCommand()) {
for (ActivatedAbility ability : commandObject.getAbilities().getActivatedAbilities(Zone.COMMAND)) { for (ActivatedAbility ability : commandObject.getAbilities().getActivatedAbilities(Zone.COMMAND)) {
if (ability.getControllerId().equals(getId()) if (ability.getControllerId().equals(getId()) && canPlay(ability, availableMana, game.getObject(ability.getSourceId()), game)) {
&& ability instanceof ActivatedAbility
&& canPlay(ability, availableMana, game.getObject(ability.getSourceId()), game)) {
playableActivated.put(ability.toString(), ability); playableActivated.put(ability.toString(), ability);
} }

View file

@ -192,7 +192,7 @@ public class UserData implements Serializable {
} }
public String getHistory() { public String getHistory() {
if (UserGroup.COMPUTER.equals(this.groupId)) { if (UserGroup.COMPUTER.equals(this.groupId)) { // Why we are checking UserGroup and integer equality??
return ""; return "";
} }
// todo: add preference to hide rating? // todo: add preference to hide rating?

View file

@ -35,8 +35,8 @@ import java.io.Serializable;
*/ */
public class UserSkipPrioritySteps implements Serializable { public class UserSkipPrioritySteps implements Serializable {
SkipPrioritySteps yourTurn; final SkipPrioritySteps yourTurn;
SkipPrioritySteps opponentTurn; final SkipPrioritySteps opponentTurn;
boolean stopOnDeclareAttackersDuringSkipAction; boolean stopOnDeclareAttackersDuringSkipAction;
boolean stopOnDeclareBlockerIfNoneAvailable; boolean stopOnDeclareBlockerIfNoneAvailable;

View file

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

View file

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

View file

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

View file

@ -78,10 +78,7 @@ public abstract class TargetObject extends TargetImpl {
@Override @Override
public boolean canTarget(UUID id, Game game) { public boolean canTarget(UUID id, Game game) {
MageObject object = game.getObject(id); MageObject object = game.getObject(id);
if (object != null && game.getState().getZone(id).match(zone)) { return object != null && game.getState().getZone(id).match(zone) && getFilter().match(object, game);
return getFilter().match(object, game);
}
return false;
} }
@Override @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) { public boolean canTarget(UUID controllerId, UUID id, UUID sourceId, Game game, boolean flag) {
Permanent permanent = game.getPermanent(id); Permanent permanent = game.getPermanent(id);
if (permanent != null) { return permanent != null && filter.match(permanent, sourceId, controllerId, game);
return filter.match(permanent, sourceId, controllerId, game);
}
return false;
} }
@Override @Override

View file

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

View file

@ -47,7 +47,7 @@ import mage.players.Player;
*/ */
public class TargetSource extends TargetObject { public class TargetSource extends TargetObject {
protected FilterObject filter; protected final FilterObject filter;
public TargetSource() { public TargetSource() {
this(1, 1, new FilterObject("source of your choice")); 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 { public class TargetSpell extends TargetObject {
protected FilterSpell filter; protected final FilterSpell filter;
public TargetSpell() { public TargetSpell() {
this(1, 1, new FilterSpell()); this(1, 1, new FilterSpell());
@ -82,10 +82,7 @@ public class TargetSpell extends TargetObject {
return false; return false;
} }
Spell spell = game.getStack().getSpell(id); Spell spell = game.getStack().getSpell(id);
if (spell != null) { return spell != null && filter.match(spell, source.getSourceId(), source.getControllerId(), game);
return filter.match(spell, source.getSourceId(), source.getControllerId(), game);
}
return false;
} }
@Override @Override

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -64,12 +64,9 @@ public class Copier<T> {
ObjectInputStream in = new CopierObjectInputStream(loader, fbos.getInputStream()); ObjectInputStream in = new CopierObjectInputStream(loader, fbos.getInputStream());
copy = (T) in.readObject(); copy = (T) in.readObject();
} }
catch(IOException e) { catch(IOException | ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return copy; return copy;
} }
@ -99,12 +96,9 @@ public class Copier<T> {
try (ObjectInputStream in = new CopierObjectInputStream(loader, new GZIPInputStream(new ByteArrayInputStream(buffer)))) { try (ObjectInputStream in = new CopierObjectInputStream(loader, new GZIPInputStream(new ByteArrayInputStream(buffer)))) {
copy = (T) in.readObject(); copy = (T) in.readObject();
} }
catch(IOException e) { catch(IOException | ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} }
catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
return copy; return copy;
} }
} }

View file

@ -41,7 +41,7 @@ import java.io.StreamCorruptedException;
public class CopierObjectInputStream extends ObjectInputStream { public class CopierObjectInputStream extends ObjectInputStream {
ClassLoader myLoader = null; ClassLoader myLoader = null;
public CopierObjectInputStream(ClassLoader newLoader, InputStream theStream) throws IOException, StreamCorruptedException { public CopierObjectInputStream(ClassLoader newLoader, InputStream theStream) throws IOException {
super(theStream); super(theStream);
myLoader = newLoader; myLoader = newLoader;
} }

View file

@ -10,7 +10,6 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Random;
import java.util.Set; import java.util.Set;
import mage.cards.Card; import mage.cards.Card;

View file

@ -83,9 +83,7 @@ public class CopyTokenFunction implements Function<Token, Card> {
} else { } else {
target.setOriginalExpansionSetCode(source.getExpansionSetCode()); target.setOriginalExpansionSetCode(source.getExpansionSetCode());
target.setOriginalCardNumber(source.getCardNumber()); target.setOriginalCardNumber(source.getCardNumber());
if (source instanceof Card) { target.setCopySourceCard(source);
target.setCopySourceCard(source);
}
} }
target.setName(sourceObj.getName()); target.setName(sourceObj.getName());

View file

@ -45,7 +45,7 @@ public abstract class Watcher implements Serializable {
protected UUID controllerId; protected UUID controllerId;
protected UUID sourceId; protected UUID sourceId;
protected boolean condition; protected boolean condition;
protected WatcherScope scope; protected final WatcherScope scope;
public Watcher(String basicKey, WatcherScope scope) { public Watcher(String basicKey, WatcherScope scope) {
this.basicKey = basicKey; this.basicKey = basicKey;

View file

@ -43,9 +43,7 @@ public class Watchers extends HashMap<String, Watcher> {
} }
public Watchers(final Watchers watchers) { public Watchers(final Watchers watchers) {
watchers.entrySet().stream().forEach((entry) -> { watchers.entrySet().stream().forEach((entry) -> this.put(entry.getKey(), entry.getValue().copy()));
this.put(entry.getKey(), entry.getValue().copy());
});
} }
public Watchers copy() { public Watchers copy() {
@ -59,16 +57,13 @@ public class Watchers extends HashMap<String, Watcher> {
} }
public void watch(GameEvent event, Game game) { public void watch(GameEvent event, Game game) {
for (Iterator<Watcher> it = this.values().iterator(); it.hasNext();) { for (Watcher watcher : this.values()) {
Watcher watcher = it.next();
watcher.watch(event, game); watcher.watch(event, game);
} }
} }
public void reset() { public void reset() {
this.values().stream().forEach((watcher) -> { this.values().stream().forEach(Watcher::reset);
watcher.reset();
});
} }
public Watcher get(String key, UUID id) { public Watcher get(String key, UUID id) {

View file

@ -20,7 +20,7 @@ import mage.watchers.Watcher;
public class AttackedThisCombatWatcher extends Watcher { public class AttackedThisCombatWatcher extends Watcher {
public Set<MageObjectReference> attackedThisTurnCreatures = new HashSet<>(); public final Set<MageObjectReference> attackedThisTurnCreatures = new HashSet<>();
public AttackedThisCombatWatcher() { public AttackedThisCombatWatcher() {
super("AttackedThisCombat", WatcherScope.GAME); super("AttackedThisCombat", WatcherScope.GAME);

View file

@ -41,7 +41,7 @@ import mage.watchers.Watcher;
public class AttackedThisTurnWatcher extends Watcher { public class AttackedThisTurnWatcher extends Watcher {
// TODO: use MageObjectReference instead of UUID // TODO: use MageObjectReference instead of UUID
public Set<UUID> attackedThisTurnCreatures = new HashSet<>(); public final Set<UUID> attackedThisTurnCreatures = new HashSet<>();
public AttackedThisTurnWatcher() { public AttackedThisTurnWatcher() {
super("AttackedThisTurn", WatcherScope.GAME); super("AttackedThisTurn", WatcherScope.GAME);

View file

@ -44,7 +44,7 @@ import mage.watchers.Watcher;
*/ */
public class BlockedAttackerWatcher extends Watcher { public class BlockedAttackerWatcher extends Watcher {
public HashMap<MageObjectReference, Set<MageObjectReference>> blockData = new HashMap<>(); public final HashMap<MageObjectReference, Set<MageObjectReference>> blockData = new HashMap<>();
public BlockedAttackerWatcher() { public BlockedAttackerWatcher() {
super("BlockedAttackerWatcher", WatcherScope.GAME); super("BlockedAttackerWatcher", WatcherScope.GAME);

View file

@ -51,7 +51,7 @@ public class BloodthirstWatcher extends Watcher {
@Override @Override
public void watch(GameEvent event, Game game) { public void watch(GameEvent event, Game game) {
if (condition == true) { //no need to check - condition has already occured if (condition) { //no need to check - condition has already occured
return; return;
} }
if (event.getType() == GameEvent.EventType.DAMAGED_PLAYER) { if (event.getType() == GameEvent.EventType.DAMAGED_PLAYER) {

View file

@ -79,10 +79,7 @@ public class CastFromGraveyardWatcher extends Watcher {
public boolean spellWasCastFromGraveyard(UUID sourceId, int zcc) { public boolean spellWasCastFromGraveyard(UUID sourceId, int zcc) {
Set zccSet = spellsCastFromGraveyard.get(sourceId); Set zccSet = spellsCastFromGraveyard.get(sourceId);
if (zccSet != null) { return zccSet != null && zccSet.contains(zcc);
return zccSet.contains(zcc);
}
return false;
} }

View file

@ -51,7 +51,7 @@ import mage.watchers.Watcher;
public class CommanderInfoWatcher extends Watcher { public class CommanderInfoWatcher extends Watcher {
public final Map<UUID, Integer> damageToPlayer = new HashMap<>(); public final Map<UUID, Integer> damageToPlayer = new HashMap<>();
public boolean checkCommanderDamage; public final boolean checkCommanderDamage;
public CommanderInfoWatcher(UUID commander, boolean checkCommanderDamage) { public CommanderInfoWatcher(UUID commander, boolean checkCommanderDamage) {
super("CommanderCombatDamageWatcher", WatcherScope.CARD); super("CommanderCombatDamageWatcher", WatcherScope.CARD);

View file

@ -45,7 +45,7 @@ import mage.watchers.Watcher;
*/ */
public class DamagedByWatcher extends Watcher { public class DamagedByWatcher extends Watcher {
public Set<MageObjectReference> damagedBySource = new HashSet<>(); public final Set<MageObjectReference> damagedBySource = new HashSet<>();
private final boolean watchPlaneswalkers; private final boolean watchPlaneswalkers;

View file

@ -16,7 +16,7 @@ import mage.watchers.Watcher;
*/ */
public class LandfallWatcher extends Watcher { public class LandfallWatcher extends Watcher {
Set<UUID> playerPlayedLand = new HashSet<>(); final Set<UUID> playerPlayedLand = new HashSet<>();
public LandfallWatcher() { public LandfallWatcher() {
super("LandPlayed", WatcherScope.GAME); super("LandPlayed", WatcherScope.GAME);

View file

@ -53,7 +53,7 @@ public class MorbidWatcher extends Watcher {
@Override @Override
public void watch(GameEvent event, Game game) { public void watch(GameEvent event, Game game) {
if (condition == true) { if (condition) {
return; return;
} }
if (event.getType() == GameEvent.EventType.ZONE_CHANGE && ((ZoneChangeEvent)event).isDiesEvent()) { if (event.getType() == GameEvent.EventType.ZONE_CHANGE && ((ZoneChangeEvent)event).isDiesEvent()) {

View file

@ -43,7 +43,7 @@ import mage.watchers.Watcher;
*/ */
public class PlayerCastCreatureWatcher extends Watcher { public class PlayerCastCreatureWatcher extends Watcher {
Set<UUID> playerIds = new HashSet<>(); final Set<UUID> playerIds = new HashSet<>();
public PlayerCastCreatureWatcher() { public PlayerCastCreatureWatcher() {
super("PlayerCastCreature", WatcherScope.GAME); super("PlayerCastCreature", WatcherScope.GAME);

View file

@ -104,10 +104,7 @@ public class ProwlWatcher extends Watcher {
return true; return true;
} }
Set<String> subtypes = damagingSubtypes.get(playerId); Set<String> subtypes = damagingSubtypes.get(playerId);
if (subtypes != null) { return subtypes != null && subtypes.contains(subtype);
return subtypes.contains(subtype);
}
return false;
} }
} }

View file

@ -44,7 +44,7 @@ import java.util.UUID;
*/ */
public class SourceDidDamageWatcher extends Watcher { public class SourceDidDamageWatcher extends Watcher {
public List<UUID> damageSources = new ArrayList<>(); public final List<UUID> damageSources = new ArrayList<>();
public SourceDidDamageWatcher() { public SourceDidDamageWatcher() {
super("SourceDidDamageWatcher", WatcherScope.GAME); super("SourceDidDamageWatcher", WatcherScope.GAME);