Program: 157761
Project: 157761
Buggy source:
/** 
 * 116.5. Each time a player would get priority, the game first performs all applicable state-based actions as a single event (see rule 704, “State-Based Actions”), then repeats this process until no state-based actions are performed. Then triggered abilities are put on the stack (see rule 603, “Handling Triggered Abilities”). These steps repeat in order until no further state-based actions are performed and no abilities trigger. Then the player who would have received priority does so.
 * @return
 */
protected boolean checkStateBasedActions(){
  boolean somethingHappened=false;
  for (  Player player : state.getPlayers().values()) {
    if (!player.hasLost() && ((player.getLife() <= 0 && player.canLoseByZeroOrLessLife()) || player.isEmptyDraw() || player.getCounters().getCount(CounterType.POISON) >= 10)) {
      player.lost(this);
    }
  }
  Iterator<Card> copiedCards=this.getState().getCopiedCards().iterator();
  while (copiedCards.hasNext()) {
    Card card=copiedCards.next();
    if (card instanceof SplitCardHalf) {
      continue;
    }
    Zone zone=state.getZone(card.getId());
    if (zone != Zone.BATTLEFIELD && zone != Zone.STACK) {
switch (zone) {
case GRAVEYARD:
        for (        Player player : getPlayers().values()) {
          if (player.getGraveyard().contains(card.getId())) {
            player.getGraveyard().remove(card);
            break;
          }
        }
      break;
case HAND:
    for (    Player player : getPlayers().values()) {
      if (player.getHand().contains(card.getId())) {
        player.getHand().remove(card);
        break;
      }
    }
  break;
case LIBRARY:
for (Player player : getPlayers().values()) {
  if (player.getLibrary().getCard(card.getId(),this) != null) {
    player.getLibrary().remove(card.getId(),this);
    break;
  }
}
break;
case EXILED:
getExile().removeCard(card,this);
break;
}
copiedCards.remove();
}
}
List<Permanent> legendary=new ArrayList<>();
List<Permanent> worldEnchantment=new ArrayList<>();
for (Permanent perm : getBattlefield().getAllActivePermanents()) {
if (perm.isCreature()) {
if (perm.getToughness().getValue() <= 0) {
if (movePermanentToGraveyardWithInfo(perm)) {
somethingHappened=true;
continue;
}
}
 else if (perm.getToughness().getValue() <= perm.getDamage() || perm.isDeathtouched()) {
if (perm.destroy(null,this,false)) {
somethingHappened=true;
continue;
}
}
if (perm.getPairedCard() != null) {
Permanent paired=perm.getPairedCard().getPermanent(this);
if (paired == null || !perm.getControllerId().equals(paired.getControllerId()) || paired.getPairedCard() == null) {
perm.setPairedCard(null);
if (paired != null) {
paired.setPairedCard(null);
}
somethingHappened=true;
}
}
}
 else if (perm.getPairedCard() != null) {
Permanent paired=perm.getPairedCard().getPermanent(this);
perm.setPairedCard(null);
if (paired != null) {
paired.setPairedCard(null);
}
somethingHappened=true;
}
if (perm.isPlaneswalker()) {
if (perm.getCounters(this).getCount(CounterType.LOYALTY) == 0) {
if (movePermanentToGraveyardWithInfo(perm)) {
somethingHappened=true;
continue;
}
}
}
if (perm.isWorld()) {
worldEnchantment.add(perm);
}
if (StaticFilters.FILTER_PERMANENT_AURA.match(perm,this)) {
if (perm.getAttachedTo() == null) {
Card card=this.getCard(perm.getId());
if (card != null && !card.isCreature()) {
if (movePermanentToGraveyardWithInfo(perm)) {
somethingHappened=true;
}
}
}
 else {
SpellAbility spellAbility=perm.getSpellAbility();
if (spellAbility.getTargets().isEmpty()) {
for (Ability ability : perm.getAbilities(this)) {
if ((ability instanceof SpellAbility) && SpellAbilityType.BASE_ALTERNATE == ((SpellAbility)ability).getSpellAbilityType() && !ability.getTargets().isEmpty()) {
spellAbility=(SpellAbility)ability;
break;
}
}
}
if (spellAbility.getTargets().isEmpty()) {
Permanent enchanted=this.getPermanent(perm.getAttachedTo());
logger.error("String_Node_Str" + perm.getName() + "String_Node_Str"+ (enchanted == null ? "String_Node_Str" : enchanted.getName()));
}
 else {
Target target=spellAbility.getTargets().get(0);
if (target instanceof TargetPermanent) {
Permanent attachedTo=getPermanent(perm.getAttachedTo());
if (attachedTo == null || !attachedTo.getAttachments().contains(perm.getId())) {
Card card=this.getCard(perm.getId());
if (card != null && card.isCreature()) {
UUID wasAttachedTo=perm.getAttachedTo();
perm.attachTo(null,this);
BestowAbility.becomeCreature(perm,this);
fireEvent(new GameEvent(GameEvent.EventType.UNATTACHED,wasAttachedTo,perm.getId(),perm.getControllerId()));
}
 else if (movePermanentToGraveyardWithInfo(perm)) {
somethingHappened=true;
}
}
 else {
Filter auraFilter=spellAbility.getTargets().get(0).getFilter();
if (auraFilter instanceof FilterControlledCreaturePermanent) {
if (!((FilterControlledCreaturePermanent)auraFilter).match(attachedTo,perm.getId(),perm.getControllerId(),this) || attachedTo.cantBeAttachedBy(perm,this)) {
  if (movePermanentToGraveyardWithInfo(perm)) {
    somethingHappened=true;
  }
}
}
 else if (!auraFilter.match(attachedTo,this) || attachedTo.cantBeAttachedBy(perm,this)) {
Card card=this.getCard(perm.getId());
if (card != null && card.isCreature()) {
  UUID wasAttachedTo=perm.getAttachedTo();
  perm.attachTo(null,this);
  fireEvent(new GameEvent(GameEvent.EventType.UNATTACHED,wasAttachedTo,perm.getId(),perm.getControllerId()));
}
 else if (movePermanentToGraveyardWithInfo(perm)) {
  somethingHappened=true;
}
}
}
}
 else if (target instanceof TargetPlayer) {
Player attachedToPlayer=getPlayer(perm.getAttachedTo());
if (attachedToPlayer == null || attachedToPlayer.hasLost()) {
if (movePermanentToGraveyardWithInfo(perm)) {
somethingHappened=true;
}
}
 else {
Filter auraFilter=spellAbility.getTargets().get(0).getFilter();
if (!auraFilter.match(attachedToPlayer,this) || attachedToPlayer.hasProtectionFrom(perm,this)) {
if (movePermanentToGraveyardWithInfo(perm)) {
  somethingHappened=true;
}
}
}
}
 else if (target instanceof TargetCard) {
Card attachedTo=getCard(perm.getAttachedTo());
if (attachedTo == null || !((TargetCard)spellAbility.getTargets().get(0)).canTarget(perm.getControllerId(),perm.getAttachedTo(),spellAbility,this)) {
if (movePermanentToGraveyardWithInfo(perm)) {
attachedTo.removeAttachment(perm.getId(),this);
somethingHappened=true;
}
}
}
}
}
}
if (this.getState().isLegendaryRuleActive() && StaticFilters.FILTER_PERMANENT_LEGENDARY.match(perm,this)) {
legendary.add(perm);
}
if (StaticFilters.FILTER_PERMANENT_EQUIPMENT.match(perm,this)) {
if (perm.getAttachedTo() != null) {
Permanent attachedTo=getPermanent(perm.getAttachedTo());
if (attachedTo != null) {
for (Ability ability : perm.getAbilities(this)) {
if (ability instanceof AttachableToRestrictedAbility) {
if (!((AttachableToRestrictedAbility)ability).canEquip(attachedTo,null,this)) {
attachedTo=null;
break;
}
}
}
}
if (attachedTo == null || !attachedTo.getAttachments().contains(perm.getId())) {
UUID wasAttachedTo=perm.getAttachedTo();
perm.attachTo(null,this);
fireEvent(new GameEvent(GameEvent.EventType.UNATTACHED,wasAttachedTo,perm.getId(),perm.getControllerId()));
}
 else if (!attachedTo.isCreature() || attachedTo.hasProtectionFrom(perm,this)) {
if (attachedTo.removeAttachment(perm.getId(),this)) {
somethingHappened=true;
}
}
}
}
if (StaticFilters.FILTER_PERMANENT_FORTIFICATION.match(perm,this)) {
if (perm.getAttachedTo() != null) {
Permanent land=getPermanent(perm.getAttachedTo());
if (land == null || !land.getAttachments().contains(perm.getId())) {
perm.attachTo(null,this);
}
 else if (!land.isLand() || land.hasProtectionFrom(perm,this)) {
if (land.removeAttachment(perm.getId(),this)) {
somethingHappened=true;
}
}
}
}
if (!perm.getAttachments().isEmpty()) {
for (UUID attachmentId : perm.getAttachments()) {
Permanent attachment=getPermanent(attachmentId);
if (attachment != null && (attachment.isCreature() || !(attachment.getSubtype(this).contains(SubType.AURA) || attachment.getSubtype(this).contains(SubType.EQUIPMENT) || attachment.getSubtype(this).contains(SubType.FORTIFICATION)))) {
if (perm.removeAttachment(attachment.getId(),this)) {
somethingHappened=true;
break;
}
}
}
}
if (perm.getCounters(this).containsKey(CounterType.P1P1) && perm.getCounters(this).containsKey(CounterType.M1M1)) {
int p1p1=perm.getCounters(this).getCount(CounterType.P1P1);
int m1m1=perm.getCounters(this).getCount(CounterType.M1M1);
int min=Math.min(p1p1,m1m1);
perm.getCounters(this).removeCounter(CounterType.P1P1,min);
perm.getCounters(this).removeCounter(CounterType.M1M1,min);
}
for (Ability ability : perm.getAbilities(this)) {
if (ability instanceof CantHaveMoreThanAmountCountersSourceAbility) {
CantHaveMoreThanAmountCountersSourceAbility counterAbility=(CantHaveMoreThanAmountCountersSourceAbility)ability;
int count=perm.getCounters(this).getCount(counterAbility.getCounterType());
if (count > counterAbility.getAmount()) {
perm.removeCounters(counterAbility.getCounterType().getName(),count - counterAbility.getAmount(),this);
somethingHappened=true;
}
}
}
}
if (legendary.size() > 1) {
for (Permanent legend : legendary) {
FilterPermanent filterLegendName=new FilterPermanent();
filterLegendName.add(new SupertypePredicate(SuperType.LEGENDARY));
filterLegendName.add(new NamePredicate(legend.getName()));
filterLegendName.add(new ControllerIdPredicate(legend.getControllerId()));
if (getBattlefield().contains(filterLegendName,legend.getControllerId(),this,2)) {
if (!replaceEvent(GameEvent.getEvent(GameEvent.EventType.DESTROY_PERMANENT_BY_LEGENDARY_RULE,legend.getId(),legend.getControllerId()))) {
Player controller=this.getPlayer(legend.getControllerId());
if (controller != null) {
Target targetLegendaryToKeep=new TargetPermanent(filterLegendName);
targetLegendaryToKeep.setTargetName(legend.getName() + "String_Node_Str");
controller.chooseTarget(Outcome.Benefit,targetLegendaryToKeep,null,this);
for (Permanent dupLegend : getBattlefield().getActivePermanents(filterLegendName,legend.getControllerId(),this)) {
if (!targetLegendaryToKeep.getTargets().contains(dupLegend.getId())) {
movePermanentToGraveyardWithInfo(dupLegend);
}
}
}
return true;
}
}
}
}
if (worldEnchantment.size() > 1) {
int newestCard=-1;
Permanent newestPermanent=null;
for (Permanent permanent : worldEnchantment) {
if (newestCard == -1) {
newestCard=permanent.getCreateOrder();
newestPermanent=permanent;
}
 else if (newestCard < permanent.getCreateOrder()) {
newestCard=permanent.getCreateOrder();
newestPermanent=permanent;
}
 else if (newestCard == permanent.getCreateOrder()) {
newestPermanent=null;
}
}
for (Permanent permanent : worldEnchantment) {
if (!Objects.equals(newestPermanent,permanent)) {
movePermanentToGraveyardWithInfo(permanent);
somethingHappened=true;
}
}
}
return somethingHappened;
}

