diff options
Diffstat (limited to 'src/main/java/lv/enes/mc/eris_alchemy/recipe/CovalenceRepair.java')
| -rw-r--r-- | src/main/java/lv/enes/mc/eris_alchemy/recipe/CovalenceRepair.java | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/recipe/CovalenceRepair.java b/src/main/java/lv/enes/mc/eris_alchemy/recipe/CovalenceRepair.java new file mode 100644 index 0000000..dca464a --- /dev/null +++ b/src/main/java/lv/enes/mc/eris_alchemy/recipe/CovalenceRepair.java | |||
| @@ -0,0 +1,169 @@ | |||
| 1 | package lv.enes.mc.eris_alchemy.recipe; | ||
| 2 | |||
| 3 | import com.google.gson.Gson; | ||
| 4 | import com.google.gson.JsonElement; | ||
| 5 | import com.google.gson.JsonObject; | ||
| 6 | import com.google.gson.JsonSyntaxException; | ||
| 7 | import jakarta.annotation.Nonnull; | ||
| 8 | import net.minecraft.core.RegistryAccess; | ||
| 9 | import net.minecraft.network.FriendlyByteBuf; | ||
| 10 | import net.minecraft.resources.ResourceLocation; | ||
| 11 | import net.minecraft.world.inventory.CraftingContainer; | ||
| 12 | import net.minecraft.world.item.ItemStack; | ||
| 13 | import net.minecraft.world.item.crafting.CraftingBookCategory; | ||
| 14 | import net.minecraft.world.item.crafting.CustomRecipe; | ||
| 15 | import net.minecraft.world.item.crafting.Ingredient; | ||
| 16 | import net.minecraft.world.item.crafting.RecipeSerializer; | ||
| 17 | import net.minecraft.world.level.Level; | ||
| 18 | import org.quiltmc.qsl.recipe.api.serializer.QuiltRecipeSerializer; | ||
| 19 | |||
| 20 | import java.util.ArrayList; | ||
| 21 | import java.util.Arrays; | ||
| 22 | |||
| 23 | public 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 | Serializer() {} | ||
| 33 | |||
| 34 | @Override | ||
| 35 | public JsonObject toJson(CovalenceRepair recipe) { | ||
| 36 | var res = new JsonObject(); | ||
| 37 | res.addProperty("category", recipe.category().toString()); | ||
| 38 | res.add("dust", recipe.dust.toJson()); | ||
| 39 | res.add("materials", recipe.materials.toJson()); | ||
| 40 | res.add("tools", recipe.tools.toJson()); | ||
| 41 | return res; | ||
| 42 | } | ||
| 43 | |||
| 44 | @Nonnull | ||
| 45 | @Override | ||
| 46 | public CovalenceRepair fromJson(ResourceLocation id, JsonObject json) { | ||
| 47 | var recipeJson = new Gson().fromJson(json, Json.class); | ||
| 48 | if (recipeJson.dust == null) { | ||
| 49 | throw new JsonSyntaxException("A required attribute is missing"); | ||
| 50 | } | ||
| 51 | |||
| 52 | var dust = Ingredient.fromJson(recipeJson.dust); | ||
| 53 | var materials = Ingredient.fromJson(recipeJson.materials); | ||
| 54 | var tools = Ingredient.fromJson(recipeJson.tools); | ||
| 55 | |||
| 56 | return new CovalenceRepair(id, recipeJson.category, dust, materials, tools); | ||
| 57 | } | ||
| 58 | |||
| 59 | @Override | ||
| 60 | public void toNetwork(FriendlyByteBuf buf, CovalenceRepair recipe) { | ||
| 61 | buf.writeEnum(recipe.category()); | ||
| 62 | recipe.dust.toNetwork(buf); | ||
| 63 | recipe.materials.toNetwork(buf); | ||
| 64 | recipe.tools.toNetwork(buf); | ||
| 65 | } | ||
| 66 | |||
| 67 | @Nonnull | ||
| 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 | @Nonnull | ||
| 108 | @Override | ||
| 109 | public ItemStack assemble(CraftingContainer inventory, RegistryAccess registryManager) { | ||
| 110 | var inputs = getInputs(inventory); | ||
| 111 | if (inputs == null) { | ||
| 112 | return ItemStack.EMPTY; | ||
| 113 | } | ||
| 114 | |||
| 115 | var newToolStack = inputs.toolStack.copy(); | ||
| 116 | var repairedAmount = inputs.toolStack.getItem().getMaxDamage() * inputs.dustCount / DUSTS_TO_FIX; | ||
| 117 | newToolStack.setDamageValue(inputs.toolStack.getDamageValue() - repairedAmount); | ||
| 118 | return newToolStack; | ||
| 119 | } | ||
| 120 | |||
| 121 | @Nonnull | ||
| 122 | @Override | ||
| 123 | public RecipeSerializer<CovalenceRepair> getSerializer() { | ||
| 124 | return ErisAlchemyRecipeSerializers.COVALENCE_REPAIR; | ||
| 125 | } | ||
| 126 | |||
| 127 | private boolean isTool(ItemStack stack) { | ||
| 128 | if (!stack.isDamageableItem() || !stack.isDamaged() || stack.getCount() != 1) { | ||
| 129 | return false; | ||
| 130 | } | ||
| 131 | |||
| 132 | if (tools.test(stack)) { | ||
| 133 | return true; | ||
| 134 | } | ||
| 135 | |||
| 136 | var item = stack.getItem(); | ||
| 137 | return Arrays.stream(materials.getItems()).anyMatch(material -> item.isValidRepairItem(stack, material)); | ||
| 138 | } | ||
| 139 | |||
| 140 | private boolean isDust(ItemStack stack) { | ||
| 141 | return dust.test(stack); | ||
| 142 | } | ||
| 143 | |||
| 144 | /** @return null if recipe isn't correct. */ | ||
| 145 | private Inputs getInputs(CraftingContainer inventory) { | ||
| 146 | ItemStack toolStack = null; | ||
| 147 | var dustStacks = new ArrayList<ItemStack>(); | ||
| 148 | for (var i = 0; i < inventory.getContainerSize(); i++) { | ||
| 149 | var stack = inventory.getItem(i); | ||
| 150 | if (stack.isEmpty()) { | ||
| 151 | continue; | ||
| 152 | } | ||
| 153 | |||
| 154 | if (isDust(stack) && (dustStacks.isEmpty() || ItemStack.isSameItemSameTags(dustStacks.get(0), stack))) { | ||
| 155 | dustStacks.add(stack); | ||
| 156 | } else if (toolStack == null) { | ||
| 157 | toolStack = stack; | ||
| 158 | } else { | ||
| 159 | return null; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | if (toolStack == null || dustStacks.isEmpty() || !isTool(toolStack)) { | ||
| 164 | return null; | ||
| 165 | } | ||
| 166 | |||
| 167 | return new Inputs(toolStack, dustStacks.size()); | ||
| 168 | } | ||
| 169 | } | ||