/
IngredientSorter.java
67 lines (54 loc) · 2.17 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
58
59
60
61
62
63
64
65
66
67
package mezz.jei.ingredients;
import mezz.jei.config.sorting.ModNameSortingConfig;
import mezz.jei.gui.ingredients.IIngredientListElement;
import net.minecraft.item.ItemStack;
import javax.annotation.Nullable;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
public final class IngredientSorter implements IIngredientSorter {
private static final Comparator<IIngredientListElementInfo<?>> ITEM_STACK_FIRST =
Comparator.comparing((IIngredientListElementInfo<?> o) -> {
IIngredientListElement<?> element = o.getElement();
Object ingredient = element.getIngredient();
return ingredient instanceof ItemStack;
}).reversed();
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;
@Nullable
private Comparator<IIngredientListElementInfo<?>> comparator;
public IngredientSorter(ModNameSortingConfig modNameSortingConfig) {
this.modNameSortingConfig = modNameSortingConfig;
}
@Override
public Comparator<IIngredientListElementInfo<?>> getComparator(Set<String> allValues) {
if (this.comparator == null) {
Comparator<IIngredientListElementInfo<?>> modName = this.modNameSortingConfig.getComparatorFromMappedValues(allValues);
this.comparator = createComparator(modName);
}
return this.comparator;
}
@Override
public void invalidateCache() {
this.comparator = null;
}
private static Comparator<IIngredientListElementInfo<?>> createModNameComparator(List<String> ordering) {
return Comparator.comparingInt(o -> {
String modNameForSorting = o.getModNameForSorting();
int index = ordering.indexOf(modNameForSorting);
if (index < 0) {
index = Integer.MAX_VALUE;
}
return index;
});
}
private static Comparator<IIngredientListElementInfo<?>> createComparator(Comparator<IIngredientListElementInfo<?>> modName) {
return modName.thenComparing(ITEM_STACK_FIRST).thenComparing(CREATIVE);
}
}