Fixed version (ground truth):
/** 
 * 116.5. Each time a player would get priority, the game first performs all applicable state-based actions as a single event (see rule 704, “State-Based Actions”), then repeats this process until no state-based actions are performed. Then triggered abilities are put on the stack (see rule 603, “Handling Triggered Abilities”). These steps repeat in order until no further state-based actions are performed and no abilities trigger. Then the player who would have received priority does so.
 * @return
 */
protected boolean checkStateBasedActions(){
  boolean somethingHappened=false;
  for (  Player player : state.getPlayers().values()) {
    if (!player.hasLost() && ((player.getLife() <= 0 && player.canLoseByZeroOrLessLife()) || player.isEmptyDraw() || player.getCounters().getCount(CounterType.POISON) >= 10)) {
      player.lost(this);
    }
  }
  Iterator<Card> copiedCards=this.getState().getCopiedCards().iterator();
  while (copiedCards.hasNext()) {
    Card card=copiedCards.next();
    if (card instanceof SplitCardHalf) {
      continue;
    }
    Zone zone=state.getZone(card.getId());
    if (zone != Zone.BATTLEFIELD && zone != Zone.STACK) {
switch (zone) {
case GRAVEYARD:
        for (        Player player : getPlayers().values()) {
          if (player.getGraveyard().contains(card.getId())) {
            player.getGraveyard().remove(card);
            break;
          }
        }
      break;
case HAND:
    for (    Player player : getPlayers().values()) {
      if (player.getHand().contains(card.getId())) {
        player.getHand().remove(card);
        break;
      }
    }
  break;
case LIBRARY:
for (Player player : getPlayers().values()) {
  if (player.getLibrary().getCard(card.getId(),this) != null) {
    player.getLibrary().remove(card.getId(),this);
    break;
  }
}
break;
case EXILED:
getExile().removeCard(card,this);
break;
}
copiedCards.remove();
}
}
List<Permanent> legendary=new ArrayList<>();
List<Permanent> worldEnchantment=new ArrayList<>();
for (Permanent perm : getBattlefield().getAllActivePermanents()) {
if (perm.isCreature()) {
if (perm.getToughness().getValue() <= 0) {
if (movePermanentToGraveyardWithInfo(perm)) {
somethingHappened=true;
continue;
}
}
 else if (perm.getToughness().getValue() <= perm.getDamage() || perm.isDeathtouched()) {
if (perm.destroy(null,this,false)) {
somethingHappened=true;
continue;
}
}
if (perm.getPairedCard() != null) {
Permanent paired=perm.getPairedCard().getPermanent(this);
if (paired == null || !perm.getControllerId().equals(paired.getControllerId()) || paired.getPairedCard() == null) {
perm.setPairedCard(null);
if (paired != null) {
paired.setPairedCard(null);
}
somethingHappened=true;
}
}
}
 else if (perm.getPairedCard() != null) {
Permanent paired=perm.getPairedCard().getPermanent(this);
perm.setPairedCard(null);
if (paired != null) {
paired.setPairedCard(null);
}
somethingHappened=true;
}
if (perm.isPlaneswalker()) {
if (perm.getCounters(this).getCount(CounterType.LOYALTY) == 0) {
if (movePermanentToGraveyardWithInfo(perm)) {
somethingHappened=true;
continue;
}
}
}
if (perm.isWorld()) {
worldEnchantment.add(perm);
}
if (StaticFilters.FILTER_PERMANENT_AURA.match(perm,this)) {
if (perm.getAttachedTo() == null) {
Card card=this.getCard(perm.getId());
if (card != null && !card.isCreature()) {
if (movePermanentToGraveyardWithInfo(perm)) {
somethingHappened=true;
}
}
}
 else {
SpellAbility spellAbility=perm.getSpellAbility();
if (spellAbility.getTargets().isEmpty()) {
for (Ability ability : perm.getAbilities(this)) {
if ((ability instanceof SpellAbility) && SpellAbilityType.BASE_ALTERNATE == ((SpellAbility)ability).getSpellAbilityType() && !ability.getTargets().isEmpty()) {
spellAbility=(SpellAbility)ability;
break;
}
}
}
if (spellAbility.getTargets().isEmpty()) {
Permanent enchanted=this.getPermanent(perm.getAttachedTo());
logger.error("String_Node_Str" + perm.getName() + "String_Node_Str"+ (enchanted == null ? "String_Node_Str" : enchanted.getName()));
}
 else {
Target target=spellAbility.getTargets().get(0);
if (target instanceof TargetPermanent) {
Permanent attachedTo=getPermanent(perm.getAttachedTo());
if (attachedTo == null || !attachedTo.getAttachments().contains(perm.getId())) {
Card card=this.getCard(perm.getId());
if (card != null && card.isCreature()) {
UUID wasAttachedTo=perm.getAttachedTo();
perm.attachTo(null,this);
BestowAbility.becomeCreature(perm,this);
fireEvent(new GameEvent(GameEvent.EventType.UNATTACHED,wasAttachedTo,perm.getId(),perm.getControllerId()));
}
 else if (movePermanentToGraveyardWithInfo(perm)) {
somethingHappened=true;
}
}
 else {
Filter auraFilter=spellAbility.getTargets().get(0).getFilter();
if (auraFilter instanceof FilterControlledCreaturePermanent) {
if (!((FilterControlledCreaturePermanent)auraFilter).match(attachedTo,perm.getId(),perm.getControllerId(),this) || attachedTo.cantBeAttachedBy(perm,this)) {
  if (movePermanentToGraveyardWithInfo(perm)) {
    somethingHappened=true;
  }
}
}
 else if (!auraFilter.match(attachedTo,this) || attachedTo.cantBeAttachedBy(perm,this)) {
Card card=this.getCard(perm.getId());
if (card != null && card.isCreature()) {
  UUID wasAttachedTo=perm.getAttachedTo();
  perm.attachTo(null,this);
  BestowAbility.becomeCreature(perm,this);
  fireEvent(new GameEvent(GameEvent.EventType.UNATTACHED,wasAttachedTo,perm.getId(),perm.getControllerId()));
}
 else if (movePermanentToGraveyardWithInfo(perm)) {
  somethingHappened=true;
}
}
}
}
 else if (target instanceof TargetPlayer) {
Player attachedToPlayer=getPlayer(perm.getAttachedTo());
if (attachedToPlayer == null || attachedToPlayer.hasLost()) {
if (movePermanentToGraveyardWithInfo(perm)) {
somethingHappened=true;
}
}
 else {
Filter auraFilter=spellAbility.getTargets().get(0).getFilter();
if (!auraFilter.match(attachedToPlayer,this) || attachedToPlayer.hasProtectionFrom(perm,this)) {
if (movePermanentToGraveyardWithInfo(perm)) {
  somethingHappened=true;
}
}
}
}
 else if (target instanceof TargetCard) {
Card attachedTo=getCard(perm.getAttachedTo());
if (attachedTo == null || !((TargetCard)spellAbility.getTargets().get(0)).canTarget(perm.getControllerId(),perm.getAttachedTo(),spellAbility,this)) {
if (movePermanentToGraveyardWithInfo(perm)) {
if (attachedTo != null) {
  attachedTo.removeAttachment(perm.getId(),this);
}
somethingHappened=true;
}
}
}
}
}
}
if (this.getState().isLegendaryRuleActive() && StaticFilters.FILTER_PERMANENT_LEGENDARY.match(perm,this)) {
legendary.add(perm);
}
if (StaticFilters.FILTER_PERMANENT_EQUIPMENT.match(perm,this)) {
if (perm.getAttachedTo() != null) {
Permanent attachedTo=getPermanent(perm.getAttachedTo());
if (attachedTo != null) {
for (Ability ability : perm.getAbilities(this)) {
if (ability instanceof AttachableToRestrictedAbility) {
if (!((AttachableToRestrictedAbility)ability).canEquip(attachedTo,null,this)) {
attachedTo=null;
break;
}
}
}
}
if (attachedTo == null || !attachedTo.getAttachments().contains(perm.getId())) {
UUID wasAttachedTo=perm.getAttachedTo();
perm.attachTo(null,this);
fireEvent(new GameEvent(GameEvent.EventType.UNATTACHED,wasAttachedTo,perm.getId(),perm.getControllerId()));
}
 else if (!attachedTo.isCreature() || attachedTo.hasProtectionFrom(perm,this)) {
if (attachedTo.removeAttachment(perm.getId(),this)) {
somethingHappened=true;
}
}
}
}
if (StaticFilters.FILTER_PERMANENT_FORTIFICATION.match(perm,this)) {
if (perm.getAttachedTo() != null) {
Permanent land=getPermanent(perm.getAttachedTo());
if (land == null || !land.getAttachments().contains(perm.getId())) {
perm.attachTo(null,this);
}
 else if (!land.isLand() || land.hasProtectionFrom(perm,this)) {
if (land.removeAttachment(perm.getId(),this)) {
somethingHappened=true;
}
}
}
}
if (!perm.getAttachments().isEmpty()) {
for (UUID attachmentId : perm.getAttachments()) {
Permanent attachment=getPermanent(attachmentId);
if (attachment != null && (attachment.isCreature() || !(attachment.getSubtype(this).contains(SubType.AURA) || attachment.getSubtype(this).contains(SubType.EQUIPMENT) || attachment.getSubtype(this).contains(SubType.FORTIFICATION)))) {
if (perm.removeAttachment(attachment.getId(),this)) {
somethingHappened=true;
break;
}
}
}
}
if (perm.getCounters(this).containsKey(CounterType.P1P1) && perm.getCounters(this).containsKey(CounterType.M1M1)) {
int p1p1=perm.getCounters(this).getCount(CounterType.P1P1);
int m1m1=perm.getCounters(this).getCount(CounterType.M1M1);
int min=Math.min(p1p1,m1m1);
perm.getCounters(this).removeCounter(CounterType.P1P1,min);
perm.getCounters(this).removeCounter(CounterType.M1M1,min);
}
for (Ability ability : perm.getAbilities(this)) {
if (ability instanceof CantHaveMoreThanAmountCountersSourceAbility) {
CantHaveMoreThanAmountCountersSourceAbility counterAbility=(CantHaveMoreThanAmountCountersSourceAbility)ability;
int count=perm.getCounters(this).getCount(counterAbility.getCounterType());
if (count > counterAbility.getAmount()) {
perm.removeCounters(counterAbility.getCounterType().getName(),count - counterAbility.getAmount(),this);
somethingHappened=true;
}
}
}
}
if (legendary.size() > 1) {
for (Permanent legend : legendary) {
FilterPermanent filterLegendName=new FilterPermanent();
filterLegendName.add(new SupertypePredicate(SuperType.LEGENDARY));
filterLegendName.add(new NamePredicate(legend.getName()));
filterLegendName.add(new ControllerIdPredicate(legend.getControllerId()));
if (getBattlefield().contains(filterLegendName,legend.getControllerId(),this,2)) {
if (!replaceEvent(GameEvent.getEvent(GameEvent.EventType.DESTROY_PERMANENT_BY_LEGENDARY_RULE,legend.getId(),legend.getControllerId()))) {
Player controller=this.getPlayer(legend.getControllerId());
if (controller != null) {
Target targetLegendaryToKeep=new TargetPermanent(filterLegendName);
targetLegendaryToKeep.setTargetName(legend.getName() + "String_Node_Str");
controller.chooseTarget(Outcome.Benefit,targetLegendaryToKeep,null,this);
for (Permanent dupLegend : getBattlefield().getActivePermanents(filterLegendName,legend.getControllerId(),this)) {
if (!targetLegendaryToKeep.getTargets().contains(dupLegend.getId())) {
movePermanentToGraveyardWithInfo(dupLegend);
}
}
}
return true;
}
}
}
}
if (worldEnchantment.size() > 1) {
int newestCard=-1;
Permanent newestPermanent=null;
for (Permanent permanent : worldEnchantment) {
if (newestCard == -1) {
newestCard=permanent.getCreateOrder();
newestPermanent=permanent;
}
 else if (newestCard < permanent.getCreateOrder()) {
newestCard=permanent.getCreateOrder();
newestPermanent=permanent;
}
 else if (newestCard == permanent.getCreateOrder()) {
newestPermanent=null;
}
}
for (Permanent permanent : worldEnchantment) {
if (!Objects.equals(newestPermanent,permanent)) {
movePermanentToGraveyardWithInfo(permanent);
somethingHappened=true;
}
}
}
return somethingHappened;
}