2013-09-07 04:44:31 +02:00
|
|
|
package com.pahimar.ee3.emc;
|
|
|
|
|
2013-10-17 04:27:07 +02:00
|
|
|
import java.util.ArrayList;
|
2013-10-21 00:30:15 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
2013-10-26 02:40:13 +02:00
|
|
|
import java.util.HashMap;
|
2013-09-07 04:44:31 +02:00
|
|
|
import java.util.List;
|
2013-10-06 04:46:29 +02:00
|
|
|
import java.util.Map;
|
2013-10-16 21:55:14 +02:00
|
|
|
import java.util.SortedMap;
|
2013-10-06 04:46:29 +02:00
|
|
|
import java.util.TreeMap;
|
|
|
|
|
2013-10-24 04:22:26 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraftforge.oredict.OreDictionary;
|
|
|
|
|
2013-10-17 04:27:07 +02:00
|
|
|
import com.google.common.collect.ImmutableSortedMap;
|
2013-10-26 02:40:13 +02:00
|
|
|
import com.pahimar.ee3.core.helper.EmcHelper;
|
2013-10-20 08:32:32 +02:00
|
|
|
import com.pahimar.ee3.core.helper.LogHelper;
|
2013-10-21 00:30:15 +02:00
|
|
|
import com.pahimar.ee3.core.helper.RecipeHelper;
|
2013-09-07 04:44:31 +02:00
|
|
|
import com.pahimar.ee3.item.CustomWrappedStack;
|
2013-10-24 04:22:26 +02:00
|
|
|
import com.pahimar.ee3.item.OreStack;
|
2013-10-21 00:30:15 +02:00
|
|
|
import com.pahimar.ee3.item.crafting.RecipeRegistry;
|
2013-09-07 04:44:31 +02:00
|
|
|
|
|
|
|
public class EmcRegistry {
|
|
|
|
|
2013-10-28 00:55:53 +01:00
|
|
|
private int MAX_ATTEMPTED_ASSIGNMENT_PASSES = 16;
|
|
|
|
|
2013-09-07 04:47:12 +02:00
|
|
|
private static EmcRegistry emcRegistry = null;
|
2013-10-06 04:46:29 +02:00
|
|
|
|
2013-10-17 04:27:07 +02:00
|
|
|
private ImmutableSortedMap<CustomWrappedStack, EmcValue> stackMappings;
|
|
|
|
private ImmutableSortedMap<EmcValue, List<CustomWrappedStack>> valueMappings;
|
2013-09-07 04:44:31 +02:00
|
|
|
|
2013-10-28 00:55:53 +01:00
|
|
|
public static void lazyInit() {
|
2013-09-07 04:44:31 +02:00
|
|
|
|
2013-09-07 04:47:12 +02:00
|
|
|
if (emcRegistry == null) {
|
|
|
|
emcRegistry = new EmcRegistry();
|
|
|
|
emcRegistry.init();
|
|
|
|
}
|
|
|
|
}
|
2013-09-07 04:44:31 +02:00
|
|
|
|
2013-09-07 04:47:12 +02:00
|
|
|
private void init() {
|
2013-10-17 04:27:07 +02:00
|
|
|
|
2013-11-08 22:02:46 +01:00
|
|
|
// TODO Duplicate protection?
|
|
|
|
|
2013-10-17 04:27:07 +02:00
|
|
|
ImmutableSortedMap.Builder<CustomWrappedStack, EmcValue> stackMappingsBuilder = ImmutableSortedMap.naturalOrder();
|
|
|
|
ImmutableSortedMap.Builder<EmcValue, List<CustomWrappedStack>> valueMappingsBuilder = ImmutableSortedMap.naturalOrder();
|
|
|
|
|
2013-10-28 20:43:45 +01:00
|
|
|
Map<CustomWrappedStack, EmcValue> defaultValues = EmcValuesDefault.getDefaultValueMap();
|
2013-10-17 04:27:07 +02:00
|
|
|
|
2013-10-29 03:28:18 +01:00
|
|
|
// Grab the default stack:value mappings
|
2013-10-17 04:27:07 +02:00
|
|
|
stackMappingsBuilder.putAll(defaultValues);
|
2013-10-29 03:28:18 +01:00
|
|
|
|
|
|
|
// Grab the pre-auto assignment values gathered from IMC
|
|
|
|
stackMappingsBuilder.putAll(EmcValuesIMC.getPreAssignedValues());
|
2013-10-24 04:22:26 +02:00
|
|
|
|
2013-10-29 03:28:18 +01:00
|
|
|
// Build the Immutable stack:value map
|
|
|
|
stackMappings = stackMappingsBuilder.build();
|
2013-10-29 01:20:37 +01:00
|
|
|
|
2013-10-26 02:40:13 +02:00
|
|
|
// Attempt auto-assignment
|
|
|
|
int passNumber = 0;
|
|
|
|
Map<CustomWrappedStack, EmcValue> computedStackValues = computeStackMappings();
|
|
|
|
|
2013-10-28 00:55:53 +01:00
|
|
|
while ((computedStackValues.size() > 0) && (passNumber < MAX_ATTEMPTED_ASSIGNMENT_PASSES)) {
|
2013-10-26 02:40:13 +02:00
|
|
|
|
|
|
|
passNumber++;
|
|
|
|
computedStackValues = computeStackMappings();
|
|
|
|
|
|
|
|
stackMappingsBuilder = ImmutableSortedMap.naturalOrder();
|
|
|
|
stackMappingsBuilder.putAll(stackMappings);
|
|
|
|
stackMappingsBuilder.putAll(computedStackValues);
|
|
|
|
stackMappings = stackMappingsBuilder.build();
|
|
|
|
}
|
|
|
|
|
2013-10-29 03:28:18 +01:00
|
|
|
// Grab the post-auto assignment values gathered from IMC
|
|
|
|
stackMappingsBuilder = ImmutableSortedMap.naturalOrder();
|
|
|
|
stackMappingsBuilder.putAll(stackMappings);
|
|
|
|
stackMappingsBuilder.putAll(EmcValuesIMC.getPostAssignedValues());
|
|
|
|
stackMappings = stackMappingsBuilder.build();
|
2013-10-29 01:20:37 +01:00
|
|
|
|
2013-10-17 04:27:07 +02:00
|
|
|
// Handle the value mappings
|
2013-10-21 00:30:15 +02:00
|
|
|
SortedMap<EmcValue, ArrayList<CustomWrappedStack>> tempValueMappings = new TreeMap<EmcValue, ArrayList<CustomWrappedStack>>();
|
|
|
|
|
|
|
|
for (CustomWrappedStack stack : defaultValues.keySet()) {
|
|
|
|
EmcValue value = defaultValues.get(stack);
|
|
|
|
|
|
|
|
if (tempValueMappings.containsKey(value)) {
|
|
|
|
if (!(tempValueMappings.get(value).contains(stack))) {
|
|
|
|
tempValueMappings.get(value).add(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tempValueMappings.put(value, new ArrayList<CustomWrappedStack>(Arrays.asList(stack)));
|
|
|
|
}
|
|
|
|
}
|
2013-10-24 04:22:26 +02:00
|
|
|
|
2013-10-17 04:27:07 +02:00
|
|
|
valueMappingsBuilder.putAll(tempValueMappings);
|
|
|
|
valueMappings = valueMappingsBuilder.build();
|
2013-09-10 03:48:34 +02:00
|
|
|
}
|
2013-10-26 02:40:13 +02:00
|
|
|
|
|
|
|
private static Map<CustomWrappedStack, EmcValue> computeStackMappings() {
|
|
|
|
|
|
|
|
Map<CustomWrappedStack, EmcValue> computedStackMap = new HashMap<CustomWrappedStack, EmcValue>();
|
|
|
|
|
|
|
|
for (CustomWrappedStack recipeOutput : RecipeRegistry.getRecipeMappings().keySet()) {
|
|
|
|
|
|
|
|
if (!hasEmcValue(recipeOutput.getWrappedStack(), false) && !computedStackMap.containsKey(recipeOutput.getWrappedStack())) {
|
|
|
|
|
|
|
|
EmcValue lowestValue = null;
|
|
|
|
|
|
|
|
for (List<CustomWrappedStack> recipeInputs : RecipeRegistry.getRecipeMappings().get(recipeOutput)) {
|
|
|
|
|
|
|
|
EmcValue computedValue = EmcHelper.computeEmcValueFromList(recipeInputs);
|
|
|
|
computedValue = EmcHelper.factorEmcValue(computedValue, recipeOutput.getStackSize());
|
|
|
|
|
|
|
|
if (computedValue != null) {
|
|
|
|
if (computedValue.compareTo(lowestValue) < 0) {
|
|
|
|
lowestValue = computedValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((lowestValue != null) && (lowestValue.getValue() > 0f)) {
|
|
|
|
computedStackMap.put(new CustomWrappedStack(recipeOutput.getWrappedStack()), lowestValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return computedStackMap;
|
|
|
|
}
|
2013-10-17 04:27:07 +02:00
|
|
|
|
2013-10-26 02:40:13 +02:00
|
|
|
public static boolean hasEmcValue(Object object, boolean strict) {
|
2013-10-17 04:27:07 +02:00
|
|
|
|
2013-10-16 21:55:14 +02:00
|
|
|
lazyInit();
|
2013-10-18 02:40:45 +02:00
|
|
|
|
|
|
|
if (CustomWrappedStack.canBeWrapped(object)) {
|
2013-10-24 04:22:26 +02:00
|
|
|
|
2013-10-18 02:40:45 +02:00
|
|
|
CustomWrappedStack stack = new CustomWrappedStack(object);
|
2013-10-24 04:22:26 +02:00
|
|
|
|
|
|
|
if (emcRegistry.stackMappings.containsKey(new CustomWrappedStack(stack.getWrappedStack()))) {
|
|
|
|
return emcRegistry.stackMappings.containsKey(new CustomWrappedStack(stack.getWrappedStack()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
2013-10-26 02:40:13 +02:00
|
|
|
if (!strict) {
|
|
|
|
if (stack.getWrappedStack() instanceof ItemStack) {
|
2013-10-24 04:22:26 +02:00
|
|
|
|
2013-10-26 02:40:13 +02:00
|
|
|
ItemStack wrappedItemStack = (ItemStack) stack.getWrappedStack();
|
2013-10-24 04:22:26 +02:00
|
|
|
|
2013-10-27 22:42:29 +01:00
|
|
|
// If its an OreDictionary item, scan its siblings for values
|
2013-10-26 02:40:13 +02:00
|
|
|
if (OreDictionary.getOreID(wrappedItemStack) != -1) {
|
|
|
|
|
|
|
|
OreStack oreStack = new OreStack(wrappedItemStack);
|
|
|
|
|
|
|
|
if (emcRegistry.stackMappings.containsKey(new CustomWrappedStack(oreStack))) {
|
|
|
|
return emcRegistry.stackMappings.containsKey(new CustomWrappedStack(oreStack));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (ItemStack itemStack : OreDictionary.getOres(OreDictionary.getOreID(wrappedItemStack))) {
|
|
|
|
if (emcRegistry.stackMappings.containsKey(new CustomWrappedStack(itemStack))) {
|
|
|
|
return emcRegistry.stackMappings.containsKey(new CustomWrappedStack(itemStack));
|
|
|
|
}
|
2013-10-24 04:22:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-27 22:42:29 +01:00
|
|
|
// Else, scan for if there is a wildcard value for it
|
|
|
|
else {
|
|
|
|
|
|
|
|
for (CustomWrappedStack valuedStack : emcRegistry.stackMappings.keySet()) {
|
|
|
|
|
|
|
|
if (valuedStack.getWrappedStack() instanceof ItemStack) {
|
|
|
|
ItemStack valuedItemStack = (ItemStack) valuedStack.getWrappedStack();
|
|
|
|
|
2013-10-28 03:03:40 +01:00
|
|
|
if ((valuedItemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE || wrappedItemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE) && valuedItemStack.itemID == wrappedItemStack.itemID) {
|
2013-10-27 22:42:29 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-24 04:22:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-18 02:40:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-09-07 04:47:12 +02:00
|
|
|
}
|
2013-10-26 02:40:13 +02:00
|
|
|
|
|
|
|
public static boolean hasEmcValue(Object object) {
|
|
|
|
return hasEmcValue(object, false);
|
|
|
|
}
|
2013-10-17 04:27:07 +02:00
|
|
|
|
2013-10-26 02:40:13 +02:00
|
|
|
public static EmcValue getEmcValue(Object object, boolean strict) {
|
2013-10-17 04:27:07 +02:00
|
|
|
|
2013-10-16 21:55:14 +02:00
|
|
|
lazyInit();
|
2013-10-18 02:40:45 +02:00
|
|
|
|
2013-10-26 02:40:13 +02:00
|
|
|
if (hasEmcValue(object, strict)) {
|
2013-10-24 04:22:26 +02:00
|
|
|
|
2013-10-18 02:40:45 +02:00
|
|
|
CustomWrappedStack stack = new CustomWrappedStack(object);
|
2013-10-24 04:22:26 +02:00
|
|
|
|
|
|
|
if (emcRegistry.stackMappings.containsKey(new CustomWrappedStack(stack.getWrappedStack()))) {
|
|
|
|
return emcRegistry.stackMappings.get(new CustomWrappedStack(stack.getWrappedStack()));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
if (stack.getWrappedStack() instanceof ItemStack) {
|
|
|
|
|
2013-10-25 21:22:59 +02:00
|
|
|
ItemStack wrappedItemStack = (ItemStack) stack.getWrappedStack();
|
2013-10-27 22:42:29 +01:00
|
|
|
EmcValue lowestValue = null;
|
2013-10-25 21:22:59 +02:00
|
|
|
|
|
|
|
if (OreDictionary.getOreID(wrappedItemStack) != -1) {
|
|
|
|
|
|
|
|
OreStack oreStack = new OreStack(wrappedItemStack);
|
2013-10-24 04:22:26 +02:00
|
|
|
|
2013-10-25 21:22:59 +02:00
|
|
|
if (emcRegistry.stackMappings.containsKey(new CustomWrappedStack(oreStack))) {
|
|
|
|
return emcRegistry.stackMappings.get(new CustomWrappedStack(oreStack));
|
|
|
|
}
|
|
|
|
else {
|
2013-10-24 04:22:26 +02:00
|
|
|
|
2013-10-25 21:22:59 +02:00
|
|
|
for (ItemStack itemStack : OreDictionary.getOres(OreDictionary.getOreID(wrappedItemStack))) {
|
2013-10-24 04:22:26 +02:00
|
|
|
|
2013-10-25 21:22:59 +02:00
|
|
|
if (emcRegistry.stackMappings.containsKey(new CustomWrappedStack(itemStack))) {
|
|
|
|
if (lowestValue == null) {
|
|
|
|
lowestValue = emcRegistry.stackMappings.get(new CustomWrappedStack(itemStack));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
EmcValue itemValue = emcRegistry.stackMappings.get(new CustomWrappedStack(itemStack));
|
|
|
|
|
|
|
|
if (itemValue.compareTo(lowestValue) < 0) {
|
|
|
|
lowestValue = itemValue;
|
|
|
|
}
|
2013-10-24 04:22:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 21:22:59 +02:00
|
|
|
|
|
|
|
return lowestValue;
|
2013-10-24 04:22:26 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-27 22:42:29 +01:00
|
|
|
else {
|
|
|
|
|
|
|
|
for (CustomWrappedStack valuedStack : emcRegistry.stackMappings.keySet()) {
|
|
|
|
|
|
|
|
EmcValue stackValue = emcRegistry.stackMappings.get(valuedStack);
|
|
|
|
|
|
|
|
if (valuedStack.getWrappedStack() instanceof ItemStack) {
|
|
|
|
|
|
|
|
ItemStack valuedItemStack = (ItemStack) valuedStack.getWrappedStack();
|
|
|
|
|
2013-10-28 03:03:40 +01:00
|
|
|
if ((valuedItemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE || wrappedItemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE) && valuedItemStack.itemID == wrappedItemStack.itemID) {
|
2013-10-27 22:42:29 +01:00
|
|
|
|
|
|
|
if (stackValue.compareTo(lowestValue) < 0) {
|
|
|
|
lowestValue = stackValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lowestValue;
|
|
|
|
}
|
2013-10-24 04:22:26 +02:00
|
|
|
}
|
|
|
|
}
|
2013-10-18 02:40:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2013-10-26 02:40:13 +02:00
|
|
|
|
|
|
|
public static EmcValue getEmcValue(Object object) {
|
|
|
|
return getEmcValue(object, false);
|
|
|
|
}
|
2013-10-18 02:40:45 +02:00
|
|
|
|
|
|
|
public static List<CustomWrappedStack> getStacksInRange(int start, int finish) {
|
|
|
|
|
|
|
|
return getStacksInRange(new EmcValue(start), new EmcValue(finish));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<CustomWrappedStack> getStacksInRange(float start, float finish) {
|
|
|
|
|
|
|
|
return getStacksInRange(new EmcValue(start), new EmcValue(finish));
|
2013-10-06 19:53:08 +02:00
|
|
|
}
|
2013-10-17 04:27:07 +02:00
|
|
|
|
|
|
|
public static List<CustomWrappedStack> getStacksInRange(EmcValue start, EmcValue finish) {
|
|
|
|
|
|
|
|
lazyInit();
|
|
|
|
|
|
|
|
List<CustomWrappedStack> stacksInRange = new ArrayList<CustomWrappedStack>();
|
|
|
|
|
|
|
|
SortedMap<EmcValue, List<CustomWrappedStack>> tailMap = emcRegistry.valueMappings.tailMap(start);
|
|
|
|
SortedMap<EmcValue, List<CustomWrappedStack>> headMap = emcRegistry.valueMappings.headMap(finish);
|
|
|
|
|
|
|
|
SortedMap<EmcValue, List<CustomWrappedStack>> smallerMap;
|
|
|
|
SortedMap<EmcValue, List<CustomWrappedStack>> biggerMap;
|
|
|
|
|
|
|
|
if (!tailMap.isEmpty() && !headMap.isEmpty()) {
|
|
|
|
|
|
|
|
if (tailMap.size() <= headMap.size()) {
|
|
|
|
smallerMap = tailMap;
|
|
|
|
biggerMap = headMap;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
smallerMap = headMap;
|
|
|
|
biggerMap = tailMap;
|
|
|
|
}
|
|
|
|
|
2013-10-17 16:11:16 +02:00
|
|
|
for (EmcValue value : smallerMap.keySet()) {
|
2013-10-17 04:27:07 +02:00
|
|
|
if (biggerMap.containsKey(value)) {
|
|
|
|
stacksInRange.addAll(emcRegistry.valueMappings.get(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stacksInRange;
|
|
|
|
}
|
2013-10-21 00:30:15 +02:00
|
|
|
|
|
|
|
public static void printStackValueMappings() {
|
|
|
|
|
2013-10-20 08:32:32 +02:00
|
|
|
lazyInit();
|
2013-10-21 00:30:15 +02:00
|
|
|
|
2013-10-20 08:32:32 +02:00
|
|
|
for (CustomWrappedStack stack : emcRegistry.stackMappings.keySet()) {
|
|
|
|
LogHelper.debug("Stack: " + stack + ", Value: " + emcRegistry.stackMappings.get(stack));
|
|
|
|
}
|
|
|
|
}
|
2013-10-21 00:30:15 +02:00
|
|
|
|
|
|
|
public static void printUnmappedStacks() {
|
|
|
|
|
|
|
|
lazyInit();
|
|
|
|
List<CustomWrappedStack> discoveredStacks = new ArrayList<CustomWrappedStack>(RecipeRegistry.getDiscoveredStacks());
|
|
|
|
Collections.sort(discoveredStacks);
|
|
|
|
for (CustomWrappedStack stack : discoveredStacks) {
|
|
|
|
if (!hasEmcValue(stack)) {
|
|
|
|
if (RecipeRegistry.getRecipeMappings().get(stack).size() > 0) {
|
|
|
|
for (List<CustomWrappedStack> recipeInputs : RecipeRegistry.getRecipeMappings().get(stack)) {
|
|
|
|
LogHelper.debug(stack + ": " + RecipeHelper.collateInputStacks(recipeInputs));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LogHelper.debug(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 03:03:40 +01:00
|
|
|
public static void printUnmappedCompoundStacks() {
|
2013-10-27 22:42:29 +01:00
|
|
|
|
|
|
|
lazyInit();
|
|
|
|
|
2013-10-28 00:55:53 +01:00
|
|
|
List<CustomWrappedStack> unmappedStacks = new ArrayList<CustomWrappedStack>();
|
2013-10-27 22:42:29 +01:00
|
|
|
|
2013-10-28 00:55:53 +01:00
|
|
|
for (CustomWrappedStack recipeOutput : RecipeRegistry.getRecipeMappings().keySet()) {
|
2013-10-27 22:42:29 +01:00
|
|
|
|
2013-10-28 00:55:53 +01:00
|
|
|
CustomWrappedStack unitStack = new CustomWrappedStack(recipeOutput.getWrappedStack());
|
2013-10-27 22:42:29 +01:00
|
|
|
|
2013-10-28 00:55:53 +01:00
|
|
|
if (!hasEmcValue(unitStack)) {
|
|
|
|
unmappedStacks.add(recipeOutput);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Collections.sort(unmappedStacks);
|
|
|
|
|
|
|
|
for (CustomWrappedStack recipeOutput : unmappedStacks) {
|
|
|
|
|
|
|
|
for (List<CustomWrappedStack> recipeInputs : RecipeRegistry.getRecipeMappings().get(recipeOutput)) {
|
|
|
|
LogHelper.debug(String.format("Recipe Output: %s, Recipe Inputs: %s", recipeOutput, RecipeHelper.collateInputStacks(recipeInputs)));
|
2013-10-27 22:42:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-07 04:44:31 +02:00
|
|
|
}
|