11package  mezz .jei .library .plugins .vanilla .anvil ;
22
3+ import  com .google .common .collect .Lists ;
34import  mezz .jei .api .constants .ModIds ;
45import  mezz .jei .api .constants .VanillaTypes ;
56import  mezz .jei .api .ingredients .IIngredientHelper ;
@@ -61,9 +62,11 @@ private EnchantmentData(Enchantment enchantment) {
6162
6263		public  List <ItemStack > getEnchantedBooks (ItemStack  ingredient ) {
6364			IPlatformItemStackHelper  itemStackHelper  = Services .PLATFORM .getItemStackHelper ();
64- 			return  enchantedBooks .stream ()
65+ 			var   list  =  enchantedBooks .stream ()
6566				.filter (enchantedBook  -> itemStackHelper .isBookEnchantable (ingredient , enchantedBook ))
6667				.toList ();
68+ 			// avoid using copy of list if it contains the exact same items 
69+ 			return  list .size () == enchantedBooks .size () ? enchantedBooks  : list ;
6770		}
6871
6972		private  boolean  canEnchant (ItemStack  ingredient ) {
@@ -109,6 +112,7 @@ private static Stream<IJeiAnvilRecipe> getBookEnchantmentRecipes(
109112		IIngredientHelper <ItemStack > ingredientHelper ,
110113		ItemStack  ingredient 
111114	) {
115+ 		var  ingredientSingletonList  = List .of (ingredient );
112116		return  enchantmentDatas .stream ()
113117			.filter (data  -> data .canEnchant (ingredient ))
114118			.map (data  -> data .getEnchantedBooks (ingredient ))
@@ -118,15 +122,16 @@ private static Stream<IJeiAnvilRecipe> getBookEnchantmentRecipes(
118122				String  ingredientId  = ingredientHelper .getUniqueId (ingredient , UidContext .Recipe );
119123				String  ingredientIdPath  = ResourceLocationUtil .sanitizePath (ingredientId );
120124				String  id  = "enchantment."  + ingredientIdPath ;
125+ 
121126				ResourceLocation  uid  = new  ResourceLocation (ModIds .MINECRAFT_ID , id );
122- 				return  vanillaRecipeFactory .createAnvilRecipe (ingredient , enchantedBooks , outputs , uid );
127+ 				// All lists given here are immutable, and we want to keep the transforming list from outputs, 
128+ 				// so we call the AnvilRecipe constructor directly 
129+ 				return  new  AnvilRecipe (ingredientSingletonList , enchantedBooks , outputs , uid );
123130			});
124131	}
125132
126133	private  static  List <ItemStack > getEnchantedIngredients (ItemStack  ingredient , List <ItemStack > enchantedBooks ) {
127- 		return  enchantedBooks .stream ()
128- 			.map (enchantedBook  -> getEnchantedIngredient (ingredient , enchantedBook ))
129- 			.toList ();
134+ 		return  Lists .transform (enchantedBooks , enchantedBook  -> getEnchantedIngredient (ingredient , enchantedBook ));
130135	}
131136
132137	private  static  ItemStack  getEnchantedIngredient (ItemStack  ingredient , ItemStack  enchantedBook ) {
@@ -271,9 +276,11 @@ private static Stream<IJeiAnvilRecipe> getRepairRecipes(
271276				ItemStack  damagedHalf  = itemStack .copy ();
272277				damagedHalf .setDamageValue (damagedHalf .getMaxDamage () / 2 );
273278
279+ 				var  damagedThreeQuartersSingletonList  = List .of (damagedThreeQuarters );
280+ 
274281				IJeiAnvilRecipe  repairWithSame  = vanillaRecipeFactory .createAnvilRecipe (
275- 					List . of ( damagedThreeQuarters ) ,
276- 					List . of ( damagedThreeQuarters ) ,
282+ 					damagedThreeQuartersSingletonList ,
283+ 					damagedThreeQuartersSingletonList ,
277284					List .of (damagedHalf ),
278285					new  ResourceLocation (itemModId , "self_repair."  + ingredientIdPath )
279286				);
@@ -285,7 +292,7 @@ private static Stream<IJeiAnvilRecipe> getRepairRecipes(
285292					IJeiAnvilRecipe  repairWithMaterial  = vanillaRecipeFactory .createAnvilRecipe (
286293						List .of (damagedFully ),
287294						repairMaterials ,
288- 						List . of ( damagedThreeQuarters ) ,
295+ 						damagedThreeQuartersSingletonList ,
289296						new  ResourceLocation (itemModId , "materials_repair."  + ingredientIdPath )
290297					);
291298					consumer .accept (repairWithMaterial );
0 commit comments