summaryrefslogtreecommitdiff
path: root/src/main/java/lv/enes/mc/eris_alchemy/CovalenceRepair.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/lv/enes/mc/eris_alchemy/CovalenceRepair.java')
-rw-r--r--src/main/java/lv/enes/mc/eris_alchemy/CovalenceRepair.java167
1 files changed, 167 insertions, 0 deletions
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/CovalenceRepair.java b/src/main/java/lv/enes/mc/eris_alchemy/CovalenceRepair.java
new file mode 100644
index 0000000..2e9da77
--- /dev/null
+++ b/src/main/java/lv/enes/mc/eris_alchemy/CovalenceRepair.java
@@ -0,0 +1,167 @@
1package lv.enes.mc.eris_alchemy;
2
3import com.google.gson.Gson;
4import com.google.gson.JsonElement;
5import com.google.gson.JsonObject;
6import com.google.gson.JsonSyntaxException;
7import net.minecraft.core.RegistryAccess;
8import net.minecraft.network.FriendlyByteBuf;
9import net.minecraft.resources.ResourceLocation;
10import net.minecraft.world.inventory.CraftingContainer;
11import net.minecraft.world.item.ItemStack;
12import net.minecraft.world.item.crafting.CraftingBookCategory;
13import net.minecraft.world.item.crafting.CustomRecipe;
14import net.minecraft.world.item.crafting.Ingredient;
15import net.minecraft.world.item.crafting.RecipeSerializer;
16import net.minecraft.world.level.Level;
17import org.quiltmc.qsl.recipe.api.serializer.QuiltRecipeSerializer;
18
19import java.util.ArrayList;
20import java.util.Arrays;
21
22public class CovalenceRepair extends CustomRecipe {
23 static class Serializer implements QuiltRecipeSerializer<CovalenceRepair> {
24 private static class Json {
25 CraftingBookCategory category = CraftingBookCategory.MISC;
26 JsonElement dust;
27 JsonElement materials = new JsonObject();
28 JsonElement tools = new JsonObject();
29 }
30
31 private Serializer() {}
32
33 public static final Serializer INSTANCE = new Serializer();
34 public static final ResourceLocation ID = new ResourceLocation(ErisAlchemy.ID, "covalence_repair");
35
36 @Override
37 public JsonObject toJson(CovalenceRepair recipe) {
38 var res = new JsonObject();
39 res.addProperty("category", recipe.category().toString());
40 res.add("dust", recipe.dust.toJson());
41 res.add("materials", recipe.materials.toJson());
42 res.add("tools", recipe.tools.toJson());
43 return res;
44 }
45
46 @Override
47 public CovalenceRepair fromJson(ResourceLocation id, JsonObject json) {
48 var recipeJson = new Gson().fromJson(json, Json.class);
49 if (recipeJson.dust == null) {
50 throw new JsonSyntaxException("A required attribute is missing");
51 }
52
53 var dust = Ingredient.fromJson(recipeJson.dust);
54 var materials = Ingredient.fromJson(recipeJson.materials);
55 var tools = Ingredient.fromJson(recipeJson.tools);
56
57 return new CovalenceRepair(id, recipeJson.category, dust, materials, tools);
58 }
59
60 @Override
61 public void toNetwork(FriendlyByteBuf buf, CovalenceRepair recipe) {
62 buf.writeEnum(recipe.category());
63 recipe.dust.toNetwork(buf);
64 recipe.materials.toNetwork(buf);
65 recipe.tools.toNetwork(buf);
66 }
67
68 @Override
69 public CovalenceRepair fromNetwork(ResourceLocation id, FriendlyByteBuf buf) {
70 var category = buf.readEnum(CraftingBookCategory.class);
71 var dust = Ingredient.fromNetwork(buf);
72 var materials = Ingredient.fromNetwork(buf);
73 var tools = Ingredient.fromNetwork(buf);
74 return new CovalenceRepair(id, category, dust, materials, tools);
75 }
76 }
77
78 private record Inputs(ItemStack toolStack, int dustCount) {}
79
80 private final static int DUSTS_TO_FIX = 8;
81
82 /** What dust do we use to repair. */
83 private final Ingredient dust;
84 /** What materials this dust can repair. */
85 private final Ingredient materials;
86 /** What tools can this dust repair. */
87 private final Ingredient tools;
88
89 public CovalenceRepair(ResourceLocation id, CraftingBookCategory category, Ingredient dust, Ingredient materials, Ingredient tools) {
90 super(id, category);
91
92 this.dust = dust;
93 this.materials = materials;
94 this.tools = tools;
95 }
96
97 @Override
98 public boolean canCraftInDimensions(int width, int height) {
99 return width * height > 2;
100 }
101
102 @Override
103 public boolean matches(CraftingContainer inventory, Level world) {
104 return getInputs(inventory) != null;
105 }
106
107 @Override
108 public ItemStack assemble(CraftingContainer inventory, RegistryAccess registryManager) {
109 var inputs = getInputs(inventory);
110 if (inputs == null) {
111 return ItemStack.EMPTY;
112 }
113
114 var newToolStack = inputs.toolStack.copy();
115 var repairedAmount = inputs.toolStack.getItem().getMaxDamage() * inputs.dustCount / DUSTS_TO_FIX;
116 newToolStack.setDamageValue(inputs.toolStack.getDamageValue() - repairedAmount);
117 return newToolStack;
118 }
119
120 @Override
121 public RecipeSerializer<?> getSerializer() {
122 return Serializer.INSTANCE;
123 }
124
125 private boolean isTool(ItemStack stack) {
126 if (!stack.isDamageableItem() || !stack.isDamaged() || stack.getCount() != 1) {
127 return false;
128 }
129
130 if (tools.test(stack)) {
131 return true;
132 }
133
134 var item = stack.getItem();
135 return Arrays.stream(materials.getItems()).anyMatch(material -> item.isValidRepairItem(stack, material));
136 }
137
138 private boolean isDust(ItemStack stack) {
139 return dust.test(stack);
140 }
141
142 /** @return null if recipe isn't correct. */
143 private Inputs getInputs(CraftingContainer inventory) {
144 ItemStack toolStack = null;
145 var dustStacks = new ArrayList<ItemStack>();
146 for (var i = 0; i < inventory.getContainerSize(); i++) {
147 var stack = inventory.getItem(i);
148 if (stack.isEmpty()) {
149 continue;
150 }
151
152 if (isDust(stack) && (dustStacks.isEmpty() || ItemStack.isSameItemSameTags(dustStacks.get(0), stack))) {
153 dustStacks.add(stack);
154 } else if (toolStack == null) {
155 toolStack = stack;
156 } else {
157 return null;
158 }
159 }
160
161 if (toolStack == null || dustStacks.isEmpty() || !isTool(toolStack)) {
162 return null;
163 }
164
165 return new Inputs(toolStack, dustStacks.size());
166 }
167}