Skip to content

Commit 90723d7

Browse files
authored
[1.20.1] Optimize AnvilRecipe storage (#3886)
1 parent c3a918d commit 90723d7

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

Library/src/main/java/mezz/jei/library/plugins/vanilla/VanillaRecipeFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public IJeiAnvilRecipe createAnvilRecipe(ItemStack leftInput, List<ItemStack> ri
3030
ErrorUtil.checkNotEmpty(outputs, "outputs");
3131
ErrorUtil.checkNotNull(uid, "uid");
3232

33-
return new AnvilRecipe(List.of(leftInput), rightInputs, outputs, uid);
33+
return new AnvilRecipe(List.of(leftInput), List.copyOf(rightInputs), List.copyOf(outputs), uid);
3434
}
3535

3636
@Override
@@ -39,7 +39,7 @@ public AnvilRecipe createAnvilRecipe(ItemStack leftInput, List<ItemStack> rightI
3939
ErrorUtil.checkNotNull(rightInputs, "rightInputs");
4040
ErrorUtil.checkNotEmpty(outputs, "outputs");
4141

42-
return new AnvilRecipe(List.of(leftInput), rightInputs, outputs, null);
42+
return new AnvilRecipe(List.of(leftInput), List.copyOf(rightInputs), List.copyOf(outputs), null);
4343
}
4444

4545
@Override
@@ -49,7 +49,7 @@ public AnvilRecipe createAnvilRecipe(List<ItemStack> leftInputs, List<ItemStack>
4949
ErrorUtil.checkNotEmpty(outputs, "outputs");
5050
ErrorUtil.checkNotNull(uid, "uid");
5151

52-
return new AnvilRecipe(leftInputs, rightInputs, outputs, uid);
52+
return new AnvilRecipe(List.copyOf(leftInputs), List.copyOf(rightInputs), List.copyOf(outputs), uid);
5353
}
5454

5555
@Override
@@ -58,7 +58,7 @@ public AnvilRecipe createAnvilRecipe(List<ItemStack> leftInputs, List<ItemStack>
5858
ErrorUtil.checkNotNull(rightInputs, "rightInputs");
5959
ErrorUtil.checkNotEmpty(outputs, "outputs");
6060

61-
return new AnvilRecipe(leftInputs, rightInputs, outputs, null);
61+
return new AnvilRecipe(List.copyOf(leftInputs), List.copyOf(rightInputs), List.copyOf(outputs), null);
6262
}
6363

6464
@Override

Library/src/main/java/mezz/jei/library/plugins/vanilla/anvil/AnvilRecipe.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ public record AnvilRecipe(
1313
List<ItemStack> outputs,
1414
@Nullable ResourceLocation uid
1515
) implements IJeiAnvilRecipe {
16-
public AnvilRecipe(List<ItemStack> leftInputs, List<ItemStack> rightInputs, List<ItemStack> outputs, @Nullable ResourceLocation uid) {
17-
this.leftInputs = List.copyOf(leftInputs);
18-
this.rightInputs = List.copyOf(rightInputs);
19-
this.outputs = List.copyOf(outputs);
20-
this.uid = uid;
21-
}
2216

2317
@Override
2418
public List<ItemStack> getLeftInputs() {

Library/src/main/java/mezz/jei/library/plugins/vanilla/anvil/AnvilRecipeMaker.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mezz.jei.library.plugins.vanilla.anvil;
22

3+
import com.google.common.collect.Lists;
34
import mezz.jei.api.constants.ModIds;
45
import mezz.jei.api.constants.VanillaTypes;
56
import 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

Comments
 (0)