From 0e735d507e1d5c45c4806a5327447dae1e6e1f50 Mon Sep 17 00:00:00 2001 From: direwolf20 <39863894+Direwolf20-MC@users.noreply.github.com> Date: Fri, 23 Feb 2024 15:35:59 -0500 Subject: [PATCH] Custom Recipe Type --- .../misc/gooblock_tier1-goospread.json | 34 +++++ .../recipes/gooblock_tier1-goospread.json | 10 ++ .../gooblocks/GooBlockBE_Base.java | 19 +++ .../justdirethings/datagen/Recipes.java | 10 ++ .../recipes/GooSpreadRecipeBuilder.java | 121 ++++++++++++++++++ .../justdirethings/setup/Registration.java | 16 ++- 6 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 src/generated/resources/data/justdirethings/advancements/recipes/misc/gooblock_tier1-goospread.json create mode 100644 src/generated/resources/data/justdirethings/recipes/gooblock_tier1-goospread.json create mode 100644 src/main/java/com/direwolf20/justdirethings/datagen/recipes/GooSpreadRecipeBuilder.java diff --git a/src/generated/resources/data/justdirethings/advancements/recipes/misc/gooblock_tier1-goospread.json b/src/generated/resources/data/justdirethings/advancements/recipes/misc/gooblock_tier1-goospread.json new file mode 100644 index 00000000..e5167f8c --- /dev/null +++ b/src/generated/resources/data/justdirethings/advancements/recipes/misc/gooblock_tier1-goospread.json @@ -0,0 +1,34 @@ +{ + "parent": "minecraft:recipes/root", + "criteria": { + "has_goo_block": { + "conditions": { + "items": [ + { + "items": [ + "justdirethings:gooblock_tier1" + ] + } + ] + }, + "trigger": "minecraft:inventory_changed" + }, + "has_the_recipe": { + "conditions": { + "recipe": "justdirethings:gooblock_tier1-goospread" + }, + "trigger": "minecraft:recipe_unlocked" + } + }, + "requirements": [ + [ + "has_the_recipe", + "has_goo_block" + ] + ], + "rewards": { + "recipes": [ + "justdirethings:gooblock_tier1-goospread" + ] + } +} \ No newline at end of file diff --git a/src/generated/resources/data/justdirethings/recipes/gooblock_tier1-goospread.json b/src/generated/resources/data/justdirethings/recipes/gooblock_tier1-goospread.json new file mode 100644 index 00000000..4bd8c55e --- /dev/null +++ b/src/generated/resources/data/justdirethings/recipes/gooblock_tier1-goospread.json @@ -0,0 +1,10 @@ +{ + "type": "justdirethings:goospread", + "id": "justdirethings:goospread1", + "input": { + "Name": "minecraft:iron_block" + }, + "output": { + "Name": "justdirethings:gooblock_tier1" + } +} \ No newline at end of file diff --git a/src/main/java/com/direwolf20/justdirethings/common/blockentities/gooblocks/GooBlockBE_Base.java b/src/main/java/com/direwolf20/justdirethings/common/blockentities/gooblocks/GooBlockBE_Base.java index 7dfe1eb5..8badade9 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/blockentities/gooblocks/GooBlockBE_Base.java +++ b/src/main/java/com/direwolf20/justdirethings/common/blockentities/gooblocks/GooBlockBE_Base.java @@ -1,9 +1,14 @@ package com.direwolf20.justdirethings.common.blockentities.gooblocks; +import com.direwolf20.justdirethings.datagen.recipes.GooSpreadRecipe; +import com.direwolf20.justdirethings.setup.Registration; import net.minecraft.core.BlockPos; +import net.minecraft.world.item.crafting.RecipeHolder; +import net.minecraft.world.item.crafting.RecipeManager; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; +import org.jetbrains.annotations.Nullable; public class GooBlockBE_Base extends BlockEntity { public GooBlockBE_Base(BlockEntityType type, BlockPos pos, BlockState state) { @@ -15,6 +20,20 @@ public void tickClient() { } public void tickServer() { + findRecipe(getBlockPos().above()); + } + + @Nullable + private GooSpreadRecipe findRecipe(BlockPos coords) { + BlockState state = getLevel().getBlockState(coords); + RecipeManager recipeManager = getLevel().getRecipeManager(); + + for (RecipeHolder recipe : recipeManager.getAllRecipesFor(Registration.GOO_SPREAD_RECIPE_TYPE.get())) { + if (recipe.value() instanceof GooSpreadRecipe gooSpreadRecipe && gooSpreadRecipe.matches(getLevel(), coords, this, state)) { + return gooSpreadRecipe; + } + } + return null; } } diff --git a/src/main/java/com/direwolf20/justdirethings/datagen/Recipes.java b/src/main/java/com/direwolf20/justdirethings/datagen/Recipes.java index 538e6636..557286a4 100644 --- a/src/main/java/com/direwolf20/justdirethings/datagen/Recipes.java +++ b/src/main/java/com/direwolf20/justdirethings/datagen/Recipes.java @@ -1,5 +1,7 @@ package com.direwolf20.justdirethings.datagen; +import com.direwolf20.justdirethings.JustDireThings; +import com.direwolf20.justdirethings.datagen.recipes.GooSpreadRecipeBuilder; import com.direwolf20.justdirethings.setup.Registration; import net.minecraft.advancements.critereon.InventoryChangeTrigger; import net.minecraft.data.PackOutput; @@ -7,7 +9,9 @@ import net.minecraft.data.recipes.RecipeOutput; import net.minecraft.data.recipes.RecipeProvider; import net.minecraft.data.recipes.ShapedRecipeBuilder; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Items; +import net.minecraft.world.level.block.Blocks; import net.neoforged.neoforge.common.Tags; public class Recipes extends RecipeProvider { @@ -54,5 +58,11 @@ protected void buildRecipes(RecipeOutput consumer) { .unlockedBy("has_fuel_canister", InventoryChangeTrigger.TriggerInstance.hasItems(Registration.Fuel_Canister.get())) .save(consumer); + //GooSpread Recipes + GooSpreadRecipeBuilder.shapeless(new ResourceLocation(JustDireThings.MODID, "goospread1"), Blocks.IRON_BLOCK.defaultBlockState(), Registration.GooBlock_Tier1.get().defaultBlockState()) + .group("justdirethings") + .unlockedBy("has_goo_block", InventoryChangeTrigger.TriggerInstance.hasItems(Registration.GooBlock_Tier1_ITEM.get())) + .save(consumer); + } } diff --git a/src/main/java/com/direwolf20/justdirethings/datagen/recipes/GooSpreadRecipeBuilder.java b/src/main/java/com/direwolf20/justdirethings/datagen/recipes/GooSpreadRecipeBuilder.java new file mode 100644 index 00000000..3c93381f --- /dev/null +++ b/src/main/java/com/direwolf20/justdirethings/datagen/recipes/GooSpreadRecipeBuilder.java @@ -0,0 +1,121 @@ +package com.direwolf20.justdirethings.datagen.recipes; + +import com.direwolf20.justdirethings.JustDireThings; +import net.minecraft.advancements.Advancement; +import net.minecraft.advancements.AdvancementRequirements; +import net.minecraft.advancements.AdvancementRewards; +import net.minecraft.advancements.Criterion; +import net.minecraft.advancements.critereon.RecipeUnlockedTrigger; +import net.minecraft.core.NonNullList; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.data.recipes.RecipeBuilder; +import net.minecraft.data.recipes.RecipeCategory; +import net.minecraft.data.recipes.RecipeOutput; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagKey; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.Ingredient; +import net.minecraft.world.level.ItemLike; +import net.minecraft.world.level.block.state.BlockState; + +import javax.annotation.Nullable; +import java.util.LinkedHashMap; +import java.util.Map; + + +public class GooSpreadRecipeBuilder implements RecipeBuilder { + /*private final RecipeCategory category; + private final Item result; + private final int count; + private final ItemStack resultStack; // Neo: add stack result support */ + + @Nullable + private String group; + + private final ResourceLocation id; + protected final BlockState input; + protected final BlockState output; + private final NonNullList ingredients = NonNullList.create(); + private final Map> criteria = new LinkedHashMap<>(); + + public GooSpreadRecipeBuilder(ResourceLocation id, BlockState input, BlockState output) { + this.id = id; + this.input = input; + this.output = output; + } + + public static GooSpreadRecipeBuilder shapeless(ResourceLocation id, BlockState input, BlockState output) { + return new GooSpreadRecipeBuilder(id, input, output); + } + + public GooSpreadRecipeBuilder requires(TagKey pTag) { + return this.requires(Ingredient.of(pTag)); + } + + public GooSpreadRecipeBuilder requires(ItemLike pItem) { + return this.requires(pItem, 1); + } + + public GooSpreadRecipeBuilder requires(ItemLike pItem, int pQuantity) { + for (int i = 0; i < pQuantity; ++i) { + this.requires(Ingredient.of(pItem)); + } + + return this; + } + + public GooSpreadRecipeBuilder requires(Ingredient pIngredient) { + return this.requires(pIngredient, 1); + } + + public GooSpreadRecipeBuilder requires(Ingredient pIngredient, int pQuantity) { + for (int i = 0; i < pQuantity; ++i) { + this.ingredients.add(pIngredient); + } + + return this; + } + + public GooSpreadRecipeBuilder unlockedBy(String pName, Criterion pCriterion) { + this.criteria.put(pName, pCriterion); + return this; + } + + public GooSpreadRecipeBuilder group(@Nullable String pGroupName) { + this.group = pGroupName; + return this; + } + + @Override + public Item getResult() { + return ItemStack.EMPTY.getItem(); + } + + public void save(RecipeOutput pRecipeOutput) { + //this.save(pRecipeOutput, new ResourceLocation(JustDireThings.MODID, this.output.getBlock(). + "-goorecipe")); + this.save(pRecipeOutput, new ResourceLocation(JustDireThings.MODID, BuiltInRegistries.BLOCK.getKey(this.output.getBlock()).getPath() + "-goospread")); + } + + @Override + public void save(RecipeOutput pRecipeOutput, ResourceLocation pId) { + this.ensureValid(pId); + Advancement.Builder advancement$builder = pRecipeOutput.advancement() + .addCriterion("has_the_recipe", RecipeUnlockedTrigger.unlocked(pId)) + .rewards(AdvancementRewards.Builder.recipe(pId)) + .requirements(AdvancementRequirements.Strategy.OR); + this.criteria.forEach(advancement$builder::addCriterion); + GooSpreadRecipe shapelessrecipe = new GooSpreadRecipe( + this.id, + this.input, + this.output + ); + pRecipeOutput.accept(pId, shapelessrecipe, advancement$builder.build(pId.withPrefix("recipes/" + RecipeCategory.MISC.getFolderName() + "/"))); + } + + private void ensureValid(ResourceLocation pId) { + if (this.criteria.isEmpty()) { + throw new IllegalStateException("No way of obtaining recipe " + pId); + } + } +} diff --git a/src/main/java/com/direwolf20/justdirethings/setup/Registration.java b/src/main/java/com/direwolf20/justdirethings/setup/Registration.java index dc344ab9..3a5b0fda 100644 --- a/src/main/java/com/direwolf20/justdirethings/setup/Registration.java +++ b/src/main/java/com/direwolf20/justdirethings/setup/Registration.java @@ -1,15 +1,20 @@ package com.direwolf20.justdirethings.setup; +import com.direwolf20.justdirethings.JustDireThings; import com.direwolf20.justdirethings.common.blockentities.gooblocks.GooBlockBE_Tier1; import com.direwolf20.justdirethings.common.blocks.gooblocks.GooBlock_Tier1; import com.direwolf20.justdirethings.common.containers.FuelCanisterContainer; import com.direwolf20.justdirethings.common.containers.PocketGeneratorContainer; import com.direwolf20.justdirethings.common.items.FuelCanister; import com.direwolf20.justdirethings.common.items.PocketGenerator; +import com.direwolf20.justdirethings.datagen.recipes.GooSpreadRecipe; import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.MenuType; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; +import net.minecraft.world.item.crafting.RecipeSerializer; +import net.minecraft.world.item.crafting.RecipeType; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.entity.BlockEntityType; import net.neoforged.bus.api.IEventBus; @@ -32,12 +37,21 @@ public class Registration { private static final DeferredRegister> CONTAINERS = DeferredRegister.create(Registries.MENU, MODID); private static final DeferredRegister> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.Keys.ATTACHMENT_TYPES, MODID); + public static final DeferredRegister> RECIPE_TYPES = DeferredRegister.create(Registries.RECIPE_TYPE, MODID); + //public static final Supplier> GOO_SPREAD_RECIPE_TYPE = RECIPE_TYPES.register("goospreadrecipe", () -> RecipeType.register("goospreadrecipe")); + public static final Supplier> GOO_SPREAD_RECIPE_TYPE = RECIPE_TYPES.register("goospreadrecipe", () -> RecipeType.simple(new ResourceLocation(MODID, "goospreadrecipe"))); + + public static final DeferredRegister> RECIPE_SERIALIZERS = DeferredRegister.create(Registries.RECIPE_SERIALIZER, JustDireThings.MODID); + public static final Supplier GOO_SPREAD_RECIPE_SERIALIZER = RECIPE_SERIALIZERS.register("goospread", GooSpreadRecipe.Serializer::new); + public static void init(IEventBus eventBus) { BLOCKS.register(eventBus); ITEMS.register(eventBus); BLOCK_ENTITIES.register(eventBus); CONTAINERS.register(eventBus); ATTACHMENT_TYPES.register(eventBus); + RECIPE_SERIALIZERS.register(eventBus); + RECIPE_TYPES.register(eventBus); } //Blocks @@ -45,7 +59,7 @@ public static void init(IEventBus eventBus) { public static final DeferredHolder GooBlock_Tier1_ITEM = ITEMS.register("gooblock_tier1", () -> new BlockItem(GooBlock_Tier1.get(), new Item.Properties())); //BlockEntities (Not TileEntities - Honest) - public static final DeferredHolder, BlockEntityType> GooBlockBE_Tier1 = BLOCK_ENTITIES.register("gooblock_tier1", () -> BlockEntityType.Builder.of(GooBlockBE_Tier1::new, GooBlock_Tier1.get()).build(null)); + public static final DeferredHolder, BlockEntityType> GooBlockBE_Tier1 = BLOCK_ENTITIES.register("gooblock_tier1", () -> BlockEntityType.Builder.of(GooBlockBE_Tier1::new, GooBlock_Tier1.get()).build(null)); //Items