Skip to content

Commit db10590

Browse files
committed
Implement a fallback search tree for the recipe book
1 parent 9e3e732 commit db10590

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

forge/src/main/java/org/embeddedt/modernfix/forge/mixin/perf/blast_search_trees/MinecraftMixin.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.minecraft.world.item.ItemStack;
1010
import net.minecraftforge.fml.ModList;
1111
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
12+
import org.embeddedt.modernfix.forge.searchtree.RecipeBookSearchTree;
1213
import org.embeddedt.modernfix.searchtree.DummySearchTree;
1314
import org.embeddedt.modernfix.forge.searchtree.JEIBackedSearchTree;
1415
import org.spongepowered.asm.mixin.Final;
@@ -28,13 +29,15 @@ private void replaceSearchTrees(CallbackInfo ci) {
2829
ci.cancel();
2930
mfix$runItemFillingQuirk();
3031
if(ModList.get().getModFileById("jei") != null && ModList.get().getModFileById("roughlyenoughitems") == null) {
31-
this.searchRegistry.register(SearchRegistry.CREATIVE_NAMES, new JEIBackedSearchTree(false));
32+
JEIBackedSearchTree mainTree = new JEIBackedSearchTree(false);
33+
this.searchRegistry.register(SearchRegistry.CREATIVE_NAMES, mainTree);
3234
this.searchRegistry.register(SearchRegistry.CREATIVE_TAGS, new JEIBackedSearchTree(true));
35+
this.searchRegistry.register(SearchRegistry.RECIPE_COLLECTIONS, new RecipeBookSearchTree(mainTree));
3336
} else {
3437
this.searchRegistry.register(SearchRegistry.CREATIVE_NAMES, new DummySearchTree<>());
3538
this.searchRegistry.register(SearchRegistry.CREATIVE_TAGS, new DummySearchTree<>());
39+
this.searchRegistry.register(SearchRegistry.RECIPE_COLLECTIONS, new DummySearchTree<>());
3640
}
37-
this.searchRegistry.register(SearchRegistry.RECIPE_COLLECTIONS, new DummySearchTree<>());
3841
}
3942

4043

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.embeddedt.modernfix.forge.searchtree;
2+
3+
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
4+
import net.minecraft.client.gui.screens.recipebook.RecipeCollection;
5+
import net.minecraft.client.searchtree.SearchTree;
6+
import net.minecraft.world.item.Item;
7+
import net.minecraft.world.item.ItemStack;
8+
import org.embeddedt.modernfix.searchtree.DummySearchTree;
9+
10+
import java.util.ArrayList;
11+
import java.util.Collections;
12+
import java.util.List;
13+
import java.util.Map;
14+
import java.util.stream.Collectors;
15+
16+
public class RecipeBookSearchTree extends DummySearchTree<RecipeCollection> {
17+
private final SearchTree<ItemStack> stackCollector;
18+
private Map<Item, List<RecipeCollection>> collectionsByItem = null;
19+
private final List<RecipeCollection> allCollections = new ArrayList<>();
20+
21+
public RecipeBookSearchTree(SearchTree<ItemStack> stackCollector) {
22+
this.stackCollector = stackCollector;
23+
}
24+
25+
private Map<Item, List<RecipeCollection>> populateCollectionMap() {
26+
Map<Item, List<RecipeCollection>> collections = this.collectionsByItem;
27+
if(collections == null) {
28+
collections = new Object2ObjectOpenHashMap<>();
29+
Map<Item, List<RecipeCollection>> finalCollection = collections;
30+
for(RecipeCollection collection : allCollections) {
31+
collection.getRecipes().stream().map(recipe -> recipe.getResultItem().getItem()).distinct().forEach(item -> {
32+
finalCollection.computeIfAbsent(item, k -> new ArrayList<>()).add(collection);
33+
});
34+
}
35+
this.collectionsByItem = collections;
36+
}
37+
return collections;
38+
}
39+
40+
@Override
41+
public void add(RecipeCollection pObj) {
42+
this.allCollections.add(pObj);
43+
}
44+
45+
@Override
46+
public void clear() {
47+
this.allCollections.clear();
48+
}
49+
50+
@Override
51+
public void refresh() {
52+
this.collectionsByItem = null;
53+
}
54+
55+
@Override
56+
public List<RecipeCollection> search(String pSearchText) {
57+
// Avoid constructing the recipe collection map until the first real search
58+
if(pSearchText.trim().length() == 0) {
59+
return this.allCollections;
60+
}
61+
List<ItemStack> stacks = stackCollector.search(pSearchText);
62+
Map<Item, List<RecipeCollection>> collections = this.populateCollectionMap();
63+
return stacks.stream().map(ItemStack::getItem).distinct().flatMap(item -> collections.getOrDefault(item, Collections.emptyList()).stream()).collect(Collectors.toList());
64+
}
65+
}

0 commit comments

Comments
 (0)