CardPool now includes the option to load custom cards and check preferences.

If Custom Card setting is disabled, an error message in logs will be printed accordingly.
Custom Cards will be loaded as such in Card Pool only if the option in settings is enabled.
This commit is contained in:
leriomaggio
2021-07-11 19:06:39 +01:00
parent e6958305b5
commit 811d6479f5

View File

@@ -78,10 +78,21 @@ public class CardPool extends ItemPool<PaperCard> {
PaperCard paperCard = StaticData.instance().getCommonCards().getCard(cardName, setCode, artIndex); PaperCard paperCard = StaticData.instance().getCommonCards().getCard(cardName, setCode, artIndex);
final boolean isCommonCard = paperCard != null; final boolean isCommonCard = paperCard != null;
boolean isCustomCard = !(isCommonCard);
if (!isCommonCard) { if (!isCommonCard) {
paperCard = StaticData.instance().getVariantCards().getCard(cardName, setCode); paperCard = StaticData.instance().getCustomCards().getCard(cardName, setCode);
if (paperCard == null) { isCustomCard = paperCard != null;
if (isCustomCard && !StaticData.instance().isEnableCustomCardsInDecks()) {
String errMsg = String.format("Custom Card \"%s\" from Set \"%s\" was found but could not be loaded. Please enable the use of Custom Cards in Forge Preferences to use this card.", paperCard.getName(), paperCard.getEdition());
System.err.println(errMsg);
paperCard = null;
}
if (!isCustomCard)
paperCard = StaticData.instance().getVariantCards().getCard(cardName, setCode);
if (paperCard == null && !isCustomCard) {
StaticData.instance().attemptToLoadCard(cardName, setCode); StaticData.instance().attemptToLoadCard(cardName, setCode);
paperCard = StaticData.instance().getVariantCards().getCard(cardName, setCode); paperCard = StaticData.instance().getVariantCards().getCard(cardName, setCode);
} }
@@ -91,10 +102,16 @@ public class CardPool extends ItemPool<PaperCard> {
if (paperCard != null) { if (paperCard != null) {
setCode = paperCard.getEdition(); setCode = paperCard.getEdition();
cardName = paperCard.getName(); cardName = paperCard.getName();
artCount = isCommonCard ? StaticData.instance().getCommonCards().getArtCount(cardName, setCode) : 1; if (isCommonCard)
artCount = StaticData.instance().getCommonCards().getArtCount(cardName, setCode);
else if (isCustomCard)
artCount = StaticData.instance().getCustomCards().getArtCount(cardName, setCode);
} else { } else {
System.err.print("An unsupported card was requested: \"" + cardName + "\" from \"" + setCode + "\". "); System.err.println("An unsupported card was requested: \"" + cardName + "\" from \"" + setCode + "\". \n");
paperCard = StaticData.instance().getCommonCards().createUnsupportedCard(cardName); paperCard = StaticData.instance().getCommonCards().createUnsupportedCard(cardName);
// the unsupported card will be temporarily added to common cards (for no use, really)
// this would happen only if custom cards has been found but option is disabled!
isCustomCard = false;
} }
boolean artIndexExplicitlySet = artIndex > 0 || Character.isDigit(cardName.charAt(cardName.length() - 1)) && cardName.charAt(cardName.length() - 2) == CardDb.NameSetSeparator; boolean artIndexExplicitlySet = artIndex > 0 || Character.isDigit(cardName.charAt(cardName.length() - 1)) && cardName.charAt(cardName.length() - 2) == CardDb.NameSetSeparator;
@@ -105,24 +122,23 @@ public class CardPool extends ItemPool<PaperCard> {
} else { } else {
// random art index specified, make sure we get different groups of cards with different art // random art index specified, make sure we get different groups of cards with different art
int[] artGroups = MyRandom.splitIntoRandomGroups(amount, artCount); int[] artGroups = MyRandom.splitIntoRandomGroups(amount, artCount);
CardDb cardDb = isCustomCard ? StaticData.instance().getCustomCards() : StaticData.instance().getCommonCards();
for (int i = 1; i <= artGroups.length; i++) { for (int i = 1; i <= artGroups.length; i++) {
int cnt = artGroups[i - 1]; int cnt = artGroups[i - 1];
if (cnt <= 0) { if (cnt <= 0) {
continue; continue;
} }
PaperCard randomCard = StaticData.instance().getCommonCards().getCard(cardName, setCode, i); PaperCard randomCard = cardDb.getCard(cardName, setCode, i);
this.add(randomCard, cnt); this.add(randomCard, cnt);
} }
} }
} }
/** /**
* Add all from a List of CardPrinted. * Add all from a List of CardPrinted.
* *
* @param list * @param list CardPrinteds to add
* CardPrinteds to add
*/ */
public void add(final Iterable<PaperCard> list) { public void add(final Iterable<PaperCard> list) {
for (PaperCard cp : list) { for (PaperCard cp : list) {
@@ -132,13 +148,14 @@ public class CardPool extends ItemPool<PaperCard> {
/** /**
* returns n-th card from this DeckSection. LINEAR time. No fixed order between changes * returns n-th card from this DeckSection. LINEAR time. No fixed order between changes
*
* @param n * @param n
* @return * @return
*/ */
public PaperCard get(int n) { public PaperCard get(int n) {
for (Entry<PaperCard, Integer> e : this) { for (Entry<PaperCard, Integer> e : this) {
n -= e.getValue(); n -= e.getValue();
if ( n <= 0 ) return e.getKey(); if (n <= 0) return e.getKey();
} }
return null; return null;
} }
@@ -153,7 +170,9 @@ public class CardPool extends ItemPool<PaperCard> {
@Override @Override
public String toString() { public String toString() {
if (this.isEmpty()) { return "[]"; } if (this.isEmpty()) {
return "[]";
}
boolean isFirst = true; boolean isFirst = true;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@@ -161,8 +180,7 @@ public class CardPool extends ItemPool<PaperCard> {
for (Entry<PaperCard, Integer> e : this) { for (Entry<PaperCard, Integer> e : this) {
if (isFirst) { if (isFirst) {
isFirst = false; isFirst = false;
} } else {
else {
sb.append(", "); sb.append(", ");
} }
sb.append(e.getValue()).append(" x ").append(e.getKey().getName()); sb.append(e.getValue()).append(" x ").append(e.getKey().getName());
@@ -171,6 +189,7 @@ public class CardPool extends ItemPool<PaperCard> {
} }
private final static Pattern p = Pattern.compile("((\\d+)\\s+)?(.*?)"); private final static Pattern p = Pattern.compile("((\\d+)\\s+)?(.*?)");
public static CardPool fromCardList(final Iterable<String> lines) { public static CardPool fromCardList(final Iterable<String> lines) {
CardPool pool = new CardPool(); CardPool pool = new CardPool();
@@ -182,7 +201,9 @@ public class CardPool extends ItemPool<PaperCard> {
final Iterator<String> lineIterator = lines.iterator(); final Iterator<String> lineIterator = lines.iterator();
while (lineIterator.hasNext()) { while (lineIterator.hasNext()) {
final String line = lineIterator.next(); final String line = lineIterator.next();
if (line.startsWith(";") || line.startsWith("#")) { continue; } // that is a comment or not-yet-supported card if (line.startsWith(";") || line.startsWith("#")) {
continue;
} // that is a comment or not-yet-supported card
final Matcher m = p.matcher(line.trim()); final Matcher m = p.matcher(line.trim());
m.matches(); m.matches();
@@ -207,7 +228,7 @@ public class CardPool extends ItemPool<PaperCard> {
boolean isFirst = true; boolean isFirst = true;
for (final Entry<PaperCard, Integer> e : main2sort) { for (final Entry<PaperCard, Integer> e : main2sort) {
if(!isFirst) if (!isFirst)
sb.append(separator); sb.append(separator);
else else
isFirst = false; isFirst = false;
@@ -222,13 +243,14 @@ public class CardPool extends ItemPool<PaperCard> {
/** /**
* Applies a predicate to this CardPool's cards. * Applies a predicate to this CardPool's cards.
*
* @param predicate the Predicate to apply to this CardPool * @param predicate the Predicate to apply to this CardPool
* @return a new CardPool made from this CardPool with only the cards that agree with the provided Predicate * @return a new CardPool made from this CardPool with only the cards that agree with the provided Predicate
*/ */
public CardPool getFilteredPool(Predicate<PaperCard> predicate){ public CardPool getFilteredPool(Predicate<PaperCard> predicate) {
CardPool filteredPool = new CardPool(); CardPool filteredPool = new CardPool();
for(PaperCard pc : this.items.keySet()){ for (PaperCard pc : this.items.keySet()) {
if(predicate.apply(pc)) filteredPool.add(pc); if (predicate.apply(pc)) filteredPool.add(pc);
} }
return filteredPool; return filteredPool;
} }