Skip to content

Commit

Permalink
fix RA2 universal chemical target using inverted dispatch rule (#1861)
Browse files Browse the repository at this point in the history
* fix RA2 universal chemical target using inverted dispatch rule

* add noOptimize
  • Loading branch information
Glease authored Apr 9, 2023
1 parent 5601f4b commit fa97edd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 34 deletions.
16 changes: 11 additions & 5 deletions src/main/java/gregtech/api/util/GT_Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -2347,7 +2347,8 @@ && isArrayEmptyOrNull(b.getFluidOutputs())
GT_UITextures.PROGRESSBAR_ARROW_MULTIPLE,
ProgressBar.Direction.RIGHT)
.setUsualFluidInputCount(6)
.setUsualFluidOutputCount(6);
.setUsualFluidOutputCount(6)
.setDisableOptimize(true);
public static final GT_Recipe_Map sDistillationRecipes = //
new GT_Recipe_Map_DistillationTower().setRecipeConfigFile("distillation", FIRST_FLUIDSTACK_INPUT)
.setProgressBar(
Expand Down Expand Up @@ -3780,10 +3781,15 @@ && findRecipe(null, false, true, Long.MAX_VALUE, r.mFluidInputs, r.mInputs) != n

public final Iterable<? extends GT_Recipe> defaultBuildRecipe(GT_RecipeBuilder builder) {
// TODO sensible validation
if (!downstreams.isEmpty() && disableOptimize && !builder.optimize) return buildOrEmpty(
builder.copy()
.noOptimize());
return buildOrEmpty(builder);
GT_RecipeBuilder b = builder;
if (disableOptimize && builder.optimize) {
b = copy(builder, b).noOptimize();
}
return buildOrEmpty(b);
}

private static GT_RecipeBuilder copy(GT_RecipeBuilder original, GT_RecipeBuilder b) {
return b == original ? b.copy() : b;
}

public GT_Recipe add(GT_Recipe aRecipe) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/gregtech/api/util/GT_RecipeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,20 @@ public GT_RecipeBuilder itemOutputs(ItemStack... outputs) {
return this;
}

/**
* Not intended to be used by recipe authors.
* Intended for recipe rewrite middlewares.
*/
public GT_RecipeBuilder itemOutputs(ItemStack[] outputs, int[] chances) {
if (DEBUG_MODE && containsNull(outputs)) handleNullRecipeComponents("itemOutputs");
this.outputs = outputs;
this.chances = chances;
if (chances != null && chances.length != outputs.length) {
throw new IllegalArgumentException("Output chances array and items array length differs");
}
return this;
}

public GT_RecipeBuilder noItemOutputs() {
return itemOutputs();
}
Expand Down Expand Up @@ -445,6 +459,10 @@ public int getDuration() {
return duration;
}

public int[] getChances() {
return chances;
}

public int getEUt() {
return eut;
}
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/gregtech/api/util/GT_RecipeConstants.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gregtech.api.util;

import static gregtech.api.util.GT_RecipeMapUtil.convertCellToFluid;

import java.util.*;

import net.minecraft.init.Items;
Expand Down Expand Up @@ -126,14 +128,19 @@ public class GT_RecipeConstants {
*/
public static final IGT_RecipeMap UniversalChemical = IGT_RecipeMap.newRecipeMap(builder -> {
for (ItemStack input : builder.getItemInputsBasic()) {
if (GT_Utility.isAnyIntegratedCircuit(input) && input.getItemDamage() >= 10) return GT_Utility.concat(
builder.copy()
.addTo(GT_Recipe_Map.sChemicalRecipes),
// LCR does not need cleanroom, for now.
builder.metadata(CLEANROOM, false)
.addTo(GT_Recipe_Map.sMultiblockChemicalRecipes));
// config >= 10 -> this is a special chemical recipe that output fluid/canned fluid variant.
// it doesn't belong to multiblocks
if (GT_Utility.isAnyIntegratedCircuit(input) && input.getItemDamage() >= 10) {
return builder.addTo(GT_Recipe_Map.sChemicalRecipes);
}
}
return builder.addTo(GT_Recipe_Map.sChemicalRecipes);
return GT_Utility.concat(
builder.copy()
.addTo(GT_Recipe_Map.sChemicalRecipes),
convertCellToFluid(builder, false)
// LCR does not need cleanroom, for now.
.metadata(CLEANROOM, false)
.addTo(GT_Recipe_Map.sMultiblockChemicalRecipes));
});

/**
Expand Down
43 changes: 21 additions & 22 deletions src/main/java/gregtech/api/util/GT_RecipeMapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.google.common.collect.Multimap;

import cpw.mods.fml.common.Loader;
import gnu.trove.list.TIntList;
import gnu.trove.list.array.TIntArrayList;
import gregtech.api.interfaces.IGT_RecipeMap;

/**
Expand Down Expand Up @@ -74,40 +76,37 @@ public static GT_RecipeTemplate asTemplate(GT_Recipe r, boolean includeTemplate)
}

public static List<GT_Recipe> buildRecipeForMultiblock(GT_RecipeBuilder b) {
List<ItemStack> itemInputs = new ArrayList<>(Arrays.asList(b.getItemInputsBasic()));
List<ItemStack> itemOutputs = new ArrayList<>(Arrays.asList(b.getItemOutputs()));
List<FluidStack> fluidInputs = new ArrayList<>(Arrays.asList(b.getFluidInputs()));
List<FluidStack> fluidOutputs = new ArrayList<>(Arrays.asList(b.getFluidOutputs()));
cellToFluid(itemInputs, fluidInputs, true);
cellToFluid(itemInputs, fluidInputs, false);
return buildOrEmpty(
b.itemInputs(itemInputs.toArray(new ItemStack[0]))
.itemOutputs(itemOutputs.toArray(new ItemStack[0]))
.fluidInputs(fluidInputs.toArray(new FluidStack[0]))
.fluidOutputs(fluidOutputs.toArray(new FluidStack[0])));
return buildOrEmpty(convertCellToFluid(b, true));

}

public static List<GT_Recipe> buildRecipeForMultiblockNoCircuit(GT_RecipeBuilder b) {
return buildOrEmpty(convertCellToFluid(b, false));
}

public static GT_RecipeBuilder convertCellToFluid(GT_RecipeBuilder b, boolean removeIntegratedCircuit) {
List<ItemStack> itemInputs = new ArrayList<>(Arrays.asList(b.getItemInputsBasic()));
List<ItemStack> itemOutputs = new ArrayList<>(Arrays.asList(b.getItemOutputs()));
List<FluidStack> fluidInputs = new ArrayList<>(Arrays.asList(b.getFluidInputs()));
List<FluidStack> fluidOutputs = new ArrayList<>(Arrays.asList(b.getFluidOutputs()));
cellToFluid(itemInputs, fluidInputs, false);
cellToFluid(itemInputs, fluidInputs, false);
return buildOrEmpty(
b.itemInputs(itemInputs.toArray(new ItemStack[0]))
.itemOutputs(itemOutputs.toArray(new ItemStack[0]))
.fluidInputs(fluidInputs.toArray(new FluidStack[0]))
.fluidOutputs(fluidOutputs.toArray(new FluidStack[0])));
TIntList chances = b.getChances() != null ? new TIntArrayList(b.getChances()) : null;
cellToFluid(itemInputs, fluidInputs, removeIntegratedCircuit, null);
cellToFluid(itemOutputs, fluidOutputs, removeIntegratedCircuit, chances);
return b.itemInputs(itemInputs.toArray(new ItemStack[0]))
.itemOutputs(itemOutputs.toArray(new ItemStack[0]), chances != null ? chances.toArray() : null)
.fluidInputs(fluidInputs.toArray(new FluidStack[0]))
.fluidOutputs(fluidOutputs.toArray(new FluidStack[0]));
}

private static void cellToFluid(List<ItemStack> items, List<FluidStack> fluids, boolean removeIntegratedCircuit) {
for (ListIterator<ItemStack> iterator = items.listIterator(items.size()); iterator.hasPrevious();) {
ItemStack item = iterator.previous();
private static void cellToFluid(List<ItemStack> items, List<FluidStack> fluids, boolean removeIntegratedCircuit,
TIntList chances) {
for (int i = items.size() - 1; i >= 0; i--) {
ItemStack item = items.get(i);
if (GT_Utility.getFluidForFilledItem(item, true) != null || GT_Utility.isCellEmpty(item)
|| (removeIntegratedCircuit && GT_Utility.isAnyIntegratedCircuit(item))) {
fluids.add(GT_Utility.convertCellToFluid(item));
iterator.remove();
items.remove(i);
if (chances != null) chances.removeAt(i);
}
}
}
Expand Down

0 comments on commit fa97edd

Please sign in to comment.