Original license: MIT Original project: https://github.com/fxmorin/carpet-fixes Optimized the RecipeManager getFirstMatch call to be up to 3x faster This is a fully vanilla optimization. Improves: [Blast]Furnace/Campfire/Smoker/Stonecutter/Crafting/Sheep Color Choosing This was mostly made for the auto crafting table, since the performance boost is much more visible while using that mod
85 lines
3.9 KiB
Diff
85 lines
3.9 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: nostalgic853 <yuu8583@proton.me>
|
|
Date: Tue, 25 Oct 2022 00:57:45 +0800
|
|
Subject: [PATCH] Use optimized RecipeManager
|
|
|
|
Original license: MIT
|
|
Original project: https://github.com/fxmorin/carpet-fixes
|
|
|
|
Optimized the RecipeManager getFirstMatch call to be up to 3x faster
|
|
This is a fully vanilla optimization. Improves: [Blast]Furnace/Campfire/Smoker/Stonecutter/Crafting/Sheep Color Choosing
|
|
This was mostly made for the auto crafting table, since the performance boost is much more visible while using that mod
|
|
|
|
diff --git a/src/main/java/net/minecraft/world/item/crafting/RecipeManager.java b/src/main/java/net/minecraft/world/item/crafting/RecipeManager.java
|
|
index 4872f70977e73c889fe6e2339d00ebc64459613d..3e9cce51080addf294abab30532244628a9c63a2 100644
|
|
--- a/src/main/java/net/minecraft/world/item/crafting/RecipeManager.java
|
|
+++ b/src/main/java/net/minecraft/world/item/crafting/RecipeManager.java
|
|
@@ -11,14 +11,7 @@ import com.google.gson.JsonParseException;
|
|
import com.google.gson.JsonSyntaxException;
|
|
import com.mojang.datafixers.util.Pair;
|
|
import com.mojang.logging.LogUtils;
|
|
-import java.util.Collection;
|
|
-import java.util.Collections;
|
|
-import java.util.Comparator;
|
|
-import java.util.Iterator;
|
|
-import java.util.List;
|
|
-import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
-import java.util.Optional;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
import javax.annotation.Nullable;
|
|
@@ -33,6 +26,7 @@ import net.minecraft.world.Container;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.level.Level;
|
|
import org.slf4j.Logger;
|
|
+import java.util.*; // KeYi
|
|
|
|
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; // CraftBukkit
|
|
|
|
@@ -103,13 +97,28 @@ public class RecipeManager extends SimpleJsonResourceReloadListener {
|
|
}
|
|
|
|
public <C extends Container, T extends Recipe<C>> Optional<T> getRecipeFor(RecipeType<T> type, C inventory, Level world) {
|
|
- // CraftBukkit start
|
|
- Optional<T> recipe = this.byType(type).values().stream().filter((irecipe) -> {
|
|
- return irecipe.matches(inventory, world);
|
|
- }).findFirst();
|
|
- inventory.setCurrentRecipe(recipe.orElse(null)); // CraftBukkit - Clear recipe when no recipe is found
|
|
- // CraftBukkit end
|
|
- return recipe;
|
|
+ // KeYi start
|
|
+ int slots = 0;
|
|
+ int count;
|
|
+ //compare size to quickly remove recipes that are not even close. Plus remove streams
|
|
+ for (int slot = 0; slot < inventory.getContainerSize(); slot++)
|
|
+ if (!inventory.getItem(slot).isEmpty()) slots++;
|
|
+ for (Recipe<C> recipe : this.getAllRecipesFor(type)) {
|
|
+ count = 0;
|
|
+ if (recipe instanceof CustomRecipe) {
|
|
+ if (recipe.matches(inventory, world)) {
|
|
+ return (Optional<T>) Optional.of(recipe);
|
|
+ }
|
|
+ } else {
|
|
+ for (Ingredient ingredient : recipe.getIngredients())
|
|
+ if (ingredient != Ingredient.EMPTY) count++;
|
|
+ if (count == slots && recipe.matches(inventory, world)) {
|
|
+ return (Optional<T>) Optional.of(recipe);
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+ return Optional.empty();
|
|
+ // KeYi end
|
|
}
|
|
|
|
public <C extends Container, T extends Recipe<C>> Optional<Pair<ResourceLocation, T>> getRecipeFor(RecipeType<T> type, C inventory, Level world, @Nullable ResourceLocation id) {
|
|
@@ -131,7 +140,7 @@ public class RecipeManager extends SimpleJsonResourceReloadListener {
|
|
}
|
|
|
|
public <C extends Container, T extends Recipe<C>> List<T> getAllRecipesFor(RecipeType<T> type) {
|
|
- return List.copyOf(this.byType(type).values());
|
|
+ return new ArrayList<>(this.getAllRecipesFor(type)); // KeYi
|
|
}
|
|
|
|
public <C extends Container, T extends Recipe<C>> List<T> getRecipesFor(RecipeType<T> type, C inventory, Level world) {
|