Skip to content

Commit

Permalink
refactor all batch triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
Susucre committed Apr 23, 2024
1 parent ec2b567 commit ca79e0b
Show file tree
Hide file tree
Showing 84 changed files with 1,560 additions and 1,771 deletions.
25 changes: 15 additions & 10 deletions Mage.Sets/src/mage/cards/a/AegarTheFreezingFlame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mage.MageInt;
import mage.MageObject;
import mage.MageObjectReference;
import mage.abilities.BatchTriggeredAbility;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.effects.common.DrawCardSourceControllerEffect;
import mage.cards.CardImpl;
Expand All @@ -11,10 +12,12 @@
import mage.game.Game;
import mage.game.events.DamagedBatchForOnePermanentEvent;
import mage.game.events.DamagedEvent;
import mage.game.events.DamagedPermanentEvent;
import mage.game.events.GameEvent;
import mage.watchers.Watcher;

import java.util.*;
import java.util.stream.Stream;

/**
* @author TheElk801
Expand Down Expand Up @@ -44,7 +47,7 @@ public AegarTheFreezingFlame copy() {
}
}

class AegarTheFreezingFlameTriggeredAbility extends TriggeredAbilityImpl {
class AegarTheFreezingFlameTriggeredAbility extends TriggeredAbilityImpl implements BatchTriggeredAbility<DamagedPermanentEvent> {

AegarTheFreezingFlameTriggeredAbility() {
super(Zone.BATTLEFIELD, new DrawCardSourceControllerEffect(1));
Expand All @@ -61,18 +64,20 @@ public boolean checkEventType(GameEvent event, Game game) {
}

@Override
public boolean checkTrigger(GameEvent event, Game game) {
DamagedBatchForOnePermanentEvent dEvent = (DamagedBatchForOnePermanentEvent) event;

int excess = dEvent.getEvents()
public Stream<DamagedPermanentEvent> filterBatchEvent(GameEvent event, Game game) {
return ((DamagedBatchForOnePermanentEvent) event)
.getEvents()
.stream()
.filter(e -> e.getExcess() > 0)
.filter(e -> game.getOpponents(getControllerId()).contains(game.getControllerId(e.getTargetId())));
}

@Override
public boolean checkTrigger(GameEvent event, Game game) {
int excessDamage = filterBatchEvent(event, game)
.mapToInt(DamagedEvent::getExcess)
.sum();

boolean controlledByOpponent =
game.getOpponents(getControllerId()).contains(game.getControllerId(event.getTargetId()));

if (excess < 1 || !controlledByOpponent) {
if (excessDamage <= 0) {
return false;
}
AegarTheFreezingFlameWatcher watcher = game.getState().getWatcher(AegarTheFreezingFlameWatcher.class);
Expand Down
75 changes: 7 additions & 68 deletions Mage.Sets/src/mage/cards/a/AngelheartVial.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
package mage.cards.a;

import mage.abilities.Ability;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.common.IsDealtDamageYouTriggeredAbility;
import mage.abilities.common.SimpleActivatedAbility;
import mage.abilities.costs.common.RemoveCountersSourceCost;
import mage.abilities.costs.common.TapSourceCost;
import mage.abilities.costs.mana.GenericManaCost;
import mage.abilities.effects.OneShotEffect;
import mage.abilities.dynamicvalue.common.SavedDamageValue;
import mage.abilities.effects.common.DrawCardSourceControllerEffect;
import mage.abilities.effects.common.GainLifeEffect;
import mage.abilities.effects.common.counter.AddCountersSourceEffect;
import mage.cards.CardImpl;
import mage.cards.CardSetInfo;
import mage.constants.CardType;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.counters.CounterType;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;

import java.util.UUID;

Expand All @@ -31,7 +28,9 @@ public AngelheartVial(UUID ownerId, CardSetInfo setInfo) {
super(ownerId, setInfo, new CardType[]{CardType.ARTIFACT}, "{5}");

// Whenever you're dealt damage, you may put that many charge counters on Angelheart Vial.
this.addAbility(new AngelheartVialTriggeredAbility());
this.addAbility(new IsDealtDamageYouTriggeredAbility(
new AddCountersSourceEffect(CounterType.CHARGE.createInstance(), SavedDamageValue.MANY, true), false
));

// {2}, {tap}, Remove four charge counters from Angelheart Vial: You gain 2 life and draw a card.
Ability ability = new SimpleActivatedAbility(Zone.BATTLEFIELD, new GainLifeEffect(2), new GenericManaCost(2));
Expand All @@ -49,64 +48,4 @@ private AngelheartVial(final AngelheartVial card) {
public AngelheartVial copy() {
return new AngelheartVial(this);
}
}

class AngelheartVialTriggeredAbility extends TriggeredAbilityImpl {

public AngelheartVialTriggeredAbility() {
super(Zone.BATTLEFIELD, new AngelheartVialEffect(), true);
}

private AngelheartVialTriggeredAbility(final AngelheartVialTriggeredAbility ability) {
super(ability);
}

@Override
public AngelheartVialTriggeredAbility copy() {
return new AngelheartVialTriggeredAbility(this);
}

@Override
public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.DAMAGED_BATCH_FOR_ONE_PLAYER;
}

@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (event.getTargetId().equals(this.getControllerId())) {
this.getEffects().get(0).setValue("damageAmount", event.getAmount());
return true;
}
return false;
}

@Override
public String getRule() {
return "Whenever you're dealt damage, you may put that many charge counters on {this}.";
}
}

class AngelheartVialEffect extends OneShotEffect {

AngelheartVialEffect() {
super(Outcome.Benefit);
}

private AngelheartVialEffect(final AngelheartVialEffect effect) {
super(effect);
}

@Override
public AngelheartVialEffect copy() {
return new AngelheartVialEffect(this);
}

@Override
public boolean apply(Game game, Ability source) {
Permanent permanent = game.getPermanent(source.getSourceId());
if (permanent != null) {
permanent.addCounters(CounterType.CHARGE.createInstance((Integer) this.getValue("damageAmount")), source.getControllerId(), source, game);
}
return true;
}
}
}
35 changes: 24 additions & 11 deletions Mage.Sets/src/mage/cards/a/Arcbond.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

package mage.cards.a;

import java.util.UUID;
import mage.MageObject;
import mage.MageObjectReference;
import mage.abilities.Ability;
import mage.abilities.BatchTriggeredAbility;
import mage.abilities.DelayedTriggeredAbility;
import mage.abilities.dynamicvalue.common.StaticValue;
import mage.abilities.effects.Effect;
Expand All @@ -21,18 +21,22 @@
import mage.filter.predicate.Predicates;
import mage.filter.predicate.permanent.PermanentIdPredicate;
import mage.game.Game;
import mage.game.events.DamagedBatchForOnePermanentEvent;
import mage.game.events.DamagedPermanentEvent;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.target.common.TargetCreaturePermanent;

import java.util.UUID;
import java.util.stream.Stream;

/**
*
* @author LevelX2
*/
public final class Arcbond extends CardImpl {

public Arcbond(UUID ownerId, CardSetInfo setInfo) {
super(ownerId,setInfo,new CardType[]{CardType.INSTANT},"{2}{R}");
super(ownerId, setInfo, new CardType[]{CardType.INSTANT}, "{2}{R}");

// Choose target creature. Whenever that creature is dealt damage this turn, it deals that much damage to each other creature and each player.
this.getSpellAbility().addEffect(new CreateDelayedTriggeredAbilityEffect(new ArcbondDelayedTriggeredAbility()));
Expand All @@ -49,7 +53,7 @@ public Arcbond copy() {
}
}

class ArcbondDelayedTriggeredAbility extends DelayedTriggeredAbility {
class ArcbondDelayedTriggeredAbility extends DelayedTriggeredAbility implements BatchTriggeredAbility<DamagedPermanentEvent> {

MageObjectReference targetObject;

Expand Down Expand Up @@ -87,16 +91,25 @@ public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.DAMAGED_BATCH_FOR_ONE_PERMANENT;
}

@Override
public Stream<DamagedPermanentEvent> filterBatchEvent(GameEvent event, Game game) {
return ((DamagedBatchForOnePermanentEvent) event)
.getEvents()
.stream()
.filter(e -> e.getTargetId().equals(targetObject.getSourceId()) && targetObject.getPermanentOrLKIBattlefield(game) != null)
.filter(e -> e.getAmount() > 0);
}

@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (event.getTargetId().equals(targetObject.getSourceId())
&& targetObject.getPermanentOrLKIBattlefield(game) != null) {
for (Effect effect : this.getEffects()) {
effect.setValue("damage", event.getAmount());
}
return true;
int amount = filterBatchEvent(event, game)
.mapToInt(DamagedPermanentEvent::getAmount)
.sum();
if (amount <= 0) {
return false;
}
return false;
getEffects().setValue("damage", amount);
return true;
}

@Override
Expand Down
16 changes: 10 additions & 6 deletions Mage.Sets/src/mage/cards/b/BindingAgony.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package mage.cards.b;

import java.util.UUID;
import mage.abilities.common.DealtDamageAttachedTriggeredAbility;
import mage.abilities.common.IsDealtDamageAttachedTriggeredAbility;
import mage.abilities.dynamicvalue.common.SavedDamageValue;
import mage.abilities.effects.common.AttachEffect;
import mage.abilities.effects.common.DamageAttachedControllerEffect;
import mage.abilities.keyword.EnchantAbility;
import mage.cards.CardImpl;
import mage.cards.CardSetInfo;
import mage.constants.*;
import mage.constants.CardType;
import mage.constants.Outcome;
import mage.constants.SubType;
import mage.target.TargetPermanent;
import mage.target.common.TargetCreaturePermanent;

import java.util.UUID;

/**
*
* @author LoneFox
*/
public final class BindingAgony extends CardImpl {

public BindingAgony(UUID ownerId, CardSetInfo setInfo) {
super(ownerId,setInfo,new CardType[]{CardType.ENCHANTMENT},"{1}{B}");
super(ownerId, setInfo, new CardType[]{CardType.ENCHANTMENT}, "{1}{B}");
this.subtype.add(SubType.AURA);

// Enchant creature
Expand All @@ -29,7 +31,9 @@ public BindingAgony(UUID ownerId, CardSetInfo setInfo) {
this.addAbility(new EnchantAbility(auraTarget));

// Whenever enchanted creature is dealt damage, Binding Agony deals that much damage to that creature's controller.
this.addAbility(new DealtDamageAttachedTriggeredAbility(new DamageAttachedControllerEffect(SavedDamageValue.MUCH), false));
this.addAbility(new IsDealtDamageAttachedTriggeredAbility(
new DamageAttachedControllerEffect(SavedDamageValue.MUCH), false, "enchanted"
));
}

private BindingAgony(final BindingAgony card) {
Expand Down

0 comments on commit ca79e0b

Please sign in to comment.