[MH2] Implemented Academy Manufactor (#7864)

* [MH2] Implemented Academy Manufactor

* [MH2] Implemented Chatterfang, Squirrel General
This commit is contained in:
Daniel Bomar 2021-06-02 20:46:51 -05:00 committed by GitHub
parent 722f4d391b
commit 71ea4a51d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 577 additions and 127 deletions

View file

@ -0,0 +1,133 @@
package mage.cards.a;
import java.util.Map;
import java.util.UUID;
import mage.MageInt;
import mage.abilities.Ability;
import mage.abilities.common.SimpleStaticAbility;
import mage.abilities.effects.ReplacementEffectImpl;
import mage.constants.Duration;
import mage.constants.Outcome;
import mage.constants.SubType;
import mage.cards.CardImpl;
import mage.cards.CardSetInfo;
import mage.constants.CardType;
import mage.game.Game;
import mage.game.events.CreateTokenEvent;
import mage.game.events.GameEvent;
import mage.game.permanent.token.ClueArtifactToken;
import mage.game.permanent.token.FoodToken;
import mage.game.permanent.token.Token;
import mage.game.permanent.token.TreasureToken;
/**
*
* @author weirddan455
*/
public final class AcademyManufactor extends CardImpl {
public AcademyManufactor(UUID ownerId, CardSetInfo setInfo) {
super(ownerId, setInfo, new CardType[]{CardType.ARTIFACT, CardType.CREATURE}, "{3}");
this.subtype.add(SubType.ASSEMBLY_WORKER);
this.power = new MageInt(1);
this.toughness = new MageInt(3);
// If you would create a Clue, Food, or Treasure token, instead create one of each.
this.addAbility(new SimpleStaticAbility(new AcademyManufactorEffect()));
}
private AcademyManufactor(final AcademyManufactor card) {
super(card);
}
@Override
public AcademyManufactor copy() {
return new AcademyManufactor(this);
}
}
class AcademyManufactorEffect extends ReplacementEffectImpl {
public AcademyManufactorEffect() {
super(Duration.WhileOnBattlefield, Outcome.Benefit);
this.staticText = "If you would create a Clue, Food, or Treasure token, instead create one of each";
}
private AcademyManufactorEffect(final AcademyManufactorEffect effect) {
super(effect);
}
@Override
public AcademyManufactorEffect copy() {
return new AcademyManufactorEffect(this);
}
@Override
public boolean checksEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.CREATE_TOKEN;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (event instanceof CreateTokenEvent && event.getPlayerId().equals(source.getControllerId())) {
CreateTokenEvent tokenEvent = (CreateTokenEvent) event;
for (Token token : tokenEvent.getTokens().keySet()) {
if (token instanceof ClueArtifactToken || token instanceof FoodToken || token instanceof TreasureToken) {
return true;
}
}
}
return false;
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
if (event instanceof CreateTokenEvent) {
CreateTokenEvent tokenEvent = (CreateTokenEvent) event;
int clues = 0;
int food = 0;
int treasures = 0;
ClueArtifactToken clueToken = null;
FoodToken foodToken = null;
TreasureToken treasureToken = null;
Map<Token, Integer> tokens = tokenEvent.getTokens();
for (Map.Entry<Token, Integer> entry : tokens.entrySet()) {
Token token = entry.getKey();
int amount = entry.getValue();
if (token instanceof ClueArtifactToken) {
clueToken = (ClueArtifactToken) token;
clues += amount;
}
else if (token instanceof FoodToken) {
foodToken = (FoodToken) token;
food += amount;
}
else if (token instanceof TreasureToken) {
treasureToken = (TreasureToken) token;
treasures += amount;
}
}
if (clueToken == null) {
clueToken = new ClueArtifactToken();
}
if (foodToken == null) {
foodToken = new FoodToken();
}
if (treasureToken == null) {
treasureToken = new TreasureToken();
}
int cluesToAdd = food + treasures;
int foodToAdd = clues + treasures;
int treasuresToAdd = clues + food;
tokens.put(clueToken, tokens.getOrDefault(clueToken, 0) + cluesToAdd);
tokens.put(foodToken, tokens.getOrDefault(foodToken, 0) + foodToAdd);
tokens.put(treasureToken, tokens.getOrDefault(treasureToken, 0) + treasuresToAdd);
}
return false;
}
}

