package com.agilemods.materiamuto.common.emc.recipe.compat.thaumcraft; import com.agilemods.materiamuto.api.CachedRecipe; import com.agilemods.materiamuto.api.IEMCRegistry; import com.agilemods.materiamuto.api.IRecipeScanner; import com.agilemods.materiamuto.api.wrapper.IStackWrapper; import com.agilemods.materiamuto.api.wrapper.VanillaStackWrapper; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import net.minecraft.item.crafting.CraftingManager; import net.minecraft.item.crafting.IRecipe; import thaumcraft.api.crafting.ShapedArcaneRecipe; import thaumcraft.api.crafting.ShapelessArcaneRecipe; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; public class ThaumcraftCraftingScanner implements IRecipeScanner { private Map<VanillaStackWrapper, Set<CachedRecipe>> outputMaps = Maps.newHashMap(); private void addRecipe(VanillaStackWrapper stackWrapper, CachedRecipe recipe) { Set<CachedRecipe> set = outputMaps.get(stackWrapper); if (set == null) { set = Sets.newHashSet(); } // Remove any null/bad entries Iterator<Map.Entry<IStackWrapper, Integer>> iterator = recipe.components.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<IStackWrapper, Integer> entry = iterator.next(); if (entry == null || entry.getKey() == null || entry.getValue() == null || !entry.getKey().valid()) { iterator.remove(); } } set.add(recipe); outputMaps.put(stackWrapper, set); } @Override public void scan() { for (IRecipe recipe : (List<IRecipe>) CraftingManager.getInstance().getRecipeList()) { VanillaStackWrapper stackWrapper = new VanillaStackWrapper(recipe.getRecipeOutput()); if (recipe instanceof ShapedArcaneRecipe) { addRecipe(stackWrapper, new CachedRecipe(((ShapedArcaneRecipe) recipe).getInput()).setResult(new VanillaStackWrapper(recipe.getRecipeOutput()))); } else if (recipe instanceof ShapelessArcaneRecipe) { addRecipe(stackWrapper, new CachedRecipe(((ShapelessArcaneRecipe) recipe).getInput()).setResult(new VanillaStackWrapper(recipe.getRecipeOutput()))); } } } @Override public double getEMC(IEMCRegistry emcRegistry, VanillaStackWrapper vanillaStackWrapper) { double emc = Double.MAX_VALUE; Set<CachedRecipe> recipeSet = outputMaps.get(vanillaStackWrapper); if (recipeSet != null) { for (CachedRecipe cachedRecipe : recipeSet) { double subEmc = cachedRecipe.getEMC() / cachedRecipe.result.stackSize; if (subEmc > 0 && subEmc < emc) { emc = subEmc; } } } else { return 0; } return emc; } }