| 1 | package net.minecraft.client.multiplayer;
|
| 2 |
|
| 3 | import java.util.IdentityHashMap;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.concurrent.CompletableFuture;
|
| 7 | import java.util.stream.Stream;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.ChatFormatting;
|
| 11 | import net.minecraft.client.ClientRecipeBook;
|
| 12 | import net.minecraft.client.gui.screens.recipebook.RecipeCollection;
|
| 13 | import net.minecraft.client.searchtree.FullTextSearchTree;
|
| 14 | import net.minecraft.client.searchtree.IdSearchTree;
|
| 15 | import net.minecraft.client.searchtree.SearchTree;
|
| 16 | import net.minecraft.core.Registry;
|
| 17 | import net.minecraft.core.RegistryAccess;
|
| 18 | import net.minecraft.core.HolderLookup.Provider;
|
| 19 | import net.minecraft.core.registries.Registries;
|
| 20 | import net.minecraft.network.chat.Component;
|
| 21 | import net.minecraft.resources.ResourceKey;
|
| 22 | import net.minecraft.tags.TagKey;
|
| 23 | import net.minecraft.util.Util;
|
| 24 | import net.minecraft.util.context.ContextMap;
|
| 25 | import net.minecraft.world.item.Item;
|
| 26 | import net.minecraft.world.item.ItemStack;
|
| 27 | import net.minecraft.world.item.TooltipFlag;
|
| 28 | import net.minecraft.world.item.Item.TooltipContext;
|
| 29 | import net.minecraft.world.item.TooltipFlag.Default;
|
| 30 | import net.minecraft.world.item.crafting.display.SlotDisplayContext;
|
| 31 | import net.minecraft.world.level.Level;
|
| 32 |
|
| 33 | @Environment(EnvType.CLIENT)
|
| 34 | public class SessionSearchTrees {
|
| 35 | private static final SessionSearchTrees.Key RECIPE_COLLECTIONS = new SessionSearchTrees.Key();
|
| 36 | private static final SessionSearchTrees.Key CREATIVE_NAMES = new SessionSearchTrees.Key();
|
| 37 | private static final SessionSearchTrees.Key CREATIVE_TAGS = new SessionSearchTrees.Key();
|
| 38 | private CompletableFuture<SearchTree<ItemStack>> creativeByNameSearch = CompletableFuture.completedFuture(SearchTree.empty());
|
| 39 | private CompletableFuture<SearchTree<ItemStack>> creativeByTagSearch = CompletableFuture.completedFuture(SearchTree.empty());
|
| 40 | private CompletableFuture<SearchTree<RecipeCollection>> recipeSearch = CompletableFuture.completedFuture(SearchTree.empty());
|
| 41 | private final Map<SessionSearchTrees.Key, Runnable> reloaders = new IdentityHashMap<>();
|
| 42 |
|
| 43 | private void register(final SessionSearchTrees.Key location, final Runnable updater) {
|
| 44 | updater.run();
|
| 45 | this.reloaders.put(location, updater);
|
| 46 | }
|
| 47 |
|
| 48 | public void rebuildAfterLanguageChange() {
|
| 49 | for (Runnable value : this.reloaders.values()) {
|
| 50 | value.run();
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | private static Stream<String> getTooltipLines(final Stream<ItemStack> items, final TooltipContext context, final TooltipFlag flag) {
|
| 55 | return items.<Component>flatMap(item -> item.getTooltipLines(context, null, flag).stream())
|
| 56 | .map(l -> ChatFormatting.stripFormatting(l.getString()).trim())
|
| 57 | .filter(s -> !s.isEmpty());
|
| 58 | }
|
| 59 |
|
| 60 | public void updateRecipes(final ClientRecipeBook recipeBook, final Level level) {
|
| 61 | this.register(
|
| 62 | RECIPE_COLLECTIONS,
|
| 63 | () -> {
|
| 64 | List<RecipeCollection> recipes = recipeBook.getCollections();
|
| 65 | RegistryAccess registryAccess = level.registryAccess();
|
| 66 | Registry<Item> itemRegistries = registryAccess.lookupOrThrow(Registries.ITEM);
|
| 67 | TooltipContext tooltipContext = TooltipContext.of(registryAccess);
|
| 68 | ContextMap recipeContext = SlotDisplayContext.fromLevel(level);
|
| 69 | TooltipFlag tooltipFlag = Default.NORMAL;
|
| 70 | CompletableFuture<?> previous = this.recipeSearch;
|
| 71 | this.recipeSearch = CompletableFuture.supplyAsync(
|
| 72 | () -> new FullTextSearchTree<>(
|
| 73 | collection -> getTooltipLines(collection.getRecipes().stream().flatMap(e -> e.resultItems(recipeContext).stream()), tooltipContext, tooltipFlag),
|
| 74 | collection -> collection.getRecipes().stream().flatMap(e -> e.resultItems(recipeContext).stream()).map(stack -> itemRegistries.getKey(stack.getItem())),
|
| 75 | recipes
|
| 76 | ),
|
| 77 | Util.backgroundExecutor()
|
| 78 | );
|
| 79 | previous.cancel(true);
|
| 80 | }
|
| 81 | );
|
| 82 | }
|
| 83 |
|
| 84 | public SearchTree<RecipeCollection> recipes() {
|
| 85 | return this.recipeSearch.join();
|
| 86 | }
|
| 87 |
|
| 88 | public void updateCreativeTags(final List<ItemStack> items) {
|
| 89 | this.register(
|
| 90 | CREATIVE_TAGS,
|
| 91 | () -> {
|
| 92 | CompletableFuture<?> previous = this.creativeByTagSearch;
|
| 93 | this.creativeByTagSearch = CompletableFuture.supplyAsync(
|
| 94 | () -> new IdSearchTree<>(itemStack -> itemStack.tags().map(TagKey::location), items), Util.backgroundExecutor()
|
| 95 | );
|
| 96 | previous.cancel(true);
|
| 97 | }
|
| 98 | );
|
| 99 | }
|
| 100 |
|
| 101 | public SearchTree<ItemStack> creativeTagSearch() {
|
| 102 | return this.creativeByTagSearch.join();
|
| 103 | }
|
| 104 |
|
| 105 | public void updateCreativeTooltips(final Provider registries, final List<ItemStack> itemStacks) {
|
| 106 | this.register(
|
| 107 | CREATIVE_NAMES,
|
| 108 | () -> {
|
| 109 | TooltipContext tooltipContext = TooltipContext.of(registries);
|
| 110 | TooltipFlag tooltipFlag = Default.NORMAL.asCreative();
|
| 111 | CompletableFuture<?> previous = this.creativeByNameSearch;
|
| 112 | this.creativeByNameSearch = CompletableFuture.supplyAsync(
|
| 113 | () -> new FullTextSearchTree<>(
|
| 114 | itemStack -> getTooltipLines(Stream.of(itemStack), tooltipContext, tooltipFlag),
|
| 115 | itemStack -> itemStack.typeHolder().unwrapKey().map(ResourceKey::identifier).stream(),
|
| 116 | itemStacks
|
| 117 | ),
|
| 118 | Util.backgroundExecutor()
|
| 119 | );
|
| 120 | previous.cancel(true);
|
| 121 | }
|
| 122 | );
|
| 123 | }
|
| 124 |
|
| 125 | public SearchTree<ItemStack> creativeNameSearch() {
|
| 126 | return this.creativeByNameSearch.join();
|
| 127 | }
|
| 128 |
|
| 129 | @Environment(EnvType.CLIENT)
|
| 130 | private static class Key {
|
| 131 | }
|
| 132 | }
|
| 133 |
|