mirror of
https://github.com/magefree/mage.git
synced 2025-12-20 02:30:08 -08:00
refactor/cleanup DoIfCostPaid (#11656)
This commit is contained in:
parent
aa642324d7
commit
a090a2a9d0
1 changed files with 47 additions and 53 deletions
|
|
@ -15,8 +15,8 @@ import mage.util.CardUtil;
|
|||
|
||||
public class DoIfCostPaid extends OneShotEffect {
|
||||
|
||||
protected Effects executingEffects = new Effects();
|
||||
protected Effects otherwiseEffects = new Effects(); // used for Imprison
|
||||
protected final Effects executingEffects;
|
||||
protected final Effects otherwiseEffects;
|
||||
protected final Cost cost;
|
||||
private final String chooseUseText;
|
||||
private final boolean optional;
|
||||
|
|
@ -43,6 +43,8 @@ public class DoIfCostPaid extends OneShotEffect {
|
|||
|
||||
public DoIfCostPaid(Effect effectOnPaid, Effect effectOnNotPaid, Cost cost, String chooseUseText, boolean optional) {
|
||||
super(Outcome.Benefit);
|
||||
this.executingEffects = new Effects();
|
||||
this.otherwiseEffects = new Effects();
|
||||
if (effectOnPaid != null) {
|
||||
this.executingEffects.add(effectOnPaid);
|
||||
}
|
||||
|
|
@ -73,72 +75,64 @@ public class DoIfCostPaid extends OneShotEffect {
|
|||
return this;
|
||||
}
|
||||
|
||||
public Effects getExecutingEffects() {
|
||||
return this.executingEffects;
|
||||
}
|
||||
|
||||
public Effects getOtherwiseEffects() {
|
||||
return this.otherwiseEffects;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean apply(Game game, Ability source) {
|
||||
Player player = getPayingPlayer(game, source);
|
||||
MageObject mageObject = game.getObject(source);
|
||||
if (player != null && mageObject != null) {
|
||||
String message;
|
||||
if (chooseUseText == null) {
|
||||
String effectText = executingEffects.getText(source.getModes().getMode());
|
||||
if (!effectText.isEmpty() && effectText.charAt(effectText.length() - 1) == '.') {
|
||||
effectText = effectText.substring(0, effectText.length() - 1);
|
||||
}
|
||||
message = CardUtil.addCostVerb(cost.getText()) + (effectText.isEmpty() ? "" : " and " + effectText) + "?";
|
||||
message = Character.toUpperCase(message.charAt(0)) + message.substring(1);
|
||||
} else {
|
||||
message = chooseUseText;
|
||||
}
|
||||
message = CardUtil.replaceSourceName(message, mageObject.getName());
|
||||
boolean result = true;
|
||||
Outcome payOutcome = executingEffects.getOutcome(source, this.outcome);
|
||||
boolean canPay = cost.canPay(source, source, player.getId(), game);
|
||||
boolean didPay = false;
|
||||
if (canPay && (!optional || player.chooseUse(payOutcome, message, source, game))) {
|
||||
cost.clearPaid();
|
||||
int bookmark = game.bookmarkState();
|
||||
if (cost.pay(source, game, source, player.getId(), false)) {
|
||||
didPay = true;
|
||||
game.informPlayers(player.getLogName() + " paid for " + mageObject.getLogName() + " - " + message);
|
||||
result &= applyEffects(game, source, executingEffects);
|
||||
player.resetStoredBookmark(game); // otherwise you can e.g. undo card drawn with Mentor of the Meek
|
||||
} else {
|
||||
// Paying cost was cancels so try to undo payment so far
|
||||
player.restoreState(bookmark, DoIfCostPaid.class.getName(), game);
|
||||
}
|
||||
}
|
||||
if (!didPay) {
|
||||
// Not leaking the information in the game log that the player could
|
||||
// not actually pay the cost, in case it is an hidden one.
|
||||
game.informPlayers(player.getLogName() + " did not pay for " + mageObject.getLogName() + " - " + message);
|
||||
result &= applyEffects(game, source, otherwiseEffects);
|
||||
}
|
||||
return result;
|
||||
if (player == null || mageObject == null) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
String message = CardUtil.replaceSourceName(makeChooseText(source), mageObject.getName());
|
||||
Outcome payOutcome = executingEffects.getOutcome(source, this.outcome);
|
||||
boolean canPay = cost.canPay(source, source, player.getId(), game);
|
||||
boolean didPay = false;
|
||||
if (canPay && (!optional || player.chooseUse(payOutcome, message, source, game))) {
|
||||
cost.clearPaid();
|
||||
int bookmark = game.bookmarkState();
|
||||
if (cost.pay(source, game, source, player.getId(), false)) {
|
||||
didPay = true;
|
||||
game.informPlayers(player.getLogName() + " paid for " + mageObject.getLogName() + " - " + message);
|
||||
applyEffects(game, source, executingEffects);
|
||||
player.resetStoredBookmark(game); // otherwise you can e.g. undo card drawn with Mentor of the Meek
|
||||
} else {
|
||||
// Paying cost was cancels so try to undo payment so far
|
||||
player.restoreState(bookmark, DoIfCostPaid.class.getName(), game);
|
||||
}
|
||||
}
|
||||
if (!didPay) {
|
||||
// Not leaking the information in the game log that the player could
|
||||
// not actually pay the cost, in case it is an hidden one.
|
||||
game.informPlayers(player.getLogName() + " did not pay for " + mageObject.getLogName() + " - " + message);
|
||||
applyEffects(game, source, otherwiseEffects);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean applyEffects(Game game, Ability source, Effects effects) {
|
||||
boolean result = true;
|
||||
private void applyEffects(Game game, Ability source, Effects effects) {
|
||||
if (!effects.isEmpty()) {
|
||||
for (Effect effect : effects) {
|
||||
effect.setTargetPointer(this.targetPointer);
|
||||
if (effect instanceof OneShotEffect) {
|
||||
result &= effect.apply(game, source);
|
||||
effect.apply(game, source);
|
||||
} else {
|
||||
game.addEffect((ContinuousEffect) effect, source);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String makeChooseText(Ability source) {
|
||||
if (chooseUseText != null && !chooseUseText.isEmpty()) {
|
||||
return chooseUseText;
|
||||
}
|
||||
String message;
|
||||
String effectText = executingEffects.getText(source.getModes().getMode());
|
||||
if (!effectText.isEmpty() && effectText.charAt(effectText.length() - 1) == '.') {
|
||||
effectText = effectText.substring(0, effectText.length() - 1);
|
||||
}
|
||||
message = CardUtil.addCostVerb(cost.getText()) + (effectText.isEmpty() ? "" : " and " + effectText) + "?";
|
||||
message = Character.toUpperCase(message.charAt(0)) + message.substring(1);
|
||||
return message;
|
||||
}
|
||||
|
||||
protected Player getPayingPlayer(Game game, Ability source) {
|
||||
|
|
@ -151,7 +145,7 @@ public class DoIfCostPaid extends OneShotEffect {
|
|||
|
||||
@Override
|
||||
public String getText(Mode mode) {
|
||||
if (!staticText.isEmpty()) {
|
||||
if (staticText != null && !staticText.isEmpty()) {
|
||||
return staticText;
|
||||
}
|
||||
return (optional ? "you may " : "")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue