/
IngredientSorter.java
57 lines (44 loc) · 2.08 KB
/
IngredientSorter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package mezz.jei.ingredients;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.config.sorting.IngredientTypeSortingConfig;
import mezz.jei.config.sorting.ModNameSortingConfig;
import mezz.jei.gui.ingredients.IIngredientListElement;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Comparator;
import java.util.Set;
import java.util.stream.Collectors;
public final class IngredientSorter implements IIngredientSorter {
private static final Comparator<IIngredientListElementInfo<?>> CREATIVE =
Comparator.comparingInt(o -> {
IIngredientListElement<?> element = o.getElement();
return element.getOrderIndex();
});
private static final Comparator<IIngredientListElementInfo<?>> ALPHABETICAL =
Comparator.comparing(IIngredientListElementInfo::getName);
private final ModNameSortingConfig modNameSortingConfig;
private final IngredientTypeSortingConfig ingredientTypeSortingConfig;
@Nullable
private Comparator<IIngredientListElementInfo<?>> comparator;
public IngredientSorter(ModNameSortingConfig modNameSortingConfig, IngredientTypeSortingConfig ingredientTypeSortingConfig) {
this.modNameSortingConfig = modNameSortingConfig;
this.ingredientTypeSortingConfig = ingredientTypeSortingConfig;
}
@Override
public Comparator<IIngredientListElementInfo<?>> getComparator(Collection<String> modNames, Collection<IIngredientType<?>> ingredientTypes) {
if (this.comparator == null) {
Comparator<IIngredientListElementInfo<?>> modName = this.modNameSortingConfig.getComparatorFromMappedValues(modNames);
Set<String> ingredientTypeStrings = ingredientTypes.stream()
.map(IIngredientType::getIngredientClass)
.map(IngredientTypeSortingConfig::getIngredientType)
.collect(Collectors.toSet());
Comparator<IIngredientListElementInfo<?>> ingredientType = this.ingredientTypeSortingConfig.getComparatorFromMappedValues(ingredientTypeStrings);
this.comparator = modName.thenComparing(ingredientType).thenComparing(CREATIVE);
}
return this.comparator;
}
@Override
public void invalidateCache() {
this.comparator = null;
}
}