mirror of
https://github.com/magefree/mage.git
synced 2026-01-23 19:59:54 -08:00
refactor: common logic for getting controller of target object (#13038)
This commit is contained in:
parent
6cc2306784
commit
737e67963d
11 changed files with 53 additions and 126 deletions
|
|
@ -5,7 +5,6 @@ import mage.abilities.dynamicvalue.DynamicValue;
|
|||
import mage.abilities.dynamicvalue.common.StaticValue;
|
||||
import mage.abilities.effects.OneShotEffect;
|
||||
import mage.constants.Outcome;
|
||||
import mage.game.Controllable;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.token.Token;
|
||||
import mage.players.Player;
|
||||
|
|
@ -21,43 +20,20 @@ public class CreateTokenControllerTargetEffect extends OneShotEffect {
|
|||
private final DynamicValue amount;
|
||||
private final boolean tapped;
|
||||
|
||||
/**
|
||||
* What the target is supposed to be, to retrieve its controller from
|
||||
*/
|
||||
public enum TargetKind {
|
||||
PERMANENT,
|
||||
SPELL
|
||||
}
|
||||
|
||||
private final TargetKind targetKind;
|
||||
|
||||
public CreateTokenControllerTargetEffect(Token token) {
|
||||
this(token, 1, false);
|
||||
}
|
||||
|
||||
public CreateTokenControllerTargetEffect(Token token, TargetKind targetKind) {
|
||||
this(token, 1, false, targetKind);
|
||||
}
|
||||
|
||||
public CreateTokenControllerTargetEffect(Token token, int amount, boolean tapped) {
|
||||
this(token, StaticValue.get(amount), tapped);
|
||||
}
|
||||
|
||||
public CreateTokenControllerTargetEffect(Token token, int amount, boolean tapped, TargetKind targetKind) {
|
||||
this(token, StaticValue.get(amount), tapped, targetKind);
|
||||
}
|
||||
|
||||
public CreateTokenControllerTargetEffect(Token token, DynamicValue amount, boolean tapped) {
|
||||
this(token, amount, tapped, TargetKind.PERMANENT);
|
||||
}
|
||||
|
||||
public CreateTokenControllerTargetEffect(Token token, DynamicValue amount, boolean tapped, TargetKind targetKind) {
|
||||
super(Outcome.Neutral);
|
||||
this.token = token;
|
||||
this.amount = amount.copy();
|
||||
this.tapped = tapped;
|
||||
this.staticText = makeText();
|
||||
this.targetKind = targetKind;
|
||||
}
|
||||
|
||||
protected CreateTokenControllerTargetEffect(final CreateTokenControllerTargetEffect effect) {
|
||||
|
|
@ -65,7 +41,6 @@ public class CreateTokenControllerTargetEffect extends OneShotEffect {
|
|||
this.token = effect.token.copy();
|
||||
this.amount = effect.amount.copy();
|
||||
this.tapped = effect.tapped;
|
||||
this.targetKind = effect.targetKind;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -75,21 +50,10 @@ public class CreateTokenControllerTargetEffect extends OneShotEffect {
|
|||
|
||||
@Override
|
||||
public boolean apply(Game game, Ability source) {
|
||||
Controllable controllable = null;
|
||||
switch (targetKind) {
|
||||
case PERMANENT:
|
||||
controllable = getTargetPointer().getFirstTargetPermanentOrLKI(game, source);
|
||||
break;
|
||||
case SPELL:
|
||||
controllable = game.getSpellOrLKIStack(getTargetPointer().getFirst(game, source));
|
||||
break;
|
||||
}
|
||||
if (controllable != null) {
|
||||
Player controllerOfTarget = game.getPlayer(controllable.getControllerId());
|
||||
if (controllerOfTarget != null) {
|
||||
int value = amount.calculate(game, source, this);
|
||||
return token.putOntoBattlefield(value, game, source, controllerOfTarget.getId(), tapped, false);
|
||||
}
|
||||
Player controllerOfTarget = getTargetPointer().getControllerOfFirstTargetOrLKI(game, source);
|
||||
if (controllerOfTarget != null) {
|
||||
int value = amount.calculate(game, source, this);
|
||||
return token.putOntoBattlefield(value, game, source, controllerOfTarget.getId(), tapped, false);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import mage.abilities.dynamicvalue.common.StaticValue;
|
|||
import mage.abilities.effects.OneShotEffect;
|
||||
import mage.constants.Outcome;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.players.Player;
|
||||
|
||||
/**
|
||||
|
|
@ -64,12 +63,9 @@ public class DamageTargetControllerEffect extends OneShotEffect {
|
|||
|
||||
@Override
|
||||
public boolean apply(Game game, Ability source) {
|
||||
Permanent permanent = getTargetPointer().getFirstTargetPermanentOrLKI(game, source);
|
||||
if (permanent != null) {
|
||||
Player targetController = game.getPlayer(permanent.getControllerId());
|
||||
if (targetController != null) {
|
||||
targetController.damage(amount.calculate(game, source, this), source.getSourceId(), source, game, false, preventable);
|
||||
}
|
||||
Player targetController = getTargetPointer().getControllerOfFirstTargetOrLKI(game, source);
|
||||
if (targetController != null) {
|
||||
targetController.damage(amount.calculate(game, source, this), source.getSourceId(), source, game, false, preventable);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ import mage.abilities.dynamicvalue.common.StaticValue;
|
|||
import mage.abilities.effects.OneShotEffect;
|
||||
import mage.constants.Outcome;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.game.stack.Spell;
|
||||
import mage.players.Player;
|
||||
import mage.util.CardUtil;
|
||||
|
||||
|
|
@ -41,16 +39,7 @@ public class DrawCardTargetControllerEffect extends OneShotEffect {
|
|||
|
||||
@Override
|
||||
public boolean apply(Game game, Ability source) {
|
||||
Player targetController = null;
|
||||
Permanent permanent = getTargetPointer().getFirstTargetPermanentOrLKI(game, source);
|
||||
if (permanent != null) {
|
||||
targetController = game.getPlayer(permanent.getControllerId());
|
||||
} else {
|
||||
Spell spell = game.getSpellOrLKIStack(getTargetPointer().getFirst(game, source));
|
||||
if (spell != null) {
|
||||
targetController = game.getPlayer(spell.getControllerId());
|
||||
}
|
||||
}
|
||||
Player targetController = getTargetPointer().getControllerOfFirstTargetOrLKI(game, source);
|
||||
if (targetController != null) {
|
||||
targetController.drawCards(amount.calculate(game, source, this), source, game);
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ import mage.abilities.dynamicvalue.common.StaticValue;
|
|||
import mage.abilities.effects.OneShotEffect;
|
||||
import mage.constants.Outcome;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.game.stack.Spell;
|
||||
import mage.players.Player;
|
||||
|
||||
/**
|
||||
|
|
@ -39,16 +37,7 @@ public class GainLifeTargetControllerEffect extends OneShotEffect {
|
|||
|
||||
@Override
|
||||
public boolean apply(Game game, Ability source) {
|
||||
Player targetController = null;
|
||||
Permanent permanent = getTargetPointer().getFirstTargetPermanentOrLKI(game, source);
|
||||
if (permanent != null) {
|
||||
targetController = game.getPlayer(permanent.getControllerId());
|
||||
} else {
|
||||
Spell spell = game.getSpellOrLKIStack(getTargetPointer().getFirst(game, source));
|
||||
if (spell != null) {
|
||||
targetController = game.getPlayer(spell.getControllerId());
|
||||
}
|
||||
}
|
||||
Player targetController = getTargetPointer().getControllerOfFirstTargetOrLKI(game, source);
|
||||
if (targetController != null) {
|
||||
targetController.gainLife(amount.calculate(game, source, this), game, source);
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import mage.constants.Outcome;
|
|||
import mage.constants.Zone;
|
||||
import mage.filter.StaticFilters;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.players.Player;
|
||||
import mage.target.common.TargetCardInLibrary;
|
||||
|
||||
|
|
@ -16,7 +15,7 @@ import mage.target.common.TargetCardInLibrary;
|
|||
*/
|
||||
public class SearchLibraryPutInPlayTargetControllerEffect extends SearchEffect {
|
||||
|
||||
private boolean tapped;
|
||||
private final boolean tapped;
|
||||
|
||||
public SearchLibraryPutInPlayTargetControllerEffect(boolean tapped) {
|
||||
this(new TargetCardInLibrary(StaticFilters.FILTER_CARD_BASIC_LAND), tapped, Outcome.PutLandInPlay, "its controller");
|
||||
|
|
@ -45,22 +44,16 @@ public class SearchLibraryPutInPlayTargetControllerEffect extends SearchEffect {
|
|||
|
||||
@Override
|
||||
public boolean apply(Game game, Ability source) {
|
||||
Permanent permanent = getTargetPointer().getFirstTargetPermanentOrLKI(game, source);
|
||||
if (permanent == null) {
|
||||
return false;
|
||||
}
|
||||
Player player = game.getPlayer(permanent.getControllerId());
|
||||
Player player = getTargetPointer().getControllerOfFirstTargetOrLKI(game, source);
|
||||
if (player == null) {
|
||||
return false;
|
||||
}
|
||||
if (!player.chooseUse(outcome, "Search your library for " + target.getDescription() + '?', source, game)) {
|
||||
return true;
|
||||
}
|
||||
if (player.searchLibrary(target, source, game)) {
|
||||
if (!target.getTargets().isEmpty()) {
|
||||
player.moveCards(new CardsImpl(target.getTargets()).getCards(game),
|
||||
Zone.BATTLEFIELD, source, game, tapped, false, false, null);
|
||||
}
|
||||
if (player.searchLibrary(target, source, game) && !target.getTargets().isEmpty()) {
|
||||
player.moveCards(new CardsImpl(target.getTargets()).getCards(game),
|
||||
Zone.BATTLEFIELD, source, game, tapped, false, false, null);
|
||||
}
|
||||
player.shuffleLibrary(source, game);
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package mage.target.targetpointer;
|
|||
import mage.abilities.Ability;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.players.Player;
|
||||
import mage.target.Targets;
|
||||
import mage.util.Copyable;
|
||||
|
||||
|
|
@ -46,6 +47,13 @@ public interface TargetPointer extends Serializable, Copyable<TargetPointer> {
|
|||
*/
|
||||
Permanent getFirstTargetPermanentOrLKI(Game game, Ability source);
|
||||
|
||||
/**
|
||||
* Finds the controller of the first target object or LKI,
|
||||
* whether it is a permanent or spell.
|
||||
* Returns null if not found.
|
||||
*/
|
||||
Player getControllerOfFirstTargetOrLKI(Game game, Ability source);
|
||||
|
||||
/**
|
||||
* Describes the appropriate subset of targets for ability text.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -1,5 +1,11 @@
|
|||
package mage.target.targetpointer;
|
||||
|
||||
import mage.abilities.Ability;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.game.stack.Spell;
|
||||
import mage.players.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -13,7 +19,7 @@ public abstract class TargetPointerImpl implements TargetPointer {
|
|||
|
||||
private boolean initialized = false;
|
||||
|
||||
public TargetPointerImpl() {
|
||||
protected TargetPointerImpl() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
|
@ -36,6 +42,21 @@ public abstract class TargetPointerImpl implements TargetPointer {
|
|||
this.initialized = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player getControllerOfFirstTargetOrLKI(Game game, Ability source) {
|
||||
Player targetController = null;
|
||||
Permanent permanent = this.getFirstTargetPermanentOrLKI(game, source);
|
||||
if (permanent != null) {
|
||||
targetController = game.getPlayer(permanent.getControllerId());
|
||||
} else {
|
||||
Spell spell = game.getSpellOrLKIStack(this.getFirst(game, source));
|
||||
if (spell != null) {
|
||||
targetController = game.getPlayer(spell.getControllerId());
|
||||
}
|
||||
}
|
||||
return targetController;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getData(String key) {
|
||||
if (data == null) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue