|
1 | 1 | package com.mrh0.createaddition.recipe.charging; |
2 | 2 |
|
| 3 | +import com.mojang.serialization.Codec; |
| 4 | +import com.mojang.serialization.MapCodec; |
| 5 | +import com.mojang.serialization.codecs.RecordCodecBuilder; |
3 | 6 | import com.mrh0.createaddition.CreateAddition; |
4 | 7 | import com.mrh0.createaddition.index.CARecipes; |
5 | 8 | import net.minecraft.core.HolderLookup; |
6 | 9 | import net.minecraft.core.Registry; |
7 | 10 | import net.minecraft.core.RegistryAccess; |
| 11 | +import net.minecraft.network.RegistryFriendlyByteBuf; |
| 12 | +import net.minecraft.network.codec.StreamCodec; |
8 | 13 | import net.minecraft.resources.ResourceLocation; |
9 | 14 | import net.minecraft.world.item.ItemStack; |
10 | | -import net.minecraft.world.item.crafting.Ingredient; |
11 | | -import net.minecraft.world.item.crafting.Recipe; |
12 | | -import net.minecraft.world.item.crafting.RecipeSerializer; |
13 | | -import net.minecraft.world.item.crafting.RecipeType; |
| 15 | +import net.minecraft.world.item.crafting.*; |
14 | 16 | import net.minecraft.world.level.Level; |
15 | 17 | import net.neoforged.neoforge.items.wrapper.RecipeWrapper; |
16 | 18 | import org.jetbrains.annotations.NotNull; |
17 | 19 |
|
18 | | -public class ChargingRecipe implements Recipe<RecipeWrapper> { |
| 20 | +public class ChargingRecipe implements Recipe<CraftingInput> { |
19 | 21 |
|
20 | | - public final ResourceLocation id; |
21 | 22 | public Ingredient ingredient; |
22 | 23 | public ItemStack output; |
23 | 24 | public int energy; |
24 | 25 | public int maxChargeRate; |
25 | 26 |
|
26 | | - public ChargingRecipe(ResourceLocation id, Ingredient ingredient, ItemStack output, int energy, int maxChargeRate) { |
27 | | - this.id = id; |
| 27 | + public ChargingRecipe(String group, Ingredient ingredient, ItemStack output, int energy, int maxChargeRate) { |
28 | 28 | this.ingredient = ingredient; |
29 | 29 | this.output = output; |
30 | 30 | this.energy = energy; |
31 | 31 | this.maxChargeRate = maxChargeRate; |
32 | 32 | } |
33 | 33 |
|
| 34 | + public CraftingBookCategory category() { |
| 35 | + return CraftingBookCategory.MISC; |
| 36 | + } |
34 | 37 |
|
35 | 38 | @Override |
36 | | - public boolean matches(@NotNull RecipeWrapper wrapper, @NotNull Level world) { |
| 39 | + public boolean matches(@NotNull CraftingInput wrapper, @NotNull Level world) { |
37 | 40 | if(ingredient == null) return false; |
38 | 41 | return ingredient.test(wrapper.getItem(0)); |
39 | 42 | } |
40 | 43 |
|
41 | 44 | @Override |
42 | | - public @NotNull ItemStack assemble(@NotNull RecipeWrapper recipeWrapper, HolderLookup.@NotNull Provider provider) { |
| 45 | + public @NotNull ItemStack assemble(@NotNull CraftingInput recipeWrapper, HolderLookup.@NotNull Provider provider) { |
43 | 46 | return output; |
44 | 47 | } |
45 | 48 |
|
@@ -76,4 +79,48 @@ public int getMaxChargeRate() { |
76 | 79 | return maxChargeRate; |
77 | 80 | } |
78 | 81 |
|
| 82 | + public static class Serializer implements RecipeSerializer<ChargingRecipe> { |
| 83 | + private static final MapCodec<ChargingRecipe> CODEC = RecordCodecBuilder.mapCodec( |
| 84 | + builder -> builder.group( |
| 85 | + Codec.STRING.optionalFieldOf("group", "").forGetter(ChargingRecipe::getGroup), |
| 86 | + //CraftingBookCategory.CODEC.fieldOf("category").orElse(CraftingBookCategory.MISC).forGetter(ChargingRecipe::category), |
| 87 | + Ingredient.CODEC.fieldOf("ingredient").forGetter(r -> r.ingredient), |
| 88 | + ItemStack.STRICT_CODEC.fieldOf("result").forGetter(r -> r.output), |
| 89 | + Codec.INT.optionalFieldOf("energy", 0).forGetter(r -> r.energy), |
| 90 | + Codec.INT.optionalFieldOf("max_charge_rate", 0).forGetter(r -> r.maxChargeRate) |
| 91 | + ).apply(builder, ChargingRecipe::new) |
| 92 | + ); |
| 93 | + |
| 94 | + public static final StreamCodec<RegistryFriendlyByteBuf, ChargingRecipe> STREAM_CODEC = StreamCodec.of( |
| 95 | + Serializer::toNetwork, Serializer::fromNetwork |
| 96 | + ); |
| 97 | + |
| 98 | + @Override |
| 99 | + public MapCodec<ChargingRecipe> codec() { |
| 100 | + return CODEC; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public StreamCodec<RegistryFriendlyByteBuf, ChargingRecipe> streamCodec() { |
| 105 | + return STREAM_CODEC; |
| 106 | + } |
| 107 | + |
| 108 | + private static ChargingRecipe fromNetwork(RegistryFriendlyByteBuf buffer) { |
| 109 | + String group = buffer.readUtf(); |
| 110 | + int maxChargeRate = buffer.readInt(); |
| 111 | + int energy = buffer.readInt(); |
| 112 | + ItemStack output = ItemStack.STREAM_CODEC.decode(buffer); |
| 113 | + Ingredient input = Ingredient.CONTENTS_STREAM_CODEC.decode(buffer); |
| 114 | + return new ChargingRecipe(group, input, output, energy, maxChargeRate); |
| 115 | + } |
| 116 | + |
| 117 | + private static void toNetwork(RegistryFriendlyByteBuf buffer, ChargingRecipe recipe) { |
| 118 | + buffer.writeUtf(recipe.getGroup()); |
| 119 | + buffer.writeInt(recipe.maxChargeRate); |
| 120 | + buffer.writeInt(recipe.energy); |
| 121 | + ItemStack.STREAM_CODEC.encode(buffer, recipe.output); |
| 122 | + Ingredient.CONTENTS_STREAM_CODEC.encode(buffer, recipe.ingredient); |
| 123 | + } |
| 124 | + } |
| 125 | + |
79 | 126 | } |
0 commit comments