refactor/cleanup DoIfCostPaid (#11656)

This commit is contained in:
xenohedron 2024-01-14 19:57:24 -05:00 committed by GitHub
parent aa642324d7
commit a090a2a9d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,8 +15,8 @@ import mage.util.CardUtil;
public class DoIfCostPaid extends OneShotEffect { public class DoIfCostPaid extends OneShotEffect {
protected Effects executingEffects = new Effects(); protected final Effects executingEffects;
protected Effects otherwiseEffects = new Effects(); // used for Imprison protected final Effects otherwiseEffects;
protected final Cost cost; protected final Cost cost;
private final String chooseUseText; private final String chooseUseText;
private final boolean optional; 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) { public DoIfCostPaid(Effect effectOnPaid, Effect effectOnNotPaid, Cost cost, String chooseUseText, boolean optional) {
super(Outcome.Benefit); super(Outcome.Benefit);
this.executingEffects = new Effects();
this.otherwiseEffects = new Effects();
if (effectOnPaid != null) { if (effectOnPaid != null) {
this.executingEffects.add(effectOnPaid); this.executingEffects.add(effectOnPaid);
} }
@ -73,32 +75,14 @@ public class DoIfCostPaid extends OneShotEffect {
return this; return this;
} }
public Effects getExecutingEffects() {
return this.executingEffects;
}
public Effects getOtherwiseEffects() {
return this.otherwiseEffects;
}
@Override @Override
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
Player player = getPayingPlayer(game, source); Player player = getPayingPlayer(game, source);
MageObject mageObject = game.getObject(source); MageObject mageObject = game.getObject(source);
if (player != null && mageObject != null) { if (player == null || mageObject == null) {
String message; return false;
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) + "?"; String message = CardUtil.replaceSourceName(makeChooseText(source), mageObject.getName());
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); Outcome payOutcome = executingEffects.getOutcome(source, this.outcome);
boolean canPay = cost.canPay(source, source, player.getId(), game); boolean canPay = cost.canPay(source, source, player.getId(), game);
boolean didPay = false; boolean didPay = false;
@ -108,7 +92,7 @@ public class DoIfCostPaid extends OneShotEffect {
if (cost.pay(source, game, source, player.getId(), false)) { if (cost.pay(source, game, source, player.getId(), false)) {
didPay = true; didPay = true;
game.informPlayers(player.getLogName() + " paid for " + mageObject.getLogName() + " - " + message); game.informPlayers(player.getLogName() + " paid for " + mageObject.getLogName() + " - " + message);
result &= applyEffects(game, source, executingEffects); applyEffects(game, source, executingEffects);
player.resetStoredBookmark(game); // otherwise you can e.g. undo card drawn with Mentor of the Meek player.resetStoredBookmark(game); // otherwise you can e.g. undo card drawn with Mentor of the Meek
} else { } else {
// Paying cost was cancels so try to undo payment so far // Paying cost was cancels so try to undo payment so far
@ -119,26 +103,36 @@ public class DoIfCostPaid extends OneShotEffect {
// Not leaking the information in the game log that the player could // Not leaking the information in the game log that the player could
// not actually pay the cost, in case it is an hidden one. // not actually pay the cost, in case it is an hidden one.
game.informPlayers(player.getLogName() + " did not pay for " + mageObject.getLogName() + " - " + message); game.informPlayers(player.getLogName() + " did not pay for " + mageObject.getLogName() + " - " + message);
result &= applyEffects(game, source, otherwiseEffects); applyEffects(game, source, otherwiseEffects);
} }
return result; return true;
}
return false;
} }
private boolean applyEffects(Game game, Ability source, Effects effects) { private void applyEffects(Game game, Ability source, Effects effects) {
boolean result = true;
if (!effects.isEmpty()) { if (!effects.isEmpty()) {
for (Effect effect : effects) { for (Effect effect : effects) {
effect.setTargetPointer(this.targetPointer); effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) { if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source); effect.apply(game, source);
} else { } else {
game.addEffect((ContinuousEffect) effect, source); 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) { protected Player getPayingPlayer(Game game, Ability source) {
@ -151,7 +145,7 @@ public class DoIfCostPaid extends OneShotEffect {
@Override @Override
public String getText(Mode mode) { public String getText(Mode mode) {
if (!staticText.isEmpty()) { if (staticText != null && !staticText.isEmpty()) {
return staticText; return staticText;
} }
return (optional ? "you may " : "") return (optional ? "you may " : "")