2017-06-29 18:30:02 +02:00
|
|
|
package de.ellpeck.actuallyadditions.mod.util.crafting;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-08-02 04:20:47 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.misc.IDisableableItem;
|
2018-05-10 11:38:58 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2017-06-29 18:30:02 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.RegistryHandler;
|
|
|
|
import net.minecraft.block.Block;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.item.crafting.IRecipe;
|
|
|
|
import net.minecraft.item.crafting.Ingredient;
|
|
|
|
import net.minecraft.item.crafting.ShapedRecipes;
|
|
|
|
import net.minecraft.item.crafting.ShapelessRecipes;
|
|
|
|
import net.minecraft.util.NonNullList;
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraftforge.common.crafting.CraftingHelper;
|
|
|
|
import net.minecraftforge.common.crafting.CraftingHelper.ShapedPrimer;
|
|
|
|
import net.minecraftforge.oredict.OreIngredient;
|
|
|
|
|
2019-02-27 19:53:05 +01:00
|
|
|
//This class created by Shadows_of_Fire
|
2017-06-29 18:30:02 +02:00
|
|
|
//MIT License
|
2018-08-04 04:22:20 +02:00
|
|
|
public final class RecipeHelper {
|
2017-06-29 18:36:33 +02:00
|
|
|
|
|
|
|
private static int j = 0;
|
2018-05-10 11:38:58 +02:00
|
|
|
private static final String MODID = ActuallyAdditions.MODID;
|
|
|
|
private static final String MODNAME = ActuallyAdditions.NAME;
|
2017-06-29 18:36:33 +02:00
|
|
|
public static final List<IRecipe> RECIPE_LIST = RegistryHandler.RECIPES_TO_REGISTER;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This adds the recipe to the list of crafting recipes. Since who cares about names, it adds it as recipesX, where X is the current recipe you are adding.
|
|
|
|
*/
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addRecipe(int j, IRecipe rec) {
|
|
|
|
addRecipe("recipes" + j, rec);
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This adds the recipe to the list of crafting recipes. Cares about names.
|
|
|
|
*/
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addRecipe(String name, IRecipe rec) {
|
|
|
|
Item i = rec.getRecipeOutput().getItem();
|
|
|
|
if (i instanceof IDisableableItem && ((IDisableableItem) i).isDisabled()) rec = new BlankRecipe();
|
|
|
|
if (rec.getRegistryName() == null) {
|
2017-06-29 18:36:33 +02:00
|
|
|
RECIPE_LIST.add(rec.setRegistryName(new ResourceLocation(MODID, name)));
|
2018-08-04 04:22:20 +02:00
|
|
|
} else {
|
2017-06-29 18:36:33 +02:00
|
|
|
RECIPE_LIST.add(rec);
|
|
|
|
}
|
2017-06-29 21:00:34 +02:00
|
|
|
RecipeHandler.lastRecipe = rec;
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This adds a shaped recipe to the list of crafting recipes, using the forge format.
|
|
|
|
*/
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addOldShaped(ItemStack output, Object... input) {
|
2017-06-29 18:36:33 +02:00
|
|
|
ShapedPrimer primer = CraftingHelper.parseShaped(input);
|
2018-08-04 04:22:20 +02:00
|
|
|
addRecipe(j++, new ShapedRecipes(new ResourceLocation(MODID, "recipes" + j).toString(), primer.width, primer.height, primer.input, output));
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This adds a shaped recipe to the list of crafting recipes, using the forge format, with a custom group.
|
|
|
|
*/
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addOldShaped(String group, ItemStack output, Object... input) {
|
2017-06-29 18:36:33 +02:00
|
|
|
ShapedPrimer primer = CraftingHelper.parseShaped(input);
|
2018-08-04 04:22:20 +02:00
|
|
|
addRecipe(j++, new ShapedRecipes(new ResourceLocation(MODID, group).toString(), primer.width, primer.height, primer.input, output));
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-02-27 19:53:05 +01:00
|
|
|
* This adds a shaped recipe to the list of crafting recipes, using the forge format, with a custom group.
|
|
|
|
*/
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addOldShaped(String name, String group, ItemStack output, Object... input) {
|
2017-06-29 18:36:33 +02:00
|
|
|
ShapedPrimer primer = CraftingHelper.parseShaped(input);
|
2018-08-04 04:22:20 +02:00
|
|
|
addRecipe(j++, new ShapedRecipes(new ResourceLocation(MODID, group).toString(), primer.width, primer.height, primer.input, output).setRegistryName(MODID, name));
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This adds a shapeless recipe to the list of crafting recipes, using the forge format.
|
|
|
|
*/
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addOldShapeless(ItemStack output, Object... input) {
|
|
|
|
addRecipe(j++, new ShapelessRecipes(new ResourceLocation(MODID, "recipes" + j).toString(), output, createInput(input)));
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This adds a shapeless recipe to the list of crafting recipes, using the forge format, with a custom group.
|
|
|
|
*/
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addOldShapeless(String group, ItemStack output, Object... input) {
|
2017-06-29 18:36:33 +02:00
|
|
|
addRecipe(j++, new ShapelessRecipes(new ResourceLocation(MODID, group).toString(), output, createInput(input)));
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addOldShapeless(String name, String group, ItemStack output, Object... input) {
|
|
|
|
addRecipe(j++, new ShapelessRecipes(new ResourceLocation(MODID, group).toString(), output, createInput(input)).setRegistryName(MODID, name));
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adds a shapeless recipe with X output using an array of inputs. Use Strings for OreDictionary support. This array is not ordered.
|
|
|
|
*/
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShapeless(ItemStack output, Object... inputs) {
|
|
|
|
addRecipe(j++, new ShapelessRecipes(MODID + ":" + j, output, createInput(inputs)));
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShapeless(Item output, Object... inputs) {
|
2017-06-29 18:36:33 +02:00
|
|
|
addShapeless(new ItemStack(output), inputs);
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShapeless(Block output, Object... inputs) {
|
2017-06-29 18:36:33 +02:00
|
|
|
addShapeless(new ItemStack(output), inputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adds a shapeless recipe with X output using an array of inputs. Use Strings for OreDictionary support. This array is not ordered. This has a custom group.
|
|
|
|
*/
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShapeless(String group, ItemStack output, Object... inputs) {
|
|
|
|
addRecipe(j++, new ShapelessRecipes(MODID + ":" + group, output, createInput(inputs)));
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShapeless(String group, Item output, Object... inputs) {
|
2017-06-29 18:36:33 +02:00
|
|
|
addShapeless(group, new ItemStack(output), inputs);
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShapeless(String group, Block output, Object... inputs) {
|
2017-06-29 18:36:33 +02:00
|
|
|
addShapeless(group, new ItemStack(output), inputs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adds a shapeless recipe with X output on a crafting grid that is W x H, using an array of inputs. Use null for nothing, use Strings for OreDictionary support, this array must have a length of width * height.
|
|
|
|
* This array is ordered, and items must follow from left to right, top to bottom of the crafting grid.
|
|
|
|
*/
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShaped(ItemStack output, int width, int height, Object... input) {
|
2017-06-29 18:36:33 +02:00
|
|
|
addRecipe(j++, genShaped(output, width, height, input));
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShaped(Item output, int width, int height, Object... input) {
|
2017-06-29 18:36:33 +02:00
|
|
|
addShaped(new ItemStack(output), width, height, input);
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShaped(Block output, int width, int height, Object... input) {
|
2017-06-29 18:36:33 +02:00
|
|
|
addShaped(new ItemStack(output), width, height, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adds a shapeless recipe with X output on a crafting grid that is W x H, using an array of inputs. Use null for nothing, use Strings for OreDictionary support, this array must have a length of width * height.
|
|
|
|
* This array is ordered, and items must follow from left to right, top to bottom of the crafting grid. This has a custom group.
|
|
|
|
*/
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShaped(String group, ItemStack output, int width, int height, Object... input) {
|
|
|
|
addRecipe(j++, genShaped(MODID + ":" + group, output, width, height, input));
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShaped(String group, Item output, int width, int height, Object... input) {
|
2017-06-29 18:36:33 +02:00
|
|
|
addShaped(group, new ItemStack(output), width, height, input);
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static void addShaped(String group, Block output, int width, int height, Object... input) {
|
2017-06-29 18:36:33 +02:00
|
|
|
addShaped(group, new ItemStack(output), width, height, input);
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static ShapedRecipes genShaped(ItemStack output, int l, int w, Object[] input) {
|
|
|
|
if (input[0] instanceof Object[]) {
|
|
|
|
input = (Object[]) input[0];
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
2018-08-04 04:22:20 +02:00
|
|
|
if (l * w != input.length) { throw new UnsupportedOperationException("Attempted to add invalid shaped recipe. Complain to the author of " + MODNAME); }
|
2017-06-29 18:36:33 +02:00
|
|
|
NonNullList<Ingredient> inputL = NonNullList.create();
|
2018-08-04 04:22:20 +02:00
|
|
|
for (int i = 0; i < input.length; i++) {
|
2017-06-29 18:36:33 +02:00
|
|
|
Object k = input[i];
|
2018-08-04 04:22:20 +02:00
|
|
|
if (k instanceof String) {
|
|
|
|
inputL.add(i, new OreIngredient((String) k));
|
|
|
|
} else if (k instanceof ItemStack && !((ItemStack) k).isEmpty()) {
|
|
|
|
inputL.add(i, Ingredient.fromStacks((ItemStack) k));
|
|
|
|
} else if (k instanceof Item) {
|
|
|
|
inputL.add(i, Ingredient.fromStacks(new ItemStack((Item) k)));
|
|
|
|
} else if (k instanceof Block) {
|
|
|
|
inputL.add(i, Ingredient.fromStacks(new ItemStack((Block) k)));
|
|
|
|
} else {
|
2017-06-29 18:36:33 +02:00
|
|
|
inputL.add(i, Ingredient.EMPTY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
return new ShapedRecipes(MODID + ":" + j, l, w, inputL, output);
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static ShapedRecipes genShaped(String group, ItemStack output, int l, int w, Object[] input) {
|
|
|
|
if (input[0] instanceof List) {
|
|
|
|
input = ((List<?>) input[0]).toArray();
|
|
|
|
} else if (input[0] instanceof Object[]) {
|
|
|
|
input = (Object[]) input[0];
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
2018-08-04 04:22:20 +02:00
|
|
|
if (l * w != input.length) { throw new UnsupportedOperationException("Attempted to add invalid shaped recipe. Complain to the author of " + MODNAME); }
|
2017-06-29 18:36:33 +02:00
|
|
|
NonNullList<Ingredient> inputL = NonNullList.create();
|
2018-08-04 04:22:20 +02:00
|
|
|
for (int i = 0; i < input.length; i++) {
|
2017-06-29 18:36:33 +02:00
|
|
|
Object k = input[i];
|
2018-08-04 04:22:20 +02:00
|
|
|
if (k instanceof String) {
|
|
|
|
inputL.add(i, new OreIngredient((String) k));
|
|
|
|
} else if (k instanceof ItemStack && !((ItemStack) k).isEmpty()) {
|
|
|
|
inputL.add(i, Ingredient.fromStacks((ItemStack) k));
|
|
|
|
} else if (k instanceof Item) {
|
|
|
|
inputL.add(i, Ingredient.fromStacks(new ItemStack((Item) k)));
|
|
|
|
} else if (k instanceof Block) {
|
|
|
|
inputL.add(i, Ingredient.fromStacks(new ItemStack((Block) k)));
|
|
|
|
} else if (k instanceof Ingredient) {
|
|
|
|
inputL.add(i, (Ingredient) k);
|
|
|
|
} else {
|
2017-06-29 18:36:33 +02:00
|
|
|
inputL.add(i, Ingredient.EMPTY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ShapedRecipes(group, l, w, inputL, output);
|
|
|
|
}
|
|
|
|
|
2018-08-04 04:22:20 +02:00
|
|
|
public static NonNullList<Ingredient> createInput(Object[] input) {
|
|
|
|
if (input[0] instanceof List) {
|
|
|
|
input = ((List<?>) input[0]).toArray();
|
|
|
|
} else if (input[0] instanceof Object[]) {
|
|
|
|
input = (Object[]) input[0];
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
NonNullList<Ingredient> inputL = NonNullList.create();
|
2018-08-04 04:22:20 +02:00
|
|
|
for (int i = 0; i < input.length; i++) {
|
2017-06-29 18:36:33 +02:00
|
|
|
Object k = input[i];
|
2018-08-04 04:22:20 +02:00
|
|
|
if (k instanceof String) {
|
|
|
|
inputL.add(i, new OreIngredient((String) k));
|
|
|
|
} else if (k instanceof ItemStack) {
|
|
|
|
inputL.add(i, Ingredient.fromStacks((ItemStack) k));
|
|
|
|
} else if (k instanceof Item) {
|
|
|
|
inputL.add(i, Ingredient.fromStacks(new ItemStack((Item) k)));
|
|
|
|
} else if (k instanceof Block) {
|
|
|
|
inputL.add(i, Ingredient.fromStacks(new ItemStack((Block) k)));
|
|
|
|
} else if (k instanceof Ingredient) {
|
|
|
|
inputL.add(i, (Ingredient) k);
|
|
|
|
} else {
|
|
|
|
throw new UnsupportedOperationException("Attempted to add invalid shapeless recipe. Complain to the author of " + MODNAME);
|
2017-06-29 18:36:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return inputL;
|
|
|
|
}
|
2017-06-29 18:30:02 +02:00
|
|
|
}
|