2013-08-23 16:59:50 +02:00
|
|
|
package com.pahimar.ee3.item.crafting;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
|
|
|
import com.google.common.collect.HashMultimap;
|
|
|
|
import com.google.common.collect.Multimap;
|
|
|
|
import com.pahimar.ee3.core.util.EnergyStack;
|
|
|
|
import com.pahimar.ee3.core.util.ItemUtil;
|
|
|
|
import com.pahimar.ee3.core.util.OreStack;
|
|
|
|
import com.pahimar.ee3.core.util.RecipeHelper;
|
|
|
|
import com.pahimar.ee3.item.CustomWrappedStack;
|
|
|
|
|
|
|
|
public class RecipeRegistry {
|
|
|
|
|
|
|
|
private static RecipeRegistry recipeRegistry = null;
|
|
|
|
|
|
|
|
private Multimap<CustomWrappedStack, List<CustomWrappedStack>> recipeMap;
|
|
|
|
private ArrayList<CustomWrappedStack> discoveredStacks;
|
|
|
|
private ArrayList<CustomWrappedStack> recipelessStacks;
|
|
|
|
private List<CustomWrappedStack> wildCardStacks;
|
|
|
|
|
|
|
|
private RecipeRegistry() {
|
|
|
|
|
|
|
|
recipeMap = HashMultimap.create();
|
|
|
|
wildCardStacks = RecipeHelper.populateWildCards();
|
|
|
|
discoveredStacks = new ArrayList<CustomWrappedStack>();
|
|
|
|
recipelessStacks = new ArrayList<CustomWrappedStack>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static RecipeRegistry getInstance() {
|
|
|
|
|
|
|
|
if (recipeRegistry == null) {
|
|
|
|
recipeRegistry = new RecipeRegistry();
|
|
|
|
recipeRegistry.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return recipeRegistry;
|
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
private void init() {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
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);
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
// 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();
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
for (List<CustomWrappedStack> recipeInputs : recipes.get(recipeOutput)) {
|
|
|
|
addRecipe(recipeOutput, recipeInputs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
private void discoverStacks(Multimap<CustomWrappedStack, List<CustomWrappedStack>> recipes) {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
Set<CustomWrappedStack> recipeKeySet = recipes.keySet();
|
|
|
|
Iterator<CustomWrappedStack> recipeKeySetIterator = recipeKeySet.iterator();
|
|
|
|
CustomWrappedStack recipeOutput = null;
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
// Discover all stacks involved in the recipes we know about
|
|
|
|
while (recipeKeySetIterator.hasNext()) {
|
|
|
|
recipeOutput = recipeKeySetIterator.next();
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
if (!discoveredStacks.contains(new CustomWrappedStack(recipeOutput.getWrappedStack())) && recipeOutput.getWrappedStack() != null) {
|
|
|
|
discoveredStacks.add(new CustomWrappedStack(recipeOutput.getWrappedStack()));
|
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
for (List<CustomWrappedStack> recipeInputs : recipes.get(recipeOutput)) {
|
|
|
|
for (CustomWrappedStack recipeInput : recipeInputs) {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
CustomWrappedStack unwrappedRecipeInput = new CustomWrappedStack(recipeInput.getWrappedStack());
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
if (!discoveredStacks.contains(unwrappedRecipeInput) && recipeInput.getWrappedStack() != null) {
|
|
|
|
discoveredStacks.add(unwrappedRecipeInput);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CustomWrappedStack customWrappedStack;
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
// Discover all stacks from the vanilla Items array
|
|
|
|
for (int i = 0; i < Item.itemsList.length; i++) {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
if (Item.itemsList[i] != null) {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
if (Item.itemsList[i].getHasSubtypes()) {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
|
|
|
for (int meta = 0; meta < 16; meta++) {
|
|
|
|
|
|
|
|
customWrappedStack = new CustomWrappedStack(new ItemStack(Item.itemsList[i].itemID, 1, meta));
|
|
|
|
|
|
|
|
if (!discoveredStacks.contains(customWrappedStack)) {
|
2013-08-23 16:59:50 +02:00
|
|
|
discoveredStacks.add(customWrappedStack);
|
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
}
|
2013-08-23 16:59:50 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
customWrappedStack = new CustomWrappedStack(new ItemStack(Item.itemsList[i]));
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
if (!discoveredStacks.contains(customWrappedStack)) {
|
|
|
|
discoveredStacks.add(customWrappedStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
/*
|
2013-09-07 04:47:12 +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
|
2013-08-23 16:59:50 +02:00
|
|
|
*/
|
|
|
|
for (CustomWrappedStack discoveredStack : discoveredStacks) {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
if (recipes.get(discoveredStack).size() == 0 && !recipelessStacks.contains(discoveredStack)) {
|
|
|
|
recipelessStacks.add(discoveredStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasRecipe(CustomWrappedStack customWrappedStack) {
|
|
|
|
|
|
|
|
return recipeMap.containsKey(customWrappedStack);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasRecipe(ItemStack itemStack) {
|
|
|
|
|
|
|
|
return hasRecipe(new CustomWrappedStack(itemStack));
|
|
|
|
}
|
|
|
|
|
|
|
|
public int countRecipesFor(CustomWrappedStack customWrappedStack) {
|
|
|
|
|
|
|
|
Collection<List<CustomWrappedStack>> keys = recipeMap.get(customWrappedStack);
|
|
|
|
|
|
|
|
return keys.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int countRecipesFor(ItemStack itemStack) {
|
|
|
|
|
|
|
|
return countRecipesFor(new CustomWrappedStack(itemStack));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<List<CustomWrappedStack>> getRecipesFor(CustomWrappedStack customWrappedStack) {
|
|
|
|
|
|
|
|
return recipeMap.get(customWrappedStack);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<List<CustomWrappedStack>> getRecipesFor(ItemStack itemStack) {
|
|
|
|
|
|
|
|
return getRecipesFor(new CustomWrappedStack(itemStack));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Item: Item (Output) <- { ... }
|
|
|
|
*/
|
|
|
|
public void addRecipe(CustomWrappedStack recipeOutput, List<?> recipeInputs) {
|
|
|
|
|
|
|
|
ArrayList<CustomWrappedStack> collatedStacks = new ArrayList<CustomWrappedStack>();
|
|
|
|
|
|
|
|
CustomWrappedStack wrappedInputStack = null;
|
|
|
|
boolean found = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
|
|
|
|
if (object instanceof ItemStack || object instanceof OreStack) {
|
|
|
|
wrappedInputStack = new CustomWrappedStack(object);
|
|
|
|
}
|
|
|
|
else if (object instanceof CustomWrappedStack) {
|
|
|
|
wrappedInputStack = (CustomWrappedStack) object;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wildCardStacks.contains(wrappedInputStack)) {
|
|
|
|
Iterator<CustomWrappedStack> wildIter = wildCardStacks.iterator();
|
|
|
|
while (wildIter.hasNext()) {
|
|
|
|
CustomWrappedStack wildCard = wildIter.next();
|
|
|
|
if (wildCard.equals(wrappedInputStack)) {
|
|
|
|
wrappedInputStack = wildCard;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collatedStacks.size() == 0) {
|
|
|
|
collatedStacks.add(wrappedInputStack);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
found = false;
|
|
|
|
|
|
|
|
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) {
|
2013-09-06 04:21:55 +02:00
|
|
|
if (OreStack.compareOreNames((OreStack) wrappedInputStack.getWrappedStack(), (OreStack) collatedStacks.get(i).getWrappedStack())) {
|
2013-08-23 16:59:50 +02:00
|
|
|
collatedStacks.get(i).setStackSize(collatedStacks.get(i).getStackSize() + wrappedInputStack.getStackSize());
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (wrappedInputStack.getWrappedStack() instanceof EnergyStack && collatedStacks.get(i).getWrappedStack() instanceof EnergyStack) {
|
|
|
|
if (((EnergyStack) wrappedInputStack.getWrappedStack()).energyName.equalsIgnoreCase(((EnergyStack) collatedStacks.get(i).getWrappedStack()).energyName)) {
|
|
|
|
collatedStacks.get(i).setStackSize(collatedStacks.get(i).getStackSize() + wrappedInputStack.getStackSize());
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
collatedStacks.add(wrappedInputStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!recipeMap.containsEntry(recipeOutput, collatedStacks)) {
|
|
|
|
recipeMap.put(recipeOutput, collatedStacks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int size() {
|
|
|
|
|
|
|
|
return recipeMap.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
for (CustomWrappedStack key : recipeMap.keySet()) {
|
|
|
|
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringBuilder.toString();
|
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
public Multimap<CustomWrappedStack, List<CustomWrappedStack>> getRecipeMappings() {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
return recipeMap;
|
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
public List<CustomWrappedStack> getDiscoveredStacks() {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
return discoveredStacks;
|
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
public List<CustomWrappedStack> getRecipelessStacks() {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
return recipelessStacks;
|
|
|
|
}
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
public List<CustomWrappedStack> getWildCardStacks() {
|
2013-09-07 04:47:12 +02:00
|
|
|
|
2013-08-23 16:59:50 +02:00
|
|
|
return wildCardStacks;
|
|
|
|
}
|
|
|
|
}
|