/
FabricatorRecipeManager.java
86 lines (70 loc) · 2.96 KB
/
FabricatorRecipeManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.factory.recipes;
import javax.annotation.Nullable;
import org.apache.commons.lang3.tuple.Pair;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import forestry.api.recipes.IFabricatorManager;
import forestry.api.recipes.IFabricatorRecipe;
import forestry.core.recipes.RecipeUtil;
import forestry.core.recipes.ShapedRecipeCustom;
import forestry.core.utils.ItemStackUtil;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fluids.FluidStack;
public class FabricatorRecipeManager implements IFabricatorManager {
public static final Pair EMPTY_RECIPE = Pair.of(null, null);
private static final Set<IFabricatorRecipe> recipes = new HashSet<>();
@Override
public void addRecipe(ItemStack plan, FluidStack molten, ItemStack result, Object[] pattern) {
ShapedRecipeCustom patternRecipe = new ShapedRecipeCustom(result, pattern);
NonNullList<NonNullList<ItemStack>> ingredients = patternRecipe.getIngredients();
IFabricatorRecipe recipe = new FabricatorRecipe(plan, molten, result, ingredients, patternRecipe.getOreDicts(), patternRecipe.getWidth(), patternRecipe.getHeight());
addRecipe(recipe);
}
@Nullable
public static Pair<IFabricatorRecipe, String[][]> findMatchingRecipe(ItemStack plan, IInventory resources) {
ItemStack[][] gridResources = RecipeUtil.getResources(resources);
for (IFabricatorRecipe recipe : recipes) {
if (!recipe.getPlan().isEmpty() && !ItemStackUtil.isCraftingEquivalent(recipe.getPlan(), plan)) {
continue;
}
String[][] oreDicts = RecipeUtil.matches(recipe.getIngredients(), recipe.getOreDicts(), recipe.getWidth(), recipe.getHeight(), gridResources);
if (oreDicts != null) {
return Pair.of(recipe, oreDicts);
}
}
return EMPTY_RECIPE;
}
public static boolean isPlan(ItemStack plan) {
for (IFabricatorRecipe recipe : recipes) {
if (ItemStackUtil.isIdenticalItem(recipe.getPlan(), plan)) {
return true;
}
}
return false;
}
@Override
public boolean addRecipe(IFabricatorRecipe recipe) {
return recipes.add(recipe);
}
@Override
public boolean removeRecipe(IFabricatorRecipe recipe) {
return recipes.remove(recipe);
}
@Override
public Set<IFabricatorRecipe> recipes() {
return Collections.unmodifiableSet(recipes);
}
}