refactoring and use a proper ability

This commit is contained in:
Loki 2012-07-07 12:29:05 +12:00
parent a0125c9581
commit 08e46fad24
3 changed files with 58 additions and 46 deletions

View file

@ -31,7 +31,7 @@ import mage.Constants;
import mage.Constants.CardType;
import mage.Constants.Rarity;
import mage.abilities.Ability;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.common.PutIntoGraveFromBattlefield;
import mage.abilities.common.SimpleStaticAbility;
import mage.abilities.effects.common.AttachEffect;
import mage.abilities.effects.common.ReturnToHandSourceEffect;
@ -40,10 +40,6 @@ import mage.abilities.effects.common.continious.GainAbilityAttachedEffect;
import mage.abilities.keyword.EnchantAbility;
import mage.abilities.keyword.TrampleAbility;
import mage.cards.CardImpl;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.ZoneChangeEvent;
import mage.game.permanent.Permanent;
import mage.target.TargetPermanent;
import mage.target.common.TargetCreaturePermanent;
@ -73,7 +69,7 @@ public class Rancor extends CardImpl<Rancor> {
this.addAbility(new SimpleStaticAbility(Constants.Zone.BATTLEFIELD, new GainAbilityAttachedEffect(TrampleAbility.getInstance(), Constants.AttachmentType.AURA)));
// When Rancor is put into a graveyard from the battlefield, return Rancor to its owner's hand.
this.addAbility(new RancorTriggeredAbility());
this.addAbility(new PutIntoGraveFromBattlefield(new ReturnToHandSourceEffect()));
}
public Rancor(final Rancor card) {
@ -85,41 +81,3 @@ public class Rancor extends CardImpl<Rancor> {
return new Rancor(this);
}
}
class RancorTriggeredAbility extends TriggeredAbilityImpl<RancorTriggeredAbility> {
public RancorTriggeredAbility() {
super(Constants.Zone.ALL, new ReturnToHandSourceEffect(), false);
}
RancorTriggeredAbility(RancorTriggeredAbility ability) {
super(ability);
}
@Override
public RancorTriggeredAbility copy() {
return new RancorTriggeredAbility(this);
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (event.getType() == GameEvent.EventType.ZONE_CHANGE) {
ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
Permanent permanent = zEvent.getTarget();
if (permanent != null &&
zEvent.getToZone() == Constants.Zone.GRAVEYARD &&
zEvent.getFromZone() == Constants.Zone.BATTLEFIELD &&
permanent.getId().equals(this.getSourceId())) {
return true;
}
}
return false;
}
@Override
public String getRule() {
return "When {this} is put into a graveyard from the battlefield, return {this} to its owner's hand";
}
}

View file

@ -33,7 +33,7 @@ import mage.Constants;
import mage.Constants.CardType;
import mage.Constants.Rarity;
import mage.abilities.Ability;
import mage.abilities.common.DiesTriggeredAbility;
import mage.abilities.common.PutIntoGraveFromBattlefield;
import mage.abilities.common.SimpleStaticAbility;
import mage.abilities.effects.common.AttachEffect;
import mage.abilities.effects.common.ReturnToHandSourceEffect;
@ -65,7 +65,7 @@ public class BrilliantHalo extends CardImpl<BrilliantHalo> {
// Enchanted creature gets +1/+2.
this.addAbility(new SimpleStaticAbility(Constants.Zone.BATTLEFIELD, new BoostEnchantedEffect(1, 2, Constants.Duration.WhileOnBattlefield)));
// When Brilliant Halo is put into a graveyard from the battlefield, return Brilliant Halo to its owner's hand.
this.addAbility(new DiesTriggeredAbility(new ReturnToHandSourceEffect()));
this.addAbility(new PutIntoGraveFromBattlefield(new ReturnToHandSourceEffect()));
}
public BrilliantHalo(final BrilliantHalo card) {

View file

@ -0,0 +1,54 @@
package mage.abilities.common;
import mage.Constants;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.effects.Effect;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.ZoneChangeEvent;
import mage.game.permanent.Permanent;
/**
* @author nantuko, loki
*/
public class PutIntoGraveFromBattlefield extends TriggeredAbilityImpl<PutIntoGraveFromBattlefield> {
public PutIntoGraveFromBattlefield(Effect effect) {
this(effect, false);
}
public PutIntoGraveFromBattlefield(Effect effect, boolean optional) {
super(Constants.Zone.ALL, effect, optional);
}
PutIntoGraveFromBattlefield(PutIntoGraveFromBattlefield ability) {
super(ability);
}
@Override
public PutIntoGraveFromBattlefield copy() {
return new PutIntoGraveFromBattlefield(this);
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (event.getType() == GameEvent.EventType.ZONE_CHANGE) {
ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
Permanent permanent = zEvent.getTarget();
if (permanent != null &&
zEvent.getToZone() == Constants.Zone.GRAVEYARD &&
zEvent.getFromZone() == Constants.Zone.BATTLEFIELD &&
permanent.getId().equals(this.getSourceId())) {
return true;
}
}
return false;
}
@Override
public String getRule() {
return "When {this} is put into a graveyard from the battlefield, return {this} to its owner's hand";
}
}