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

RecipeMapGroup implementation #316

Merged
merged 4 commits into from
Oct 2, 2024
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
2 changes: 2 additions & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ dependencies {
compileOnly rfg.deobf("curse.maven:travelers-backpack-321117:3150850")

compileOnly rfg.deobf("curse.maven:barrels-drums-storage-more-319404:2708193")

api("CraftTweaker2:CraftTweaker2-MC1120-Main:1.12-4.1.20.700")
}
63 changes: 63 additions & 0 deletions src/main/java/supersymmetry/api/recipes/RecipeMapGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package supersymmetry.api.recipes;

import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeBuilder;
import gregtech.api.recipes.RecipeMap;
import gregtech.api.recipes.builders.SimpleRecipeBuilder;
import gregtech.api.recipes.category.GTRecipeCategory;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class RecipeMapGroup<R extends RecipeBuilder<R>> extends RecipeMap<R> {

protected final RecipeMap<?>[] recipeMaps;

protected RecipeMapGroup(@NotNull String unlocalizedName, int maxInputs, int maxOutputs, int maxFluidInputs, int maxFluidOutputs, @NotNull R defaultRecipeBuilder, boolean isHidden, @NotNull RecipeMap<?>[] recipeMaps) {
super(unlocalizedName, maxInputs, maxOutputs, maxFluidInputs, maxFluidOutputs, defaultRecipeBuilder, isHidden);
this.recipeMaps = recipeMaps;
}

@Nonnull
public static RecipeMapGroup<SimpleRecipeBuilder> create(@NotNull String unlocalizedName, @NotNull RecipeMap<?>... recipeMaps) {
int maxInputs = 0, maxOutputs = 0, maxFluidInputs = 0, maxFluidOutputs = 0;
for (RecipeMap<?> recipeMap : recipeMaps) {
maxInputs = Math.max(maxInputs, recipeMap.getMaxInputs());
maxOutputs = Math.max(maxOutputs, recipeMap.getMaxOutputs());
maxFluidInputs = Math.max(maxFluidInputs, recipeMap.getMaxFluidInputs());
maxFluidOutputs = Math.max(maxFluidOutputs, recipeMap.getMaxFluidOutputs());
}
return new RecipeMapGroup<>(unlocalizedName, maxInputs, maxOutputs, maxFluidInputs, maxFluidOutputs, new SimpleRecipeBuilder(), true, recipeMaps);
}

@Nullable
@Override
public Recipe findRecipe(long voltage, List<ItemStack> inputs, List<FluidStack> fluidInputs, boolean exactVoltage) {
AtomicReference<Recipe> recipe = new AtomicReference<>();
Arrays.stream(recipeMaps)
.parallel()
.map(recipeMap -> recipeMap.findRecipe(voltage, inputs, fluidInputs, exactVoltage))
.filter(java.util.Objects::nonNull)
.findFirst().ifPresent(recipe::set);
return recipe.get();
}

@Nonnull
@Override
public Map<GTRecipeCategory, List<Recipe>> getRecipesByCategory() {
Map<GTRecipeCategory, List<Recipe>> res = new Object2ObjectOpenHashMap<>();
for (RecipeMap<?> recipeMap : recipeMaps) {
res.putAll(recipeMap.getRecipesByCategory());
}
return Collections.unmodifiableMap(res);
}
}