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

Improve TagPrefix-based Recipe Generation #2616

Open
wants to merge 3 commits into
base: 1.20.1
Choose a base branch
from
Open
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 @@ -5,9 +5,11 @@
import com.gregtechceu.gtceu.api.fluids.store.FluidStorage;
import com.gregtechceu.gtceu.api.fluids.store.FluidStorageImpl;
import com.gregtechceu.gtceu.api.fluids.store.FluidStorageKey;
import com.gregtechceu.gtceu.api.fluids.store.FluidStorageKeys;
import com.gregtechceu.gtceu.api.registry.registrate.GTRegistrate;

import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.fluids.FluidStack;

import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -25,6 +27,8 @@ public class FluidProperty implements IMaterialProperty, FluidStorage {
@Getter
@Setter
private FluidStorageKey primaryKey = null;
@Setter
private @Nullable Fluid solidifyingFluid = null;

public FluidProperty(@NotNull FluidStorageKey key, @NotNull FluidBuilder builder) {
enqueueRegistration(key, builder);
Expand Down Expand Up @@ -71,6 +75,28 @@ public void store(@NotNull FluidStorageKey key, @NotNull Supplier<? extends Flui
return storage.getQueuedBuilder(key);
}

/**
* @return the Fluid which solidifies into the material.
*/
public @Nullable Fluid solidifiesFrom() {
if (this.solidifyingFluid == null) {
this.solidifyingFluid = getStorage().get(FluidStorageKeys.LIQUID);
}
return solidifyingFluid;
}

/**
* @param amount the size of the returned FluidStack.
* @return a FluidStack of the Fluid which solidifies into the material.
*/
public @NotNull FluidStack solidifiesFrom(int amount) {
Fluid fluid = solidifiesFrom();
if (fluid == null) {
return FluidStack.EMPTY;
}
return new FluidStack(fluid, amount);
}

@Override
public void verifyProperty(MaterialProperties properties) {
if (this.primaryKey == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class OreProperty implements IMaterialProperty {
Expand Down Expand Up @@ -123,7 +124,31 @@ public void setSeparatedInto(Material... materials) {
this.separatedInto.addAll(Arrays.asList(materials));
}

public void setOreByProducts(Material... materials) {
/**
* Set the ore byproducts for this property
*
* @param materials the materials to use as byproducts
*/
public void setOreByProducts(@NotNull Material... materials) {
setOreByProducts(Arrays.asList(materials));
}

/**
* Set the ore byproducts for this property
*
* @param materials the materials to use as byproducts
*/
public void setOreByProducts(@NotNull Collection<Material> materials) {
this.oreByProducts.clear();
this.oreByProducts.addAll(materials);
}

/**
* Add ore byproducts to this property
*
* @param materials the materials to add as byproducts
*/
public void addOreByProducts(@NotNull Material @NotNull... materials) {
this.oreByProducts.addAll(Arrays.asList(materials));
}

Expand Down
24 changes: 6 additions & 18 deletions src/main/java/com/gregtechceu/gtceu/api/data/tag/TagPrefix.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.gregtechceu.gtceu.api.data.tag;

import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.GTCEuAPI;
import com.gregtechceu.gtceu.api.GTValues;
import com.gregtechceu.gtceu.api.addon.AddonFinder;
import com.gregtechceu.gtceu.api.addon.IGTAddon;
import com.gregtechceu.gtceu.api.data.chemical.ChemicalHelper;
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
import com.gregtechceu.gtceu.api.data.chemical.material.info.MaterialFlags;
import com.gregtechceu.gtceu.api.data.chemical.material.info.MaterialIconType;
import com.gregtechceu.gtceu.api.data.chemical.material.properties.IMaterialProperty;
import com.gregtechceu.gtceu.api.data.chemical.material.properties.PropertyKey;
import com.gregtechceu.gtceu.api.data.chemical.material.stack.MaterialStack;
import com.gregtechceu.gtceu.api.item.tool.GTToolType;
Expand All @@ -24,7 +22,6 @@

import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.recipes.FinishedRecipe;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -1085,21 +1082,12 @@ public boolean doGenerateBlock(Material material) {
hasItemTable() && this.itemTable.get() != null && getItemFromTable(material) != null;
}

@FunctionalInterface
public interface MaterialRecipeHandler<T extends IMaterialProperty> {

void accept(TagPrefix prefix, Material material, T property, Consumer<FinishedRecipe> provider);
}

public <T extends IMaterialProperty> void executeHandler(Consumer<FinishedRecipe> provider,
PropertyKey<T> propertyKey,
MaterialRecipeHandler<T> handler) {
for (Material material : GTCEuAPI.materialManager.getRegisteredMaterials()) {
if (material.hasProperty(propertyKey) && !material.hasFlag(MaterialFlags.NO_UNIFICATION) &&
!ChemicalHelper.get(this, material).isEmpty()) {
handler.accept(this, material, material.getProperty(propertyKey), provider);
}
}
/**
* @param material the material to check
* @return if the material should have recipes autogenerated
*/
public boolean shouldGenerateRecipes(@NotNull Material material) {
return !material.hasFlag(MaterialFlags.NO_UNIFICATION) && !ChemicalHelper.get(this, material).isEmpty();
}

public String getUnlocalizedName() {
Expand Down
32 changes: 22 additions & 10 deletions src/main/java/com/gregtechceu/gtceu/common/data/GTRecipes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.gregtechceu.gtceu.common.data;

import com.gregtechceu.gtceu.api.GTCEuAPI;
import com.gregtechceu.gtceu.api.addon.AddonFinder;
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
import com.gregtechceu.gtceu.api.data.chemical.material.info.MaterialFlags;
import com.gregtechceu.gtceu.data.recipe.MaterialInfoLoader;
import com.gregtechceu.gtceu.data.recipe.configurable.RecipeAddition;
import com.gregtechceu.gtceu.data.recipe.configurable.RecipeRemoval;
Expand Down Expand Up @@ -42,18 +45,27 @@ public static void recipeAddition(Consumer<FinishedRecipe> originalConsumer) {
// Decomposition info loading
MaterialInfoLoader.init();

ToolRecipeHandler.setup();

// com.gregtechceu.gtceu.data.recipe.generated.*
DecompositionRecipeHandler.init(consumer);
MaterialRecipeHandler.init(consumer);
OreRecipeHandler.init(consumer);
PartsRecipeHandler.init(consumer);
PipeRecipeHandler.init(consumer);
PolarizingRecipeHandler.init(consumer);
RecyclingRecipeHandler.init(consumer);
ToolRecipeHandler.init(consumer);
WireCombiningHandler.init(consumer);
WireRecipeHandler.init(consumer);
for (Material material : GTCEuAPI.materialManager.getRegisteredMaterials()) {
if (material.hasFlag(MaterialFlags.NO_UNIFICATION)) {
continue;
}

DecompositionRecipeHandler.run(consumer, material);
MaterialRecipeHandler.run(consumer, material);
OreRecipeHandler.run(consumer, material);
PartsRecipeHandler.run(consumer, material);
PipeRecipeHandler.run(consumer, material);
PolarizingRecipeHandler.run(consumer, material);
RecyclingRecipeHandler.run(consumer, material);
ToolRecipeHandler.run(consumer, material);
WireCombiningHandler.run(consumer, material);
WireRecipeHandler.run(consumer, material);
}

CustomToolRecipes.init(consumer);
AirScrubberRecipes.init(consumer);
ChemistryRecipes.init(consumer);
MetaTileEntityMachineRecipeLoader.init(consumer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.gregtechceu.gtceu.common.item;

import com.gregtechceu.gtceu.api.GTCEuAPI;
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
import com.gregtechceu.gtceu.api.data.chemical.material.properties.PropertyKey;
import com.gregtechceu.gtceu.api.item.IComponentItem;
import com.gregtechceu.gtceu.api.item.component.IMaterialPartItem;
Expand Down Expand Up @@ -29,14 +31,21 @@ public class TurbineRotorBehaviour implements IMaterialPartItem, ISubItemHandler

@Override
public void fillItemCategory(Item item, CreativeModeTab category, NonNullList<ItemStack> items) {
turbineBlade.executeHandler(null, PropertyKey.INGOT, (tagPrefix, material, property, provider) -> {
for (Material material : GTCEuAPI.materialManager.getRegisteredMaterials()) {
if (!turbineBlade.shouldGenerateRecipes(material)) {
return;
}
if (!material.hasProperty(PropertyKey.INGOT)) {
return;
}

var rotorStack = new ItemStack(item);
var behavior = TurbineRotorBehaviour.getBehaviour(rotorStack);
if (behavior != null) {
behavior.setPartMaterial(rotorStack, material);
items.add(rotorStack);
}
});
}
}

public int getRotorPower(ItemStack stack) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.gregtechceu.gtceu.data.recipe.generated;

import com.gregtechceu.gtceu.api.GTCEuAPI;
import com.gregtechceu.gtceu.api.data.chemical.ChemicalHelper;
import com.gregtechceu.gtceu.api.data.chemical.material.Material;
import com.gregtechceu.gtceu.api.data.chemical.material.properties.PropertyKey;
import com.gregtechceu.gtceu.api.data.chemical.material.stack.MaterialStack;
import com.gregtechceu.gtceu.api.data.tag.TagPrefix;
import com.gregtechceu.gtceu.data.recipe.builder.GTRecipeBuilder;

import net.minecraft.data.recipes.FinishedRecipe;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
Expand All @@ -22,17 +22,15 @@
import static com.gregtechceu.gtceu.common.data.GTRecipeTypes.CENTRIFUGE_RECIPES;
import static com.gregtechceu.gtceu.common.data.GTRecipeTypes.ELECTROLYZER_RECIPES;

public class DecompositionRecipeHandler {
public final class DecompositionRecipeHandler {

public static void init(Consumer<FinishedRecipe> provider) {
for (var material : GTCEuAPI.materialManager.getRegisteredMaterials()) {
var prefix = material.hasProperty(PropertyKey.DUST) ? dust : null;
processDecomposition(prefix, material, provider);
}
private DecompositionRecipeHandler() {}

public static void run(@NotNull Consumer<FinishedRecipe> provider, @NotNull Material material) {
processDecomposition(provider, material);
}

private static void processDecomposition(TagPrefix decomposePrefix, Material material,
Consumer<FinishedRecipe> provider) {
private static void processDecomposition(@NotNull Consumer<FinishedRecipe> provider, @NotNull Material material) {
if (material.getMaterialComponents().isEmpty() ||
(!material.hasFlag(DECOMPOSITION_BY_ELECTROLYZING) &&
!material.hasFlag(DECOMPOSITION_BY_CENTRIFUGING)) ||
Expand All @@ -56,7 +54,8 @@ private static void processDecomposition(TagPrefix decomposePrefix, Material mat
}

// only reduce items
if (decomposePrefix != null) {
boolean hasDust = material.hasProperty(PropertyKey.DUST);
if (hasDust) {
// calculate lowest common denominator
List<Integer> materialAmounts = new ArrayList<>();
materialAmounts.add(totalInputAmount);
Expand Down Expand Up @@ -110,8 +109,8 @@ private static void processDecomposition(TagPrefix decomposePrefix, Material mat
builder.outputFluids(fluidOutputs.toArray(FluidStack[]::new));

// finish builder
if (decomposePrefix != null) {
builder.inputItems(decomposePrefix, material, totalInputAmount);
if (hasDust) {
builder.inputItems(dust, material, totalInputAmount);
} else {
builder.inputFluids(material.getFluid(1000));
}
Expand Down
Loading
Loading