forked from External/mage
updated implementation of abilities which trigger on the second card in a turn being drawn
This commit is contained in:
parent
f450de0d51
commit
b7e5d44b9d
2 changed files with 60 additions and 34 deletions
|
|
@ -1,58 +1,46 @@
|
|||
package mage.abilities.common;
|
||||
|
||||
import mage.abilities.TriggeredAbilityImpl;
|
||||
import mage.abilities.dynamicvalue.common.CardsDrawnThisTurnDynamicValue;
|
||||
import mage.abilities.effects.Effect;
|
||||
import mage.abilities.hint.Hint;
|
||||
import mage.abilities.hint.ValueHint;
|
||||
import mage.constants.WatcherScope;
|
||||
import mage.constants.Zone;
|
||||
import mage.game.Game;
|
||||
import mage.game.events.GameEvent;
|
||||
import mage.watchers.common.CardsAmountDrawnThisTurnWatcher;
|
||||
import mage.watchers.Watcher;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author TheElk801
|
||||
*/
|
||||
public class DrawSecondCardTriggeredAbility extends TriggeredAbilityImpl {
|
||||
|
||||
private boolean triggeredOnce = false;
|
||||
private static final Hint hint = new ValueHint(
|
||||
"Cards drawn this turn", CardsDrawnThisTurnDynamicValue.instance
|
||||
);
|
||||
|
||||
public DrawSecondCardTriggeredAbility(Effect effect, boolean optional) {
|
||||
super(Zone.ALL, effect, optional);
|
||||
this.addWatcher(new CardsAmountDrawnThisTurnWatcher());
|
||||
super(Zone.BATTLEFIELD, effect, optional);
|
||||
this.addWatcher(new DrawSecondCardWatcher());
|
||||
this.addHint(hint);
|
||||
}
|
||||
|
||||
private DrawSecondCardTriggeredAbility(final DrawSecondCardTriggeredAbility ability) {
|
||||
super(ability);
|
||||
this.triggeredOnce = ability.triggeredOnce;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkEventType(GameEvent event, Game game) {
|
||||
return event.getType() == GameEvent.EventType.DREW_CARD
|
||||
|| event.getType() == GameEvent.EventType.END_PHASE_POST;
|
||||
return event.getType() == GameEvent.EventType.DREW_CARD;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkTrigger(GameEvent event, Game game) {
|
||||
if (event.getType() == GameEvent.EventType.END_PHASE_POST) {
|
||||
triggeredOnce = false;
|
||||
return false;
|
||||
}
|
||||
if (event.getType() != GameEvent.EventType.DREW_CARD
|
||||
|| !event.getPlayerId().equals(controllerId)
|
||||
|| game.getPermanent(sourceId) == null) {
|
||||
return false;
|
||||
}
|
||||
if (triggeredOnce) {
|
||||
return false;
|
||||
}
|
||||
CardsAmountDrawnThisTurnWatcher watcher = game.getState().getWatcher(CardsAmountDrawnThisTurnWatcher.class);
|
||||
if (watcher == null) {
|
||||
return false;
|
||||
}
|
||||
if (watcher.getAmountCardsDrawn(controllerId) > 1) {
|
||||
triggeredOnce = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
DrawSecondCardWatcher watcher = game.getState().getWatcher(DrawSecondCardWatcher.class);
|
||||
return watcher != null && watcher.checkEvent(getControllerId(), event);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -65,3 +53,36 @@ public class DrawSecondCardTriggeredAbility extends TriggeredAbilityImpl {
|
|||
return new DrawSecondCardTriggeredAbility(this);
|
||||
}
|
||||
}
|
||||
|
||||
class DrawSecondCardWatcher extends Watcher {
|
||||
|
||||
private final Set<UUID> drewOnce = new HashSet<>();
|
||||
private final Map<UUID, UUID> secondDrawMap = new HashMap<>();
|
||||
|
||||
DrawSecondCardWatcher() {
|
||||
super(WatcherScope.GAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void watch(GameEvent event, Game game) {
|
||||
if (event.getType() != GameEvent.EventType.DREW_CARD) {
|
||||
return;
|
||||
}
|
||||
if (drewOnce.contains(event.getPlayerId())) {
|
||||
secondDrawMap.putIfAbsent(event.getPlayerId(), event.getId());
|
||||
} else {
|
||||
drewOnce.add(event.getPlayerId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
super.reset();
|
||||
drewOnce.clear();
|
||||
secondDrawMap.clear();
|
||||
}
|
||||
|
||||
boolean checkEvent(UUID playerId, GameEvent event) {
|
||||
return event.getId().equals(secondDrawMap.getOrDefault(playerId, null));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue