Added toast generation for datagen'd recipes

This commit is contained in:
Ayutac 2022-02-09 20:29:50 +01:00
parent a097f9eae1
commit 6fa922c022
6 changed files with 62 additions and 9 deletions

View file

@ -25,6 +25,7 @@
package reborncore.common.misc;
import net.minecraft.tag.Tag;
import org.jetbrains.annotations.Contract;
/**
* Tells if an item, block etc. has a tag solely for compatibility with other mods.
@ -39,4 +40,16 @@ public interface TagConvertible<T> {
*/
Tag.Identified<T> asTag();
/**
* Converts a given object into its tag form if the item is a {@link TagConvertible}.
* @param obj the object to convert
* @return The tag of the object or the object itself if it is not a {@link TagConvertible}.
*/
@Contract("null -> null")
static Object convertIf(Object obj) {
if (obj instanceof TagConvertible<?> convertible)
return convertible.asTag();
return obj;
}
}

View file

@ -69,7 +69,7 @@ abstract class TechRebornRecipesProvider extends FabricRecipesProvider {
if (input instanceof ItemConvertible) {
return hasItem(input)
} else if (input instanceof Tag.Identified) {
return "has_tag_" + input.getId()
return "has_tag_" + input.getId().toUnderscoreSeparatedString()
}
throw new IllegalArgumentException()

View file

@ -25,7 +25,13 @@
package techreborn.datagen.recipes.machine
import com.google.gson.JsonObject
import net.minecraft.advancement.Advancement.Task
import net.minecraft.advancement.AdvancementRewards
import net.minecraft.advancement.CriterionMerger
import net.minecraft.advancement.criterion.CriterionConditions
import net.minecraft.advancement.criterion.RecipeUnlockedCriterion
import net.minecraft.data.server.recipe.RecipeJsonProvider
import net.minecraft.data.server.recipe.ShapelessRecipeJsonFactory
import net.minecraft.item.ItemConvertible
import net.minecraft.item.ItemStack
import net.minecraft.recipe.RecipeSerializer
@ -41,6 +47,7 @@ import java.util.function.Consumer
class MachineRecipeJsonFactory<R extends RebornRecipe> {
protected final RebornRecipeType<R> type
protected final Task builder = Task.create()
protected final List<RebornIngredient> ingredients = new ArrayList<>()
protected final List<ItemStack> outputs = new ArrayList<>()
@ -162,9 +169,18 @@ class MachineRecipeJsonFactory<R extends RebornRecipe> {
}
}
MachineRecipeJsonFactory<R> criterion(String string, CriterionConditions criterionConditions) {
builder.criterion(string, criterionConditions)
return this
}
void offerTo(Consumer<RecipeJsonProvider> exporter) {
validate()
exporter.accept(new MachineRecipeJsonProvider<R>(type, createRecipe(getIdentifier())))
Identifier recipeId = getIdentifier()
Identifier advancementId = new Identifier(recipeId.getNamespace(), "recipes/" + recipeId.getPath())
// "has the recipe" condition
builder.parent(new Identifier("recipes/root")).criterion("has_the_recipe", RecipeUnlockedCriterion.create(recipeId)).rewards(AdvancementRewards.Builder.recipe(recipeId)).criteriaMerger(CriterionMerger.OR)
exporter.accept(new MachineRecipeJsonProvider<R>(type, createRecipe(recipeId), advancementId, builder))
}
def getIdentifier() {
@ -183,10 +199,14 @@ class MachineRecipeJsonFactory<R extends RebornRecipe> {
static class MachineRecipeJsonProvider<R extends RebornRecipe> implements RecipeJsonProvider {
private final RebornRecipeType<R> type
private final R recipe
private final Identifier advancementId
private final Task builder
MachineRecipeJsonProvider(RebornRecipeType<R> type, R recipe) {
MachineRecipeJsonProvider(RebornRecipeType<R> type, R recipe, Identifier advancementId = null, Task builder = null) {
this.type = type
this.recipe = recipe
this.advancementId = advancementId
this.builder = builder
}
@Override
@ -211,12 +231,14 @@ class MachineRecipeJsonFactory<R extends RebornRecipe> {
@Override
JsonObject toAdvancementJson() {
if (builder == null)
return null
return builder.toJson()
}
@Override
Identifier getAdvancementId() {
return null
return advancementId
}
}
}

View file

@ -79,6 +79,7 @@ class BlastFurnaceRecipesProvider extends TechRebornRecipesProvider {
time ARMOR_TIME
heat ARMOR_HEAT
source "boots"
criterion getCriterionName(boots), getCriterionConditions(boots)
}
}
}
@ -103,6 +104,7 @@ class BlastFurnaceRecipesProvider extends TechRebornRecipesProvider {
time ARMOR_TIME
heat ARMOR_HEAT
source "chestplate"
criterion getCriterionName(chestplate), getCriterionConditions(chestplate)
}
}
}
@ -127,6 +129,7 @@ class BlastFurnaceRecipesProvider extends TechRebornRecipesProvider {
time ARMOR_TIME
heat ARMOR_HEAT
source "helmet"
criterion getCriterionName(helmet), getCriterionConditions(helmet)
}
}
}
@ -151,6 +154,7 @@ class BlastFurnaceRecipesProvider extends TechRebornRecipesProvider {
time ARMOR_TIME
heat ARMOR_HEAT
source "leggings"
criterion getCriterionName(leggings), getCriterionConditions(leggings)
}
}
}

