From 26bd475cdbd5cda155d6efeeec6a87b684770876 Mon Sep 17 00:00:00 2001 From: shartte Date: Sun, 11 Aug 2024 02:45:48 +0200 Subject: [PATCH] Implement support for multiple of the same ingredient in transform recipes (#8137) --- .../recipes/transform/TransformLogic.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/java/appeng/recipes/transform/TransformLogic.java b/src/main/java/appeng/recipes/transform/TransformLogic.java index 78a219c0463..1bbcf589062 100644 --- a/src/main/java/appeng/recipes/transform/TransformLogic.java +++ b/src/main/java/appeng/recipes/transform/TransformLogic.java @@ -23,7 +23,8 @@ import net.neoforged.neoforge.event.AddReloadListenerEvent; import net.neoforged.neoforge.event.server.ServerStartedEvent; -import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; +import it.unimi.dsi.fastutil.objects.Reference2IntMap; +import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap; public final class TransformLogic { public static boolean canTransformInFluid(ItemEntity entity, FluidState fluid) { @@ -55,37 +56,38 @@ public static boolean tryTransform(ItemEntity entity, Predicate missingIngredients = Lists.newArrayList(recipe.ingredients); - Set selectedEntities = new ReferenceOpenHashSet<>(missingIngredients.size()); + Reference2IntMap consumedItems = new Reference2IntOpenHashMap<>(missingIngredients.size()); if (recipe.circumstance.isExplosion()) { if (missingIngredients.stream().noneMatch(i -> i.test(entity.getItem()))) continue; } else { - if (!missingIngredients.get(0).test(entity.getItem())) + if (!missingIngredients.getFirst().test(entity.getItem())) continue; } for (var itemEntity : itemEntities) { - final ItemStack other = itemEntity.getItem(); + var other = itemEntity.getItem(); if (!other.isEmpty()) { for (var it = missingIngredients.iterator(); it.hasNext();) { Ingredient ing = it.next(); - if (ing.test(other)) { - selectedEntities.add(itemEntity); + var alreadyClaimed = consumedItems.getInt(itemEntity); + if (ing.test(other) && other.getCount() - alreadyClaimed > 0) { + consumedItems.merge(itemEntity, 1, Integer::sum); it.remove(); - break; } } } } if (missingIngredients.isEmpty()) { - var items = new ArrayList(selectedEntities.size()); - for (var e : selectedEntities) { - items.add(e.getItem().split(1)); + var items = new ArrayList(consumedItems.size()); + for (var e : consumedItems.reference2IntEntrySet()) { + var itemEntity = e.getKey(); + items.add(itemEntity.getItem().split(e.getIntValue())); - if (e.getItem().getCount() <= 0) { - e.discard(); + if (itemEntity.getItem().getCount() <= 0) { + itemEntity.discard(); } } var recipeInput = new TransformRecipeInput(items);