From c0349d87fbfa2a426cf8a9f3e6a4ef2cc4d711a5 Mon Sep 17 00:00:00 2001 From: "Jamin W. Collins" Date: Fri, 13 Mar 2020 18:32:36 -0600 Subject: [PATCH] implement and use new PredicateCard This fixes the THB and ELD draft, possibly others Signed-off-by: Jamin W. Collins --- .../src/main/java/forge/item/IPaperCard.java | 22 +++++ .../item/generation/BoosterGenerator.java | 8 +- .../main/java/forge/util/PredicateCard.java | 83 +++++++++++++++++++ 3 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 forge-core/src/main/java/forge/util/PredicateCard.java diff --git a/forge-core/src/main/java/forge/item/IPaperCard.java b/forge-core/src/main/java/forge/item/IPaperCard.java index 0c14064e059..53ea1d066e4 100644 --- a/forge-core/src/main/java/forge/item/IPaperCard.java +++ b/forge-core/src/main/java/forge/item/IPaperCard.java @@ -6,6 +6,7 @@ import forge.card.CardRarity; import forge.card.CardRules; import forge.card.CardType.CoreType; import forge.card.MagicColor; +import forge.util.PredicateCard; import forge.util.PredicateString; import org.apache.commons.lang3.StringUtils; @@ -60,6 +61,8 @@ public interface IPaperCard extends InventoryItem { return new PredicateNames(what); } + public static PredicateCards cards(final List what) { return new PredicateCards(what); } + private static final class PredicateColor implements Predicate { private final byte operand; @@ -161,6 +164,25 @@ public interface IPaperCard extends InventoryItem { } } + private static final class PredicateCards extends PredicateCard { + private final List operand; + + @Override + public boolean apply(final PaperCard card) { + for (final PaperCard element : this.operand) { + if (this.op(card, element)) { + return true; + } + } + return false; + } + + private PredicateCards(final List operand) { + super(StringOp.EQUALS); + this.operand = operand; + } + } + /** * Pre-built predicates are stored here to allow their re-usage and * easier access from code. diff --git a/forge-core/src/main/java/forge/item/generation/BoosterGenerator.java b/forge-core/src/main/java/forge/item/generation/BoosterGenerator.java index 896e8d6de6b..833ee7e6bf7 100644 --- a/forge-core/src/main/java/forge/item/generation/BoosterGenerator.java +++ b/forge-core/src/main/java/forge/item/generation/BoosterGenerator.java @@ -566,12 +566,8 @@ public class BoosterGenerator { toAdd = IPaperCard.Predicates.printedInSets(sets); } else if (operator.startsWith("fromSheet(") && invert) { String sheetName = StringUtils.strip(operator.substring(9), "()\" "); - Iterable src = StaticData.instance().getPrintSheets().get(sheetName).toFlatList(); - List cardNames = Lists.newArrayList(); - for (PaperCard card : src) { - cardNames.add(card.getName()); - } - toAdd = IPaperCard.Predicates.names(Lists.newArrayList(cardNames)); + Iterable cards = StaticData.instance().getPrintSheets().get(sheetName).toFlatList(); + toAdd = IPaperCard.Predicates.cards(Lists.newArrayList(cards)); } if (toAdd == null) { diff --git a/forge-core/src/main/java/forge/util/PredicateCard.java b/forge-core/src/main/java/forge/util/PredicateCard.java new file mode 100644 index 00000000000..308b34f68ca --- /dev/null +++ b/forge-core/src/main/java/forge/util/PredicateCard.java @@ -0,0 +1,83 @@ +/* + * Forge: Play Magic: the Gathering. + * Copyright (C) 2020 Jamin W. Collins + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package forge.util; + +import com.google.common.base.Predicate; +import forge.item.PaperCard; + +/** + * Special predicate class to perform string operations. + * + * @param + * the generic type + */ +public abstract class PredicateCard implements Predicate { + /** Possible operators for string operands. */ + public enum StringOp { + /** The EQUALS. */ + EQUALS, + } + + /** The operator. */ + private final StringOp operator; + + /** + * Op. + * + * @param op1 + * the op1 + * @param op2 + * the op2 + * @return true, if successful + */ + protected final boolean op(final PaperCard op1, final PaperCard op2) { + switch (this.getOperator()) { + case EQUALS: + return op1.equals(op2); + default: + return false; + } + } + + /** + * Instantiates a new predicate string. + * + * @param operator + * the operator + */ + public PredicateCard(final StringOp operator) { + this.operator = operator; + } + + /** + * @return the operator + */ + public StringOp getOperator() { + return operator; + } + + public static PredicateCard equals(final PaperCard what) { + return new PredicateCard(StringOp.EQUALS) { + @Override + public boolean apply(PaperCard subject) { + return op(subject, what); + } + }; + } + +}