View file

@ -43,20 +43,26 @@ class CompressorRecipesProvider extends TechRebornRecipesProvider {
@Override
void generateRecipes() {
TRContent.Plates.values().each {plate ->
if (plate.getSource() != null)
if (plate.getSource() != null) {
var ingredient = TagConvertible.convertIf(plate.getSource())
offerCompressorRecipe {
ingredients (plate.getSource() instanceof TagConvertible<Item> ? ((TagConvertible<Item>)plate.getSource()).asTag() : plate.getSource())
ingredients ingredient
outputs new ItemStack(plate, 1)
power 10
time 300
criterion getCriterionName(ingredient), getCriterionConditions(ingredient)
}
if (plate.getSourceBlock() != null)
}
if (plate.getSourceBlock() != null) {
var ingredient = TagConvertible.convertIf(plate.getSourceBlock())
offerCompressorRecipe {
ingredients (plate.getSourceBlock() instanceof TagConvertible<Item> ? ((TagConvertible<Item>)plate.getSourceBlock()).asTag() : plate.getSourceBlock())
ingredients ingredient
outputs new ItemStack(plate, 9)
power 10
time 300
source "block"
criterion getCriterionName(ingredient), getCriterionConditions(ingredient)
}
}
}
}

View file

@ -29,6 +29,7 @@ import net.fabricmc.fabric.api.tag.TagFactory
import net.minecraft.item.ItemStack
import net.minecraft.item.Items
import net.minecraft.util.Identifier
import net.minecraft.util.registry.Registry
import techreborn.TechReborn
import techreborn.datagen.recipes.TechRebornRecipesProvider
import techreborn.init.TRContent
@ -51,6 +52,7 @@ class GrinderRecipesProvider extends TechRebornRecipesProvider {
outputs new ItemStack(raw, 2)
power 2
time 270
criterion getCriterionName(oreTag), getCriterionConditions(oreTag)
}
}
// TR raw metals
@ -61,6 +63,7 @@ class GrinderRecipesProvider extends TechRebornRecipesProvider {
outputs new ItemStack(raw, 2)
power 2
time 270
criterion getCriterionName(ore.asTag()), getCriterionConditions(ore.asTag())
}
}
// vanilla gems
@ -72,6 +75,7 @@ class GrinderRecipesProvider extends TechRebornRecipesProvider {
outputs dust
power 2
time 200
criterion getCriterionName(gem.asTag()), getCriterionConditions(gem.asTag())
}
if (gem.getOre() != null)
offerGrinderRecipe {
@ -80,6 +84,7 @@ class GrinderRecipesProvider extends TechRebornRecipesProvider {
power 2
time 220
source "ore"
criterion getCriterionName(gem.getOre().asTag()), getCriterionConditions(gem.getOre().asTag())
}
if (gem.getStorageBlock() != null)
offerGrinderRecipe {
@ -88,6 +93,7 @@ class GrinderRecipesProvider extends TechRebornRecipesProvider {
power 2
time 1500
source "block"
criterion getCriterionName(gem.getStorageBlock().asTag()), getCriterionConditions(gem.getStorageBlock().asTag())
}
}
// vanilla ingots
@ -99,6 +105,7 @@ class GrinderRecipesProvider extends TechRebornRecipesProvider {
outputs dust
power 5
time 200
criterion getCriterionName(ingot.asTag()), getCriterionConditions(ingot.asTag())
}
if (ingot.getStorageBlock() != null)
offerGrinderRecipe {
@ -107,6 +114,7 @@ class GrinderRecipesProvider extends TechRebornRecipesProvider {
power 5
time 1500
source "block"
criterion getCriterionName(ingot.getStorageBlock().asTag()), getCriterionConditions(ingot.getStorageBlock().asTag())
}
}
}