From dc156c21ffb4dfb9234e92d759ab78871471e1ba Mon Sep 17 00:00:00 2001 From: MrJeremyFisher <63616270+MrJeremyFisher@users.noreply.github.com> Date: Mon, 1 Apr 2024 14:55:10 -0400 Subject: [PATCH] Fix incorrect fullness checks --- .../civmodcore/inventory/items/ItemMap.java | 4 +- .../factories/FurnCraftChestFactory.java | 2 +- .../recipes/DecompactingRecipe.java | 2 +- .../igotyou/FactoryMod/recipes/IRecipe.java | 2 +- .../FactoryMod/recipes/ProductionRecipe.java | 47 ++++++++++--------- .../recipes/RandomOutputRecipe.java | 38 +++++++++------ 6 files changed, 56 insertions(+), 39 deletions(-) diff --git a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/inventory/items/ItemMap.java b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/inventory/items/ItemMap.java index d9ed76ece..f858d7db1 100644 --- a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/inventory/items/ItemMap.java +++ b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/inventory/items/ItemMap.java @@ -520,8 +520,10 @@ public class ItemMap { return false; } + ClonedInventory clonedInventory = ClonedInventory.cloneInventory(i); + for(ItemStack itemStack : this.getItemStackRepresentation()) { - if (!ClonedInventory.cloneInventory(i).addItem(itemStack).isEmpty()) { + if (!clonedInventory.addItem(itemStack).isEmpty()) { return false; } } diff --git a/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/factories/FurnCraftChestFactory.java b/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/factories/FurnCraftChestFactory.java index 5ba394572..18db08bb6 100644 --- a/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/factories/FurnCraftChestFactory.java +++ b/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/factories/FurnCraftChestFactory.java @@ -335,7 +335,7 @@ public class FurnCraftChestFactory extends Factory implements IIOFInventoryProvi } // Ensure the recipe effect can be applied - var effectFeasibility = currentRecipe.evaluateEffectFeasibility(getInputInventory(), getOutputInventory()); + var effectFeasibility = currentRecipe.evaluateEffectFeasibility(getInputInventory(), getOutputInventory(), this); if (!(effectFeasibility.isFeasible())) { LoggingUtils.log(String.format("Skipping activation of recipe [%s], since the effect wasn't feasible.", currentRecipe.getName())); if (p != null) { diff --git a/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/DecompactingRecipe.java b/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/DecompactingRecipe.java index 4a85abcec..06c56a0b4 100644 --- a/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/DecompactingRecipe.java +++ b/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/DecompactingRecipe.java @@ -45,7 +45,7 @@ public class DecompactingRecipe extends InputRecipe { } @Override - public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv) { + public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv, FurnCraftChestFactory fccf) { boolean isFeasible = Arrays.stream(inputInv.getContents()) .filter(Objects::nonNull) .filter(this::isDecompactable) diff --git a/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/IRecipe.java b/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/IRecipe.java index 7d55f754b..fbaa37373 100644 --- a/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/IRecipe.java +++ b/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/IRecipe.java @@ -39,7 +39,7 @@ public interface IRecipe { * input/output inventories, or other custom recipe logic. * By default, this method returns a result indicating that the effect is always feasible to be applied. */ - default public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv) { + default public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv, FurnCraftChestFactory fccf) { return new EffectFeasibility(true, null); } diff --git a/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/ProductionRecipe.java b/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/ProductionRecipe.java index 9d8bdab4a..773464bef 100644 --- a/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/ProductionRecipe.java +++ b/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/ProductionRecipe.java @@ -2,11 +2,6 @@ package com.github.igotyou.FactoryMod.recipes; import com.github.igotyou.FactoryMod.factories.FurnCraftChestFactory; import com.github.igotyou.FactoryMod.recipes.scaling.ProductionRecipeModifier; -import java.text.DecimalFormat; -import java.util.List; -import java.util.Map.Entry; -import java.util.Random; - import com.github.igotyou.FactoryMod.utility.MultiInventoryWrapper; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -15,6 +10,11 @@ import org.bukkit.inventory.ItemStack; import vg.civcraft.mc.civmodcore.inventory.items.ItemMap; import vg.civcraft.mc.civmodcore.inventory.items.ItemUtils; +import java.text.DecimalFormat; +import java.util.List; +import java.util.Map.Entry; +import java.util.Random; + /** * Consumes a set of materials from a container and outputs another set of * materials to the same container @@ -27,6 +27,7 @@ public class ProductionRecipe extends InputRecipe { private ProductionRecipeModifier modifier; private Random rng; private DecimalFormat decimalFormatting; + private ItemMap guaranteedOutput; public ProductionRecipe( String identifier, @@ -113,8 +114,24 @@ public class ProductionRecipe extends InputRecipe { } @Override - public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv) { - boolean isFeasible = input.fitsIn(outputInv); + public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv, FurnCraftChestFactory fccf) { + ItemMap toAdd; + if (getModifier() == null) { + toAdd = output.clone(); + } else { + toAdd = getGuaranteedOutput(fccf.getRecipeLevel(this), fccf.getRunCount(this)); + double factor = modifier.getFactor(fccf.getRecipeLevel(this), fccf.getRunCount(this)); + for(Entry entry : output.getEntrySet()) { + double additionalChance = (((double) entry.getValue()) * factor) - toAdd.getAmount(entry.getKey()); + if (rng.nextDouble() <= additionalChance) { + toAdd.addItemAmount(entry.getKey(), 1); + } + } + } + + guaranteedOutput = toAdd; + + boolean isFeasible = guaranteedOutput.fitsIn(outputInv); String reasonSnippet = isFeasible ? null : "it ran out of storage space"; return new EffectFeasibility( isFeasible, @@ -127,20 +144,8 @@ public class ProductionRecipe extends InputRecipe { MultiInventoryWrapper combo = new MultiInventoryWrapper(inputInv, outputInv); logBeforeRecipeRun(combo, fccf); ItemMap toRemove = input.clone(); - ItemMap toAdd; - if (getModifier() == null) { - toAdd = output.clone(); - } - else { - toAdd = getGuaranteedOutput(fccf.getRecipeLevel(this), fccf.getRunCount(this)); - double factor = modifier.getFactor(fccf.getRecipeLevel(this), fccf.getRunCount(this)); - for(Entry entry : output.getEntrySet()) { - double additionalChance = (((double) entry.getValue()) * factor) - toAdd.getAmount(entry.getKey()); - if (rng.nextDouble() <= additionalChance) { - toAdd.addItemAmount(entry.getKey(), 1); - } - } - } + ItemMap toAdd = guaranteedOutput; + if (toRemove.isContainedIn(inputInv)) { if (!toAdd.fitsIn(outputInv)) { // does not fit in chest return false; diff --git a/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/RandomOutputRecipe.java b/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/RandomOutputRecipe.java index e22eb444a..74dc2837f 100644 --- a/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/RandomOutputRecipe.java +++ b/plugins/factorymod-paper/src/main/java/com/github/igotyou/FactoryMod/recipes/RandomOutputRecipe.java @@ -20,6 +20,7 @@ public class RandomOutputRecipe extends InputRecipe { private Map outputs; private static Random rng; private ItemMap lowestChanceMap; + private ItemMap guaranteedOutput; public RandomOutputRecipe(String identifier, String name, int productionTime, ItemMap input, Map outputs, ItemMap displayOutput) { @@ -47,8 +48,27 @@ public class RandomOutputRecipe extends InputRecipe { } @Override - public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv) { - boolean isFeasible = input.fitsIn(outputInv); + public EffectFeasibility evaluateEffectFeasibility(Inventory inputInv, Inventory outputInv, FurnCraftChestFactory fccf) { + int counter = 0; + while(counter < 20) { + guaranteedOutput = getRandomOutput(); + if (guaranteedOutput != null) { + guaranteedOutput = guaranteedOutput.clone(); + break; + } + else { + counter++; + } + } + + if (guaranteedOutput == null) { + return new EffectFeasibility( + false, + "it failed to find a random item" + ); + } + + boolean isFeasible = guaranteedOutput.fitsIn(outputInv); String reasonSnippet = isFeasible ? null : "it ran out of storage space"; return new EffectFeasibility( isFeasible, @@ -61,18 +81,8 @@ public class RandomOutputRecipe extends InputRecipe { MultiInventoryWrapper combo = new MultiInventoryWrapper(inputInv, outputInv); logBeforeRecipeRun(combo, fccf); ItemMap toRemove = input.clone(); - ItemMap toAdd = null; - int counter = 0; - while(counter < 20) { - toAdd = getRandomOutput(); - if (toAdd != null) { - toAdd = toAdd.clone(); - break; - } - else { - counter++; - } - } + ItemMap toAdd = guaranteedOutput; + if (toAdd == null) { FactoryMod.getInstance().warning("Unable to find a random item to output. Recipe execution was cancelled," + fccf.getLogData()); return false;