Skip to content

Commit 033cfd3

Browse files
committed
Merge remote-tracking branch 'origin/1.18' into 1.19.2
2 parents aa6b7fa + c3eeb1e commit 033cfd3

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import net.minecraft.client.searchtree.SearchRegistry;
66
import org.embeddedt.modernfix.ModernFix;
77
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
8-
import org.embeddedt.modernfix.searchtree.DummySearchTree;
8+
import org.embeddedt.modernfix.searchtree.RecipeBookSearchTree;
99
import org.embeddedt.modernfix.searchtree.SearchTreeProviderRegistry;
1010
import org.lwjgl.glfw.GLFW;
1111
import org.lwjgl.glfw.GLFWErrorCallback;
@@ -29,7 +29,7 @@ private void replaceSearchTrees(CallbackInfo ci) {
2929
ModernFix.LOGGER.info("Replacing search trees with '{}' provider", provider.getName());
3030
this.searchRegistry.register(SearchRegistry.CREATIVE_NAMES, list -> provider.getSearchTree(false));
3131
this.searchRegistry.register(SearchRegistry.CREATIVE_TAGS, list -> provider.getSearchTree(true));
32-
this.searchRegistry.register(SearchRegistry.RECIPE_COLLECTIONS, list -> new DummySearchTree<>());
32+
this.searchRegistry.register(SearchRegistry.RECIPE_COLLECTIONS, list -> new RecipeBookSearchTree(provider.getSearchTree(false)));
3333
// grab components for all key mappings in order to prevent them from being loaded off-thread later
3434
// this populates the LazyLoadedValues
3535
// we also need to suppress GLFW errors to prevent crashes if a key is missing
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.embeddedt.modernfix.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+
9+
import java.util.ArrayList;
10+
import java.util.Collections;
11+
import java.util.List;
12+
import java.util.Map;
13+
import java.util.stream.Collectors;
14+
15+
public class RecipeBookSearchTree extends DummySearchTree<RecipeCollection> {
16+
private final SearchTree<ItemStack> stackCollector;
17+
private Map<Item, List<RecipeCollection>> collectionsByItem = null;
18+
private final List<RecipeCollection> allCollections = new ArrayList<>();
19+
20+
public RecipeBookSearchTree(SearchTree<ItemStack> stackCollector) {
21+
this.stackCollector = stackCollector;
22+
}
23+
24+
private Map<Item, List<RecipeCollection>> populateCollectionMap() {
25+
Map<Item, List<RecipeCollection>> collections = this.collectionsByItem;
26+
if(collections == null) {
27+
collections = new Object2ObjectOpenHashMap<>();
28+
Map<Item, List<RecipeCollection>> finalCollection = collections;
29+
for(RecipeCollection collection : allCollections) {
30+
collection.getRecipes().stream().map(recipe -> recipe.getResultItem().getItem()).distinct().forEach(item -> {
31+
finalCollection.computeIfAbsent(item, k -> new ArrayList<>()).add(collection);
32+
});
33+
}
34+
this.collectionsByItem = collections;
35+
}
36+
return collections;
37+
}
38+
39+
@Override
40+
public void add(RecipeCollection pObj) {
41+
this.allCollections.add(pObj);
42+
}
43+
44+
@Override
45+
public void clear() {
46+
this.allCollections.clear();
47+
}
48+
49+
@Override
50+
public void refresh() {
51+
this.collectionsByItem = null;
52+
}
53+
54+
@Override
55+
public List<RecipeCollection> search(String pSearchText) {
56+
// Avoid constructing the recipe collection map until the first real search
57+
if(pSearchText.trim().length() == 0) {
58+
return this.allCollections;
59+
}
60+
List<ItemStack> stacks = stackCollector.search(pSearchText);
61+
Map<Item, List<RecipeCollection>> collections = this.populateCollectionMap();
62+
return stacks.stream().map(ItemStack::getItem).distinct().flatMap(item -> collections.getOrDefault(item, Collections.emptyList()).stream()).collect(Collectors.toList());
63+
}
64+
}

0 commit comments

Comments
 (0)