View file

@ -0,0 +1,116 @@
package mage.cards.c;
import java.util.Map;
import java.util.UUID;
import mage.MageInt;
import mage.abilities.Ability;
import mage.abilities.common.SimpleActivatedAbility;
import mage.abilities.common.SimpleStaticAbility;
import mage.abilities.costs.common.SacrificeXTargetCost;
import mage.abilities.costs.mana.ManaCostsImpl;
import mage.abilities.dynamicvalue.common.GetXValue;
import mage.abilities.dynamicvalue.common.SignInversionDynamicValue;
import mage.abilities.effects.ReplacementEffectImpl;
import mage.abilities.effects.common.continuous.BoostTargetEffect;
import mage.constants.*;
import mage.abilities.keyword.ForestwalkAbility;
import mage.cards.CardImpl;
import mage.cards.CardSetInfo;
import mage.filter.common.FilterControlledPermanent;
import mage.game.Game;
import mage.game.events.CreateTokenEvent;
import mage.game.events.GameEvent;
import mage.game.permanent.token.SquirrelToken;
import mage.game.permanent.token.Token;
import mage.target.common.TargetCreaturePermanent;
/**
*
* @author weirddan455
*/
public final class ChatterfangSquirrelGeneral extends CardImpl {
private static final FilterControlledPermanent filter = new FilterControlledPermanent(SubType.SQUIRREL, "Squirrels");
public ChatterfangSquirrelGeneral(UUID ownerId, CardSetInfo setInfo) {
super(ownerId, setInfo, new CardType[]{CardType.CREATURE}, "{2}{G}");
this.addSuperType(SuperType.LEGENDARY);
this.subtype.add(SubType.SQUIRREL);
this.subtype.add(SubType.WARRIOR);
this.power = new MageInt(3);
this.toughness = new MageInt(3);
// Forestwalk
this.addAbility(new ForestwalkAbility());
// If one or more tokens would be created under your control, those tokens plus that many 1/1 green Squirrel creature tokens are created instead.
this.addAbility(new SimpleStaticAbility(new ChatterfangSquirrelGeneralReplacementEffect()));
// {B}, Sacrifice X Squirrels: Target creature gets +X/-X until end of turn.
Ability ability = new SimpleActivatedAbility(new BoostTargetEffect(
GetXValue.instance, new SignInversionDynamicValue(GetXValue.instance), Duration.EndOfTurn),
new ManaCostsImpl<>("{B}")
);
ability.addCost(new SacrificeXTargetCost(filter));
ability.addTarget(new TargetCreaturePermanent());
this.addAbility(ability);
}
private ChatterfangSquirrelGeneral(final ChatterfangSquirrelGeneral card) {
super(card);
}
@Override
public ChatterfangSquirrelGeneral copy() {
return new ChatterfangSquirrelGeneral(this);
}
}
class ChatterfangSquirrelGeneralReplacementEffect extends ReplacementEffectImpl {
public ChatterfangSquirrelGeneralReplacementEffect() {
super(Duration.WhileOnBattlefield, Outcome.Benefit);
this.staticText = "If one or more tokens would be created under your control, those tokens plus that many 1/1 green Squirrel creature tokens are created instead";
}
private ChatterfangSquirrelGeneralReplacementEffect(final ChatterfangSquirrelGeneralReplacementEffect effect) {
super(effect);
}
@Override
public ChatterfangSquirrelGeneralReplacementEffect copy() {
return new ChatterfangSquirrelGeneralReplacementEffect(this);
}
@Override
public boolean checksEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.CREATE_TOKEN;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
return true;
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
if (event instanceof CreateTokenEvent) {
CreateTokenEvent tokenEvent = (CreateTokenEvent) event;
SquirrelToken squirrelToken = null;
int amount = 0;
Map<Token, Integer> tokens = tokenEvent.getTokens();
for (Map.Entry<Token, Integer> entry : tokens.entrySet()) {
amount += entry.getValue();
if (entry.getKey() instanceof SquirrelToken) {
squirrelToken = (SquirrelToken) entry.getKey();
}
}
if (squirrelToken == null) {
squirrelToken = new SquirrelToken();
}
tokens.put(squirrelToken, tokens.getOrDefault(squirrelToken, 0) + amount);
}
return false;
}
}

