diff --git a/README.md b/README.md index 9160b7202..0ddf2a1e9 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +## Fork notice +This repo contains the fixes for Mekanism 9 for 1.12.2. +Right now, the only fix is the updated recipe autodistribution in factories (it now does distribution instantly and respects the count of item needed for the recipe). [![Build Status](https://travis-ci.org/aidancbrady/Mekanism.svg?branch=master)](https://travis-ci.org/aidancbrady/Mekanism) @@ -55,4 +58,4 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI YourKit supports open source projects with innovative and intelligent tools for monitoring and profiling Java and .NET applications. YourKit is the creator of [YourKit Java Profiler](https://www.yourkit.com/java/profiler), [YourKit .NET Profiler](https://www.yourkit.com/.net/profiler/) and [YourKit YouMonitor](https://www.yourkit.com/youmonitor/). -Mekanism uses YourKit for all our profiling needs! \ No newline at end of file +Mekanism uses YourKit for all our profiling needs! diff --git a/libs/InventoryTweaks-1.64+dev.146-api.jar b/libs/InventoryTweaks-1.64+dev.146-api.jar new file mode 100644 index 000000000..457ac814c Binary files /dev/null and b/libs/InventoryTweaks-1.64+dev.146-api.jar differ diff --git a/src/main/java/mekanism/common/tile/TileEntityFactory.java b/src/main/java/mekanism/common/tile/TileEntityFactory.java index 46cbf48e2..65322d0b0 100644 --- a/src/main/java/mekanism/common/tile/TileEntityFactory.java +++ b/src/main/java/mekanism/common/tile/TileEntityFactory.java @@ -1,29 +1,14 @@ package mekanism.common.tile; import io.netty.buffer.ByteBuf; -import java.util.Arrays; -import java.util.Objects; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import mekanism.api.EnumColor; import mekanism.api.IConfigCardAccess.ISpecialConfigData; import mekanism.api.TileNetworkList; -import mekanism.api.gas.Gas; -import mekanism.api.gas.GasStack; -import mekanism.api.gas.GasTank; -import mekanism.api.gas.GasTankInfo; -import mekanism.api.gas.IGasHandler; -import mekanism.api.gas.IGasItem; +import mekanism.api.gas.*; import mekanism.api.infuse.InfuseObject; import mekanism.api.infuse.InfuseRegistry; import mekanism.api.transmitters.TransmissionType; -import mekanism.common.InfuseStorage; -import mekanism.common.Mekanism; -import mekanism.common.MekanismBlocks; -import mekanism.common.MekanismItems; -import mekanism.common.PacketHandler; -import mekanism.common.SideData; -import mekanism.common.Upgrade; +import mekanism.common.*; import mekanism.common.base.IComparatorSupport; import mekanism.common.base.IFactory.MachineFuelType; import mekanism.common.base.IFactory.RecipeType; @@ -40,28 +25,17 @@ import mekanism.common.recipe.inputs.AdvancedMachineInput; import mekanism.common.recipe.inputs.DoubleMachineInput; import mekanism.common.recipe.inputs.InfusionInput; import mekanism.common.recipe.inputs.ItemStackInput; -import mekanism.common.recipe.machines.AdvancedMachineRecipe; -import mekanism.common.recipe.machines.BasicMachineRecipe; -import mekanism.common.recipe.machines.ChanceMachineRecipe; -import mekanism.common.recipe.machines.DoubleMachineRecipe; -import mekanism.common.recipe.machines.MachineRecipe; -import mekanism.common.recipe.machines.MetallurgicInfuserRecipe; +import mekanism.common.recipe.machines.*; +import mekanism.common.recipe.outputs.ChanceOutput; import mekanism.common.recipe.outputs.ItemStackOutput; +import mekanism.common.recipe.outputs.PressurizedOutput; import mekanism.common.tier.BaseTier; import mekanism.common.tier.FactoryTier; import mekanism.common.tile.component.TileComponentConfig; import mekanism.common.tile.component.TileComponentEjector; import mekanism.common.tile.prefab.TileEntityAdvancedElectricMachine; import mekanism.common.tile.prefab.TileEntityMachine; -import mekanism.common.util.ChargeUtils; -import mekanism.common.util.GasUtils; -import mekanism.common.util.InventoryUtils; -import mekanism.common.util.ItemDataUtils; -import mekanism.common.util.LangUtils; -import mekanism.common.util.MekanismUtils; -import mekanism.common.util.StackUtils; -import mekanism.common.util.StatUtils; -import mekanism.common.util.TileUtils; +import mekanism.common.util.*; import net.minecraft.inventory.Container; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -69,12 +43,26 @@ import net.minecraft.util.EnumFacing; import net.minecraft.util.NonNullList; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.fml.common.FMLCommonHandler; -import net.minecraftforge.items.ItemHandlerHelper; +import org.apache.commons.lang3.tuple.ImmutablePair; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.*; + +@SuppressWarnings("rawtypes") public class TileEntityFactory extends TileEntityMachine implements IComputerIntegration, ISideConfiguration, IGasHandler, ISpecialConfigData, ITierUpgradeable, - ISustainedData, IComparatorSupport { + ISustainedData, IComparatorSupport { private static final String[] methods = new String[]{"getEnergy", "getProgress", "facing", "canOperate", "getMaxEnergy", "getEnergyNeeded"}; + /** + * How long it takes this factory to switch recipe types. + */ + private static final int RECIPE_TICKS_REQUIRED = 40; + /** + * The amount of infuse this machine has stored. + */ + public final InfuseStorage infuseStored = new InfuseStorage(); + public final GasTank gasTank; private final MachineRecipe[] cachedRecipe; /** * This Factory's tier. @@ -94,34 +82,20 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt * How many ticks it takes, with upgrades, to run an operation */ public int ticksRequired = 200; + public boolean sorting; + public boolean upgraded; + public double lastUsage; + public TileComponentEjector ejectorComponent; + public TileComponentConfig configComponent; /** * How much secondary energy each operation consumes per tick */ private double secondaryEnergyPerTick = 0; private int secondaryEnergyThisTick; - /** - * How long it takes this factory to switch recipe types. - */ - private static int RECIPE_TICKS_REQUIRED = 40; /** * How many recipe ticks have progressed. */ private int recipeTicks; - /** - * The amount of infuse this machine has stored. - */ - public final InfuseStorage infuseStored = new InfuseStorage(); - - public final GasTank gasTank; - - public boolean sorting; - - public boolean upgraded; - - public double lastUsage; - - public TileComponentEjector ejectorComponent; - public TileComponentConfig configComponent; /** * This machine's recipe type. */ @@ -278,7 +252,7 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt for (int process = 0; process < tier.processes; process++) { if (MekanismUtils.canFunction(this) && canOperate(getInputSlot(process), getOutputSlot(process)) - && getEnergy() >= energyPerTick && gasTank.getStored() >= secondaryEnergyThisTick) { + && getEnergy() >= energyPerTick && gasTank.getStored() >= secondaryEnergyThisTick) { if ((progress[process] + 1) < ticksRequired) { progress[process]++; gasTank.draw(secondaryEnergyThisTick, true); @@ -362,38 +336,174 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt //If something went wrong finding the tier don't sort it return; } - for (int i = 0; i < inputSlots.length; i++) { - int slotID = inputSlots[i]; - ItemStack stack = inventory.get(slotID); - int count = stack.getCount(); - ItemStack output = inventory.get(tier.processes + slotID); - for (int j = i + 1; j < inputSlots.length; j++) { - int checkSlotID = inputSlots[j]; - ItemStack checkStack = inventory.get(checkSlotID); - if (Math.abs(count - checkStack.getCount()) < 2 || - !InventoryUtils.areItemsStackable(stack, checkStack)) { - continue; - } - //Output/Input will not match - // Only check if the input spot is empty otherwise assume it works - if (stack.isEmpty() && !inputProducesOutput(checkSlotID, checkStack, output, true) || - checkStack.isEmpty() && !inputProducesOutput(slotID, stack, inventory.get(tier.processes + checkSlotID), true)) { - continue; + Map recipeSlots = new HashMap<>(); + List freeSlots = new ArrayList<>(); + List outputBlockedSlots = new ArrayList<>(); + for (int slotID : inputSlots) { + ItemStack inStack = inventory.get(slotID); + ItemStack outStack = inventory.get(slotID + tier.processes); + MachineRecipe slotRecipe = getSlotRecipe(slotID, inStack); + if (slotRecipe != null) { + ItemStack recipeIn = getRecipeInput(slotRecipe); + ItemStack recipeOut = getRecipeOutput(slotRecipe); + + // We don't care about extras here since there's no possibility to have + // recipes with different extras in one factory at the same time + String id = new ImmutablePair<>(recipeIn, recipeOut).toString(); + + if (!recipeSlots.containsKey(id)) { + recipeSlots.put(id, new ProcessDescription(recipeIn, recipeOut)); } - //Balance the two slots - int total = count + checkStack.getCount(); - ItemStack newStack = stack.isEmpty() ? checkStack : stack; - inventory.set(slotID, StackUtils.size(newStack, (total + 1) / 2)); - inventory.set(checkSlotID, StackUtils.size(newStack, total / 2)); + recipeSlots.get(id).occupySlot(slotID); + } else { + if (outStack.getCount() <= 0 || outStack.isEmpty()) { + freeSlots.add(slotID); + } else { + outputBlockedSlots.add(slotID); + } + } + } - markDirty(); - return; + if (recipeSlots.size() == 0) return; + + int freePerSlot = freeSlots.size() / recipeSlots.size(); + int excess = freeSlots.size() - (freePerSlot * recipeSlots.size()); + + for (Map.Entry line : recipeSlots.entrySet()) { + int takeSlots = freePerSlot; + + if (excess > 0) { + takeSlots += 1; + excess -= 1; + } + + while (takeSlots > 0) { + line.getValue().occupySlot(freeSlots.get(0)); + freeSlots.remove(0); + takeSlots -= 1; + } + + Set taken = new HashSet<>(); + + outputBlockedSlots.forEach(slot -> { + ItemStack outStack = inventory.get(slot + tier.processes); + ItemStack recipeOutStack = line.getValue().getOutput(); + + if (taken.contains(slot)) return; + + if (outStack.isItemEqual(recipeOutStack) || recipeOutStack.isEmpty()) { + taken.add(slot); + line.getValue().occupySlot(slot); + } + }); + + } + + for (Map.Entry line : recipeSlots.entrySet()) { + int total = line.getValue().getOccupiedSlots().stream() + .map(v -> inventory.get(v).getCount()) + .reduce(0, Integer::sum); + int count = line.getValue().getOccupiedSlots().size(); + + if (count == 0) continue; + + int minCount = line.getValue().getInput().getCount(); + + int itemsPerSlot = total / count; + int excessItems = total - (itemsPerSlot * count); + + for (Integer slot : line.getValue().getOccupiedSlots()) { + int actualCount = itemsPerSlot; + + if (excessItems > 0) { + excessItems -= 1; + actualCount += 1; + } + + actualCount = Math.max(actualCount, minCount); + + if (total - actualCount < minCount) { + actualCount = total; + } + + inventory.set(slot, StackUtils.size(line.getValue().getInput(), actualCount)); + + total -= actualCount; } } } } + public ItemStack getRecipeOutput(MachineRecipe recipe) { + if (recipe.recipeOutput instanceof ItemStackOutput) { + return ((ItemStackOutput) recipe.recipeOutput).output; + } else if (recipe.recipeOutput instanceof ChanceOutput) { + return ((ChanceOutput) recipe.recipeOutput).primaryOutput; + } else if (recipe.recipeOutput instanceof PressurizedOutput) { + return ((PressurizedOutput) recipe.recipeOutput).getItemOutput(); + } else { + return ItemStack.EMPTY; + } + } + + public ItemStack getRecipeInput(MachineRecipe recipe) { + if (recipe.recipeInput instanceof ItemStackInput) { + return ((ItemStackInput) recipe.recipeInput).ingredient; + } else if (recipe.recipeInput instanceof AdvancedMachineInput) { + AdvancedMachineInput advancedInput = (AdvancedMachineInput) recipe.recipeInput; + return advancedInput.itemStack; + } else if (recipe.recipeInput instanceof DoubleMachineInput) { + DoubleMachineInput doubleMachineInput = (DoubleMachineInput) recipe.recipeInput; + return doubleMachineInput.itemStack; + } else if (recipe.recipeInput instanceof InfusionInput) { + InfusionInput infusionInput = (InfusionInput) recipe.recipeInput; + return infusionInput.inputStack; + } else { + return ItemStack.EMPTY; + } + } + + public MachineRecipe getSlotRecipe(int slotID, ItemStack fallbackInput) { + int process = getOperation(slotID); + //cached recipe may be invalid + MachineRecipe cached = cachedRecipe[process]; + ItemStack extra = inventory.get(4); + if (cached == null) { + cached = recipeType.getAnyRecipe(fallbackInput, extra, gasTank.getGasType(), infuseStored); + + if (cached == null) { // We have not enough input probably + cached = recipeType.getAnyRecipe(StackUtils.size(fallbackInput, fallbackInput.getMaxStackSize()), extra, gasTank.getGasType(), infuseStored); + } + } else { + ItemStack recipeInput = ItemStack.EMPTY; + boolean secondaryMatch = true; + if (cached.recipeInput instanceof ItemStackInput) { + recipeInput = ((ItemStackInput) cached.recipeInput).ingredient; + } else if (cached.recipeInput instanceof AdvancedMachineInput) { + AdvancedMachineInput advancedInput = (AdvancedMachineInput) cached.recipeInput; + recipeInput = advancedInput.itemStack; + secondaryMatch = gasTank.getGasType() == null || advancedInput.gasType == gasTank.getGasType(); + } else if (cached.recipeInput instanceof DoubleMachineInput) { + DoubleMachineInput doubleMachineInput = (DoubleMachineInput) cached.recipeInput; + recipeInput = doubleMachineInput.itemStack; + secondaryMatch = extra.isEmpty() || ItemStack.areItemsEqual(doubleMachineInput.extraStack, extra); + } else if (cached.recipeInput instanceof InfusionInput) { + InfusionInput infusionInput = (InfusionInput) cached.recipeInput; + recipeInput = infusionInput.inputStack; + secondaryMatch = infuseStored.getAmount() == 0 || infuseStored.getType() == infusionInput.infuse.getType(); + } + //If there is no cached item input or it doesn't match our fallback + // then it is an out of date cache so we compare against the new one + // and update the cache while we are at it + if (recipeInput.isEmpty() || !secondaryMatch || !ItemStack.areItemsEqual(recipeInput, fallbackInput)) { + cached = recipeType.getAnyRecipe(fallbackInput, extra, gasTank.getGasType(), infuseStored); + } + } + + return cached; + } + /** * Checks if the cached recipe (or recipe for current factory if the cache is out of date) can produce a specific output. * @@ -401,7 +511,6 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt * @param fallbackInput Used if the cached recipe is null or to validate the cached recipe is not out of date. * @param output The output we want. * @param updateCache True to make the cached recipe get updated if it is out of date. - * * @return True if the recipe produces the given output. */ public boolean inputProducesOutput(int slotID, ItemStack fallbackInput, ItemStack output, boolean updateCache) { @@ -524,10 +633,7 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt return true; } else if (tier == FactoryTier.ELITE && slotID >= 12 && slotID <= 18) { return true; - } else if (recipeType.getFuelType() == MachineFuelType.CHANCE && slotID == 4) { - return true; - } - return false; + } else return recipeType.getFuelType() == MachineFuelType.CHANCE && slotID == 4; } @Override @@ -608,7 +714,7 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt if (recipeType.getFuelType() == MachineFuelType.ADVANCED) { if (cachedRecipe[process] instanceof AdvancedMachineRecipe && - ((AdvancedMachineRecipe) cachedRecipe[process]).inputMatches(inventory, inputSlot, gasTank, secondaryEnergyThisTick)) { + ((AdvancedMachineRecipe) cachedRecipe[process]).inputMatches(inventory, inputSlot, gasTank, secondaryEnergyThisTick)) { return ((AdvancedMachineRecipe) cachedRecipe[process]).canOperate(inventory, inputSlot, outputSlot, gasTank, secondaryEnergyThisTick); } AdvancedMachineRecipe recipe = recipeType.getRecipe(inventory.get(inputSlot), gasTank.getGasType()); @@ -848,7 +954,7 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt return new Object[]{"No such operation found."}; } return new Object[]{ - canOperate(getInputSlot((Integer) arguments[0]), getOutputSlot((Integer) arguments[0]))}; + canOperate(getInputSlot((Integer) arguments[0]), getOutputSlot((Integer) arguments[0]))}; case 4: return new Object[]{getMaxEnergy()}; case 5: @@ -922,7 +1028,7 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt return false; } return capability == Capabilities.GAS_HANDLER_CAPABILITY || capability == Capabilities.CONFIG_CARD_CAPABILITY - || capability == Capabilities.SPECIAL_CONFIG_DATA_CAPABILITY || super.hasCapability(capability, side); + || capability == Capabilities.SPECIAL_CONFIG_DATA_CAPABILITY || super.hasCapability(capability, side); } @Override @@ -931,7 +1037,7 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt return null; } if (capability == Capabilities.GAS_HANDLER_CAPABILITY || capability == Capabilities.CONFIG_CARD_CAPABILITY - || capability == Capabilities.SPECIAL_CONFIG_DATA_CAPABILITY) { + || capability == Capabilities.SPECIAL_CONFIG_DATA_CAPABILITY) { return (T) this; } return super.getCapability(capability, side); @@ -1000,4 +1106,32 @@ public class TileEntityFactory extends TileEntityMachine implements IComputerInt public int getRedstoneLevel() { return Container.calcRedstoneFromInventory(this); } + + private class ProcessDescription { + private final ItemStack input; + private final ItemStack output; + private final Set occupiedSlots; + + public ProcessDescription(ItemStack input, ItemStack output) { + this.input = input; + this.output = output; + this.occupiedSlots = new HashSet<>(); + } + + public ItemStack getInput() { + return input; + } + + public ItemStack getOutput() { + return output; + } + + public Set getOccupiedSlots() { + return occupiedSlots; + } + + void occupySlot(Integer slot) { + occupiedSlots.add(slot); + } + } } \ No newline at end of file