Skip to content

Commit

Permalink
[1.20.1] Optimize AnvilRecipe storage (#3886)
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt authored Jan 11, 2025
1 parent c3a918d commit 90723d7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public IJeiAnvilRecipe createAnvilRecipe(ItemStack leftInput, List<ItemStack> 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
Expand All @@ -39,7 +39,7 @@ public AnvilRecipe createAnvilRecipe(ItemStack leftInput, List<ItemStack> 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
Expand All @@ -49,7 +49,7 @@ public AnvilRecipe createAnvilRecipe(List<ItemStack> leftInputs, List<ItemStack>
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
Expand All @@ -58,7 +58,7 @@ public AnvilRecipe createAnvilRecipe(List<ItemStack> leftInputs, List<ItemStack>
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ public record AnvilRecipe(
List<ItemStack> outputs,
@Nullable ResourceLocation uid
) implements IJeiAnvilRecipe {
public AnvilRecipe(List<ItemStack> leftInputs, List<ItemStack> rightInputs, List<ItemStack> 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<ItemStack> getLeftInputs() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -61,9 +62,11 @@ private EnchantmentData(Enchantment enchantment) {

public List<ItemStack> 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) {
Expand Down Expand Up @@ -109,6 +112,7 @@ private static Stream<IJeiAnvilRecipe> getBookEnchantmentRecipes(
IIngredientHelper<ItemStack> ingredientHelper,
ItemStack ingredient
) {
var ingredientSingletonList = List.of(ingredient);
return enchantmentDatas.stream()
.filter(data -> data.canEnchant(ingredient))
.map(data -> data.getEnchantedBooks(ingredient))
Expand All @@ -118,15 +122,16 @@ private static Stream<IJeiAnvilRecipe> 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<ItemStack> getEnchantedIngredients(ItemStack ingredient, List<ItemStack> 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) {
Expand Down Expand Up @@ -271,9 +276,11 @@ private static Stream<IJeiAnvilRecipe> 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)
);
Expand All @@ -285,7 +292,7 @@ private static Stream<IJeiAnvilRecipe> getRepairRecipes(
IJeiAnvilRecipe repairWithMaterial = vanillaRecipeFactory.createAnvilRecipe(
List.of(damagedFully),
repairMaterials,
List.of(damagedThreeQuarters),
damagedThreeQuartersSingletonList,
new ResourceLocation(itemModId, "materials_repair." + ingredientIdPath)
);
consumer.accept(repairWithMaterial);
Expand Down

0 comments on commit 90723d7

Please sign in to comment.