More use of visitor pattern to avoid allocations.

This commit is contained in:
Myrd
2014-12-31 04:10:21 +00:00
parent 6d7c7fe63c
commit c57af8efc5

View File

@@ -72,6 +72,7 @@ import forge.util.maps.HashMapOfLists;
import forge.util.maps.MapOfLists; import forge.util.maps.MapOfLists;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import java.util.*; import java.util.*;
@@ -5914,63 +5915,59 @@ public class Card extends GameEntity implements Comparable<Card> {
} }
final Card source = sa.getHostCard(); final Card source = sa.getHostCard();
final MutableBoolean result = new MutableBoolean();
if (getKeywords() != null) { visitKeywords(currentState, new Visitor<String>() {
for (String kw : getKeywords()) { @Override
public void visit(String kw) {
if (kw.equals("Shroud")) { if (kw.equals("Shroud")) {
return false; result.setFalse();
} }
else if (kw.equals("Hexproof")) {
if (kw.equals("Hexproof")) {
if (sa.getActivatingPlayer().getOpponents().contains(getController())) { if (sa.getActivatingPlayer().getOpponents().contains(getController())) {
if (!sa.getActivatingPlayer().hasKeyword("Spells and abilities you control can target hexproof creatures")) { if (!sa.getActivatingPlayer().hasKeyword("Spells and abilities you control can target hexproof creatures")) {
return false; result.setFalse();
} }
} }
} }
else if (kw.equals("CARDNAME can't be the target of Aura spells.")) {
if (kw.equals("CARDNAME can't be the target of Aura spells.")) {
if (source.isAura() && sa.isSpell()) { if (source.isAura() && sa.isSpell()) {
return false; result.setFalse();
} }
} }
else if (kw.equals("CARDNAME can't be enchanted.")) {
if (kw.equals("CARDNAME can't be enchanted.")) {
if (source.isAura()) { if (source.isAura()) {
return false; result.setFalse();
} }
} //Sets source as invalid enchant target for computer player only. } //Sets source as invalid enchant target for computer player only.
else if (kw.equals("CARDNAME can't be equipped.")) {
if (kw.equals("CARDNAME can't be equipped.")) {
if (source.isEquipment()) { if (source.isEquipment()) {
return false; result.setFalse();
} }
} //Sets source as invalid equip target for computer player only. } //Sets source as invalid equip target for computer player only.
else if (kw.equals("CARDNAME can't be the target of red spells or abilities from red sources.")) {
if (kw.equals("CARDNAME can't be the target of red spells or abilities from red sources.")) {
if (source.isRed()) { if (source.isRed()) {
return false; result.setFalse();
} }
} }
else if (kw.equals("CARDNAME can't be the target of black spells.")) {
if (kw.equals("CARDNAME can't be the target of black spells.")) {
if (source.isBlack() && sa.isSpell()) { if (source.isBlack() && sa.isSpell()) {
return false; result.setFalse();
} }
} }
else if (kw.equals("CARDNAME can't be the target of blue spells.")) {
if (kw.equals("CARDNAME can't be the target of blue spells.")) {
if (source.isBlue() && sa.isSpell()) { if (source.isBlue() && sa.isSpell()) {
return false; result.setFalse();
} }
} }
else if (kw.equals("CARDNAME can't be the target of spells.")) {
if (kw.equals("CARDNAME can't be the target of spells.")) {
if (sa.isSpell()) { if (sa.isSpell()) {
return false; result.setFalse();
} }
} }
} }
});
if (result.isFalse()) {
return false;
} }
if (sa.isSpell() && source.hasStartOfKeyword("SpellCantTarget")) { if (sa.isSpell() && source.hasStartOfKeyword("SpellCantTarget")) {
final int keywordPosition = source.getKeywordPosition("SpellCantTarget"); final int keywordPosition = source.getKeywordPosition("SpellCantTarget");