Skip to content

Commit

Permalink
Rework of Recipe Properties (#1580)
Browse files Browse the repository at this point in the history
Recipe properties now provides information regarding formatting and rendering
Strongly typed values are used
JEI shows minimal Coil tier for Electric Blast furnace recipes
  • Loading branch information
LAGIdiot authored May 17, 2021
1 parent 3921d1a commit e3125ae
Show file tree
Hide file tree
Showing 21 changed files with 640 additions and 113 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ local.properties
*~

run/

logs/
90 changes: 59 additions & 31 deletions src/main/java/gregtech/api/recipes/Recipe.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package gregtech.api.recipes;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import gregtech.api.capability.IMultipleTankHandler;
import gregtech.api.recipes.recipeproperties.RecipeProperty;
import gregtech.api.recipes.recipeproperties.RecipePropertyStorage;
import gregtech.api.util.GTUtility;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.items.IItemHandlerModifiable;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.tuple.Pair;

import java.util.*;
Expand Down Expand Up @@ -58,12 +58,12 @@ public static String formatChanceValue(int outputChance) {
*/
private final boolean hidden;

private final Map<String, Object> recipeProperties;
private final RecipePropertyStorage recipePropertyStorage;

public Recipe(List<CountableIngredient> inputs, List<ItemStack> outputs, List<ChanceEntry> chancedOutputs,
List<FluidStack> fluidInputs, List<FluidStack> fluidOutputs,
Map<String, Object> recipeProperties, int duration, int EUt, boolean hidden) {
this.recipeProperties = ImmutableMap.copyOf(recipeProperties);
int duration, int EUt, boolean hidden) {
this.recipePropertyStorage = new RecipePropertyStorage();
this.inputs = NonNullList.create();
this.inputs.addAll(inputs);
this.outputs = NonNullList.create();
Expand All @@ -78,6 +78,20 @@ public Recipe(List<CountableIngredient> inputs, List<ItemStack> outputs, List<Ch
this.inputs.sort(Comparator.comparing(CountableIngredient::getCount).reversed());
}

/**
* @deprecated use {@link #Recipe(List inputs, List outputs, List chancedOutputs, List fluidInputs,
* List fluidOutputs, int duration, int EUt, boolean hidden)} instead
* Recipe properties are added by {@link RecipePropertyStorage#store(Map recipeProperties)}
* on {@link #getRecipePropertyStorage()}
*/
@Deprecated
public Recipe(List<CountableIngredient> inputs, List<ItemStack> outputs, List<ChanceEntry> chancedOutputs,
List<FluidStack> fluidInputs, List<FluidStack> fluidOutputs,
Map<String, Object> recipeProperties, int duration, int EUt, boolean hidden) {
this(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs, duration, EUt, hidden);
recipePropertyStorage.storeOldFormat(recipeProperties);
}

public final boolean matches(boolean consumeIfSuccessful, IItemHandlerModifiable inputs, IMultipleTankHandler fluidInputs, MatchingMode matchingMode) {
return matches(consumeIfSuccessful, GTUtility.itemHandlerToList(inputs), GTUtility.fluidHandlerToList(fluidInputs), matchingMode);
}
Expand Down Expand Up @@ -290,47 +304,61 @@ public boolean hasValidInputsForDisplay() {
return hasValidInputs;
}

public Set<String> getPropertyKeys() {
return recipeProperties.keySet();
//region RecipeProperties

/**
* Provides full access to {@link RecipePropertyStorage} for this Recipe
* @return RecipePropertyStorage
*/
public RecipePropertyStorage getRecipePropertyStorage(){
return recipePropertyStorage;
}

/**
* @deprecated use {@link RecipePropertyStorage#getRecipePropertyValue(RecipeProperty recipeProperty, Object defaultValue)}
* on {@link #getRecipePropertyStorage()}
*/
@Deprecated
public boolean getBooleanProperty(String key) {
Validate.notNull(key);
Object o = this.recipeProperties.get(key);
if (!(o instanceof Boolean)) {
throw new IllegalArgumentException();
}
return (boolean) o;
return getProperty(key);
}

/**
* @deprecated use {@link RecipePropertyStorage#getRecipePropertyValue(RecipeProperty recipeProperty, Object defaultValue)}
* on {@link #getRecipePropertyStorage()}
*/
@Deprecated
public int getIntegerProperty(String key) {
Validate.notNull(key);
Object o = this.recipeProperties.get(key);
if (!(o instanceof Integer)) {
throw new IllegalArgumentException();
}
return (int) o;
return getProperty(key);
}

/**
* @deprecated use {@link RecipePropertyStorage#getRecipePropertyValue(RecipeProperty recipeProperty, Object defaultValue)}
* on {@link #getRecipePropertyStorage()}
*/
@Deprecated
public String getStringProperty(String key) {
return getProperty(key);
}

/**
* @deprecated use {@link RecipePropertyStorage#getRecipePropertyValue(RecipeProperty recipeProperty, Object defaultValue)}
* on {@link #getRecipePropertyStorage()}
*/
@Deprecated
@SuppressWarnings("unchecked")
public <T> T getProperty(String key) {
Validate.notNull(key);
Object o = this.recipeProperties.get(key);
if (o == null) {
throw new IllegalArgumentException();
}
return (T) o;
}
AbstractMap.SimpleEntry<RecipeProperty<?>, Object> recipePropertySet = getRecipePropertyStorage().getRecipeProperty(key);

public String getStringProperty(String key) {
Validate.notNull(key);
Object o = this.recipeProperties.get(key);
if (!(o instanceof String)) {
if (recipePropertySet == null) {
throw new IllegalArgumentException();
}
return (String) o;

return (T) recipePropertySet.getKey().castValue(recipePropertySet.getValue());
}

//endregion RecipeProperties

public static class ChanceEntry {
private final ItemStack itemStack;
private final int chance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gregtech.api.recipes.builders;

import com.google.common.collect.ImmutableMap;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeBuilder;
import gregtech.api.recipes.RecipeMap;
Expand Down Expand Up @@ -34,12 +33,12 @@ public void buildAndRegister() {
for (FluidMaterial material : new FluidMaterial[]{Materials.Argon, Materials.Nitrogen}) {
int plasmaAmount = (int) Math.max(1L, this.duration / (material.getAverageMass() * 16L));
SimpleRecipeBuilder builder = RecipeMaps.PLASMA_ARC_FURNACE_RECIPES.recipeBuilder()
.inputsIngredients(this.inputs)
.outputs(this.outputs)
.duration(Math.max(1, this.duration / 16))
.EUt(this.EUt / 3)
.fluidInputs(material.getPlasma(plasmaAmount))
.fluidOutputs(material.getFluid(plasmaAmount));
.inputsIngredients(this.inputs)
.outputs(this.outputs)
.duration(Math.max(1, this.duration / 16))
.EUt(this.EUt / 3)
.fluidInputs(material.getPlasma(plasmaAmount))
.fluidOutputs(material.getFluid(plasmaAmount));
builder.buildAndRegister();
}
}
Expand All @@ -48,8 +47,7 @@ public void buildAndRegister() {

public ValidationResult<Recipe> build() {
return ValidationResult.newResult(finalizeAndValidate(),
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs,
ImmutableMap.of(), duration, EUt, hidden));
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs, duration, EUt, hidden));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeBuilder;
import gregtech.api.recipes.RecipeMap;
import gregtech.api.recipes.recipeproperties.BlastTemperatureProperty;
import gregtech.api.util.EnumValidationResult;
import gregtech.api.util.GTLog;
import gregtech.api.util.ValidationResult;
Expand All @@ -18,7 +19,7 @@ public BlastRecipeBuilder() {

public BlastRecipeBuilder(Recipe recipe, RecipeMap<BlastRecipeBuilder> recipeMap) {
super(recipe, recipeMap);
this.blastFurnaceTemp = recipe.getIntegerProperty("blastFurnaceTemp");
this.blastFurnaceTemp = recipe.getRecipePropertyStorage().getRecipePropertyValue(BlastTemperatureProperty.getInstance(), 0);
}

public BlastRecipeBuilder(RecipeBuilder<BlastRecipeBuilder> recipeBuilder) {
Expand Down Expand Up @@ -49,17 +50,20 @@ public BlastRecipeBuilder blastFurnaceTemp(int blastFurnaceTemp) {
}

public ValidationResult<Recipe> build() {
return ValidationResult.newResult(finalizeAndValidate(),
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs,
ImmutableMap.of("blast_furnace_temperature", blastFurnaceTemp),
duration, EUt, hidden));
Recipe recipe = new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs,
duration, EUt, hidden);
if (!recipe.getRecipePropertyStorage().store(ImmutableMap.of(BlastTemperatureProperty.getInstance(), blastFurnaceTemp))) {
return ValidationResult.newResult(EnumValidationResult.INVALID, recipe);
}

return ValidationResult.newResult(finalizeAndValidate(), recipe);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.appendSuper(super.toString())
.append("blast_furnace_temperature", blastFurnaceTemp)
.toString();
.appendSuper(super.toString())
.append(BlastTemperatureProperty.getInstance().getKey(), blastFurnaceTemp)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gregtech.api.recipes.builders;

import com.google.common.collect.ImmutableMap;
import gregtech.api.recipes.ModHandler;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeBuilder;
Expand Down Expand Up @@ -29,22 +28,21 @@ public CutterRecipeBuilder copy() {
@Override
public ValidationResult<Recipe> build() {
return ValidationResult.newResult(finalizeAndValidate(),
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs,
ImmutableMap.of(), duration, EUt, hidden));
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs, duration, EUt, hidden));
}

@Override
public void buildAndRegister() {
if (fluidInputs.isEmpty()) {
recipeMap.addRecipe(this.copy()
.fluidInputs(Materials.Water.getFluid(Math.max(4, Math.min(1000, duration * EUt / 320))))
.duration(duration * 2).build());
.fluidInputs(Materials.Water.getFluid(Math.max(4, Math.min(1000, duration * EUt / 320))))
.duration(duration * 2).build());
recipeMap.addRecipe(this.copy()
.fluidInputs(ModHandler.getDistilledWater(Math.max(3, Math.min(750, duration * EUt / 426))))
.duration((int) (duration * 1.3)).build());
.fluidInputs(ModHandler.getDistilledWater(Math.max(3, Math.min(750, duration * EUt / 426))))
.duration((int) (duration * 1.3)).build());
recipeMap.addRecipe(this.copy()
.fluidInputs(Materials.Lubricant.getFluid(Math.max(1, Math.min(250, duration * EUt / 1280))))
.duration(Math.max(1, duration / 2)).build());
.fluidInputs(Materials.Lubricant.getFluid(Math.max(1, Math.min(250, duration * EUt / 1280))))
.duration(Math.max(1, duration / 2)).build());
} else {
recipeMap.addRecipe(build());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gregtech.api.recipes.builders;

import com.google.common.collect.ImmutableMap;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeBuilder;
import gregtech.api.recipes.RecipeMap;
Expand Down Expand Up @@ -71,27 +70,25 @@ public ImplosionRecipeBuilder explosivesType(ItemStack explosivesType) {

@Override
public void buildAndRegister() {
int amount = Math.max(1, explosivesAmount / 2);
if(explosivesType == null) {
int amount = Math.max(1, explosivesAmount / 2);
if (explosivesType == null) {
explosivesType = new ItemStack(Blocks.TNT, amount);
}
else {
} else {
explosivesType = new ItemStack(explosivesType.getItem(), amount, explosivesType.getMetadata());
}
recipeMap.addRecipe(this.copy().inputs(explosivesType).build());
}

public ValidationResult<Recipe> build() {
return ValidationResult.newResult(finalizeAndValidate(),
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs,
ImmutableMap.of(), duration, EUt, hidden));
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs, duration, EUt, hidden));
}

@Override
public String toString() {
return new ToStringBuilder(this)
.appendSuper(super.toString())
.append("explosivesAmount", explosivesAmount)
.toString();
.appendSuper(super.toString())
.append("explosivesAmount", explosivesAmount)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gregtech.api.recipes.builders;

import com.google.common.collect.ImmutableMap;
import gregtech.api.recipes.CountableIngredient;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeBuilder;
Expand Down Expand Up @@ -60,15 +59,14 @@ protected EnumValidationResult finalizeAndValidate() {

public ValidationResult<Recipe> build() {
return ValidationResult.newResult(finalizeAndValidate(),
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs,
ImmutableMap.of(), duration, EUt, hidden));
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs, duration, EUt, hidden));
}

@Override
public String toString() {
return new ToStringBuilder(this)
.appendSuper(super.toString())
.append("circuitMeta", circuitMeta)
.toString();
.appendSuper(super.toString())
.append("circuitMeta", circuitMeta)
.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gregtech.api.recipes.builders;

import com.google.common.collect.ImmutableMap;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeBuilder;
import gregtech.api.recipes.RecipeMap;
Expand All @@ -26,7 +25,6 @@ public SimpleRecipeBuilder copy() {

public ValidationResult<Recipe> build() {
return ValidationResult.newResult(finalizeAndValidate(),
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs,
ImmutableMap.of(), duration, EUt, hidden));
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs, duration, EUt, hidden));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gregtech.api.recipes.builders;

import com.google.common.collect.ImmutableMap;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeBuilder;
import gregtech.api.recipes.RecipeMap;
Expand Down Expand Up @@ -29,9 +28,9 @@ public UniversalDistillationRecipeBuilder copy() {
@Override
public void buildAndRegister() {
IntCircuitRecipeBuilder builder = RecipeMaps.DISTILLERY_RECIPES.recipeBuilder()
.fluidInputs(this.fluidInputs.toArray(new FluidStack[0]))
.duration(this.duration * 2)
.EUt(this.EUt / 4);
.fluidInputs(this.fluidInputs.toArray(new FluidStack[0]))
.duration(this.duration * 2)
.EUt(this.EUt / 4);

for (int i = 0; i < fluidOutputs.size(); i++) {
builder.copy().circuitMeta(i).fluidOutputs(this.fluidOutputs.get(i)).buildAndRegister();
Expand All @@ -42,8 +41,7 @@ public void buildAndRegister() {

public ValidationResult<Recipe> build() {
return ValidationResult.newResult(finalizeAndValidate(),
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs,
ImmutableMap.of(), duration, EUt, hidden));
new Recipe(inputs, outputs, chancedOutputs, fluidInputs, fluidOutputs, duration, EUt, hidden));
}

}
4 changes: 2 additions & 2 deletions src/main/java/gregtech/api/recipes/crafttweaker/CTRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ public boolean isHidden() {

@ZenGetter("propertyKeys")
public List<String> getPropertyKeys() {
return new ArrayList<>(this.backingRecipe.getPropertyKeys());
return new ArrayList<>(this.backingRecipe.getRecipePropertyStorage().getRecipePropertyKeys());
}

@ZenMethod
public Object getProperty(String key) {
return this.backingRecipe.getProperty(key);
return this.backingRecipe.getRecipePropertyStorage().getRawRecipePropertyValue(key);
}

@ZenMethod
Expand Down
Loading

0 comments on commit e3125ae

Please sign in to comment.