Connive ability - fixed game error on usage (NPE), fixed game freeze on disconnect, fixed miss LKI related code;

This commit is contained in:
Oleg Agafonov 2023-05-09 15:36:33 +04:00
parent 3c66dc8706
commit a315171ca4
6 changed files with 54 additions and 21 deletions

View file

@ -14,12 +14,31 @@ import mage.players.Player;
import mage.util.CardUtil;
/**
* 701.47. Connive
* <p>
* 701.47a Certain abilities instruct a permanent to connive. To do so, that permanents controller draws a card,
* then discards a card. If a nonland card is discarded this way, that player puts a +1/+1 counter on the
* conniving permanent.
* <p>
* 701.47b A permanent connives after the process described in rule 701.47a is complete, even if some or
* all of those actions were impossible.
* <p>
* 701.47c If a permanent changes zones before an effect causes it to connive, its last known information is
* used to determine which object connived and who controlled it.
* <p>
* 701.47d If multiple permanents are instructed to connive at the same time, the first player in APNAP order
* who controls one or more of those permanents chooses one of them and it connives. Then if any permanents
* remain on the battlefield which have been instructed to connive and have not done so, this process is repeated.
* <p>
* 701.47e Connive N is a variant of connive. The permanents controller draws N cards, discards N cards, then
* puts a number of +1/+1 counters on the permanent equal to the number of nonland cards discarded this way.
*
* @author TheElk801
*/
public class ConniveSourceEffect extends OneShotEffect {
private final String selfName;
private final ReflexiveTriggeredAbility ability;
private final ReflexiveTriggeredAbility ability; // apply ability after connived
public ConniveSourceEffect() {
this("it");
@ -48,31 +67,37 @@ public class ConniveSourceEffect extends OneShotEffect {
@Override
public boolean apply(Game game, Ability source) {
Permanent permanent = source.getSourcePermanentIfItStillExists(game);
// 701.47c If a permanent changes zones before an effect causes it to connive,
// its last known information is used to determine which object connived and who controlled it.
Permanent permanent = source.getSourcePermanentOrLKI(game);
boolean connived = connive(permanent, 1, source, game);
if (ability != null) {
if (ability != null && connived) {
game.fireReflexiveTriggeredAbility(ability, source);
}
return connived || ability != null;
return connived;
}
/**
* @param permanent must use game.getPermanentOrLKIBattlefield in parent method due rules
* @param amount
* @param source
* @param game
* @return
*/
public static boolean connive(Permanent permanent, int amount, Ability source, Game game) {
if (amount < 1) {
return false;
}
boolean permanentStillOnBattlefield;
if (permanent == null) {
// If the permanent was killed, get last known information
permanent = (Permanent) game.getLastKnownInformation(source.getSourceId(), Zone.BATTLEFIELD);
permanentStillOnBattlefield = false;
} else {
permanentStillOnBattlefield = true;
return false;
}
boolean permanentStillOnBattlefield = game.getState().getZone(permanent.getId()) == Zone.BATTLEFIELD;
Player player = game.getPlayer(permanent.getControllerId());
if (player == null) {
return false;
}
player.drawCards(amount, source, game);
int counters = player
.discard(amount, false, false, source, game)