Merge branch 'paco_deckeditorloadingtimes' into 'master'

Fix Deck Editor filter update times

See merge request core-developers/forge!6160
This commit is contained in:
Michael Kamensky
2022-02-06 04:57:34 +00:00
3 changed files with 59 additions and 50 deletions

View File

@@ -63,9 +63,10 @@ public enum ColumnDef {
new Function<Entry<InventoryItem, Integer>, Comparable<?>>() {
@Override
public Comparable<?> apply(final Entry<InventoryItem, Integer> from) {
if (from.getKey() instanceof PaperCard)
return toSortableName(from.getKey().toString());
return toSortableName(from.getKey().getName());
if (from.getKey() instanceof PaperCard) {
return ((PaperCard)from.getKey()).getSortableName();
}
return InventoryItem.toSortableName(from.getKey().getName());
}
},
new Function<Entry<? extends InventoryItem, Integer>, Object>() {
@@ -540,53 +541,6 @@ public enum ColumnDef {
return this.longName;
}
/**
* Converts a card name to a sortable name.
* Trim leading quotes, then move article last, then replace characters.
* Because An-Havva Constable.
* Capitals and lowercase sorted as one: "my deck" before "Myr Retribution"
* Apostrophes matter, though: "D'Avenant" before "Danitha"
* TO DO: Commas before apostrophes: "Rakdos, Lord of Riots" before "Rakdos's Return"
*
* @param printedName The name of the card.
* @return A sortable name.
*/
private static String toSortableName(String printedName) {
if (printedName.startsWith("\"")) printedName = printedName.substring(1);
return moveArticleToEnd(printedName).toLowerCase().replaceAll("[^\\s'0-9a-z]", "");
}
/**
* Article words. These words get kicked to the end of a sortable name.
* For localization, simply overwrite this array with appropriate words.
* Words in this list are used by the method String moveArticleToEnd(String), useful
* for alphabetizing phrases, in particular card or other inventory object names.
*/
private static final String[] ARTICLE_WORDS = {
"A",
"An",
"The"
};
/**
* Detects whether a string begins with an article word
*
* @param str The name of the card.
* @return The sort-friendly name of the card. Example: "The Hive" becomes "Hive The".
*/
private static String moveArticleToEnd(String str) {
String articleWord;
for (int i = 0; i < ARTICLE_WORDS.length; i++) {
articleWord = ARTICLE_WORDS[i];
if (str.startsWith(articleWord + " ")) {
str = str.substring(articleWord.length() + 1) + " " + articleWord;
return str;
}
}
return str;
}
private static String toType(final InventoryItem i) {
return i instanceof IPaperCard ? ((IPaperCard) i).getRules().getType().toString() : i.getItemType();
}