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