updated Hotheaded Giant to not use player-scope watcher

This commit is contained in:
Evan Kranzler 2019-01-21 19:10:35 -05:00
parent dad4a3e9ff
commit 0c953e964f

View file

@ -1,13 +1,11 @@
package mage.cards.h; package mage.cards.h;
import java.util.UUID;
import mage.MageInt; import mage.MageInt;
import mage.ObjectColor; import mage.MageObjectReference;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.common.EntersBattlefieldAbility; import mage.abilities.common.EntersBattlefieldAbility;
import mage.abilities.condition.Condition; import mage.abilities.condition.Condition;
import mage.abilities.condition.InvertCondition;
import mage.abilities.decorator.ConditionalOneShotEffect; import mage.abilities.decorator.ConditionalOneShotEffect;
import mage.abilities.effects.common.counter.AddCountersSourceEffect; import mage.abilities.effects.common.counter.AddCountersSourceEffect;
import mage.abilities.keyword.HasteAbility; import mage.abilities.keyword.HasteAbility;
@ -17,22 +15,21 @@ import mage.constants.CardType;
import mage.constants.SubType; import mage.constants.SubType;
import mage.constants.WatcherScope; import mage.constants.WatcherScope;
import mage.counters.CounterType; import mage.counters.CounterType;
import mage.filter.FilterSpell;
import mage.filter.predicate.mageobject.ColorPredicate;
import mage.game.Game; import mage.game.Game;
import mage.game.events.GameEvent; import mage.game.events.GameEvent;
import mage.game.events.GameEvent.EventType; import mage.game.events.GameEvent.EventType;
import mage.game.stack.Spell; import mage.game.stack.Spell;
import mage.watchers.Watcher; import mage.watchers.Watcher;
import java.util.*;
/** /**
*
* @author jeffwadsworth * @author jeffwadsworth
*/ */
public final class HotheadedGiant extends CardImpl { public final class HotheadedGiant extends CardImpl {
public HotheadedGiant(UUID ownerId, CardSetInfo setInfo) { public HotheadedGiant(UUID ownerId, CardSetInfo setInfo) {
super(ownerId,setInfo,new CardType[]{CardType.CREATURE},"{3}{R}"); super(ownerId, setInfo, new CardType[]{CardType.CREATURE}, "{3}{R}");
this.subtype.add(SubType.GIANT); this.subtype.add(SubType.GIANT);
this.subtype.add(SubType.WARRIOR); this.subtype.add(SubType.WARRIOR);
@ -43,12 +40,15 @@ public final class HotheadedGiant extends CardImpl {
this.addAbility(HasteAbility.getInstance()); this.addAbility(HasteAbility.getInstance());
// Hotheaded Giant enters the battlefield with two -1/-1 counters on it unless you've cast another red spell this turn. // Hotheaded Giant enters the battlefield with two -1/-1 counters on it unless you've cast another red spell this turn.
Condition condition = new CastRedSpellThisTurnCondition(); this.addAbility(new EntersBattlefieldAbility(
this.addAbility(new EntersBattlefieldAbility(new ConditionalOneShotEffect(new AddCountersSourceEffect(CounterType.M1M1.createInstance(2)), new InvertCondition(condition), ""), "with two -1/-1 counters on it unless you've cast another red spell this turn"), new HotHeadedGiantWatcher(this.getId())); new ConditionalOneShotEffect(
new AddCountersSourceEffect(CounterType.M1M1.createInstance(2)),
CastRedSpellThisTurnCondition.instance, ""
), "with two -1/-1 counters on it unless you've cast another red spell this turn"
), new HotHeadedGiantWatcher());
} }
public HotheadedGiant(final HotheadedGiant card) { private HotheadedGiant(final HotheadedGiant card) {
super(card); super(card);
} }
@ -58,36 +58,29 @@ public final class HotheadedGiant extends CardImpl {
} }
} }
class CastRedSpellThisTurnCondition implements Condition { enum CastRedSpellThisTurnCondition implements Condition {
instance;
@Override @Override
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
HotHeadedGiantWatcher watcher = game.getState().getWatcher(HotHeadedGiantWatcher.class, source.getControllerId()); HotHeadedGiantWatcher watcher = game.getState().getWatcher(HotHeadedGiantWatcher.class);
if (watcher != null) { if (watcher != null) {
return watcher.conditionMet(); return watcher.conditionMet(source, game);
} }
return false; return true;
} }
} }
class HotHeadedGiantWatcher extends Watcher { class HotHeadedGiantWatcher extends Watcher {
private final Map<UUID, Set<MageObjectReference>> playerMap = new HashMap();
private static final FilterSpell filter = new FilterSpell(); HotHeadedGiantWatcher() {
super(HotHeadedGiantWatcher.class.getSimpleName(), WatcherScope.GAME);
static {
filter.add(new ColorPredicate(ObjectColor.RED));
} }
private UUID cardId; private HotHeadedGiantWatcher(final HotHeadedGiantWatcher watcher) {
public HotHeadedGiantWatcher(UUID cardId) {
super(HotHeadedGiantWatcher.class.getSimpleName(), WatcherScope.PLAYER);
this.cardId = cardId;
}
public HotHeadedGiantWatcher(final HotHeadedGiantWatcher watcher) {
super(watcher); super(watcher);
this.cardId = watcher.cardId; this.playerMap.putAll(watcher.playerMap);
} }
@Override @Override
@ -97,21 +90,30 @@ class HotHeadedGiantWatcher extends Watcher {
@Override @Override
public void watch(GameEvent event, Game game) { public void watch(GameEvent event, Game game) {
if (condition == true) { //no need to check - condition has already occured if (event.getType() == EventType.SPELL_CAST) {
return;
}
if (event.getType() == EventType.SPELL_CAST
&& controllerId.equals(event.getPlayerId())) {
Spell spell = game.getStack().getSpell(event.getTargetId()); Spell spell = game.getStack().getSpell(event.getTargetId());
if (!spell.getSourceId().equals(cardId) && filter.match(spell, game)) { if (spell.getColor(game).isRed()) {
condition = true; playerMap.putIfAbsent(event.getPlayerId(), new HashSet());
playerMap.get(event.getPlayerId()).add(new MageObjectReference(spell, game));
} }
} }
} }
public boolean conditionMet(Ability source, Game game) {
if (!playerMap.containsKey(source.getControllerId())) {
return true;
}
for (MageObjectReference mor : playerMap.get(source.getControllerId())) {
if (!mor.refersTo(source.getSourceId(), game)) {
return false;
}
}
return true;
}
@Override @Override
public void reset() { public void reset() {
super.reset(); super.reset();
condition = false; playerMap.clear();
} }
} }