Skip to content

Commit

Permalink
Optimize AnvilRecipe storage (#3733)
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt authored Sep 3, 2024
1 parent 78211bf commit 18440d8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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 @@ -42,7 +42,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 @@ -52,7 +52,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 @@ -61,7 +61,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 @@ -63,9 +64,11 @@ private EnchantmentData(Holder<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 @@ -111,6 +114,7 @@ private static Stream<IJeiAnvilRecipe> getBookEnchantmentRecipes(
List<EnchantmentData> enchantmentDatas,
ItemStack ingredient
) {
var ingredientSingletonList = List.of(ingredient);
return enchantmentDatas.stream()
.filter(data -> data.canEnchant(ingredient))
.map(data -> data.getEnchantedBooks(ingredient))
Expand All @@ -121,14 +125,14 @@ private static Stream<IJeiAnvilRecipe> getBookEnchantmentRecipes(
String ingredientIdPath = ResourceLocationUtil.sanitizePath(ingredientId);
String id = "enchantment." + ingredientIdPath;
ResourceLocation uid = ResourceLocation.fromNamespaceAndPath(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 @@ -277,9 +281,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),
ResourceLocation.fromNamespaceAndPath(itemModId, "self_repair." + ingredientIdPath)
);
Expand All @@ -291,7 +297,7 @@ private static Stream<IJeiAnvilRecipe> getRepairRecipes(
IJeiAnvilRecipe repairWithMaterial = vanillaRecipeFactory.createAnvilRecipe(
List.of(damagedFully),
repairMaterials,
List.of(damagedThreeQuarters),
damagedThreeQuartersSingletonList,
ResourceLocation.fromNamespaceAndPath(itemModId, "materials_repair." + ingredientIdPath)
);
consumer.accept(repairWithMaterial);
Expand Down

0 comments on commit 18440d8

Please sign in to comment.