| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import com.google.common.collect.HashBasedTable;
|
| 4 | import com.google.common.collect.ImmutableList;
|
| 5 | import com.google.common.collect.Table;
|
| 6 | import com.google.common.collect.ImmutableList.Builder;
|
| 7 | import java.util.ArrayList;
|
| 8 | import java.util.Collections;
|
| 9 | import java.util.HashMap;
|
| 10 | import java.util.HashSet;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.OptionalInt;
|
| 14 | import java.util.Set;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import net.minecraft.client.gui.screens.recipebook.RecipeCollection;
|
| 18 | import net.minecraft.client.gui.screens.recipebook.SearchRecipeBookCategory;
|
| 19 | import net.minecraft.stats.RecipeBook;
|
| 20 | import net.minecraft.world.item.crafting.ExtendedRecipeBookCategory;
|
| 21 | import net.minecraft.world.item.crafting.RecipeBookCategory;
|
| 22 | import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
|
| 23 | import net.minecraft.world.item.crafting.display.RecipeDisplayId;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public class ClientRecipeBook extends RecipeBook {
|
| 27 | private final Map<RecipeDisplayId, RecipeDisplayEntry> known = new HashMap<>();
|
| 28 | private final Set<RecipeDisplayId> highlight = new HashSet<>();
|
| 29 | private Map<ExtendedRecipeBookCategory, List<RecipeCollection>> collectionsByTab = Map.of();
|
| 30 | private List<RecipeCollection> allCollections = List.of();
|
| 31 |
|
| 32 | public void add(final RecipeDisplayEntry display) {
|
| 33 | this.known.put(display.id(), display);
|
| 34 | }
|
| 35 |
|
| 36 | public void remove(final RecipeDisplayId id) {
|
| 37 | this.known.remove(id);
|
| 38 | this.highlight.remove(id);
|
| 39 | }
|
| 40 |
|
| 41 | public void clear() {
|
| 42 | this.known.clear();
|
| 43 | this.highlight.clear();
|
| 44 | }
|
| 45 |
|
| 46 | public boolean willHighlight(final RecipeDisplayId recipe) {
|
| 47 | return this.highlight.contains(recipe);
|
| 48 | }
|
| 49 |
|
| 50 | public void removeHighlight(final RecipeDisplayId id) {
|
| 51 | this.highlight.remove(id);
|
| 52 | }
|
| 53 |
|
| 54 | public void addHighlight(final RecipeDisplayId id) {
|
| 55 | this.highlight.add(id);
|
| 56 | }
|
| 57 |
|
| 58 | public void rebuildCollections() {
|
| 59 | Map<RecipeBookCategory, List<List<RecipeDisplayEntry>>> recipeListsByCategory = categorizeAndGroupRecipes(this.known.values());
|
| 60 | Map<ExtendedRecipeBookCategory, List<RecipeCollection>> byCategory = new HashMap<>();
|
| 61 | Builder<RecipeCollection> all = ImmutableList.builder();
|
| 62 | recipeListsByCategory.forEach(
|
| 63 | (category, categoryRecipes) -> byCategory.put(
|
| 64 | category, categoryRecipes.stream().map(RecipeCollection::new).peek(all::add).collect(ImmutableList.toImmutableList())
|
| 65 | )
|
| 66 | );
|
| 67 |
|
| 68 | for (SearchRecipeBookCategory searchCategory : SearchRecipeBookCategory.values()) {
|
| 69 | byCategory.put(
|
| 70 | searchCategory,
|
| 71 | searchCategory.includedCategories()
|
| 72 | .stream()
|
| 73 | .flatMap(subCategory -> byCategory.getOrDefault(subCategory, List.of()).stream())
|
| 74 | .collect(ImmutableList.toImmutableList())
|
| 75 | );
|
| 76 | }
|
| 77 |
|
| 78 | this.collectionsByTab = Map.copyOf(byCategory);
|
| 79 | this.allCollections = all.build();
|
| 80 | }
|
| 81 |
|
| 82 | private static Map<RecipeBookCategory, List<List<RecipeDisplayEntry>>> categorizeAndGroupRecipes(final Iterable<RecipeDisplayEntry> recipes) {
|
| 83 | Map<RecipeBookCategory, List<List<RecipeDisplayEntry>>> result = new HashMap<>();
|
| 84 | Table<RecipeBookCategory, Integer, List<RecipeDisplayEntry>> multiItemGroups = HashBasedTable.create();
|
| 85 |
|
| 86 | for (RecipeDisplayEntry entry : recipes) {
|
| 87 | RecipeBookCategory category = entry.category();
|
| 88 | OptionalInt groupId = entry.group();
|
| 89 | if (groupId.isEmpty()) {
|
| 90 | result.computeIfAbsent(category, key -> new ArrayList<>()).add(List.of(entry));
|
| 91 | } else {
|
| 92 | List<RecipeDisplayEntry> groupRecipes = multiItemGroups.get(category, groupId.getAsInt());
|
| 93 | if (groupRecipes == null) {
|
| 94 | groupRecipes = new ArrayList<>();
|
| 95 | multiItemGroups.put(category, groupId.getAsInt(), groupRecipes);
|
| 96 | result.computeIfAbsent(category, key -> new ArrayList<>()).add(groupRecipes);
|
| 97 | }
|
| 98 |
|
| 99 | groupRecipes.add(entry);
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | return result;
|
| 104 | }
|
| 105 |
|
| 106 | public List<RecipeCollection> getCollections() {
|
| 107 | return this.allCollections;
|
| 108 | }
|
| 109 |
|
| 110 | public List<RecipeCollection> getCollection(final ExtendedRecipeBookCategory category) {
|
| 111 | return this.collectionsByTab.getOrDefault(category, Collections.emptyList());
|
| 112 | }
|
| 113 | }
|
| 114 |
|