foul-magics/Mage/src/main/java/mage/abilities/common/ExploitCreatureTriggeredAbility.java
2021-09-27 16:35:10 -05:00

80 lines
2.7 KiB
Java

package mage.abilities.common;
import mage.MageObject;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.effects.Effect;
import mage.constants.SetTargetPointer;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.target.targetpointer.FixedTarget;
/**
*
* @author LevelX2
*/
public class ExploitCreatureTriggeredAbility extends TriggeredAbilityImpl {
private SetTargetPointer setTargetPointer;
public ExploitCreatureTriggeredAbility(Effect effect, boolean optional) {
this(effect, optional, SetTargetPointer.NONE);
}
public ExploitCreatureTriggeredAbility(Effect effect, boolean optional, SetTargetPointer setTargetPointer) {
super(Zone.BATTLEFIELD, effect, optional);
this.setTargetPointer = setTargetPointer;
// For example: if the creature with the Exploit ability is sacrificed, the trigger ability must use the controller of the LKI, not the owner
setLeavesTheBattlefieldTrigger(true); // https://github.com/magefree/mage/issues/8317
}
public ExploitCreatureTriggeredAbility(final ExploitCreatureTriggeredAbility ability) {
super(ability);
this.setTargetPointer = ability.setTargetPointer;
}
@Override
public ExploitCreatureTriggeredAbility copy() {
return new ExploitCreatureTriggeredAbility(this);
}
@Override
public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.EXPLOITED_CREATURE;
}
@Override
public boolean isInUseableZone(Game game, MageObject source, GameEvent event) {
Permanent sourcePermanent = null;
if (game.getState().getZone(getSourceId()) == Zone.BATTLEFIELD) {
sourcePermanent = game.getPermanent(getSourceId());
} else {
if (game.getShortLivingLKI(getSourceId(), Zone.BATTLEFIELD)) {
sourcePermanent = (Permanent) game.getLastKnownInformation(getSourceId(), Zone.BATTLEFIELD);
}
}
if (sourcePermanent == null) {
return false;
}
return hasSourceObjectAbility(game, sourcePermanent, event);
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (event.getSourceId().equals(getSourceId())) {
for (Effect effect : getEffects()) {
if (setTargetPointer == SetTargetPointer.PERMANENT) {
effect.setTargetPointer(new FixedTarget(event.getTargetId()));
}
}
return true;
}
return false;
}
@Override
public String getTriggerPhrase() {
return "When {this} exploits a creature, " ;
}
}