View file

@ -1,5 +1,7 @@
package mage.cards.d;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import mage.abilities.Ability;
import mage.abilities.common.SimpleStaticAbility;
@ -14,6 +16,7 @@ import mage.game.Game;
import mage.game.events.CreateTokenEvent;
import mage.game.events.GameEvent;
import mage.game.permanent.token.AngelVigilanceToken;
import mage.game.permanent.token.Token;
/**
*
@ -62,13 +65,34 @@ class DivineVisitationEffect extends ReplacementEffectImpl {
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
return event.getPlayerId().equals(source.getControllerId())
&& ((CreateTokenEvent) event).getToken().isCreature();
if (event instanceof CreateTokenEvent && event.getPlayerId().equals(source.getControllerId())) {
CreateTokenEvent tokenEvent = (CreateTokenEvent) event;
for (Token token : tokenEvent.getTokens().keySet()) {
if (token.isCreature()) {
return true;
}
}
}
return false;
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
((CreateTokenEvent) event).setToken(new AngelVigilanceToken());
if (event instanceof CreateTokenEvent) {
int amount = 0;
CreateTokenEvent tokenEvent = (CreateTokenEvent) event;
Iterator<Map.Entry<Token, Integer>> it = tokenEvent.getTokens().entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Token, Integer> entry = it.next();
if (entry.getKey().isCreature()) {
amount += entry.getValue();
it.remove();
}
}
if (amount > 0) {
tokenEvent.getTokens().put(new AngelVigilanceToken(), amount);
}
}
return false;
}

View file

@ -9,6 +9,7 @@ import mage.constants.CardType;
import mage.constants.Duration;
import mage.constants.Outcome;
import mage.game.Game;
import mage.game.events.CreateTokenEvent;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
@ -69,7 +70,9 @@ class DoublingSeasonTokenEffect extends ReplacementEffectImpl {
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
event.setAmount(event.getAmount() * 2);
if (event instanceof CreateTokenEvent) {
((CreateTokenEvent) event).doubleTokens();
}
return false;
}

View file

@ -107,7 +107,12 @@ class EsixFractalBloomEffect extends ReplacementEffectImpl {
if (permanent == null) {
return false;
}
((CreateTokenEvent) event).setToken(copyPermanentToToken(permanent, game, source));
if (event instanceof CreateTokenEvent) {
CreateTokenEvent tokenEvent = (CreateTokenEvent) event;
int amount = tokenEvent.getAmount();
tokenEvent.getTokens().clear();
tokenEvent.getTokens().put(copyPermanentToToken(permanent, game, source), amount);
}
return false;
}

View file

@ -13,6 +13,7 @@ import mage.game.Game;
import mage.game.events.CreateTokenEvent;
import mage.game.events.GameEvent;
import mage.game.events.ZoneChangeEvent;
import mage.game.permanent.token.Token;
import mage.players.Player;
import java.util.UUID;
@ -77,9 +78,15 @@ class GatherSpecimensReplacementEffect extends ReplacementEffectImpl {
}
}
}
if (event.getType() == GameEvent.EventType.CREATE_TOKEN && ((CreateTokenEvent) event).getToken().isCreature()) {
if (event.getType() == GameEvent.EventType.CREATE_TOKEN) {
Player controller = game.getPlayer(source.getControllerId());
return controller != null && controller.hasOpponent(event.getPlayerId(), game);
if (controller != null && controller.hasOpponent(event.getPlayerId(), game)) {
for (Token token : ((CreateTokenEvent) event).getTokens().keySet()) {
if (token.isCreature()) {
return true;
}
}
}
}
return false;
}

View file

@ -10,6 +10,7 @@ import mage.constants.Duration;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.CreateTokenEvent;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
@ -73,7 +74,9 @@ class PrimalVigorTokenEffect extends ReplacementEffectImpl {
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
event.setAmount(event.getAmount() * 2);
if (event instanceof CreateTokenEvent) {
((CreateTokenEvent) event).doubleTokens();
}
return false;
}

View file

@ -29,6 +29,7 @@ public final class ModernHorizons2 extends ExpansionSet {
cards.add(new SetCardInfo("Abiding Grace", 1, Rarity.UNCOMMON, mage.cards.a.AbidingGrace.class));
cards.add(new SetCardInfo("Abundant Harvest", 147, Rarity.COMMON, mage.cards.a.AbundantHarvest.class));
cards.add(new SetCardInfo("Academy Manufactor", 219, Rarity.RARE, mage.cards.a.AcademyManufactor.class));
cards.add(new SetCardInfo("Aeromoeba", 37, Rarity.COMMON, mage.cards.a.Aeromoeba.class));
cards.add(new SetCardInfo("Aeve, Progenitor Ooze", 148, Rarity.RARE, mage.cards.a.AeveProgenitorOoze.class));
cards.add(new SetCardInfo("Angelic Curator", 262, Rarity.UNCOMMON, mage.cards.a.AngelicCurator.class));
@ -69,6 +70,7 @@ public final class ModernHorizons2 extends ExpansionSet {
cards.add(new SetCardInfo("Captured by Lagacs", 188, Rarity.COMMON, mage.cards.c.CapturedByLagacs.class));
cards.add(new SetCardInfo("Chainer, Nightmare Adept", 289, Rarity.RARE, mage.cards.c.ChainerNightmareAdept.class));
cards.add(new SetCardInfo("Chance Encounter", 277, Rarity.RARE, mage.cards.c.ChanceEncounter.class));
cards.add(new SetCardInfo("Chatterfang, Squirrel General", 151, Rarity.MYTHIC, mage.cards.c.ChatterfangSquirrelGeneral.class));
cards.add(new SetCardInfo("Chatterstorm", 152, Rarity.COMMON, mage.cards.c.Chatterstorm.class));
cards.add(new SetCardInfo("Chitterspitter", 153, Rarity.RARE, mage.cards.c.Chitterspitter.class));
cards.add(new SetCardInfo("Chrome Courier", 190, Rarity.COMMON, mage.cards.c.ChromeCourier.class));

View file

@ -0,0 +1,71 @@
package org.mage.test.cards.replacement;
import mage.constants.PhaseStep;
import mage.constants.Zone;
import org.junit.Test;
import org.mage.test.serverside.base.CardTestPlayerBase;
public class AcademyManufactorTest extends CardTestPlayerBase {
@Test
public void testAcademyManufactor() {
addCard(Zone.BATTLEFIELD, playerA, "Academy Manufactor");
addCard(Zone.BATTLEFIELD, playerA, "Plains");
addCard(Zone.HAND, playerA, "Thraben Inspector");
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Thraben Inspector");
setStopAt(1, PhaseStep.END_TURN);
execute();
assertAllCommandsUsed();
assertPermanentCount(playerA, "Plains", 1);
assertPermanentCount(playerA, "Academy Manufactor", 1);
assertPermanentCount(playerA, "Thraben Inspector", 1);
assertPermanentCount(playerA, "Clue", 1);
assertPermanentCount(playerA, "Food", 1);
assertPermanentCount(playerA, "Treasure", 1);
}
@Test
public void testMultipleReplacementEffect() {
addCard(Zone.BATTLEFIELD, playerA, "Academy Manufactor", 2);
addCard(Zone.BATTLEFIELD, playerA, "Anointed Procession");
addCard(Zone.BATTLEFIELD, playerA, "Plains");
addCard(Zone.HAND, playerA, "Thraben Inspector");
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Thraben Inspector");
setStopAt(1, PhaseStep.END_TURN);
execute();
assertAllCommandsUsed();
assertPermanentCount(playerA, "Plains", 1);
assertPermanentCount(playerA, "Academy Manufactor", 2);
assertPermanentCount(playerA, "Anointed Procession", 1);
assertPermanentCount(playerA, "Thraben Inspector", 1);
assertPermanentCount(playerA, "Clue", 6);
assertPermanentCount(playerA, "Food", 6);
assertPermanentCount(playerA, "Treasure", 6);
}
@Test
public void testTokenLimit() {
addCard(Zone.BATTLEFIELD, playerA, "Academy Manufactor", 6);
addCard(Zone.BATTLEFIELD, playerA, "Plains");
addCard(Zone.HAND, playerA, "Thraben Inspector");
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Thraben Inspector");
setStopAt(1, PhaseStep.END_TURN);
execute();
assertAllCommandsUsed();
assertPermanentCount(playerA, "Plains", 1);
assertPermanentCount(playerA, "Academy Manufactor", 6);
assertPermanentCount(playerA, "Thraben Inspector", 1);
// 8 permanents above + 500 token limit
assertPermanentCount(playerA, 508);
}
}

View file

@ -0,0 +1,54 @@
package org.mage.test.cards.replacement;
import mage.constants.PhaseStep;
import mage.constants.Zone;
import org.junit.Test;
import org.mage.test.serverside.base.CardTestPlayerBase;
public class ChatterfangSquirrelGeneralTest extends CardTestPlayerBase {
private static final String chatterfang = "Chatterfang, Squirrel General";
@Test
public void testChatterfang() {
addCard(Zone.BATTLEFIELD, playerA, chatterfang);
addCard(Zone.BATTLEFIELD, playerA, "Plains", 2);
addCard(Zone.HAND, playerA, "Raise the Alarm");
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Raise the Alarm");
setStopAt(1, PhaseStep.END_TURN);
execute();
assertAllCommandsUsed();
assertPermanentCount(playerA, "Plains", 2);
assertPermanentCount(playerA, chatterfang, 1);
assertPermanentCount(playerA, "Soldier", 2);
assertPermanentCount(playerA, "Squirrel", 2);
}
@Test
public void testChatterfangPlusAcademyManufactor() {
addCard(Zone.BATTLEFIELD, playerA, chatterfang);
addCard(Zone.BATTLEFIELD, playerA, "Academy Manufactor");
addCard(Zone.BATTLEFIELD, playerA, "Plains");
addCard(Zone.HAND, playerA, "Thraben Inspector");
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Thraben Inspector");
// Order Academy Manufactor replacement effect first
setChoice(playerA, "Academy Manufactor");
setStrictChooseMode(true);
setStopAt(1, PhaseStep.END_TURN);
execute();
assertAllCommandsUsed();
assertPermanentCount(playerA, "Plains", 1);
assertPermanentCount(playerA, chatterfang, 1);
assertPermanentCount(playerA, "Academy Manufactor", 1);
assertPermanentCount(playerA, "Clue", 1);
assertPermanentCount(playerA, "Food", 1);
assertPermanentCount(playerA, "Treasure" ,1);
assertPermanentCount(playerA, "Squirrel", 3);
}
}

View file

@ -5,6 +5,7 @@ import mage.abilities.effects.ReplacementEffectImpl;
import mage.constants.Duration;
import mage.constants.Outcome;
import mage.game.Game;
import mage.game.events.CreateTokenEvent;
import mage.game.events.GameEvent;
/**
@ -39,7 +40,9 @@ public class CreateTwiceThatManyTokensEffect extends ReplacementEffectImpl {
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
event.setAmount(event.getAmount() * 2);
if (event instanceof CreateTokenEvent) {
((CreateTokenEvent) event).doubleTokens();
}
return false;
}

View file

@ -3,22 +3,40 @@ package mage.game.events;
import mage.abilities.Ability;
import mage.game.permanent.token.Token;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class CreateTokenEvent extends GameEvent {
private Token token;
private final Map<Token, Integer> tokens = new HashMap<>();
public CreateTokenEvent(Ability source, UUID controllerId, int amount, Token token) {
super(GameEvent.EventType.CREATE_TOKEN, null, source, controllerId, amount, false);
this.token = token;
tokens.put(token, amount);
}
public Token getToken() {
return token;
public Map<Token, Integer> getTokens() {
return tokens;
}
public void setToken(Token token) {
this.token = token;
public void doubleTokens() {
for (Map.Entry<Token, Integer> entry : tokens.entrySet()) {
entry.setValue(entry.getValue() * 2);
}
}
@Override
public int getAmount() {
int amount = 0;
for (Integer num : tokens.values()) {
amount += num;
}
return amount;
}
@Override
public void setAmount(int amount) {
throw new UnsupportedOperationException("Do not use event.setAmount for tokens. Amount must be set individually in event.getTokens");
}
}

View file

@ -14,10 +14,8 @@ import mage.game.permanent.PermanentToken;
import mage.players.Player;
import mage.util.RandomUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.*;
import mage.abilities.SpellAbility;
import mage.abilities.effects.Effect;
import mage.abilities.effects.common.AttachEffect;
@ -184,13 +182,24 @@ public abstract class TokenImpl extends MageObjectImpl implements Token {
if (!created || !game.replaceEvent(event)) {
int currentTokens = game.getBattlefield().countTokens(event.getPlayerId());
int tokenSlots = Math.max(MAX_TOKENS_PER_GAME - currentTokens, 0);
if (event.getAmount() > tokenSlots) {
int amountToRemove = event.getAmount() - tokenSlots;
if (amountToRemove > 0) {
game.informPlayers(
"The token limit per player is " + MAX_TOKENS_PER_GAME + ", " + controller.getName()
+ " will only create " + tokenSlots + " tokens."
);
Iterator<Map.Entry<Token, Integer>> it = event.getTokens().entrySet().iterator();
while (it.hasNext() && amountToRemove > 0) {
Map.Entry<Token, Integer> entry = it.next();
int newValue = entry.getValue() - amountToRemove;
if (newValue > 0) {
entry.setValue(newValue);
break;
}
amountToRemove -= entry.getValue();
it.remove();
}
}
event.setAmount(Math.min(event.getAmount(), tokenSlots));
putOntoBattlefieldHelper(event, game, source, tapped, attacking, attackedPlayer, created);
return true;
}
@ -203,129 +212,131 @@ public abstract class TokenImpl extends MageObjectImpl implements Token {
return;
}
Token token = event.getToken();
int amount = event.getAmount();
String setCode = token instanceof TokenImpl ? ((TokenImpl) token).getSetCode(game, event.getSourceId()) : null;
for (Map.Entry<Token, Integer> entry : event.getTokens().entrySet()) {
Token token = entry.getKey();
int amount = entry.getValue();
String setCode = token instanceof TokenImpl ? ((TokenImpl) token).getSetCode(game, event.getSourceId()) : null;
List<Permanent> needTokens = new ArrayList<>();
List<Permanent> allowedTokens = new ArrayList<>();
List<Permanent> needTokens = new ArrayList<>();
List<Permanent> allowedTokens = new ArrayList<>();
// prepare tokens to enter
for (int i = 0; i < amount; i++) {
// use event.getPlayerId() as controller cause it can be replaced by replacement effect
PermanentToken newPermanent = new PermanentToken(token, event.getPlayerId(), setCode, game);
game.getState().addCard(newPermanent);
needTokens.add(newPermanent);
game.getPermanentsEntering().put(newPermanent.getId(), newPermanent);
newPermanent.setTapped(tapped);
// prepare tokens to enter
for (int i = 0; i < amount; i++) {
// use event.getPlayerId() as controller cause it can be replaced by replacement effect
PermanentToken newPermanent = new PermanentToken(token, event.getPlayerId(), setCode, game);
game.getState().addCard(newPermanent);
needTokens.add(newPermanent);
game.getPermanentsEntering().put(newPermanent.getId(), newPermanent);
newPermanent.setTapped(tapped);
ZoneChangeEvent emptyEvent = new ZoneChangeEvent(newPermanent, newPermanent.getControllerId(), Zone.OUTSIDE, Zone.BATTLEFIELD);
// tokens zcc must simulate card's zcc too keep copied card/spell settings
// (example: etb's kicker ability of copied creature spell, see tests with Deathforge Shaman)
newPermanent.updateZoneChangeCounter(game, emptyEvent);
}
ZoneChangeEvent emptyEvent = new ZoneChangeEvent(newPermanent, newPermanent.getControllerId(), Zone.OUTSIDE, Zone.BATTLEFIELD);
// tokens zcc must simulate card's zcc too keep copied card/spell settings
// (example: etb's kicker ability of copied creature spell, see tests with Deathforge Shaman)
newPermanent.updateZoneChangeCounter(game, emptyEvent);
}
// check ETB effects
game.setScopeRelevant(true);
for (Permanent permanent : needTokens) {
if (permanent.entersBattlefield(source, game, Zone.OUTSIDE, true)) {
allowedTokens.add(permanent);
} else {
// check ETB effects
game.setScopeRelevant(true);
for (Permanent permanent : needTokens) {
if (permanent.entersBattlefield(source, game, Zone.OUTSIDE, true)) {
allowedTokens.add(permanent);
} else {
game.getPermanentsEntering().remove(permanent.getId());
}
}
game.setScopeRelevant(false);
// put allowed tokens to play
int createOrder = game.getState().getNextPermanentOrderNumber();
for (Permanent permanent : allowedTokens) {
game.addPermanent(permanent, createOrder);
permanent.setZone(Zone.BATTLEFIELD, game);
game.getPermanentsEntering().remove(permanent.getId());
}
}
game.setScopeRelevant(false);
// put allowed tokens to play
int createOrder = game.getState().getNextPermanentOrderNumber();
for (Permanent permanent : allowedTokens) {
game.addPermanent(permanent, createOrder);
permanent.setZone(Zone.BATTLEFIELD, game);
game.getPermanentsEntering().remove(permanent.getId());
// keep tokens ids
if (token instanceof TokenImpl) {
((TokenImpl) token).lastAddedTokenIds.add(permanent.getId());
((TokenImpl) token).lastAddedTokenId = permanent.getId();
}
// created token events
ZoneChangeEvent zccEvent = new ZoneChangeEvent(permanent, permanent.getControllerId(), Zone.OUTSIDE, Zone.BATTLEFIELD);
game.addSimultaneousEvent(zccEvent);
if (permanent instanceof PermanentToken && created) {
game.addSimultaneousEvent(new CreatedTokenEvent(source, (PermanentToken) permanent));
}
// handle auras coming into the battlefield
// code refactored from CopyPermanentEffect
if (permanent.getSubtype().contains(SubType.AURA)) {
Outcome auraOutcome = Outcome.BoostCreature;
Target auraTarget = null;
// attach - search effect in spell ability (example: cast Utopia Sprawl, cast Estrid's Invocation on it)
for (Ability ability : permanent.getAbilities()) {
if (!(ability instanceof SpellAbility)) {
continue;
}
auraOutcome = ability.getEffects().getOutcome(ability);
for (Effect effect : ability.getEffects()) {
if (!(effect instanceof AttachEffect)) {
continue;
}
if (permanent.getSpellAbility().getTargets().size() > 0) {
auraTarget = permanent.getSpellAbility().getTargets().get(0);
}
}
// keep tokens ids
if (token instanceof TokenImpl) {
((TokenImpl) token).lastAddedTokenIds.add(permanent.getId());
((TokenImpl) token).lastAddedTokenId = permanent.getId();
}
// enchant - search in all abilities (example: cast Estrid's Invocation on enchanted creature by Estrid, the Masked second ability, cast Estrid's Invocation on it)
if (auraTarget == null) {
// created token events
ZoneChangeEvent zccEvent = new ZoneChangeEvent(permanent, permanent.getControllerId(), Zone.OUTSIDE, Zone.BATTLEFIELD);
game.addSimultaneousEvent(zccEvent);
if (permanent instanceof PermanentToken && created) {
game.addSimultaneousEvent(new CreatedTokenEvent(source, (PermanentToken) permanent));
}
// handle auras coming into the battlefield
// code refactored from CopyPermanentEffect
if (permanent.getSubtype().contains(SubType.AURA)) {
Outcome auraOutcome = Outcome.BoostCreature;
Target auraTarget = null;
// attach - search effect in spell ability (example: cast Utopia Sprawl, cast Estrid's Invocation on it)
for (Ability ability : permanent.getAbilities()) {
if (!(ability instanceof EnchantAbility)) {
if (!(ability instanceof SpellAbility)) {
continue;
}
auraOutcome = ability.getEffects().getOutcome(ability);
if (ability.getTargets().size() > 0) { // Animate Dead don't have targets
auraTarget = ability.getTargets().get(0);
for (Effect effect : ability.getEffects()) {
if (!(effect instanceof AttachEffect)) {
continue;
}
if (permanent.getSpellAbility().getTargets().size() > 0) {
auraTarget = permanent.getSpellAbility().getTargets().get(0);
}
}
}
// enchant - search in all abilities (example: cast Estrid's Invocation on enchanted creature by Estrid, the Masked second ability, cast Estrid's Invocation on it)
if (auraTarget == null) {
for (Ability ability : permanent.getAbilities()) {
if (!(ability instanceof EnchantAbility)) {
continue;
}
auraOutcome = ability.getEffects().getOutcome(ability);
if (ability.getTargets().size() > 0) { // Animate Dead don't have targets
auraTarget = ability.getTargets().get(0);
}
}
}
// if this is a copy of a copy, the copy's target has been copied and needs to be cleared
if (auraTarget == null) {
break;
}
// clear selected target
if (auraTarget.getFirstTarget() != null) {
auraTarget.remove(auraTarget.getFirstTarget());
}
// select new target
auraTarget.setNotTarget(true);
if (!controller.choose(auraOutcome, auraTarget, source.getSourceId(), game)) {
break;
}
UUID targetId = auraTarget.getFirstTarget();
Permanent targetPermanent = game.getPermanent(targetId);
Player targetPlayer = game.getPlayer(targetId);
if (targetPermanent != null) {
targetPermanent.addAttachment(permanent.getId(), source, game);
} else if (targetPlayer != null) {
targetPlayer.addAttachment(permanent.getId(), source, game);
}
}
// end of aura code : just remove this line if everything works out well
// must attack
if (attacking && game.getCombat() != null && game.getActivePlayerId().equals(permanent.getControllerId())) {
game.getCombat().addAttackingCreature(permanent.getId(), game, attackedPlayer);
}
// if this is a copy of a copy, the copy's target has been copied and needs to be cleared
if (auraTarget == null) {
break;
// game logs
if (created) {
game.informPlayers(controller.getLogName() + " creates a " + permanent.getLogName() + " token");
} else {
game.informPlayers(permanent.getLogName() + " enters the battlefield as a token under " + controller.getLogName() + "'s control'");
}
// clear selected target
if (auraTarget.getFirstTarget() != null) {
auraTarget.remove(auraTarget.getFirstTarget());
}
// select new target
auraTarget.setNotTarget(true);
if (!controller.choose(auraOutcome, auraTarget, source.getSourceId(), game)) {
break;
}
UUID targetId = auraTarget.getFirstTarget();
Permanent targetPermanent = game.getPermanent(targetId);
Player targetPlayer = game.getPlayer(targetId);
if (targetPermanent != null) {
targetPermanent.addAttachment(permanent.getId(), source, game);
} else if (targetPlayer != null) {
targetPlayer.addAttachment(permanent.getId(), source, game);
}
}
// end of aura code : just remove this line if everything works out well
// must attack
if (attacking && game.getCombat() != null && game.getActivePlayerId().equals(permanent.getControllerId())) {
game.getCombat().addAttackingCreature(permanent.getId(), game, attackedPlayer);
}
// game logs
if (created) {
game.informPlayers(controller.getLogName() + " creates a " + permanent.getLogName() + " token");
} else {
game.informPlayers(permanent.getLogName() + " enters the battlefield as a token under " + controller.getLogName() + "'s control'");
}
}
game.getState().applyEffects(game); // Needed to do it here without LKIReset i.e. do get SwordOfTheMeekTest running correctly.