mirror of
https://github.com/magefree/mage.git
synced 2025-12-25 21:12:04 -08:00
Created class for reflexive triggered abilities (Ready for review) (#6500)
* added class for reflexive triggered abilities * added DoWhenCostPaid * a few more refactors * some more refactoring * almost all refactors done * finished refactoring * updated text generation * Delete SendOptionUsedEventEffect.java * fixed Wildborn Preserver text
This commit is contained in:
parent
8a3ba6729f
commit
bde65d6279
25 changed files with 505 additions and 1278 deletions
|
|
@ -0,0 +1,50 @@
|
|||
package mage.abilities.common.delayed;
|
||||
|
||||
import mage.abilities.DelayedTriggeredAbility;
|
||||
import mage.abilities.effects.Effect;
|
||||
import mage.constants.Duration;
|
||||
import mage.game.Game;
|
||||
import mage.game.events.GameEvent;
|
||||
|
||||
/**
|
||||
* @author TheElk801
|
||||
*/
|
||||
public class ReflexiveTriggeredAbility extends DelayedTriggeredAbility {
|
||||
|
||||
private final String text;
|
||||
|
||||
public ReflexiveTriggeredAbility(Effect effect, boolean optional, String text) {
|
||||
super(effect, Duration.EndOfTurn, true, optional);
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
protected ReflexiveTriggeredAbility(final ReflexiveTriggeredAbility ability) {
|
||||
super(ability);
|
||||
this.text = ability.text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkEventType(GameEvent event, Game game) {
|
||||
return event.getType() == GameEvent.EventType.OPTION_USED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkTrigger(GameEvent event, Game game) {
|
||||
return event.getPlayerId().equals(this.getControllerId())
|
||||
&& event.getSourceId().equals(this.getSourceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRule() {
|
||||
return text.substring(0, 1).toUpperCase() + text.substring(1) + '.';
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReflexiveTriggeredAbility copy() {
|
||||
return new ReflexiveTriggeredAbility(this);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
package mage.abilities.effects.common;
|
||||
|
||||
import mage.MageObject;
|
||||
import mage.abilities.Ability;
|
||||
import mage.abilities.Mode;
|
||||
import mage.abilities.common.delayed.ReflexiveTriggeredAbility;
|
||||
import mage.abilities.costs.Cost;
|
||||
import mage.abilities.effects.OneShotEffect;
|
||||
import mage.constants.Outcome;
|
||||
import mage.game.Game;
|
||||
import mage.players.Player;
|
||||
import mage.util.CardUtil;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class DoWhenCostPaid extends OneShotEffect {
|
||||
|
||||
private final ReflexiveTriggeredAbility ability;
|
||||
private final Cost cost;
|
||||
private final String chooseUseText;
|
||||
private final boolean optional;
|
||||
|
||||
public DoWhenCostPaid(ReflexiveTriggeredAbility ability, Cost cost, String chooseUseText) {
|
||||
this(ability, cost, chooseUseText, true);
|
||||
}
|
||||
|
||||
public DoWhenCostPaid(ReflexiveTriggeredAbility ability, Cost cost, String chooseUseText, boolean optional) {
|
||||
super(Outcome.Benefit);
|
||||
this.ability = ability;
|
||||
this.cost = cost;
|
||||
this.chooseUseText = chooseUseText;
|
||||
this.optional = optional;
|
||||
}
|
||||
|
||||
private DoWhenCostPaid(final DoWhenCostPaid effect) {
|
||||
super(effect);
|
||||
this.ability = effect.ability.copy();
|
||||
this.cost = effect.cost.copy();
|
||||
this.chooseUseText = effect.chooseUseText;
|
||||
this.optional = effect.optional;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean apply(Game game, Ability source) {
|
||||
Player player = game.getPlayer(source.getControllerId());
|
||||
MageObject mageObject = game.getObject(source.getSourceId());
|
||||
if (player == null || mageObject == null) {
|
||||
return false;
|
||||
}
|
||||
String message = CardUtil.replaceSourceName(chooseUseText, mageObject.getLogName());
|
||||
Outcome payOutcome = ability.getEffects().getOutcome(source, this.outcome);
|
||||
if (!cost.canPay(source, source.getSourceId(), player.getId(), game)
|
||||
|| (optional && !player.chooseUse(payOutcome, message, source, game))) {
|
||||
return false;
|
||||
}
|
||||
cost.clearPaid();
|
||||
int bookmark = game.bookmarkState();
|
||||
if (cost.pay(source, game, source.getSourceId(), player.getId(), false)) {
|
||||
game.fireReflexiveTriggeredAbility(ability, source);
|
||||
player.resetStoredBookmark(game);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Cost getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText(Mode mode) {
|
||||
if (!staticText.isEmpty()) {
|
||||
return staticText;
|
||||
}
|
||||
return (optional ? "you may " : "") + getCostText() + ". When you do, " + ability.getText();
|
||||
}
|
||||
|
||||
private String getCostText() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String costText = cost.getText();
|
||||
if (costText != null
|
||||
&& !costText.toLowerCase(Locale.ENGLISH).startsWith("put")
|
||||
&& !costText.toLowerCase(Locale.ENGLISH).startsWith("return")
|
||||
&& !costText.toLowerCase(Locale.ENGLISH).startsWith("exile")
|
||||
&& !costText.toLowerCase(Locale.ENGLISH).startsWith("discard")
|
||||
&& !costText.toLowerCase(Locale.ENGLISH).startsWith("sacrifice")
|
||||
&& !costText.toLowerCase(Locale.ENGLISH).startsWith("remove")
|
||||
&& !costText.toLowerCase(Locale.ENGLISH).startsWith("pay")) {
|
||||
sb.append("pay ");
|
||||
}
|
||||
return sb.append(costText).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(String key, Object value) {
|
||||
super.setValue(key, value);
|
||||
ability.getEffects().setValue(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DoWhenCostPaid copy() {
|
||||
return new DoWhenCostPaid(this);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
|
||||
package mage.abilities.effects.common;
|
||||
|
||||
import mage.abilities.Ability;
|
||||
import mage.abilities.effects.OneShotEffect;
|
||||
import mage.constants.Outcome;
|
||||
import mage.game.Game;
|
||||
import mage.game.events.GameEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author LevelX2
|
||||
*/
|
||||
public class SendOptionUsedEventEffect extends OneShotEffect {
|
||||
|
||||
private final int value;
|
||||
|
||||
public SendOptionUsedEventEffect() {
|
||||
this(0);
|
||||
}
|
||||
|
||||
public SendOptionUsedEventEffect(int value) {
|
||||
super(Outcome.Detriment);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public SendOptionUsedEventEffect(final SendOptionUsedEventEffect effect) {
|
||||
super(effect);
|
||||
this.value = effect.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SendOptionUsedEventEffect copy() {
|
||||
return new SendOptionUsedEventEffect(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean apply(Game game, Ability source) {
|
||||
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.OPTION_USED, source.getOriginalId(), source.getSourceId(), source.getControllerId(), value));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import mage.abilities.Ability;
|
|||
import mage.abilities.ActivatedAbility;
|
||||
import mage.abilities.DelayedTriggeredAbility;
|
||||
import mage.abilities.TriggeredAbility;
|
||||
import mage.abilities.common.delayed.ReflexiveTriggeredAbility;
|
||||
import mage.abilities.effects.ContinuousEffect;
|
||||
import mage.abilities.effects.ContinuousEffects;
|
||||
import mage.abilities.effects.PreventionEffectData;
|
||||
|
|
@ -398,6 +399,8 @@ public interface Game extends MageItem, Serializable {
|
|||
|
||||
UUID addDelayedTriggeredAbility(DelayedTriggeredAbility delayedAbility, Ability source);
|
||||
|
||||
UUID fireReflexiveTriggeredAbility(ReflexiveTriggeredAbility reflexiveAbility, Ability source);
|
||||
|
||||
void applyEffects();
|
||||
|
||||
boolean checkStateAndTriggered();
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import mage.abilities.*;
|
|||
import mage.abilities.common.AttachableToRestrictedAbility;
|
||||
import mage.abilities.common.CantHaveMoreThanAmountCountersSourceAbility;
|
||||
import mage.abilities.common.SagaAbility;
|
||||
import mage.abilities.common.delayed.ReflexiveTriggeredAbility;
|
||||
import mage.abilities.effects.ContinuousEffect;
|
||||
import mage.abilities.effects.ContinuousEffects;
|
||||
import mage.abilities.effects.Effect;
|
||||
|
|
@ -1748,6 +1749,13 @@ public abstract class GameImpl implements Game, Serializable {
|
|||
return newAbility.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID fireReflexiveTriggeredAbility(ReflexiveTriggeredAbility reflexiveAbility, Ability source) {
|
||||
UUID uuid = this.addDelayedTriggeredAbility(reflexiveAbility, source);
|
||||
this.fireEvent(GameEvent.getEvent(GameEvent.EventType.OPTION_USED, source.getOriginalId(), source.getSourceId(), source.getControllerId()));
|
||||
return uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* 116.5. Each time a player would get priority, the game first performs all
|
||||
* applicable state-based actions as a single event (see rule 704,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue