foul-magics/Mage/src/main/java/mage/abilities/common/SpellCastControllerTriggeredAbility.java
2021-01-10 20:36:32 -05:00

88 lines
2.9 KiB
Java

package mage.abilities.common;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.effects.Effect;
import mage.constants.Zone;
import mage.filter.FilterSpell;
import mage.filter.StaticFilters;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.stack.Spell;
import mage.target.targetpointer.FixedTarget;
/**
* @author North
*/
public class SpellCastControllerTriggeredAbility extends TriggeredAbilityImpl {
protected FilterSpell filter;
protected String rule;
/**
* If true, the source that triggered the ability will be set as target to
* effect.
*/
protected boolean rememberSource = false;
public SpellCastControllerTriggeredAbility(Effect effect, boolean optional) {
this(Zone.BATTLEFIELD, effect, StaticFilters.FILTER_SPELL_A, optional, false);
}
public SpellCastControllerTriggeredAbility(Effect effect, FilterSpell filter, boolean optional) {
this(effect, filter, optional, false);
}
public SpellCastControllerTriggeredAbility(Effect effect, FilterSpell filter, boolean optional, String rule) {
this(effect, filter, optional, false);
this.rule = rule;
}
public SpellCastControllerTriggeredAbility(Effect effect, FilterSpell filter, boolean optional, boolean rememberSource) {
this(Zone.BATTLEFIELD, effect, filter, optional, rememberSource);
}
public SpellCastControllerTriggeredAbility(Zone zone, Effect effect, FilterSpell filter, boolean optional, boolean rememberSource) {
super(zone, effect, optional);
this.filter = filter;
this.rememberSource = rememberSource;
}
public SpellCastControllerTriggeredAbility(final SpellCastControllerTriggeredAbility ability) {
super(ability);
this.filter = ability.filter;
this.rememberSource = ability.rememberSource;
this.rule = ability.rule;
}
@Override
public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.SPELL_CAST;
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (event.getPlayerId().equals(this.getControllerId())) {
Spell spell = game.getStack().getSpell(event.getTargetId());
if (spell != null && filter.match(spell, getSourceId(), getControllerId(), game)) {
if (rememberSource) {
this.getEffects().get(0).setTargetPointer(new FixedTarget(spell.getId(), game));
}
return true;
}
}
return false;
}
@Override
public String getRule() {
if (rule != null && !rule.isEmpty()) {
return rule;
}
return "Whenever you cast " + filter.getMessage() + ", " + super.getRule();
}
@Override
public SpellCastControllerTriggeredAbility copy() {
return new SpellCastControllerTriggeredAbility(this);
}
}