From 90723d7809e3728e79b11cc9d27cd1f60f298bb3 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Fri, 10 Jan 2025 20:33:34 -0500 Subject: [PATCH] [1.20.1] Optimize AnvilRecipe storage (#3886) --- .../plugins/vanilla/VanillaRecipeFactory.java | 8 +++---- .../plugins/vanilla/anvil/AnvilRecipe.java | 6 ----- .../vanilla/anvil/AnvilRecipeMaker.java | 23 ++++++++++++------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/Library/src/main/java/mezz/jei/library/plugins/vanilla/VanillaRecipeFactory.java b/Library/src/main/java/mezz/jei/library/plugins/vanilla/VanillaRecipeFactory.java index 95707efdc..06ab7e781 100644 --- a/Library/src/main/java/mezz/jei/library/plugins/vanilla/VanillaRecipeFactory.java +++ b/Library/src/main/java/mezz/jei/library/plugins/vanilla/VanillaRecipeFactory.java @@ -30,7 +30,7 @@ public IJeiAnvilRecipe createAnvilRecipe(ItemStack leftInput, List ri ErrorUtil.checkNotEmpty(outputs, "outputs"); ErrorUtil.checkNotNull(uid, "uid"); - return new AnvilRecipe(List.of(leftInput), rightInputs, outputs, uid); + return new AnvilRecipe(List.of(leftInput), List.copyOf(rightInputs), List.copyOf(outputs), uid); } @Override @@ -39,7 +39,7 @@ public AnvilRecipe createAnvilRecipe(ItemStack leftInput, List rightI ErrorUtil.checkNotNull(rightInputs, "rightInputs"); ErrorUtil.checkNotEmpty(outputs, "outputs"); - return new AnvilRecipe(List.of(leftInput), rightInputs, outputs, null); + return new AnvilRecipe(List.of(leftInput), List.copyOf(rightInputs), List.copyOf(outputs), null); } @Override @@ -49,7 +49,7 @@ public AnvilRecipe createAnvilRecipe(List leftInputs, List ErrorUtil.checkNotEmpty(outputs, "outputs"); ErrorUtil.checkNotNull(uid, "uid"); - return new AnvilRecipe(leftInputs, rightInputs, outputs, uid); + return new AnvilRecipe(List.copyOf(leftInputs), List.copyOf(rightInputs), List.copyOf(outputs), uid); } @Override @@ -58,7 +58,7 @@ public AnvilRecipe createAnvilRecipe(List leftInputs, List ErrorUtil.checkNotNull(rightInputs, "rightInputs"); ErrorUtil.checkNotEmpty(outputs, "outputs"); - return new AnvilRecipe(leftInputs, rightInputs, outputs, null); + return new AnvilRecipe(List.copyOf(leftInputs), List.copyOf(rightInputs), List.copyOf(outputs), null); } @Override diff --git a/Library/src/main/java/mezz/jei/library/plugins/vanilla/anvil/AnvilRecipe.java b/Library/src/main/java/mezz/jei/library/plugins/vanilla/anvil/AnvilRecipe.java index 0a2c16f68..d91a68802 100644 --- a/Library/src/main/java/mezz/jei/library/plugins/vanilla/anvil/AnvilRecipe.java +++ b/Library/src/main/java/mezz/jei/library/plugins/vanilla/anvil/AnvilRecipe.java @@ -13,12 +13,6 @@ public record AnvilRecipe( List outputs, @Nullable ResourceLocation uid ) implements IJeiAnvilRecipe { - public AnvilRecipe(List leftInputs, List rightInputs, List outputs, @Nullable ResourceLocation uid) { - this.leftInputs = List.copyOf(leftInputs); - this.rightInputs = List.copyOf(rightInputs); - this.outputs = List.copyOf(outputs); - this.uid = uid; - } @Override public List getLeftInputs() { diff --git a/Library/src/main/java/mezz/jei/library/plugins/vanilla/anvil/AnvilRecipeMaker.java b/Library/src/main/java/mezz/jei/library/plugins/vanilla/anvil/AnvilRecipeMaker.java index 2e17ee705..d2dc46dde 100644 --- a/Library/src/main/java/mezz/jei/library/plugins/vanilla/anvil/AnvilRecipeMaker.java +++ b/Library/src/main/java/mezz/jei/library/plugins/vanilla/anvil/AnvilRecipeMaker.java @@ -1,5 +1,6 @@ package mezz.jei.library.plugins.vanilla.anvil; +import com.google.common.collect.Lists; import mezz.jei.api.constants.ModIds; import mezz.jei.api.constants.VanillaTypes; import mezz.jei.api.ingredients.IIngredientHelper; @@ -61,9 +62,11 @@ private EnchantmentData(Enchantment enchantment) { public List getEnchantedBooks(ItemStack ingredient) { IPlatformItemStackHelper itemStackHelper = Services.PLATFORM.getItemStackHelper(); - return enchantedBooks.stream() + var list = enchantedBooks.stream() .filter(enchantedBook -> itemStackHelper.isBookEnchantable(ingredient, enchantedBook)) .toList(); + // avoid using copy of list if it contains the exact same items + return list.size() == enchantedBooks.size() ? enchantedBooks : list; } private boolean canEnchant(ItemStack ingredient) { @@ -109,6 +112,7 @@ private static Stream getBookEnchantmentRecipes( IIngredientHelper ingredientHelper, ItemStack ingredient ) { + var ingredientSingletonList = List.of(ingredient); return enchantmentDatas.stream() .filter(data -> data.canEnchant(ingredient)) .map(data -> data.getEnchantedBooks(ingredient)) @@ -118,15 +122,16 @@ private static Stream getBookEnchantmentRecipes( String ingredientId = ingredientHelper.getUniqueId(ingredient, UidContext.Recipe); String ingredientIdPath = ResourceLocationUtil.sanitizePath(ingredientId); String id = "enchantment." + ingredientIdPath; + ResourceLocation uid = new ResourceLocation(ModIds.MINECRAFT_ID, id); - return vanillaRecipeFactory.createAnvilRecipe(ingredient, enchantedBooks, outputs, uid); + // All lists given here are immutable, and we want to keep the transforming list from outputs, + // so we call the AnvilRecipe constructor directly + return new AnvilRecipe(ingredientSingletonList, enchantedBooks, outputs, uid); }); } private static List getEnchantedIngredients(ItemStack ingredient, List enchantedBooks) { - return enchantedBooks.stream() - .map(enchantedBook -> getEnchantedIngredient(ingredient, enchantedBook)) - .toList(); + return Lists.transform(enchantedBooks, enchantedBook -> getEnchantedIngredient(ingredient, enchantedBook)); } private static ItemStack getEnchantedIngredient(ItemStack ingredient, ItemStack enchantedBook) { @@ -271,9 +276,11 @@ private static Stream getRepairRecipes( ItemStack damagedHalf = itemStack.copy(); damagedHalf.setDamageValue(damagedHalf.getMaxDamage() / 2); + var damagedThreeQuartersSingletonList = List.of(damagedThreeQuarters); + IJeiAnvilRecipe repairWithSame = vanillaRecipeFactory.createAnvilRecipe( - List.of(damagedThreeQuarters), - List.of(damagedThreeQuarters), + damagedThreeQuartersSingletonList, + damagedThreeQuartersSingletonList, List.of(damagedHalf), new ResourceLocation(itemModId, "self_repair." + ingredientIdPath) ); @@ -285,7 +292,7 @@ private static Stream getRepairRecipes( IJeiAnvilRecipe repairWithMaterial = vanillaRecipeFactory.createAnvilRecipe( List.of(damagedFully), repairMaterials, - List.of(damagedThreeQuarters), + damagedThreeQuartersSingletonList, new ResourceLocation(itemModId, "materials_repair." + ingredientIdPath) ); consumer.accept(repairWithMaterial);