1 | package net.gamelog.suntrot.recipe;
|
2 |
|
3 | import com.google.gson.Gson;
|
4 | import com.google.gson.JsonObject;
|
5 | import net.minecraft.inventory.Inventory;
|
6 | import net.minecraft.item.Item;
|
7 | import net.minecraft.item.ItemStack;
|
8 | import net.minecraft.network.PacketByteBuf;
|
9 | import net.minecraft.recipe.*;
|
10 | import net.minecraft.registry.DynamicRegistryManager;
|
11 | import net.minecraft.registry.Registries;
|
12 | import net.minecraft.util.Identifier;
|
13 | import net.minecraft.world.World;
|
14 | import org.spongepowered.include.com.google.gson.JsonSyntaxException;
|
15 |
|
16 | public class SewingRecipe implements Recipe<Inventory> {
|
17 |
|
18 | private final Ingredient item;
|
19 | private final Ingredient dye;
|
20 | private final ItemStack result;
|
21 | private final Identifier id;
|
22 |
|
23 | public SewingRecipe(Ingredient item, Ingredient dye, ItemStack result, Identifier id) {
|
24 | this.item = item;
|
25 | this.dye = dye;
|
26 | this.result = result;
|
27 | this.id = id;
|
28 | }
|
29 |
|
30 | public Ingredient getItem() {
|
31 | return item;
|
32 | }
|
33 |
|
34 | public Ingredient getDye() {
|
35 | return dye;
|
36 | }
|
37 |
|
38 | @Override
|
39 | public boolean matches(Inventory inventory, World world) {
|
40 | if (inventory.size() < 2) return false;
|
41 | return item.test(inventory.getStack(0)) && dye.test(inventory.getStack(1));
|
42 | }
|
43 |
|
44 | @Override
|
45 | public ItemStack craft(Inventory inventory, DynamicRegistryManager registryManager) {
|
46 | return ItemStack.EMPTY;
|
47 | }
|
48 |
|
49 | @Override
|
50 | public boolean fits(int var1, int var2) {
|
51 | return false;
|
52 | }
|
53 |
|
54 | @Override
|
55 | public ItemStack getOutput(DynamicRegistryManager registryManager) {
|
56 | return result;
|
57 | }
|
58 |
|
59 | @Override
|
60 | public Identifier getId() {
|
61 | return id;
|
62 | }
|
63 |
|
64 | public static class Type implements RecipeType<SewingRecipe> {
|
65 | // Define ExampleRecipe.Type as a singleton by making its constructor private and exposing an instance.
|
66 | private Type() {}
|
67 | public static final Type INSTANCE = new Type();
|
68 |
|
69 | public static final String ID = "two_slot_recipe";
|
70 | }
|
71 | @Override
|
72 | public RecipeType<?> getType() {
|
73 | return Type.INSTANCE;
|
74 | }
|
75 |
|
76 | class SewingRecipeJsonFormat{
|
77 | JsonObject item;
|
78 | JsonObject dye;
|
79 | String result;
|
80 | int count;
|
81 | }
|
82 |
|
83 | public static class SewingRecipeSerializer implements RecipeSerializer<SewingRecipe> {
|
84 |
|
85 | private SewingRecipeSerializer() {}
|
86 |
|
87 | public static final SewingRecipeSerializer INSTANCE = new SewingRecipeSerializer();
|
88 |
|
89 | // This will be the "type" field in the json
|
90 | public static final Identifier ID = new Identifier("suntrot:sewing_recipe");
|
91 |
|
92 |
|
93 |
|
94 | @Override
|
95 | public SewingRecipe read(Identifier id, JsonObject json) {
|
96 | SewingRecipeJsonFormat recipeJson = new Gson().fromJson(json, SewingRecipeJsonFormat.class);
|
97 | // Validate all fields are there
|
98 | if (recipeJson.item == null || recipeJson.dye == null || recipeJson.result == null) {
|
99 | throw new JsonSyntaxException("A required attribute is missing!");
|
100 | }
|
101 | // We'll allow to not specify the output, and default it to 1.
|
102 | if (recipeJson.count == 0) recipeJson.count = 1;
|
103 |
|
104 | Ingredient inputA = Ingredient.fromJson(recipeJson.item);
|
105 | Ingredient inputB = Ingredient.fromJson(recipeJson.dye);
|
106 | Item outputItem = Registries.ITEM.getOrEmpty(new Identifier(recipeJson.result))
|
107 | // Validate the inputted item actually exists
|
108 | .orElseThrow(() -> new JsonSyntaxException("No such item " + recipeJson.result));
|
109 | ItemStack output = new ItemStack(outputItem, recipeJson.count);
|
110 |
|
111 | return new SewingRecipe(inputA, inputB, output, id);
|
112 | }
|
113 |
|
114 | @Override
|
115 | public SewingRecipe read(Identifier recipeId, PacketByteBuf packetData) {
|
116 | // Make sure the read in the same order you have written!
|
117 | Ingredient inputA = Ingredient.fromPacket(packetData);
|
118 | Ingredient inputB = Ingredient.fromPacket(packetData);
|
119 | ItemStack output = packetData.readItemStack();
|
120 | return new SewingRecipe(inputA, inputB, output, recipeId);
|
121 | }
|
122 |
|
123 | @Override
|
124 | public void write(PacketByteBuf packetData, SewingRecipe recipe) {
|
125 | recipe.getItem().write(packetData);
|
126 | recipe.getDye().write(packetData);
|
127 | packetData.writeItemStack(recipe.getOutput(null));
|
128 | }
|
129 | }
|
130 |
|
131 | @Override
|
132 | public RecipeSerializer<?> getSerializer() {
|
133 | return SewingRecipeSerializer.INSTANCE;
|
134 | }
|
135 | }
|