|
| 1 | +package org.embeddedt.modernfix.common.mixin.perf.lazy_search_tree_registry; |
| 2 | + |
| 3 | +import com.google.common.base.Stopwatch; |
| 4 | +import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod; |
| 5 | +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
| 6 | +import net.minecraft.client.gui.screens.recipebook.RecipeCollection; |
| 7 | +import net.minecraft.client.multiplayer.SessionSearchTrees; |
| 8 | +import net.minecraft.client.searchtree.SearchTree; |
| 9 | +import org.embeddedt.modernfix.ModernFix; |
| 10 | +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; |
| 11 | +import org.spongepowered.asm.mixin.Mixin; |
| 12 | +import org.spongepowered.asm.mixin.Shadow; |
| 13 | +import org.spongepowered.asm.mixin.injection.At; |
| 14 | +import org.spongepowered.asm.mixin.injection.ModifyArg; |
| 15 | + |
| 16 | +import java.util.concurrent.CompletableFuture; |
| 17 | +import java.util.function.Supplier; |
| 18 | + |
| 19 | +@Mixin(SessionSearchTrees.class) |
| 20 | +@ClientOnlyMixin |
| 21 | +public class SessionSearchTreesMixin { |
| 22 | + @Shadow private CompletableFuture<SearchTree<RecipeCollection>> recipeSearch; |
| 23 | + private Supplier<SearchTree<RecipeCollection>> mfix$deferredSearchTreeSupplier; |
| 24 | + |
| 25 | + @ModifyArg(method = { "method_60367", "lambda$updateRecipes$8" }, at = @At(value = "INVOKE", target = "Ljava/util/concurrent/CompletableFuture;supplyAsync(Ljava/util/function/Supplier;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;")) |
| 26 | + private Supplier<SearchTree<RecipeCollection>> mfix$deferProcessing(Supplier<SearchTree<RecipeCollection>> supplier) { |
| 27 | + this.mfix$deferredSearchTreeSupplier = supplier; |
| 28 | + return SearchTree::empty; |
| 29 | + } |
| 30 | + |
| 31 | + @WrapMethod(method = "recipes") |
| 32 | + private SearchTree<RecipeCollection> mfix$processDeferredBuild(Operation<SearchTree<RecipeCollection>> original) { |
| 33 | + synchronized (this) { |
| 34 | + if (mfix$deferredSearchTreeSupplier != null) { |
| 35 | + Stopwatch watch = Stopwatch.createStarted(); |
| 36 | + this.recipeSearch = CompletableFuture.completedFuture(mfix$deferredSearchTreeSupplier.get()); |
| 37 | + watch.stop(); |
| 38 | + ModernFix.LOGGER.info("Building recipe book search tree took {}", watch); |
| 39 | + mfix$deferredSearchTreeSupplier = null; |
| 40 | + } |
| 41 | + return original.call(); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments