Skip to content

Commit

Permalink
adds configurable new sorting algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
maggi373 committed May 17, 2024
1 parent 4c3cf3b commit 68be3bc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/mekanism/common/config/MEKCEConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class MEKCEConfig extends BaseConfig {
public final BooleanOption EnableSiliconCompat = new BooleanOption(this, "mekce", "EnableSiliconCompat", true,
"When a mod that adds silicon (galacticraft, enderio, projectred and ae2) is detected, recipe for control circuit is changed from using iron to silicon in the metalurgic infuser");

public final BooleanOption EnableNewSortAlgorithm = new BooleanOption(this, "mekce", "EnableNewSortAlgorithm", true,
"Enables the new sorting algorithm on factories, you might need to replace the placed machine for the new code to work correctly, option is here to resolve any compat issues a modpack might have");

//public final BooleanOption enableBoPProgression = new BooleanOption(this, "mekce", "enableBoPProgression", true,
// "when true and biome's o plenty is installed atomic alloy is made by using ender instead of obsidian");

Expand Down
52 changes: 51 additions & 1 deletion src/main/java/mekanism/common/tile/TileEntityFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import mekanism.common.base.ITierUpgradeable;
import mekanism.common.block.states.BlockStateMachine.MachineType;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.config.MekanismConfig;
import mekanism.common.integration.computer.IComputerIntegration;
import mekanism.common.item.ItemBlockMachine;
import mekanism.common.recipe.GasConversionHandler;
Expand Down Expand Up @@ -241,7 +242,11 @@ public void onUpdate() {
ChargeUtils.discharge(1, this);

handleSecondaryFuel();
inventorySorter.sort();
if (MekanismConfig.current().mekce.EnableNewSortAlgorithm.val()) {
inventorySorter.sort();
} else {
sortInventory();
}
ItemStack machineSwapItem = inventory.get(2);
if (!machineSwapItem.isEmpty() && machineSwapItem.getItem() instanceof ItemBlockMachine && inventory.get(3).isEmpty()) {

Expand Down Expand Up @@ -355,6 +360,51 @@ public boolean sideIsConsumer(EnumFacing side) {
return configComponent.hasSideForData(TransmissionType.ENERGY, facing, 1, side);
}

public void sortInventory() {
if (sorting) {
int[] inputSlots;
if (tier == FactoryTier.BASIC) {
inputSlots = new int[]{5, 6, 7};
} else if (tier == FactoryTier.ADVANCED) {
inputSlots = new int[]{5, 6, 7, 8, 9};
} else if (tier == FactoryTier.ELITE) {
inputSlots = new int[]{5, 6, 7, 8, 9, 10, 11};
} else {
//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;
}

//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));

markDirty();
return;
}
}
}
}

/**
* Checks if the cached recipe (or recipe for current factory if the cache is out of date) can produce a specific output.
*
Expand Down

0 comments on commit 68be3bc

Please sign in to comment.