Fix incorrect fullness checks

This commit is contained in:
MrJeremyFisher
2024-04-01 14:55:10 -04:00
parent 84f0ff22f6
commit dc156c21ff
6 changed files with 56 additions and 39 deletions

View File

@@ -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;
}
}

View File

@@ -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) {

View File

@@ -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)

View File

@@ -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);
}

View File

@@ -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<ItemStack, Integer> 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<ItemStack, Integer> 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;

View File

@@ -20,6 +20,7 @@ public class RandomOutputRecipe extends InputRecipe {
private Map<ItemMap, Double> outputs;
private static Random rng;
private ItemMap lowestChanceMap;
private ItemMap guaranteedOutput;
public RandomOutputRecipe(String identifier, String name, int productionTime, ItemMap input,
Map<ItemMap, Double> 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;