File size: 4,625 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package net.minecraft.client;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Table;
import com.google.common.collect.ImmutableList.Builder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.Set;
import net.minecraft.client.gui.screens.recipebook.RecipeCollection;
import net.minecraft.client.gui.screens.recipebook.SearchRecipeBookCategory;
import net.minecraft.stats.RecipeBook;
import net.minecraft.world.item.crafting.ExtendedRecipeBookCategory;
import net.minecraft.world.item.crafting.RecipeBookCategory;
import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
import net.minecraft.world.item.crafting.display.RecipeDisplayId;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class ClientRecipeBook extends RecipeBook {
    private final Map<RecipeDisplayId, RecipeDisplayEntry> known = new HashMap<>();
    private final Set<RecipeDisplayId> highlight = new HashSet<>();
    private Map<ExtendedRecipeBookCategory, List<RecipeCollection>> collectionsByTab = Map.of();
    private List<RecipeCollection> allCollections = List.of();

    public void add(RecipeDisplayEntry p_367545_) {
        this.known.put(p_367545_.id(), p_367545_);
    }

    public void remove(RecipeDisplayId p_365017_) {
        this.known.remove(p_365017_);
        this.highlight.remove(p_365017_);
    }

    public void clear() {
        this.known.clear();
        this.highlight.clear();
    }

    public boolean willHighlight(RecipeDisplayId p_364304_) {
        return this.highlight.contains(p_364304_);
    }

    public void removeHighlight(RecipeDisplayId p_365808_) {
        this.highlight.remove(p_365808_);
    }

    public void addHighlight(RecipeDisplayId p_364710_) {
        this.highlight.add(p_364710_);
    }

    public void rebuildCollections() {
        Map<RecipeBookCategory, List<List<RecipeDisplayEntry>>> map = categorizeAndGroupRecipes(this.known.values());
        Map<ExtendedRecipeBookCategory, List<RecipeCollection>> map1 = new HashMap<>();
        Builder<RecipeCollection> builder = ImmutableList.builder();
        map.forEach(
            (p_357635_, p_357636_) -> map1.put(
                    p_357635_, p_357636_.stream().map(RecipeCollection::new).peek(builder::add).collect(ImmutableList.toImmutableList())
                )
        );

        for (SearchRecipeBookCategory searchrecipebookcategory : SearchRecipeBookCategory.values()) {
            map1.put(
                searchrecipebookcategory,
                searchrecipebookcategory.includedCategories()
                    .stream()
                    .flatMap(p_357639_ -> map1.getOrDefault(p_357639_, List.of()).stream())
                    .collect(ImmutableList.toImmutableList())
            );
        }

        this.collectionsByTab = Map.copyOf(map1);
        this.allCollections = builder.build();
    }

    private static Map<RecipeBookCategory, List<List<RecipeDisplayEntry>>> categorizeAndGroupRecipes(Iterable<RecipeDisplayEntry> p_90643_) {
        Map<RecipeBookCategory, List<List<RecipeDisplayEntry>>> map = new HashMap<>();
        Table<RecipeBookCategory, Integer, List<RecipeDisplayEntry>> table = HashBasedTable.create();

        for (RecipeDisplayEntry recipedisplayentry : p_90643_) {
            RecipeBookCategory recipebookcategory = recipedisplayentry.category();
            OptionalInt optionalint = recipedisplayentry.group();
            if (optionalint.isEmpty()) {
                map.computeIfAbsent(recipebookcategory, p_357637_ -> new ArrayList<>()).add(List.of(recipedisplayentry));
            } else {
                List<RecipeDisplayEntry> list = table.get(recipebookcategory, optionalint.getAsInt());
                if (list == null) {
                    list = new ArrayList<>();
                    table.put(recipebookcategory, optionalint.getAsInt(), list);
                    map.computeIfAbsent(recipebookcategory, p_357640_ -> new ArrayList<>()).add(list);
                }

                list.add(recipedisplayentry);
            }
        }

        return map;
    }

    public List<RecipeCollection> getCollections() {
        return this.allCollections;
    }

    public List<RecipeCollection> getCollection(ExtendedRecipeBookCategory p_362967_) {
        return this.collectionsByTab.getOrDefault(p_362967_, Collections.emptyList());
    }
}