2013-06-12 22:01:45 +02:00
|
|
|
package com.pahimar.ee3.item.crafting;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2013-06-24 05:48:49 +02:00
|
|
|
import java.util.Iterator;
|
2013-06-12 22:01:45 +02:00
|
|
|
import java.util.List;
|
2013-07-03 22:26:10 +02:00
|
|
|
import java.util.Set;
|
2013-06-12 22:01:45 +02:00
|
|
|
|
2013-07-03 22:26:10 +02:00
|
|
|
import net.minecraft.item.Item;
|
2013-06-12 22:01:45 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
|
|
|
import com.google.common.collect.HashMultimap;
|
|
|
|
import com.google.common.collect.Multimap;
|
2013-06-28 21:27:21 +02:00
|
|
|
import com.pahimar.ee3.core.util.EnergyStack;
|
2013-06-24 05:48:49 +02:00
|
|
|
import com.pahimar.ee3.core.util.ItemUtil;
|
2013-06-19 04:20:50 +02:00
|
|
|
import com.pahimar.ee3.core.util.OreStack;
|
2013-06-12 22:01:45 +02:00
|
|
|
import com.pahimar.ee3.core.util.RecipeHelper;
|
|
|
|
import com.pahimar.ee3.item.CustomWrappedStack;
|
|
|
|
|
2013-06-19 04:20:50 +02:00
|
|
|
public class RecipeRegistry {
|
2013-06-12 22:01:45 +02:00
|
|
|
|
2013-06-19 04:20:50 +02:00
|
|
|
private static RecipeRegistry recipeRegistry = null;
|
2013-06-12 22:01:45 +02:00
|
|
|
|
|
|
|
private Multimap<CustomWrappedStack, List<CustomWrappedStack>> recipeMap;
|
2013-07-03 22:26:10 +02:00
|
|
|
private ArrayList<CustomWrappedStack> discoveredStacks;
|
|
|
|
private ArrayList<CustomWrappedStack> recipelessStacks;
|
|
|
|
private List<CustomWrappedStack> wildCardStacks;
|
2013-06-12 22:01:45 +02:00
|
|
|
|
2013-06-19 04:20:50 +02:00
|
|
|
private RecipeRegistry() {
|
2013-06-12 22:01:45 +02:00
|
|
|
|
|
|
|
recipeMap = HashMultimap.create();
|
2013-07-03 22:26:10 +02:00
|
|
|
wildCardStacks = RecipeHelper.populateWildCards();
|
|
|
|
discoveredStacks = new ArrayList<CustomWrappedStack>();
|
|
|
|
recipelessStacks = new ArrayList<CustomWrappedStack>();
|
2013-06-12 22:01:45 +02:00
|
|
|
}
|
|
|
|
|
2013-06-19 04:20:50 +02:00
|
|
|
public static RecipeRegistry getInstance() {
|
2013-06-12 22:01:45 +02:00
|
|
|
|
2013-06-19 04:20:50 +02:00
|
|
|
if (recipeRegistry == null) {
|
|
|
|
recipeRegistry = new RecipeRegistry();
|
2013-07-03 22:26:10 +02:00
|
|
|
recipeRegistry.init();
|
2013-06-12 22:01:45 +02:00
|
|
|
}
|
|
|
|
|
2013-06-19 04:20:50 +02:00
|
|
|
return recipeRegistry;
|
2013-06-12 22:01:45 +02:00
|
|
|
}
|
2013-07-03 22:26:10 +02:00
|
|
|
|
|
|
|
private void init() {
|
|
|
|
|
|
|
|
Multimap<CustomWrappedStack, List<CustomWrappedStack>> recipes = HashMultimap.create();
|
|
|
|
|
|
|
|
// Add potion recipes
|
|
|
|
recipes.putAll(RecipesPotions.getPotionRecipes());
|
|
|
|
|
|
|
|
// Add smelting recipes in the vanilla smelting manager
|
|
|
|
recipes.putAll(RecipesSmelting.getSmeltingRecipes());
|
|
|
|
|
|
|
|
// Add recipes in the vanilla crafting manager
|
|
|
|
recipes.putAll(RecipesVanilla.getVanillaRecipes());
|
|
|
|
|
|
|
|
// Add recipes gathered via IMC
|
|
|
|
// TODO Gather IMC recipes
|
|
|
|
|
|
|
|
// Populate the discovered stacks list with all stacks that we are involved in a recipe we are aware of
|
|
|
|
discoverStacks(recipes);
|
|
|
|
|
|
|
|
// Add items that have no recipe, using the list of discovered stacks to determine if it's in a recipe or not
|
|
|
|
for (CustomWrappedStack stack : recipelessStacks) {
|
|
|
|
recipes.put(stack, new ArrayList<CustomWrappedStack>());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through every recipe in the map, and add them to the registry
|
|
|
|
Set<CustomWrappedStack> recipeKeySet = recipes.keySet();
|
|
|
|
Iterator<CustomWrappedStack> recipeKeySetIterator = recipeKeySet.iterator();
|
|
|
|
CustomWrappedStack recipeOutput = null;
|
|
|
|
|
|
|
|
while (recipeKeySetIterator.hasNext()) {
|
|
|
|
recipeOutput = recipeKeySetIterator.next();
|
|
|
|
|
|
|
|
for (List<CustomWrappedStack> recipeInputs : recipes.get(recipeOutput)) {
|
|
|
|
addRecipe(recipeOutput, recipeInputs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void discoverStacks(Multimap<CustomWrappedStack, List<CustomWrappedStack>> recipes) {
|
|
|
|
|
|
|
|
Set<CustomWrappedStack> recipeKeySet = recipes.keySet();
|
|
|
|
Iterator<CustomWrappedStack> recipeKeySetIterator = recipeKeySet.iterator();
|
|
|
|
CustomWrappedStack recipeOutput = null;
|
|
|
|
|
2013-07-04 00:57:58 +02:00
|
|
|
// Discover all stacks involved in the recipes we know about
|
2013-07-03 22:26:10 +02:00
|
|
|
while (recipeKeySetIterator.hasNext()) {
|
|
|
|
recipeOutput = recipeKeySetIterator.next();
|
|
|
|
|
2013-07-22 12:48:31 +02:00
|
|
|
if (!discoveredStacks.contains(new CustomWrappedStack(recipeOutput.getWrappedStack())) && recipeOutput.getWrappedStack() != null) {
|
2013-07-03 22:26:10 +02:00
|
|
|
discoveredStacks.add(new CustomWrappedStack(recipeOutput.getWrappedStack()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (List<CustomWrappedStack> recipeInputs : recipes.get(recipeOutput)) {
|
|
|
|
for (CustomWrappedStack recipeInput : recipeInputs) {
|
|
|
|
|
2013-07-04 00:57:58 +02:00
|
|
|
CustomWrappedStack unwrappedRecipeInput = new CustomWrappedStack(recipeInput.getWrappedStack());
|
|
|
|
|
2013-07-22 12:48:31 +02:00
|
|
|
if (!discoveredStacks.contains(unwrappedRecipeInput) && recipeInput.getWrappedStack() != null) {
|
2013-07-04 00:57:58 +02:00
|
|
|
discoveredStacks.add(unwrappedRecipeInput);
|
2013-07-03 22:26:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-22 12:48:31 +02:00
|
|
|
CustomWrappedStack customWrappedStack;
|
2013-07-03 22:26:10 +02:00
|
|
|
|
2013-07-22 12:48:31 +02:00
|
|
|
// Discover all stacks from the vanilla Items array
|
2013-07-03 22:26:10 +02:00
|
|
|
for (int i = 0; i < Item.itemsList.length; i++) {
|
2013-07-22 12:48:31 +02:00
|
|
|
|
2013-07-03 22:26:10 +02:00
|
|
|
if (Item.itemsList[i] != null) {
|
2013-07-22 12:48:31 +02:00
|
|
|
|
2013-07-03 22:26:10 +02:00
|
|
|
if (Item.itemsList[i].getHasSubtypes()) {
|
2013-07-22 12:48:31 +02:00
|
|
|
|
|
|
|
for (int meta = 0; meta < 16; meta++) {
|
|
|
|
|
|
|
|
customWrappedStack = new CustomWrappedStack(new ItemStack(Item.itemsList[i].itemID, 1, meta));
|
|
|
|
|
|
|
|
if (!discoveredStacks.contains(customWrappedStack)) {
|
|
|
|
discoveredStacks.add(customWrappedStack);
|
2013-07-03 22:26:10 +02:00
|
|
|
}
|
2013-07-22 12:48:31 +02:00
|
|
|
}
|
2013-07-03 22:26:10 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2013-07-22 12:48:31 +02:00
|
|
|
customWrappedStack = new CustomWrappedStack(new ItemStack(Item.itemsList[i]));
|
2013-07-03 22:26:10 +02:00
|
|
|
|
|
|
|
if (!discoveredStacks.contains(customWrappedStack)) {
|
|
|
|
discoveredStacks.add(customWrappedStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 00:57:58 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For every stack we have discovered, check to see if we know a recipe for it. If we don't
|
|
|
|
* and we haven't already added it to the recipeless stack list, add it to the recipeless stack
|
|
|
|
* list
|
|
|
|
*/
|
|
|
|
for (CustomWrappedStack discoveredStack : discoveredStacks) {
|
|
|
|
|
|
|
|
if (recipes.get(discoveredStack).size() == 0 && !recipelessStacks.contains(discoveredStack)) {
|
|
|
|
recipelessStacks.add(discoveredStack);
|
|
|
|
}
|
|
|
|
}
|
2013-07-03 22:26:10 +02:00
|
|
|
}
|
2013-06-12 22:01:45 +02:00
|
|
|
|
|
|
|
public boolean hasRecipe(CustomWrappedStack customWrappedStack) {
|
|
|
|
|
|
|
|
return recipeMap.containsKey(customWrappedStack);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasRecipe(ItemStack itemStack) {
|
|
|
|
|
|
|
|
return hasRecipe(new CustomWrappedStack(itemStack));
|
|
|
|
}
|
|
|
|
|
2013-07-03 22:26:10 +02:00
|
|
|
public int countRecipesFor(CustomWrappedStack customWrappedStack) {
|
2013-06-12 22:01:45 +02:00
|
|
|
|
|
|
|
Collection<List<CustomWrappedStack>> keys = recipeMap.get(customWrappedStack);
|
|
|
|
|
|
|
|
return keys.size();
|
|
|
|
}
|
|
|
|
|
2013-07-03 22:26:10 +02:00
|
|
|
public int countRecipesFor(ItemStack itemStack) {
|
2013-06-12 22:01:45 +02:00
|
|
|
|
2013-07-03 22:26:10 +02:00
|
|
|
return countRecipesFor(new CustomWrappedStack(itemStack));
|
2013-06-12 22:01:45 +02:00
|
|
|
}
|
|
|
|
|
2013-07-03 22:26:10 +02:00
|
|
|
public Collection<List<CustomWrappedStack>> getRecipesFor(CustomWrappedStack customWrappedStack) {
|
2013-06-12 22:01:45 +02:00
|
|
|
|
|
|
|
return recipeMap.get(customWrappedStack);
|
|
|
|
}
|
|
|
|
|
2013-07-03 22:26:10 +02:00
|
|
|
public Collection<List<CustomWrappedStack>> getRecipesFor(ItemStack itemStack) {
|
2013-06-12 22:01:45 +02:00
|
|
|
|
2013-07-03 22:26:10 +02:00
|
|
|
return getRecipesFor(new CustomWrappedStack(itemStack));
|
2013-06-12 22:01:45 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 19:04:55 +02:00
|
|
|
/*
|
2013-07-02 21:41:40 +02:00
|
|
|
* Item: Item (Output) <- { ... }
|
2013-06-21 19:04:55 +02:00
|
|
|
*/
|
2013-06-12 22:01:45 +02:00
|
|
|
public void addRecipe(CustomWrappedStack recipeOutput, List<?> recipeInputs) {
|
|
|
|
|
|
|
|
ArrayList<CustomWrappedStack> collatedStacks = new ArrayList<CustomWrappedStack>();
|
|
|
|
|
2013-06-19 04:20:50 +02:00
|
|
|
CustomWrappedStack wrappedInputStack = null;
|
2013-06-24 05:48:49 +02:00
|
|
|
boolean found = false;
|
2013-06-12 22:01:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For every input in the input list, check to see if we have discovered
|
|
|
|
* it already - If we have, add it to the one we already have - If we
|
|
|
|
* have not, add it to the collection of discovered items
|
|
|
|
*/
|
|
|
|
for (Object object : recipeInputs) {
|
|
|
|
|
2013-06-19 04:20:50 +02:00
|
|
|
if (object instanceof ItemStack || object instanceof OreStack) {
|
|
|
|
wrappedInputStack = new CustomWrappedStack(object);
|
|
|
|
}
|
|
|
|
else if (object instanceof CustomWrappedStack) {
|
|
|
|
wrappedInputStack = (CustomWrappedStack) object;
|
|
|
|
}
|
2013-07-02 21:41:40 +02:00
|
|
|
|
2013-07-03 22:26:10 +02:00
|
|
|
if (wildCardStacks.contains(wrappedInputStack)) {
|
|
|
|
Iterator<CustomWrappedStack> wildIter = wildCardStacks.iterator();
|
2013-06-24 05:48:49 +02:00
|
|
|
while (wildIter.hasNext()) {
|
|
|
|
CustomWrappedStack wildCard = wildIter.next();
|
|
|
|
if (wildCard.equals(wrappedInputStack)) {
|
|
|
|
wrappedInputStack = wildCard;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 21:41:40 +02:00
|
|
|
|
2013-06-24 05:48:49 +02:00
|
|
|
if (collatedStacks.size() == 0) {
|
|
|
|
collatedStacks.add(wrappedInputStack);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
found = false;
|
2013-07-02 21:41:40 +02:00
|
|
|
|
2013-06-24 05:48:49 +02:00
|
|
|
for (int i = 0; i < collatedStacks.size(); i++) {
|
|
|
|
if (collatedStacks.get(i) != null) {
|
|
|
|
if (wrappedInputStack.getWrappedStack() instanceof ItemStack && collatedStacks.get(i).getWrappedStack() instanceof ItemStack) {
|
|
|
|
if (ItemUtil.compare((ItemStack) wrappedInputStack.getWrappedStack(), (ItemStack) collatedStacks.get(i).getWrappedStack())) {
|
|
|
|
collatedStacks.get(i).setStackSize(collatedStacks.get(i).getStackSize() + wrappedInputStack.getStackSize());
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (wrappedInputStack.getWrappedStack() instanceof OreStack && collatedStacks.get(i).getWrappedStack() instanceof OreStack) {
|
|
|
|
if (OreStack.compareStacks((OreStack) wrappedInputStack.getWrappedStack(), (OreStack) collatedStacks.get(i).getWrappedStack())) {
|
|
|
|
collatedStacks.get(i).setStackSize(collatedStacks.get(i).getStackSize() + wrappedInputStack.getStackSize());
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
2013-06-28 21:27:21 +02:00
|
|
|
else if (wrappedInputStack.getWrappedStack() instanceof EnergyStack && collatedStacks.get(i).getWrappedStack() instanceof EnergyStack) {
|
2013-07-02 21:41:40 +02:00
|
|
|
if (((EnergyStack) wrappedInputStack.getWrappedStack()).energyName.equalsIgnoreCase(((EnergyStack) collatedStacks.get(i).getWrappedStack()).energyName)) {
|
2013-06-28 21:27:21 +02:00
|
|
|
collatedStacks.get(i).setStackSize(collatedStacks.get(i).getStackSize() + wrappedInputStack.getStackSize());
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 05:48:49 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-02 21:41:40 +02:00
|
|
|
|
2013-06-24 05:48:49 +02:00
|
|
|
if (!found) {
|
|
|
|
collatedStacks.add(wrappedInputStack);
|
|
|
|
}
|
2013-06-21 19:04:55 +02:00
|
|
|
}
|
2013-06-19 04:20:50 +02:00
|
|
|
}
|
2013-07-03 20:36:35 +02:00
|
|
|
|
2013-07-02 21:41:40 +02:00
|
|
|
if (!recipeMap.containsEntry(recipeOutput, collatedStacks)) {
|
|
|
|
recipeMap.put(recipeOutput, collatedStacks);
|
2013-06-24 05:48:49 +02:00
|
|
|
}
|
2013-06-19 04:20:50 +02:00
|
|
|
}
|
2013-07-03 20:36:35 +02:00
|
|
|
|
2013-07-02 21:41:40 +02:00
|
|
|
public int size() {
|
2013-07-03 20:36:35 +02:00
|
|
|
|
2013-07-02 21:41:40 +02:00
|
|
|
return recipeMap.size();
|
|
|
|
}
|
2013-07-03 20:36:35 +02:00
|
|
|
|
2013-07-02 21:41:40 +02:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2013-07-03 20:36:35 +02:00
|
|
|
|
2013-07-02 21:41:40 +02:00
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
2013-07-03 20:36:35 +02:00
|
|
|
|
2013-07-02 21:41:40 +02:00
|
|
|
for (CustomWrappedStack key : recipeMap.keySet()) {
|
|
|
|
|
2013-07-03 20:36:35 +02:00
|
|
|
Collection<List<CustomWrappedStack>> recipeMappings = recipeMap.get(key);
|
|
|
|
|
|
|
|
for (List<CustomWrappedStack> recipeList : recipeMappings) {
|
|
|
|
stringBuilder.append(String.format("Recipe Output: %s, Recipe Input: %s\n", key.toString(), recipeList.toString()));
|
2013-06-28 04:35:42 +02:00
|
|
|
}
|
2013-06-12 22:01:45 +02:00
|
|
|
}
|
2013-07-03 20:36:35 +02:00
|
|
|
|
2013-07-02 21:41:40 +02:00
|
|
|
return stringBuilder.toString();
|
2013-06-12 22:01:45 +02:00
|
|
|
}
|
2013-07-03 22:26:10 +02:00
|
|
|
|
|
|
|
public Multimap<CustomWrappedStack, List<CustomWrappedStack>> getRecipeMappings() {
|
|
|
|
|
|
|
|
return recipeMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<CustomWrappedStack> getDiscoveredStacks() {
|
|
|
|
|
|
|
|
return discoveredStacks;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<CustomWrappedStack> getRecipelessStacks() {
|
|
|
|
|
|
|
|
return recipelessStacks;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<CustomWrappedStack> getWildCardStacks() {
|
|
|
|
|
|
|
|
return wildCardStacks;
|
|
|
|
}
|
2013-06-12 22:01:45 +02:00
|
|
|
}
|