From 6660c6eece60257b8f0384ba8a3f78d76af47d85 Mon Sep 17 00:00:00 2001 From: BetaSteward Date: Fri, 7 Oct 2011 12:25:45 -0400 Subject: [PATCH] can now add Abilities to players --- .../sets/magic2011/LeylineOfSanctity.java | 6 +- .../abilities/effects/ContinuousEffects.java | 19 ++++++ .../GainAbilityControllerEffect.java} | 66 ++++++++----------- Mage/src/mage/players/Player.java | 1 + Mage/src/mage/players/PlayerImpl.java | 21 ++++-- 5 files changed, 65 insertions(+), 48 deletions(-) rename Mage/src/mage/abilities/effects/common/{CantTargetControllerEffect.java => continious/GainAbilityControllerEffect.java} (56%) diff --git a/Mage.Sets/src/mage/sets/magic2011/LeylineOfSanctity.java b/Mage.Sets/src/mage/sets/magic2011/LeylineOfSanctity.java index 027c9fba07..b1b5ab3e5c 100644 --- a/Mage.Sets/src/mage/sets/magic2011/LeylineOfSanctity.java +++ b/Mage.Sets/src/mage/sets/magic2011/LeylineOfSanctity.java @@ -30,12 +30,12 @@ package mage.sets.magic2011; import java.util.UUID; import mage.Constants.CardType; -import mage.Constants.Duration; import mage.Constants.Rarity; import mage.Constants.TargetController; import mage.Constants.Zone; import mage.abilities.common.SimpleStaticAbility; -import mage.abilities.effects.common.CantTargetControllerEffect; +import mage.abilities.effects.common.continious.GainAbilityControllerEffect; +import mage.abilities.keyword.HexproofAbility; import mage.abilities.keyword.LeylineAbility; import mage.cards.CardImpl; import mage.filter.FilterStackObject; @@ -57,7 +57,7 @@ public class LeylineOfSanctity extends CardImpl { this.expansionSetCode = "M11"; this.color.setWhite(true); this.addAbility(LeylineAbility.getInstance()); - this.addAbility(new SimpleStaticAbility(Zone.BATTLEFIELD, new CantTargetControllerEffect(filter, Duration.WhileOnBattlefield))); + this.addAbility(new SimpleStaticAbility(Zone.BATTLEFIELD, new GainAbilityControllerEffect(HexproofAbility.getInstance()))); } public LeylineOfSanctity(final LeylineOfSanctity card) { diff --git a/Mage/src/mage/abilities/effects/ContinuousEffects.java b/Mage/src/mage/abilities/effects/ContinuousEffects.java index 175396277b..2fe0e8c687 100644 --- a/Mage/src/mage/abilities/effects/ContinuousEffects.java +++ b/Mage/src/mage/abilities/effects/ContinuousEffects.java @@ -329,6 +329,25 @@ public class ContinuousEffects implements Serializable { } } } + //get all applicable Replacement effects on players + for (Player player: game.getPlayers().values()) { + for (Ability ability: player.getAbilities().getStaticAbilities(Zone.BATTLEFIELD)) { + for (Effect effect: ability.getEffects(game, EffectType.REPLACEMENT)) { + ReplacementEffect rEffect = (ReplacementEffect) effect; + if (rEffect.applies(event, ability, game)) { + replaceEffects.add(rEffect); + abilityMap.put(rEffect.getId(), ability); + } + } + for (Effect effect: ability.getEffects(game, EffectType.PREVENTION)) { + ReplacementEffect rEffect = (ReplacementEffect) effect; + if (rEffect.applies(event, ability, game)) { + replaceEffects.add(rEffect); + abilityMap.put(rEffect.getId(), ability); + } + } + } + } //get all applicable transient Replacement effects for (ReplacementEffect effect: replacementEffects) { if (effect.getDuration() != Duration.OneUse || !effect.isUsed()) { diff --git a/Mage/src/mage/abilities/effects/common/CantTargetControllerEffect.java b/Mage/src/mage/abilities/effects/common/continious/GainAbilityControllerEffect.java similarity index 56% rename from Mage/src/mage/abilities/effects/common/CantTargetControllerEffect.java rename to Mage/src/mage/abilities/effects/common/continious/GainAbilityControllerEffect.java index 41ee0572a0..35ac739347 100644 --- a/Mage/src/mage/abilities/effects/common/CantTargetControllerEffect.java +++ b/Mage/src/mage/abilities/effects/common/continious/GainAbilityControllerEffect.java @@ -26,69 +26,57 @@ * or implied, of BetaSteward_at_googlemail.com. */ -package mage.abilities.effects.common; +package mage.abilities.effects.common.continious; import mage.Constants.Duration; +import mage.Constants.Layer; import mage.Constants.Outcome; +import mage.Constants.SubLayer; import mage.abilities.Ability; -import mage.abilities.effects.ReplacementEffectImpl; -import mage.filter.FilterStackObject; +import mage.abilities.effects.ContinuousEffectImpl; import mage.game.Game; -import mage.game.events.GameEvent; -import mage.game.events.GameEvent.EventType; -import mage.game.stack.StackObject; +import mage.players.Player; /** * * @author BetaSteward_at_googlemail.com */ -public class CantTargetControllerEffect extends ReplacementEffectImpl { +public class GainAbilityControllerEffect extends ContinuousEffectImpl { - private FilterStackObject filterSource; + protected Ability ability; - public CantTargetControllerEffect(FilterStackObject filterSource, Duration duration) { - super(duration, Outcome.Benefit); - this.filterSource = filterSource; - setText(); + /** + * Add ability with Duration.WhileOnBattlefield + * @param ability + */ + public GainAbilityControllerEffect(Ability ability) { + this(ability, Duration.WhileOnBattlefield); } - public CantTargetControllerEffect(final CantTargetControllerEffect effect) { + public GainAbilityControllerEffect(Ability ability, Duration duration) { + super(duration, Layer.AbilityAddingRemovingEffects_6, SubLayer.NA, Outcome.AddAbility); + this.ability = ability; + staticText = "You have " + ability.getRule() + " " + duration.toString(); + } + + public GainAbilityControllerEffect(final GainAbilityControllerEffect effect) { super(effect); - this.filterSource = effect.filterSource.copy(); + this.ability = effect.ability.copy(); } @Override - public CantTargetControllerEffect copy() { - return new CantTargetControllerEffect(this); + public GainAbilityControllerEffect copy() { + return new GainAbilityControllerEffect(this); } @Override public boolean apply(Game game, Ability source) { - return true; - } - - @Override - public boolean replaceEvent(GameEvent event, Ability source, Game game) { - return true; - } - - @Override - public boolean applies(GameEvent event, Ability source, Game game) { - if (event.getType() == EventType.TARGET && event.getTargetId().equals(source.getControllerId())) { - StackObject sourceObject = game.getStack().getStackObject(event.getSourceId()); - if (sourceObject != null && filterSource.match(sourceObject)) { - return true; - } + Player player = game.getPlayer(source.getControllerId()); + if (player != null) { + player.addAbility(ability); + return true; } return false; } - private void setText() { - StringBuilder sb = new StringBuilder(); - sb.append("You can't be the targets of "); - sb.append(filterSource.getMessage()); - sb.append(" ").append(duration.toString()); - staticText = sb.toString(); - } - } diff --git a/Mage/src/mage/players/Player.java b/Mage/src/mage/players/Player.java index c83c2ee7c5..d51ae96f70 100644 --- a/Mage/src/mage/players/Player.java +++ b/Mage/src/mage/players/Player.java @@ -80,6 +80,7 @@ public interface Player extends MageItem, Copyable { public Library getLibrary(); public Cards getGraveyard(); public Abilities getAbilities(); + public void addAbility(Ability ability); public Counters getCounters(); public int getLife(); public void setLife(int life, Game game); diff --git a/Mage/src/mage/players/PlayerImpl.java b/Mage/src/mage/players/PlayerImpl.java index c5a0d3dec7..9ed3707b66 100644 --- a/Mage/src/mage/players/PlayerImpl.java +++ b/Mage/src/mage/players/PlayerImpl.java @@ -420,11 +420,14 @@ public abstract class PlayerImpl> implements Player, Ser @Override public boolean discard(Card card, Ability source, Game game) { //20100716 - 701.7 - removeFromHand(card, game); - card.moveToZone(Zone.GRAVEYARD, source==null?null:source.getId(), game, false); - - game.fireEvent(GameEvent.getEvent(GameEvent.EventType.DISCARDED_CARD, card.getId(), source==null?null:source.getId(), playerId)); - return true; + if (card != null) { + removeFromHand(card, game); + card.moveToZone(Zone.GRAVEYARD, source==null?null:source.getId(), game, false); + + game.fireEvent(GameEvent.getEvent(GameEvent.EventType.DISCARDED_CARD, card.getId(), source==null?null:source.getId(), playerId)); + return true; + } + return false; } @Override @@ -917,6 +920,12 @@ public abstract class PlayerImpl> implements Player, Ser public Abilities getAbilities() { return this.abilities; } + + @Override + public void addAbility(Ability ability) { + ability.setSourceId(playerId); + this.abilities.add(ability); + } @Override public int getLandsPerTurn() { @@ -955,7 +964,7 @@ public abstract class PlayerImpl> implements Player, Ser this.library = player.getLibrary(); this.hand = player.getHand(); this.graveyard = player.getGraveyard(); - this.abilities = player.getAbilities(); +// this.abilities = player.getAbilities(); this.manaPool = player.getManaPool(); this.life = player.getLife(); this.counters = player.getCounters();