Skip to content

Commit aa2d464

Browse files
committed
custom recipe types
1 parent 8d19d2c commit aa2d464

File tree

8 files changed

+173
-5
lines changed

8 files changed

+173
-5
lines changed

src/main/java/net/kaupenjoe/tutorialmod/TutorialMod.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import net.kaupenjoe.tutorialmod.item.ModItems;
2020
import net.kaupenjoe.tutorialmod.particle.ModParticles;
2121
import net.kaupenjoe.tutorialmod.potion.ModPotions;
22+
import net.kaupenjoe.tutorialmod.recipe.ModRecipes;
2223
import net.kaupenjoe.tutorialmod.screen.ModScreenHandlers;
2324
import net.kaupenjoe.tutorialmod.sound.ModSounds;
2425
import net.kaupenjoe.tutorialmod.util.HammerUsageEvent;
@@ -69,6 +70,8 @@ public void onInitialize() {
6970
ModBlockEntities.registerBlockEntities();
7071
ModScreenHandlers.registerScreenHandlers();
7172

73+
ModRecipes.registerRecipes();
74+
7275
FuelRegistry.INSTANCE.add(ModItems.STARLIGHT_ASHES, 600);
7376

7477
PlayerBlockBreakEvents.BEFORE.register(new HammerUsageEvent());

src/main/java/net/kaupenjoe/tutorialmod/block/entity/custom/GrowthChamberBlockEntity.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import net.kaupenjoe.tutorialmod.block.entity.ImplementedInventory;
55
import net.kaupenjoe.tutorialmod.block.entity.ModBlockEntities;
66
import net.kaupenjoe.tutorialmod.item.ModItems;
7+
import net.kaupenjoe.tutorialmod.recipe.GrowthChamberRecipe;
8+
import net.kaupenjoe.tutorialmod.recipe.GrowthChamberRecipeInput;
9+
import net.kaupenjoe.tutorialmod.recipe.ModRecipes;
710
import net.kaupenjoe.tutorialmod.screen.custom.GrowthChamberScreenHandler;
811
import net.minecraft.block.BlockState;
912
import net.minecraft.block.entity.BlockEntity;
@@ -16,6 +19,7 @@
1619
import net.minecraft.network.listener.ClientPlayPacketListener;
1720
import net.minecraft.network.packet.Packet;
1821
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
22+
import net.minecraft.recipe.RecipeEntry;
1923
import net.minecraft.registry.RegistryWrapper;
2024
import net.minecraft.screen.PropertyDelegate;
2125
import net.minecraft.screen.ScreenHandler;
@@ -26,6 +30,8 @@
2630
import net.minecraft.world.World;
2731
import org.jetbrains.annotations.Nullable;
2832

33+
import java.util.Optional;
34+
2935
public class GrowthChamberBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory<BlockPos>, ImplementedInventory {
3036
private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(2, ItemStack.EMPTY);
3137

@@ -120,8 +126,9 @@ private void resetProgress() {
120126
}
121127

122128
private void craftItem() {
123-
ItemStack output = new ItemStack(ModItems.PINK_GARNET, 6);
129+
Optional<RecipeEntry<GrowthChamberRecipe>> recipe = getCurrentRecipe();
124130

131+
ItemStack output = recipe.get().value().output();
125132
this.removeStack(INPUT_SLOT, 1);
126133
this.setStack(OUTPUT_SLOT, new ItemStack(output.getItem(),
127134
this.getStack(OUTPUT_SLOT).getCount() + output.getCount()));
@@ -136,11 +143,18 @@ private void increaseCraftingProgress() {
136143
}
137144

138145
private boolean hasRecipe() {
139-
Item input = ModItems.RAW_PINK_GARNET;
140-
ItemStack output = new ItemStack(ModItems.PINK_GARNET, 6);
146+
Optional<RecipeEntry<GrowthChamberRecipe>> recipe = getCurrentRecipe();
147+
if(recipe.isEmpty()) {
148+
return false;
149+
}
150+
151+
ItemStack output = recipe.get().value().output();
152+
return canInsertAmountIntoOutputSlot(output.getCount()) && canInsertItemIntoOutputSlot(output);
153+
}
141154

142-
return this.getStack(INPUT_SLOT).isOf(input) &&
143-
canInsertAmountIntoOutputSlot(output.getCount()) && canInsertItemIntoOutputSlot(output);
155+
private Optional<RecipeEntry<GrowthChamberRecipe>> getCurrentRecipe() {
156+
return this.getWorld().getRecipeManager()
157+
.getFirstMatch(ModRecipes.GROWTH_CHAMBER_TYPE, new GrowthChamberRecipeInput(inventory.get(INPUT_SLOT)), this.getWorld());
144158
}
145159

146160
private boolean canInsertItemIntoOutputSlot(ItemStack output) {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package net.kaupenjoe.tutorialmod.recipe;
2+
3+
import com.mojang.serialization.MapCodec;
4+
import com.mojang.serialization.codecs.RecordCodecBuilder;
5+
import net.minecraft.item.ItemStack;
6+
import net.minecraft.network.RegistryByteBuf;
7+
import net.minecraft.network.codec.PacketCodec;
8+
import net.minecraft.recipe.Ingredient;
9+
import net.minecraft.recipe.Recipe;
10+
import net.minecraft.recipe.RecipeSerializer;
11+
import net.minecraft.recipe.RecipeType;
12+
import net.minecraft.registry.RegistryWrapper;
13+
import net.minecraft.util.collection.DefaultedList;
14+
import net.minecraft.world.World;
15+
16+
public record GrowthChamberRecipe(Ingredient inputItem, ItemStack output) implements Recipe<GrowthChamberRecipeInput> {
17+
@Override
18+
public DefaultedList<Ingredient> getIngredients() {
19+
DefaultedList<Ingredient> list = DefaultedList.of();
20+
list.add(this.inputItem);
21+
return list;
22+
}
23+
24+
// read Recipe JSON files --> new GrowthChamberRecipe
25+
26+
@Override
27+
public boolean matches(GrowthChamberRecipeInput input, World world) {
28+
if(world.isClient()) {
29+
return false;
30+
}
31+
32+
return inputItem.test(input.getStackInSlot(0));
33+
}
34+
35+
@Override
36+
public ItemStack craft(GrowthChamberRecipeInput input, RegistryWrapper.WrapperLookup lookup) {
37+
return output.copy();
38+
}
39+
40+
@Override
41+
public boolean fits(int width, int height) {
42+
return true;
43+
}
44+
45+
@Override
46+
public ItemStack getResult(RegistryWrapper.WrapperLookup registriesLookup) {
47+
return output;
48+
}
49+
50+
@Override
51+
public RecipeSerializer<?> getSerializer() {
52+
return ModRecipes.GROWTH_CHAMBER_SERIALIZER;
53+
}
54+
55+
@Override
56+
public RecipeType<?> getType() {
57+
return ModRecipes.GROWTH_CHAMBER_TYPE;
58+
}
59+
60+
public static class Serializer implements RecipeSerializer<GrowthChamberRecipe> {
61+
public static final MapCodec<GrowthChamberRecipe> CODEC = RecordCodecBuilder.mapCodec(inst -> inst.group(
62+
Ingredient.DISALLOW_EMPTY_CODEC.fieldOf("ingredient").forGetter(GrowthChamberRecipe::inputItem),
63+
ItemStack.CODEC.fieldOf("result").forGetter(GrowthChamberRecipe::output)
64+
).apply(inst, GrowthChamberRecipe::new));
65+
66+
public static final PacketCodec<RegistryByteBuf, GrowthChamberRecipe> STREAM_CODEC =
67+
PacketCodec.tuple(
68+
Ingredient.PACKET_CODEC, GrowthChamberRecipe::inputItem,
69+
ItemStack.PACKET_CODEC, GrowthChamberRecipe::output,
70+
GrowthChamberRecipe::new);
71+
72+
@Override
73+
public MapCodec<GrowthChamberRecipe> codec() {
74+
return CODEC;
75+
}
76+
77+
@Override
78+
public PacketCodec<RegistryByteBuf, GrowthChamberRecipe> packetCodec() {
79+
return STREAM_CODEC;
80+
}
81+
}
82+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.kaupenjoe.tutorialmod.recipe;
2+
3+
import net.minecraft.item.ItemStack;
4+
import net.minecraft.recipe.input.RecipeInput;
5+
6+
public record GrowthChamberRecipeInput(ItemStack input) implements RecipeInput {
7+
@Override
8+
public ItemStack getStackInSlot(int slot) {
9+
return input;
10+
}
11+
12+
@Override
13+
public int getSize() {
14+
return 1;
15+
}
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.kaupenjoe.tutorialmod.recipe;
2+
3+
import net.kaupenjoe.tutorialmod.TutorialMod;
4+
import net.minecraft.recipe.RecipeSerializer;
5+
import net.minecraft.recipe.RecipeType;
6+
import net.minecraft.registry.Registries;
7+
import net.minecraft.registry.Registry;
8+
import net.minecraft.util.Identifier;
9+
10+
public class ModRecipes {
11+
public static final RecipeSerializer<GrowthChamberRecipe> GROWTH_CHAMBER_SERIALIZER = Registry.register(
12+
Registries.RECIPE_SERIALIZER, Identifier.of(TutorialMod.MOD_ID, "growth_chamber"),
13+
new GrowthChamberRecipe.Serializer());
14+
public static final RecipeType<GrowthChamberRecipe> GROWTH_CHAMBER_TYPE = Registry.register(
15+
Registries.RECIPE_TYPE, Identifier.of(TutorialMod.MOD_ID, "growth_chamber"), new RecipeType<GrowthChamberRecipe>() {
16+
@Override
17+
public String toString() {
18+
return "growth_chamber";
19+
}
20+
});
21+
22+
public static void registerRecipes() {
23+
TutorialMod.LOGGER.info("Registering Custom Recipes for " + TutorialMod.MOD_ID);
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "tutorialmod:growth_chamber",
3+
"ingredient": {
4+
"item": "minecraft:feather"
5+
},
6+
"result": {
7+
"id": "minecraft:coal"
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "tutorialmod:growth_chamber",
3+
"ingredient": {
4+
"item": "minecraft:stick"
5+
},
6+
"result": {
7+
"id": "minecraft:end_rod"
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"type": "tutorialmod:growth_chamber",
3+
"ingredient": {
4+
"item": "tutorialmod:raw_pink_garnet"
5+
},
6+
"result": {
7+
"count": 16,
8+
"id": "tutorialmod:pink_garnet"
9+
}
10+
}

0 commit comments

Comments
 (0)