refactor: some code linting (#10624)

* remove obsolete check method
* code clean: PermanentImpl
* code clean: PlayerImpl
* protected constructors in CardImpl
This commit is contained in:
xenohedron 2023-07-15 17:50:24 -04:00 committed by GitHub
parent b960b77774
commit 8372c358b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 155 additions and 210 deletions

View file

@ -111,23 +111,23 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
protected int createOrder;
protected boolean legendRuleApplies = true;
private static final List<UUID> emptyList = Collections.unmodifiableList(new ArrayList<UUID>());
private static final List<UUID> emptyList = Collections.unmodifiableList(new ArrayList<>());
public PermanentImpl(UUID ownerId, UUID controllerId, String name) {
protected PermanentImpl(UUID ownerId, UUID controllerId, String name) {
super(ownerId, name);
this.originalControllerId = controllerId;
this.controllerId = controllerId;
this.counters = new Counters();
}
public PermanentImpl(UUID id, UUID ownerId, UUID controllerId, String name) {
protected PermanentImpl(UUID id, UUID ownerId, UUID controllerId, String name) {
super(id, ownerId, name);
this.originalControllerId = controllerId;
this.controllerId = controllerId;
this.counters = new Counters();
}
public PermanentImpl(final PermanentImpl permanent) {
protected PermanentImpl(final PermanentImpl permanent) {
super(permanent);
this.tapped = permanent.tapped;
this.flipped = permanent.flipped;
@ -146,7 +146,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
this.markedLifelink = permanent.markedLifelink;
for (Map.Entry<String, List<UUID>> entry : permanent.connectedCards.entrySet()) {
this.connectedCards.put(entry.getKey(), entry.getValue());
this.connectedCards.put(entry.getKey(), new ArrayList<>(entry.getValue()));
}
if (permanent.dealtDamageByThisTurn != null) {
dealtDamageByThisTurn = new HashSet<>(permanent.dealtDamageByThisTurn);
@ -544,12 +544,10 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
@Override
public boolean untap(Game game) {
//20091005 - 701.15b
if (tapped) {
if (!replaceEvent(EventType.UNTAP, game)) {
this.tapped = false;
fireEvent(EventType.UNTAPPED, game);
return true;
}
if (tapped && !replaceEvent(EventType.UNTAP, game)) {
this.tapped = false;
fireEvent(EventType.UNTAPPED, game);
return true;
}
return false;
}
@ -562,12 +560,10 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
@Override
public boolean tap(boolean forCombat, Ability source, Game game) {
//20091005 - 701.15a
if (!tapped) {
if (!replaceEvent(EventType.TAP, game)) {
this.tapped = true;
game.fireEvent(new GameEvent(GameEvent.EventType.TAPPED, objectId, source, controllerId, 0, forCombat));
return true;
}
if (!tapped && !replaceEvent(EventType.TAP, game)) {
this.tapped = true;
game.fireEvent(new GameEvent(GameEvent.EventType.TAPPED, objectId, source, controllerId, 0, forCombat));
return true;
}
return false;
}
@ -589,12 +585,10 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
@Override
public boolean flip(Game game) {
if (!flipped) {
if (!replaceEvent(EventType.FLIP, game)) {
this.flipped = true;
fireEvent(EventType.FLIPPED, game);
return true;
}
if (!flipped && !replaceEvent(EventType.FLIP, game)) {
this.flipped = true;
fireEvent(EventType.FLIPPED, game);
return true;
}
return false;
}
@ -668,23 +662,18 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
@Override
public boolean phaseIn(Game game, boolean onlyDirect) {
if (!phasedIn) {
if (!replaceEvent(EventType.PHASE_IN, game)
&& (!onlyDirect || !indirectPhase)) {
this.phasedIn = true;
this.indirectPhase = false;
if (!game.isSimulation()) {
game.informPlayers(getLogName() + " phased in");
if (!phasedIn && !replaceEvent(EventType.PHASE_IN, game) && (!onlyDirect || !indirectPhase)) {
this.phasedIn = true;
this.indirectPhase = false;
game.informPlayers(getLogName() + " phased in");
for (UUID attachedId : this.getAttachments()) {
Permanent attachedPerm = game.getPermanent(attachedId);
if (attachedPerm != null) {
attachedPerm.phaseIn(game, false);
}
for (UUID attachedId : this.getAttachments()) {
Permanent attachedPerm = game.getPermanent(attachedId);
if (attachedPerm != null) {
attachedPerm.phaseIn(game, false);
}
}
fireEvent(EventType.PHASED_IN, game);
return true;
}
fireEvent(EventType.PHASED_IN, game);
return true;
}
return false;
}
@ -696,23 +685,19 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
@Override
public boolean phaseOut(Game game, boolean indirectPhase) {
if (phasedIn) {
if (!replaceEvent(EventType.PHASE_OUT, game)) {
for (UUID attachedId : this.getAttachments()) {
Permanent attachedPerm = game.getPermanent(attachedId);
if (attachedPerm != null) {
attachedPerm.phaseOut(game, true);
}
if (phasedIn && !replaceEvent(EventType.PHASE_OUT, game)) {
for (UUID attachedId : this.getAttachments()) {
Permanent attachedPerm = game.getPermanent(attachedId);
if (attachedPerm != null) {
attachedPerm.phaseOut(game, true);
}
this.removeFromCombat(game);
this.phasedIn = false;
this.indirectPhase = indirectPhase;
if (!game.isSimulation()) {
game.informPlayers(getLogName() + " phased out");
}
fireEvent(EventType.PHASED_OUT, game);
return true;
}
this.removeFromCombat(game);
this.phasedIn = false;
this.indirectPhase = indirectPhase;
game.informPlayers(getLogName() + " phased out");
fireEvent(EventType.PHASED_OUT, game);
return true;
}
return false;
}
@ -804,13 +789,11 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
}
// Losing control of a ring bearer clear its status.
public void removeUncontrolledRingBearer(Game game){
if(isRingBearer()) {
UUID controllerId = beforeResetControllerId;
Player controller = controllerId == null ? null : game.getPlayer(controllerId);
public void removeUncontrolledRingBearer(Game game) {
if (isRingBearer()) {
Player controller = beforeResetControllerId == null ? null : game.getPlayer(beforeResetControllerId);
String controllerName = controller == null ? "" : controller.getLogName();
game.informPlayers(controllerName + " has lost control of " + getLogName() + ". It is no longer a Ring-bearer.");
this.setRingBearer(game, false);
}
}
@ -824,7 +807,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
this.getAbilities(game).setControllerId(controllerId);
game.getContinuousEffects().setController(objectId, controllerId);
// the controller of triggered abilites is always set/checked before the abilities triggers so not needed here
// the controller of triggered abilities is always set/checked before the abilities triggers so not needed here
game.fireEvent(new GameEvent(GameEvent.EventType.LOST_CONTROL, objectId, null, beforeResetControllerId));
game.fireEvent(new GameEvent(GameEvent.EventType.GAINED_CONTROL, objectId, null, controllerId));
@ -898,8 +881,8 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
this.attachedTo = attachToObjectId;
this.attachedToZoneChangeCounter = game.getState().getZoneChangeCounter(attachToObjectId);
for (Ability ability : this.getAbilities()) {
for (Iterator<Effect> ite = ability.getEffects(game, EffectType.CONTINUOUS).iterator(); ite.hasNext(); ) {
ContinuousEffect effect = (ContinuousEffect) ite.next();
for (Effect value : ability.getEffects(game, EffectType.CONTINUOUS)) {
ContinuousEffect effect = (ContinuousEffect) value;
game.getContinuousEffects().setOrder(effect);
// It's important to update the timestamp of the copied effect in ContinuousEffects because it does the action
for (ContinuousEffect conEffect : game.getContinuousEffects().getLayeredEffects(game)) {
@ -914,7 +897,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
this.addInfo("attachedTo", null, game);
if (this.attachedTo != null) {
Permanent attachedToPerm = game.getPermanent(this.getAttachedTo());
// If what this permanent is attached to isn't also a permenent, such as a
// If what this permanent is attached to isn't also a permanent, such as a
// player or card in graveyard, it is important to mention what it is attached
// to in the tooltip. The rules let you attach a permanent to any kind of object
// or player, although currently the only objects it's possible to attach a
@ -978,8 +961,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
* @return
*/
private int doDamage(int damageAmount, UUID attackerId, Ability source, Game game, boolean preventable, boolean combat, boolean markDamage, List<UUID> appliedEffects) {
int damageDone = 0;
if (damageAmount < 1 || !canDamage(game.getObject(attackerId), game)) {
if (damageAmount < 1) {
return 0;
}
DamageEvent event = new DamagePermanentEvent(objectId, attackerId, controllerId, damageAmount, preventable, combat);
@ -987,8 +969,8 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
if (game.replaceEvent(event)) {
return 0;
}
int actualDamage = checkProtectionAbilities(event, attackerId, source, game);
if (actualDamage < 1) {
int actualDamageDone = checkProtectionAbilities(event, attackerId, source, game);
if (actualDamageDone < 1) {
return 0;
}
int lethal = getLethalDamage(attackerId, game);
@ -997,22 +979,22 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
if (checkWither(event, attacker, game)) {
if (markDamage) {
// mark damage only
markDamage(CounterType.M1M1.createInstance(actualDamage), attacker, true);
markDamage(CounterType.M1M1.createInstance(actualDamageDone), attacker, true);
} else {
Ability damageSourceAbility = null;
if (attacker instanceof Permanent) {
damageSourceAbility = ((Permanent) attacker).getSpellAbility();
}
// deal damage immediately
addCounters(CounterType.M1M1.createInstance(actualDamage), game.getControllerId(attackerId), damageSourceAbility, game);
addCounters(CounterType.M1M1.createInstance(actualDamageDone), game.getControllerId(attackerId), damageSourceAbility, game);
}
} else {
this.damage = CardUtil.overflowInc(this.damage, actualDamage);
this.damage = CardUtil.overflowInc(this.damage, actualDamageDone);
}
}
if (this.isPlaneswalker(game)) {
int loyalty = getCounters(game).getCount(CounterType.LOYALTY);
int countersToRemove = Math.min(actualDamage, loyalty);
int countersToRemove = Math.min(actualDamageDone, loyalty);
if (attacker != null && markDamage) {
markDamage(CounterType.LOYALTY.createInstance(countersToRemove), attacker, false);
} else {
@ -1021,21 +1003,17 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
}
if (this.isBattle(game)) {
int defense = getCounters(game).getCount(CounterType.DEFENSE);
int countersToRemove = Math.min(actualDamage, defense);
int countersToRemove = Math.min(actualDamageDone, defense);
if (attacker != null && markDamage) {
markDamage(CounterType.DEFENSE.createInstance(countersToRemove), attacker, false);
} else {
removeCounters(CounterType.DEFENSE.getName(), countersToRemove, source, game);
}
}
DamagedEvent damagedEvent = new DamagedPermanentEvent(this.getId(), attackerId, this.getControllerId(), actualDamage, combat);
damagedEvent.setExcess(actualDamage - lethal);
DamagedEvent damagedEvent = new DamagedPermanentEvent(this.getId(), attackerId, this.getControllerId(), actualDamageDone, combat);
damagedEvent.setExcess(actualDamageDone - lethal);
game.fireEvent(damagedEvent);
game.getState().addSimultaneousDamage(damagedEvent, game);
damageDone = actualDamage;
if (damageDone < 1) {
return 0;
}
UUID sourceControllerId = null;
Abilities sourceAbilities = null;
attacker = game.getPermanentOrLKIBattlefield(attackerId);
@ -1065,10 +1043,10 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
if (attacker != null && sourceAbilities != null) {
if (sourceAbilities.containsKey(LifelinkAbility.getInstance().getId())) {
if (markDamage) {
game.getPermanent(attackerId).markLifelink(damageDone);
game.getPermanent(attackerId).markLifelink(actualDamageDone);
} else {
Player player = game.getPlayer(sourceControllerId);
player.gainLife(damageDone, game, source);
player.gainLife(actualDamageDone, game, source);
}
}
if (sourceAbilities.containsKey(DeathtouchAbility.getInstance().getId())) {
@ -1080,16 +1058,16 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
// Unstable ability - Earl of Squirrel
if (sourceAbilities.containsKey(SquirrellinkAbility.getInstance().getId())) {
Player player = game.getPlayer(sourceControllerId);
new SquirrelToken().putOntoBattlefield(damageDone, game, source, player.getId());
new SquirrelToken().putOntoBattlefield(actualDamageDone, game, source, player.getId());
}
dealtDamageByThisTurn.add(new MageObjectReference(attacker, game));
}
if (attacker == null) {
game.informPlayers(getLogName() + " gets " + damageDone + " damage");
game.informPlayers(getLogName() + " gets " + actualDamageDone + " damage");
} else {
game.informPlayers(attacker.getLogName() + " deals " + damageDone + " damage to " + getLogName());
game.informPlayers(attacker.getLogName() + " deals " + actualDamageDone + " damage to " + getLogName());
}
return damageDone;
return actualDamageDone;
}
private static boolean checkWither(DamageEvent event, MageObject attacker, Game game) {
@ -1132,7 +1110,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
for (MarkedDamageInfo mdi : markedDamage) {
Ability source = null;
if (mdi.sourceObject instanceof PermanentToken) {
/* Tokens dont have a spellAbility. We must make a phony one as the source so the events in addCounters
/* Tokens don't have a spellAbility. We must make a phony one as the source so the events in addCounters
* can trace the source back to an object/controller.
*/
source = new SpellAbility(null, ((PermanentToken) mdi.sourceObject).name);
@ -1155,9 +1133,9 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
int lethal = Integer.MAX_VALUE;
if (this.isCreature(game)) {
if (game.getState().getActivePowerInsteadOfToughnessForDamageLethalityFilters().stream().anyMatch(f -> f.match(this, game))) {
lethal = Math.min(lethal, power.getValue());
lethal = power.getValue();
} else {
lethal = Math.min(lethal, toughness.getValue());
lethal = toughness.getValue();
}
lethal = Math.max(lethal - this.damage, 0);
Card attacker = game.getPermanent(attackerId);
@ -1312,13 +1290,6 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
return game.getContinuousEffects().preventedByRuleModification(new StayAttachedEvent(this.getId(), attachment.getId(), source), null, game, silentMode);
}
protected boolean canDamage(MageObject source, Game game) {
//noxx: having protection doesn't prevents from dealing damage
// instead it adds damage prevention
//return (!hasProtectionFrom(source, game));
return true;
}
@Override
public boolean destroy(Ability source, Game game) {
return destroy(source, game, false);
@ -1326,7 +1297,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
@Override
public boolean destroy(Ability source, Game game, boolean noRegen) {
// Only permanets on the battlefield can be destroyed
// Only permanents on the battlefield can be destroyed
if (!game.getState().getZone(getId()).equals(Zone.BATTLEFIELD)) {
return false;
}
@ -1337,21 +1308,19 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DESTROY_PERMANENT, objectId, source, controllerId, noRegen ? 1 : 0))) {
// this means destroy was successful, if object movement to graveyard will be replaced (e.g. commander to command zone) it's still
// handled as successful destroying (but not as sucessful "dies this way" for destroying).
// handled as successful destroying (but not as successful "dies this way" for destroying).
if (moveToZone(Zone.GRAVEYARD, source, game, false)) {
if (!game.isSimulation()) {
String logName;
Card card = game.getCard(this.getId());
if (card != null) {
logName = card.getLogName();
} else {
logName = this.getLogName();
}
if (this.isCreature(game)) {
game.informPlayers(logName + " died" + CardUtil.getSourceLogName(game, " by ", source, "", ""));
} else {
game.informPlayers(logName + " was destroyed" + CardUtil.getSourceLogName(game, " by ", source, "", ""));
}
String logName;
Card card = game.getCard(this.getId());
if (card != null) {
logName = card.getLogName();
} else {
logName = this.getLogName();
}
if (this.isCreature(game)) {
game.informPlayers(logName + " died" + CardUtil.getSourceLogName(game, " by ", source, "", ""));
} else {
game.informPlayers(logName + " was destroyed" + CardUtil.getSourceLogName(game, " by ", source, "", ""));
}
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.DESTROYED_PERMANENT, objectId, source, controllerId));
}
@ -1368,7 +1337,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
// so the return value of the moveToZone is not taken into account here
moveToZone(Zone.GRAVEYARD, source, game, false);
Player player = game.getPlayer(getControllerId());
if (player != null && !game.isSimulation()) {
if (player != null) {
game.informPlayers(player.getLogName() + " sacrificed " + this.getLogName() + CardUtil.getSourceLogName(game, source));
}
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.SACRIFICED_PERMANENT, objectId, source, controllerId));
@ -1593,10 +1562,8 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
public boolean removeFromCombat(Game game, boolean withEvent) {
if (this.isAttacking() || this.blocking > 0) {
return game.getCombat().removeFromCombat(objectId, game, withEvent);
} else if (this.isPlaneswalker(game)) {
if (game.getCombat().getDefenders().contains(getId())) {
game.getCombat().removeDefendingPermanentFromCombat(objectId, game);
}
} else if (this.isPlaneswalker(game) && game.getCombat().getDefenders().contains(getId())) {
game.getCombat().removeDefendingPermanentFromCombat(objectId, game);
}
return false;
}
@ -1626,11 +1593,7 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
@Override
public List<UUID> getImprinted() {
if (this.connectedCards.containsKey("imprint")) {
return this.connectedCards.get("imprint");
} else {
return emptyList;
}
return this.connectedCards.getOrDefault("imprint", emptyList);
}
@Override