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

@ -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.
*/

View file

@ -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) {