Skip to content

Commit

Permalink
Custom Recipe Type
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Feb 23, 2024
1 parent 8d5b169 commit 0e735d5
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "justdirethings:goospread",
"id": "justdirethings:goospread1",
"input": {
"Name": "minecraft:iron_block"
},
"output": {
"Name": "justdirethings:gooblock_tier1"
}
}
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/direwolf20/justdirethings/datagen/Recipes.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
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;
import net.minecraft.data.recipes.RecipeCategory;
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 {
Expand Down Expand Up @@ -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);

}
}
Original file line number Diff line number Diff line change
@@ -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<Ingredient> ingredients = NonNullList.create();
private final Map<String, Criterion<?>> 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<Item> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -32,20 +37,29 @@ public class Registration {
private static final DeferredRegister<MenuType<?>> CONTAINERS = DeferredRegister.create(Registries.MENU, MODID);
private static final DeferredRegister<AttachmentType<?>> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.Keys.ATTACHMENT_TYPES, MODID);

public static final DeferredRegister<RecipeType<?>> RECIPE_TYPES = DeferredRegister.create(Registries.RECIPE_TYPE, MODID);
//public static final Supplier<RecipeType<GooSpreadRecipe>> GOO_SPREAD_RECIPE_TYPE = RECIPE_TYPES.register("goospreadrecipe", () -> RecipeType.register("goospreadrecipe"));
public static final Supplier<RecipeType<GooSpreadRecipe>> GOO_SPREAD_RECIPE_TYPE = RECIPE_TYPES.register("goospreadrecipe", () -> RecipeType.simple(new ResourceLocation(MODID, "goospreadrecipe")));

public static final DeferredRegister<RecipeSerializer<?>> RECIPE_SERIALIZERS = DeferredRegister.create(Registries.RECIPE_SERIALIZER, JustDireThings.MODID);
public static final Supplier<GooSpreadRecipe.Serializer> 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
public static final DeferredHolder<Block, GooBlock_Tier1> GooBlock_Tier1 = BLOCKS.register("gooblock_tier1", GooBlock_Tier1::new);
public static final DeferredHolder<Item, BlockItem> GooBlock_Tier1_ITEM = ITEMS.register("gooblock_tier1", () -> new BlockItem(GooBlock_Tier1.get(), new Item.Properties()));

//BlockEntities (Not TileEntities - Honest)
public static final DeferredHolder<BlockEntityType<?>, BlockEntityType<GooBlockBE_Tier1>> GooBlockBE_Tier1 = BLOCK_ENTITIES.register("gooblock_tier1", () -> BlockEntityType.Builder.of(GooBlockBE_Tier1::new, GooBlock_Tier1.get()).build(null));
public static final DeferredHolder<BlockEntityType<?>, BlockEntityType<com.direwolf20.justdirethings.common.blockentities.gooblocks.GooBlockBE_Tier1>> GooBlockBE_Tier1 = BLOCK_ENTITIES.register("gooblock_tier1", () -> BlockEntityType.Builder.of(GooBlockBE_Tier1::new, GooBlock_Tier1.get()).build(null));


//Items
Expand Down

0 comments on commit 0e735d5

Please sign in to comment.