Skip to content

Instantly share code, notes, and snippets.

@Commoble
Created April 3, 2021 15:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Commoble/0ff497c4a70f496839665c8e0a9195fa to your computer and use it in GitHub Desktop.
Save Commoble/0ff497c4a70f496839665c8e0a9195fa to your computer and use it in GitHub Desktop.
Cooking recipe codec for generating cooking recipe jsons for minecraft forge
/*
The MIT License (MIT)
Copyright (c) 2021 Joseph Bettendorff aka "Commoble"
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package commoble.morered.datagen;
import com.google.gson.JsonElement;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.JsonOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;
// for use with JsonDataProvider
// https://gist.github.com/Commoble/0a2d6d5a918e743bf04890aa4b036367
public class CookingRecipeDefinition
{
// result itemstack json format isn't the same as the standard format for itemstacks, stupidly
public static final Codec<ItemStack> ITEMSTACK_CODEC = RecordCodecBuilder.create(instance -> instance.group(
Registry.ITEM.fieldOf("item").forGetter(itemstack -> itemstack.getItem()),
Codec.INT.optionalFieldOf("count",1).forGetter(itemstack -> itemstack.getCount()),
CompoundNBT.CODEC.optionalFieldOf("nbt", new CompoundNBT()).forGetter(ItemStack::getTag)
).apply(instance, ItemStack::new));
public static final Codec<Ingredient> INGREDIENT_CODEC = Codec.PASSTHROUGH.comapFlatMap(dynamic ->
{
try
{
Ingredient ingredient = Ingredient.fromJson(dynamic.convert(JsonOps.INSTANCE).getValue());
return DataResult.success(ingredient);
}
catch(Exception e)
{
return DataResult.error(e.getMessage());
}
},
ingredient -> new Dynamic<JsonElement>(JsonOps.INSTANCE, ingredient.toJson()));
public static final Codec<Either<Item,ItemStack>> ITEM_OR_STACK_CODEC = Codec.either(Registry.ITEM, ITEMSTACK_CODEC);
public static final Codec<CookingRecipeDefinition> CODEC = RecordCodecBuilder.create(instance -> instance.group(
ResourceLocation.CODEC.fieldOf("type").forGetter(CookingRecipeDefinition::getType),
INGREDIENT_CODEC.fieldOf("ingredient").forGetter(CookingRecipeDefinition::getIngredient),
ITEM_OR_STACK_CODEC.fieldOf("result").forGetter(CookingRecipeDefinition::getResult),
Codec.FLOAT.fieldOf("experience").forGetter(CookingRecipeDefinition::getExperience),
Codec.INT.fieldOf("cookingtime").forGetter(CookingRecipeDefinition::getCookingTime)
).apply(instance, CookingRecipeDefinition::new));
private final ResourceLocation type; public ResourceLocation getType() { return this.type; }
private final Ingredient ingredient; public Ingredient getIngredient() { return this.ingredient; }
private final Either<Item,ItemStack> result; public Either<Item,ItemStack> getResult() { return this.result; }
private final float experience; public float getExperience() { return this.experience; }
private final int cookingtime; public int getCookingTime() { return this.cookingtime; }
public CookingRecipeDefinition(ResourceLocation type, Ingredient ingredient, Either<Item,ItemStack> result, float experience, int cookingtime)
{
this.type = type;
this.ingredient = ingredient;
this.result = result;
this.experience = experience;
this.cookingtime = cookingtime;
}
public static CookingRecipeDefinition forItemResult(ResourceLocation type, Ingredient ingredient, Item result, float experience, int cookingtime)
{
return new CookingRecipeDefinition(type, ingredient, Either.left(result), experience, cookingtime);
}
public static CookingRecipeDefinition forItemStackResult(ResourceLocation type, Ingredient ingredient, ItemStack result, float experience, int cookingtime)
{
return new CookingRecipeDefinition(type, ingredient, Either.right(result), experience, cookingtime);
}
}
package commoble.morered.datagen;
import java.util.Arrays;
import com.google.common.collect.ImmutableMap;
import commoble.morered.ItemRegistrar;
import commoble.morered.MoreRed;
import commoble.morered.datagen.JsonDataProvider.ResourceType;
import commoble.morered.wires.WireBlockItem;
import net.minecraft.data.DataGenerator;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.item.DyeColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.tags.ITag.INamedTag;
import net.minecraft.tags.ItemTags;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Util;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.fml.event.lifecycle.GatherDataEvent;
// example data generator using the above codec and JsonDataProvider
// https://gist.github.com/Commoble/0a2d6d5a918e743bf04890aa4b036367
@Mod("yourmod")
@EventBusSubscriber(modid="yourmod", bus=Bus.MOD)
public class DataGenHandler
{
@SubscribeEvent
static void onGatherData(GatherDataEvent event)
{
DataGenerator generator = event.getGenerator();
ResourceLocation smeltingID = new ResourceLocation("minecraft:smelting");
// the itemstack constructor that takes an NBT is private
// we could use mixins to get at it, but it's simpler to just make the itemstack here and then set the nbt
ItemStack enchantedPotatos = new ItemStack(Items.BAKED_POTATO, 5);
EnchantmentHelper.setEnchantments(ImmutableMap.<Enchantment,Integer>builder().put(Enchantments.FIRE_ASPECT, 1).build(), enchantedPotatos);
// create the data provider and tell it about the recipes we want to generate
JsonDataProvider<CookingRecipeDefinition> cookingRecipes = new JsonDataProvider<CookingRecipeDefinition>(generator, ResourceType.DATA, "recipes/test", CookingRecipeDefinition.CODEC)
// cooking dirt gives us a potato
.with(new ResourceLocation("test:item_result_test"), CookingRecipeDefinition.forItemResult(smeltingID, Ingredient.of(Items.DIRT), Items.BAKED_POTATO, 0.35F, 200))
// cooking diamond gives us five enchanted potatos
.with(new ResourceLocation("test:itemstack_result_test"), CookingRecipeDefinition.forItemStackResult(smeltingID, Ingredient.of(Items.DIAMOND), enchantedPotatos, 0.35F, 200))
// cooking items from the anvil tag gives us a potato
.with(new ResourceLocation("test:tag_ingredient_test"), CookingRecipeDefinition.forItemResult(smeltingID, Ingredient.of(ItemTags.ANVIL), Items.BAKED_POTATO, 0.35F, 200));
// add the data provider to the generator
generator.addProvider(cookingRecipes);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment