refactor: common logic for getting controller of target object (#13038)

This commit is contained in:
xenohedron 2024-10-26 20:23:34 -04:00 committed by GitHub
parent 6cc2306784
commit 737e67963d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 53 additions and 126 deletions

View file

@ -1,16 +1,11 @@
package mage.abilities.effects.common;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.dynamicvalue.common.StaticValue;
import mage.abilities.effects.OneShotEffect;
import mage.cards.Card;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.game.stack.Spell;
import mage.players.Player;
/**
@ -42,31 +37,10 @@ public class LoseLifeTargetControllerEffect extends OneShotEffect {
@Override
public boolean apply(Game game, Ability source) {
MageObject targetCard = getTargetPointer().getFirstTargetPermanentOrLKI(game, source);
// if target is a countered spell
if (targetCard == null) {
targetCard = game.getLastKnownInformation(getTargetPointer().getFirst(game, source), Zone.STACK);
}
if (targetCard != null) {
Player controller = null;
//Handles interaction with permanents that were on the battlefield.
if (targetCard instanceof Permanent) {
Permanent targetPermanent = (Permanent) targetCard;
controller = game.getPlayer(targetPermanent.getControllerId());
}
//Handles interaction with spells that were on the stack.
else if (targetCard instanceof Spell) {
Spell targetSpell = (Spell) targetCard;
controller = game.getPlayer(targetSpell.getControllerId());
}
if (controller != null) {
controller.loseLife(amount.calculate(game, source, this), game, source, false);
return true;
}
Player targetController = getTargetPointer().getControllerOfFirstTargetOrLKI(game, source);
if (targetController != null) {
targetController.loseLife(amount.calculate(game, source, this), game, source, false);
return true;
}
return false;
}