Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework notified logic for distinct busses #126

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import gregtech.api.recipes.Recipe;
import net.minecraftforge.items.IItemHandlerModifiable;

import java.util.ArrayList;
import java.util.List;

public class MultiblockRecipeLogic extends AbstractRecipeLogic {

// Used for distinct mode
protected int lastRecipeIndex = 0;
protected List<IItemHandlerModifiable> invalidatedInputList = new ArrayList<>();


public MultiblockRecipeLogic(RecipeMapMultiblockController tileEntity) {
Expand Down Expand Up @@ -110,15 +112,24 @@ protected void trySearchNewRecipeDistinct() {
List<IItemHandlerModifiable> importInventory = getInputBuses();
IMultipleTankHandler importFluids = getInputTank();

//if fluids changed, iterate all input busses again
if (metaTileEntity.getNotifiedFluidInputList().size() > 0) {
for (IItemHandlerModifiable ihm : importInventory){
if (!metaTileEntity.getNotifiedItemInputList().contains(ihm)){
metaTileEntity.getNotifiedItemInputList().add(ihm);
}
}
metaTileEntity.getNotifiedFluidInputList().clear();
}

// Our caching implementation
// This guarantees that if we get a recipe cache hit, our efficiency is no different from other machines
if (previousRecipe != null && previousRecipe.matches(false, importInventory.get(lastRecipeIndex), importFluids)) {
currentRecipe = previousRecipe;
// If a valid recipe is found, immediately attempt to return it to prevent inventory scanning
if (setupAndConsumeRecipeInputs(currentRecipe, importInventory.get(lastRecipeIndex))) {
setupRecipe(currentRecipe);
metaTileEntity.getNotifiedItemInputList().remove(lastRecipeIndex);
metaTileEntity.getNotifiedFluidInputList().remove(lastRecipeIndex);
metaTileEntity.getNotifiedItemInputList().remove(importInventory.get(lastRecipeIndex));

// No need to cache the previous recipe here, as it is not null and matched by the current recipe,
// so it will always be the same
Expand All @@ -130,30 +141,30 @@ protected void trySearchNewRecipeDistinct() {
// each bus individually instead of the combined inventory all at once.
for (int i = 0; i < importInventory.size(); i++) {
IItemHandlerModifiable bus = importInventory.get(i);
boolean inputsChanged = hasNotifiedInputs();
// If the inputs have changed since the last recipe calculation, find a new recipe based on the new inputs
if (inputsChanged) {
currentRecipe = findRecipe(maxVoltage, bus, importFluids, MatchingMode.DEFAULT);
// Cache the current recipe, if one is found
if (currentRecipe != null) {
this.previousRecipe = currentRecipe;
}
// Skip this bus if no recipe was found last time and the inventory did not change
if (invalidatedInputList.contains(bus) && !metaTileEntity.getNotifiedItemInputList().contains(bus)) {
continue;
} else {
invalidatedInputList.remove(bus);
}

invalidInputsForRecipes = (currentRecipe == null);

if (currentRecipe != null && setupAndConsumeRecipeInputs(currentRecipe, importInventory.get(i))) {
lastRecipeIndex = i;
setupRecipe(currentRecipe);
metaTileEntity.getNotifiedItemInputList().remove(i);
metaTileEntity.getNotifiedFluidInputList().remove(i);
break;
// Look for a new recipe after a cache miss
currentRecipe = findRecipe(maxVoltage, bus, importFluids, MatchingMode.DEFAULT);
// Cache the current recipe, if one is found
if (currentRecipe != null) {
this.previousRecipe = currentRecipe;
if (setupAndConsumeRecipeInputs(currentRecipe, importInventory.get(i))) {
lastRecipeIndex = i;
setupRecipe(currentRecipe);
metaTileEntity.getNotifiedItemInputList().remove(bus);
return;
}
} else {
invalidatedInputList.add(bus);
}
}

//If no matching recipes are found, clear the notified inputs so we know when new items are given
metaTileEntity.getNotifiedItemInputList().clear();
metaTileEntity.getNotifiedFluidInputList().clear();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ protected void addDisplayText(List<ITextComponent> textList) {
protected void handleDisplayClick(String componentData, Widget.ClickData clickData) {
super.handleDisplayClick(componentData, clickData);
isDistinct = !isDistinct;
//mark busses as changed on distinct toggle
if (isDistinct) {
this.notifiedItemInputList.addAll(this.getAbilities(MultiblockAbility.IMPORT_ITEMS));
} else {
this.notifiedItemInputList.add(this.inputInventory);
}
}

@Override
Expand Down
Loading