/* * This file is part of ViaFabricPlus - https://github.com/FlorianMichael/ViaFabricPlus * Copyright (C) 2021-2024 FlorianMichael/EnZaXD and RK_01/RaphiMC * Copyright (C) 2023-2024 contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package de.florianmichael.viafabricplus.fixes.data.recipe; import net.minecraft.item.ItemConvertible; import net.minecraft.item.ItemStack; import net.minecraft.recipe.*; import net.minecraft.recipe.book.CookingRecipeCategory; import net.minecraft.recipe.book.CraftingRecipeCategory; import net.minecraft.util.Identifier; import net.minecraft.util.collection.DefaultedList; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Optional; import java.util.function.Supplier; public final class RecipeInfo> { private final Supplier> creator; private final RecipeSerializer recipeType; private final ItemStack output; private String distinguisher = ""; private RecipeInfo(Supplier> creator, RecipeSerializer recipeType, ItemStack output) { this.creator = creator; this.recipeType = recipeType; this.output = output; } public static > RecipeInfo of(Supplier> creator, RecipeSerializer recipeType, ItemStack output) { return new RecipeInfo<>(creator, recipeType, output); } public static > RecipeInfo of(Supplier> creator, RecipeSerializer recipeType, ItemConvertible output) { return of(creator, recipeType, new ItemStack(output)); } public static > RecipeInfo of(Supplier> creator, RecipeSerializer recipeType, ItemConvertible output, int count) { return of(creator, recipeType, new ItemStack(output, count)); } public static RecipeInfo shaped(ItemStack output, Object... args) { return shaped("", output, args); } public static RecipeInfo shaped(ItemConvertible output, Object... args) { return shaped("", output, args); } public static RecipeInfo shaped(int count, ItemConvertible output, Object... args) { return shaped("", count, output, args); } public static RecipeInfo shaped(String group, ItemStack output, Object... args) { int i; int width = 0; List shape = new ArrayList<>(); for (i = 0; i < args.length && args[i] instanceof String str; i++) { if (i == 0) width = str.length(); else if (str.length() != width) throw new IllegalArgumentException("Rows do not have consistent width"); shape.add(str); } var legend = new HashMap(); while (i < args.length && args[i] instanceof Character key) { i++; List items = new ArrayList<>(); for (; i < args.length && args[i] instanceof ItemConvertible; i++) { items.add((ItemConvertible) args[i]); } legend.put(key, Ingredient.ofItems(items.toArray(new ItemConvertible[0]))); } if (i != args.length) throw new IllegalArgumentException("Unexpected argument at index " + i + ": " + args[i]); int height = shape.size(); DefaultedList ingredients = DefaultedList.of(); for (String row : shape) { for (int x = 0; x < width; x++) { char key = row.charAt(x); Ingredient ingredient = legend.get(key); if (ingredient == null) { if (key == ' ') ingredient = Ingredient.EMPTY; else throw new IllegalArgumentException("Unknown character in shape: " + key); } ingredients.add(ingredient); } } final int width_f = width; return new RecipeInfo<>(() -> new ShapedRecipe(group, CraftingRecipeCategory.MISC, new RawShapedRecipe(width_f, height, ingredients, Optional.empty()), output, false), RecipeSerializer.SHAPED, output); } public static RecipeInfo shaped(String group, ItemConvertible output, Object... args) { return shaped(group, new ItemStack(output), args); } public static RecipeInfo shaped(String group, int count, ItemConvertible output, Object... args) { return shaped(group, new ItemStack(output, count), args); } public static RecipeInfo shapeless(String group, ItemStack output, ItemConvertible... inputs) { ItemConvertible[][] newInputs = new ItemConvertible[inputs.length][1]; for (int i = 0; i < inputs.length; i++) newInputs[i] = new ItemConvertible[]{inputs[i]}; return shapeless(group, output, newInputs); } public static RecipeInfo shapeless(String group, ItemConvertible output, ItemConvertible... inputs) { return shapeless(group, new ItemStack(output), inputs); } public static RecipeInfo shapeless(String group, int count, ItemConvertible output, ItemConvertible... inputs) { return shapeless(group, new ItemStack(output, count), inputs); } public static RecipeInfo shapeless(String group, ItemStack output, ItemConvertible[]... inputs) { DefaultedList ingredients = DefaultedList.of(); for (ItemConvertible[] input : inputs) { ingredients.add(Ingredient.ofItems(input)); } return new RecipeInfo<>(() -> new ShapelessRecipe(group, CraftingRecipeCategory.MISC, output, ingredients), RecipeSerializer.SHAPELESS, output); } public static RecipeInfo shapeless(String group, ItemConvertible output, ItemConvertible[]... inputs) { return shapeless(group, new ItemStack(output), inputs); } public static RecipeInfo shapeless(String group, int count, ItemConvertible output, ItemConvertible[]... inputs) { return shapeless(group, new ItemStack(output, count), inputs); } public static RecipeInfo shapeless(ItemStack output, ItemConvertible... inputs) { return shapeless("", output, inputs); } public static RecipeInfo shapeless(ItemConvertible output, ItemConvertible... inputs) { return shapeless("", output, inputs); } public static RecipeInfo shapeless(int count, ItemConvertible output, ItemConvertible... inputs) { return shapeless("", count, output, inputs); } public static RecipeInfo shapeless(ItemStack output, ItemConvertible[]... inputs) { return shapeless("", output, inputs); } public static RecipeInfo shapeless(ItemConvertible output, ItemConvertible[]... inputs) { return shapeless("", output, inputs); } public static RecipeInfo shapeless(int count, ItemConvertible output, ItemConvertible[]... inputs) { return shapeless("", count, output, inputs); } public static RecipeInfo smelting(ItemConvertible output, ItemConvertible input, float experience) { return smelting(output, input, experience, 200); } public static RecipeInfo smelting(ItemConvertible output, Ingredient input, float experience) { return smelting(output, input, experience, 200); } public static RecipeInfo smelting(ItemConvertible output, ItemConvertible input, float experience, int cookTime) { return smelting(output, Ingredient.ofItems(input), experience, cookTime); } public static RecipeInfo smelting(ItemConvertible output, Ingredient input, float experience, int cookTime) { ItemStack outputStack = new ItemStack(output); return new RecipeInfo<>(() -> new SmeltingRecipe("", CookingRecipeCategory.MISC, input, outputStack, experience, cookTime), RecipeSerializer.SMELTING, outputStack); } public RecipeInfo distinguisher(String distinguisher) { this.distinguisher = distinguisher; return this; } public RecipeEntry create(Identifier id) { return new RecipeEntry>(id, this.creator.get()); } public RecipeSerializer getRecipeType() { return this.recipeType; } public ItemStack getOutput() { return this.output; } public String getDistinguisher() { return this.distinguisher; } }