Some work on assigning pre/post auto value assignment EmcValues to

things. Buggy, more work required
This commit is contained in:
pahimar 2013-10-28 22:28:18 -04:00
parent 9fa390fcae
commit c580d70954
6 changed files with 126 additions and 38 deletions

View file

@ -162,7 +162,6 @@ public class EquivalentExchange3 {
public void modsLoaded(FMLPostInitializationEvent event) {
EmcRegistry.lazyInit();
EmcRegistry.printUnmappedCompoundStacks();
}
@EventHandler

View file

@ -19,6 +19,8 @@ import cpw.mods.fml.common.event.FMLInterModComms.IMCMessage;
public class InterModCommsHandler {
// TODO Revisit logging levels and the use of String.format for logging messages
public static void processIMCMessages(IMCEvent event) {
for (IMCMessage imcMessage : event.getMessages()) {
@ -98,9 +100,11 @@ public class InterModCommsHandler {
private static void processPreAssignEmcValueMessage(IMCMessage imcMessage) {
// FIXME Not all methods of passing mappings work, and logging not 100% yet
NBTTagCompound encodedEmcValueMapping = imcMessage.getNBTValue();
Map<CustomWrappedStack, EmcValue> emcValueMapping = NBTHelper.decodeEmcValueMapping(encodedEmcValueMapping);
Map<CustomWrappedStack, EmcValue> emcValueMapping = NBTHelper.decodeEmcValueMappings(encodedEmcValueMapping);
if (emcValueMapping != null && emcValueMapping.size() > 0) {
for (CustomWrappedStack stack : emcValueMapping.keySet()) {
@ -108,16 +112,23 @@ public class InterModCommsHandler {
if (stack.getWrappedStack() != null && emcValue != null && emcValue.getValue() > 0) {
EmcValuesIMC.addPreAssignedValued(stack, emcValue);
LogHelper.fine(String.format("[IMC] Mod '%s' added a pre auto assignment EmcValue of %s (%s) for object '%s'",imcMessage.getSender(), emcValue.getValue(), emcValue.toString(), stack));
}
else {
LogHelper.severe(String.format("[IMC] Mod '%s' failed in attempting to add a pre auto assignment EmcValue of %s (%s) for object '%s'",imcMessage.getSender(), emcValue.getValue(), emcValue.toString(), stack));
}
}
}
else {
LogHelper.severe(String.format("[IMC] Mod failed in attempting to add a pre auto assignment: %s", encodedEmcValueMapping));
}
}
private static void processPostAssignEmcValueMessage(IMCMessage imcMessage) {
NBTTagCompound encodedEmcValueMapping = imcMessage.getNBTValue();
NBTTagCompound encodedEmcValueMapping = imcMessage.getNBTValue();
Map<CustomWrappedStack, EmcValue> emcValueMapping = NBTHelper.decodeEmcValueMapping(encodedEmcValueMapping);
Map<CustomWrappedStack, EmcValue> emcValueMapping = NBTHelper.decodeEmcValueMappings(encodedEmcValueMapping);
if (emcValueMapping != null && emcValueMapping.size() > 0) {
for (CustomWrappedStack stack : emcValueMapping.keySet()) {
@ -125,6 +136,10 @@ public class InterModCommsHandler {
if (stack.getWrappedStack() != null && emcValue != null && emcValue.getValue() > 0) {
EmcValuesIMC.addPostAssignedValued(stack, emcValue);
LogHelper.fine(String.format("[IMC] Mod '%s' added a post auto assignment EmcValue of %s (%s) for object '%s'",imcMessage.getSender(), emcValue.getValue(), emcValue.toString(), stack));
}
else {
LogHelper.severe(String.format("[IMC] Mod '%s' failed in attempting to add a post auto assignment EmcValue of %s (%s) for object '%s'",imcMessage.getSender(), emcValue.getValue(), emcValue.toString(), stack));
}
}
}

View file

@ -14,6 +14,7 @@ import com.pahimar.ee3.emc.EmcValue;
import com.pahimar.ee3.item.CustomWrappedStack;
import com.pahimar.ee3.item.EnergyStack;
import com.pahimar.ee3.item.OreStack;
import com.pahimar.ee3.lib.InterModComms;
import com.pahimar.ee3.lib.Strings;
/**
@ -29,7 +30,7 @@ public class NBTHelper {
public static NBTTagCompound encodeEmcValue(EmcValue emcValue) {
return encodeEmcValue("", emcValue);
return encodeEmcValue(InterModComms.EMC_VALUE_TAG_NAME, emcValue);
}
public static NBTTagCompound encodeEmcValue(String compoundName, EmcValue emcValue) {
@ -37,7 +38,7 @@ public class NBTHelper {
NBTTagCompound encodedValue = new NBTTagCompound(compoundName);
for (EmcType emcType: EmcType.TYPES) {
encodedValue.setFloat(String.format("componentOrdinal_%s", emcType.ordinal()), emcValue.components[emcType.ordinal()]);
encodedValue.setFloat(String.format(InterModComms.EMC_VALUE_COMPONENT_ORDINAL_TEMPLATE, emcType.ordinal()), emcValue.components[emcType.ordinal()]);
}
return encodedValue;
@ -48,8 +49,8 @@ public class NBTHelper {
float[] subValues = new float[EmcType.TYPES.length];
for (EmcType emcType : EmcType.TYPES) {
if (encodedValue.hasKey(String.format("componentOrdinal_%s", emcType.ordinal())) && emcType.ordinal() < subValues.length) {
subValues[emcType.ordinal()] = encodedValue.getFloat(String.format("componentOrdinal_%s", emcType.ordinal()));
if (encodedValue.hasKey(String.format(InterModComms.EMC_VALUE_COMPONENT_ORDINAL_TEMPLATE, emcType.ordinal())) && emcType.ordinal() < subValues.length) {
subValues[emcType.ordinal()] = encodedValue.getFloat(String.format(InterModComms.EMC_VALUE_COMPONENT_ORDINAL_TEMPLATE, emcType.ordinal()));
}
}
@ -58,34 +59,90 @@ public class NBTHelper {
public static NBTTagCompound encodeEmcValueMapping(Object object, EmcValue emcValue) {
NBTTagCompound tagCompound = new NBTTagCompound("emcValueMapping");
return encodeEmcValueMapping(InterModComms.STACK_VALUE_MAPPING_TAG_NAME, object, emcValue);
}
private static NBTTagCompound encodeEmcValueMapping(String compoundName, Object object, EmcValue emcValue) {
tagCompound.setCompoundTag("customWrappedStack", encodeStackAsNBT("stack", object));
tagCompound.setCompoundTag("emcValue", encodeEmcValue("value", emcValue));
NBTTagCompound tagCompound = new NBTTagCompound(compoundName);
tagCompound.setCompoundTag(InterModComms.STACK_TAG_NAME, encodeStackAsNBT(InterModComms.STACK_TAG_NAME, object));
tagCompound.setCompoundTag(InterModComms.EMC_VALUE_TAG_NAME, encodeEmcValue(InterModComms.EMC_VALUE_TAG_NAME, emcValue));
return tagCompound;
}
public static Map<CustomWrappedStack, EmcValue> decodeEmcValueMapping(NBTTagCompound encodedEmcValueMapping) {
public static NBTTagCompound encodeEmcValueMappings(String compoundName, Map<CustomWrappedStack, EmcValue> stackValueMap) {
NBTTagCompound tagCompound = new NBTTagCompound(compoundName);
tagCompound.setInteger(InterModComms.COUNT_TAG_NAME, stackValueMap.keySet().size());
int i = 0;
for (CustomWrappedStack stack : stackValueMap.keySet()) {
tagCompound.setCompoundTag(String.format(InterModComms.STACK_VALUE_MAPPING_TEMPLATE, i), encodeEmcValueMapping(stack, stackValueMap.get(stack)));
}
return tagCompound;
}
public static NBTTagCompound encodeEmcValueMappings(Map<CustomWrappedStack, EmcValue> stackValueMap) {
return encodeEmcValueMappings(InterModComms.STACK_VALUE_MAPPING_TAG_NAME, stackValueMap);
}
public static NBTTagCompound encodeEmcValueMappings(String compoundName, Object object, EmcValue emcValue) {
HashMap<CustomWrappedStack, EmcValue> stackValueMap = new HashMap<CustomWrappedStack, EmcValue>();
if (CustomWrappedStack.canBeWrapped(object)) {
CustomWrappedStack wrappedObject = new CustomWrappedStack(object);
stackValueMap.put(wrappedObject, emcValue);
return encodeEmcValueMappings(compoundName, stackValueMap);
}
else {
return null;
}
}
public static Map<CustomWrappedStack, EmcValue> decodeEmcValueMappings(NBTTagCompound encodedEmcValueMap) {
Map<CustomWrappedStack, EmcValue> decodedEmcValueMapping = new HashMap<CustomWrappedStack, EmcValue>();
if (encodedEmcValueMapping.getName().equals("emcValueMapping")) {
if (encodedEmcValueMapping.hasKey("customWrappedStack") && encodedEmcValueMapping.hasKey("emcValue")) {
CustomWrappedStack wrappedStack = decodeStackFromNBT(encodedEmcValueMapping.getCompoundTag("customWrappedStack"));
EmcValue emcValue = decodeEmcValue(encodedEmcValueMapping.getCompoundTag("emcValue"));
if (wrappedStack.getWrappedStack() != null && emcValue.getValue() > 0f) {
decodedEmcValueMapping.put(wrappedStack, emcValue);
}
else {
decodedEmcValueMapping = null;
if (encodedEmcValueMap.getName().equals(InterModComms.STACK_VALUE_MAPPING_TAG_NAME)) {
int count = encodedEmcValueMap.getInteger(InterModComms.COUNT_TAG_NAME);
for (int i = 0; i < count; i++) {
if (encodedEmcValueMap.hasKey(String.format(InterModComms.STACK_VALUE_MAPPING_TEMPLATE, i))) {
Map<CustomWrappedStack, EmcValue> decodedMapping = decodeEmcValueMapping(encodedEmcValueMap.getCompoundTag(String.format(InterModComms.STACK_VALUE_MAPPING_TEMPLATE, i)));
if (decodedMapping != null && decodedMapping.size() > 0) {
decodedEmcValueMapping.putAll(decodedMapping);
}
}
}
}
return decodedEmcValueMapping;
}
public static Map<CustomWrappedStack, EmcValue> decodeEmcValueMapping(NBTTagCompound encodedEmcValueMapping) {
Map<CustomWrappedStack, EmcValue> decodedEmcValueMapping = new HashMap<CustomWrappedStack, EmcValue>();
if (encodedEmcValueMapping.hasKey(InterModComms.STACK_TAG_NAME) && encodedEmcValueMapping.hasKey(InterModComms.EMC_VALUE_TAG_NAME)) {
CustomWrappedStack stack = NBTHelper.decodeStackFromNBT(encodedEmcValueMapping.getCompoundTag(InterModComms.STACK_TAG_NAME));
EmcValue emcValue = NBTHelper.decodeEmcValue(encodedEmcValueMapping.getCompoundTag(InterModComms.EMC_VALUE_TAG_NAME));
if (stack != null && emcValue != null) {
if (stack.getWrappedStack() != null && emcValue.getValue() > 0) {
decodedEmcValueMapping.put(stack, emcValue);
return decodedEmcValueMapping;
}
}
}
return null;
}
// TODO Link this method to some API stuffs
public static NBTTagCompound encodeStackAsNBT(Object stackObject) {

View file

@ -13,7 +13,6 @@ import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Multimap;
import com.pahimar.ee3.core.helper.EmcHelper;
import com.pahimar.ee3.core.helper.LogHelper;
import com.pahimar.ee3.core.helper.RecipeHelper;
@ -45,12 +44,14 @@ public class EmcRegistry {
Map<CustomWrappedStack, EmcValue> defaultValues = EmcValuesDefault.getDefaultValueMap();
// Handle the stack mappings
// Grab the default stack:value mappings
stackMappingsBuilder.putAll(defaultValues);
stackMappings = stackMappingsBuilder.build();
// Grab the pre-auto assignment values gathered from IMC
stackMappingsBuilder.putAll(EmcValuesIMC.getPreAssignedValues());
// Pass in the pre-auto assignment values gathered from IMC
Multimap<CustomWrappedStack, EmcValue> preAssignedValues = EmcValuesIMC.getPreAssignedValues();
// Build the Immutable stack:value map
stackMappings = stackMappingsBuilder.build();
// Attempt auto-assignment
int passNumber = 0;
@ -67,8 +68,11 @@ public class EmcRegistry {
stackMappings = stackMappingsBuilder.build();
}
// Pass in the post-auto assignment values gathered from IMC
Multimap<CustomWrappedStack, EmcValue> postAssignedValues = EmcValuesIMC.getPreAssignedValues();
// Grab the post-auto assignment values gathered from IMC
stackMappingsBuilder = ImmutableSortedMap.naturalOrder();
stackMappingsBuilder.putAll(stackMappings);
stackMappingsBuilder.putAll(EmcValuesIMC.getPostAssignedValues());
stackMappings = stackMappingsBuilder.build();
// Handle the value mappings
SortedMap<EmcValue, ArrayList<CustomWrappedStack>> tempValueMappings = new TreeMap<EmcValue, ArrayList<CustomWrappedStack>>();
@ -273,7 +277,6 @@ public class EmcRegistry {
lazyInit();
// FIXME Doesn't work properly, fix me!
List<CustomWrappedStack> stacksInRange = new ArrayList<CustomWrappedStack>();
SortedMap<EmcValue, List<CustomWrappedStack>> tailMap = emcRegistry.valueMappings.tailMap(start);

View file

@ -1,7 +1,8 @@
package com.pahimar.ee3.emc;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import java.util.Map;
import java.util.TreeMap;
import com.pahimar.ee3.item.CustomWrappedStack;
/**
@ -15,22 +16,28 @@ import com.pahimar.ee3.item.CustomWrappedStack;
*/
public class EmcValuesIMC {
private static Multimap<CustomWrappedStack, EmcValue> preAssignedValueMap = HashMultimap.create();
private static Multimap<CustomWrappedStack, EmcValue> postAssignedValueMap = HashMultimap.create();
private static Map<CustomWrappedStack, EmcValue> preAssignedValueMap = new TreeMap<CustomWrappedStack, EmcValue>();
private static Map<CustomWrappedStack, EmcValue> postAssignedValueMap = new TreeMap<CustomWrappedStack, EmcValue>();
public static Multimap<CustomWrappedStack, EmcValue> getPreAssignedValues() {
public static Map<CustomWrappedStack, EmcValue> getPreAssignedValues() {
return preAssignedValueMap;
}
public static Multimap<CustomWrappedStack, EmcValue> getPostAssignedValues() {
public static Map<CustomWrappedStack, EmcValue> getPostAssignedValues() {
return postAssignedValueMap;
}
public static void addPreAssignedValued(CustomWrappedStack wrappedStack, EmcValue emcValue) {
if (!preAssignedValueMap.containsKey(wrappedStack)) {
preAssignedValueMap.put(wrappedStack, emcValue);
}
}
public static void addPostAssignedValued(CustomWrappedStack wrappedStack, EmcValue emcValue) {
if (!postAssignedValueMap.containsKey(wrappedStack)) {
postAssignedValueMap.put(wrappedStack, emcValue);
}
}
}

View file

@ -12,4 +12,11 @@ public class InterModComms {
// Interacting with the EMC value mappings
public static final String ASSIGN_EMC_VALUE_PRE = "assign-emc-value-pre";
public static final String ASSIGN_EMC_VALUE_POST = "assign-emc-value-post";
public static final String STACK_VALUE_MAPPING_TAG_NAME = "stackValueMap";
public static final String COUNT_TAG_NAME = "count";
public static final String STACK_VALUE_MAPPING_TEMPLATE = "stackValueMapping_%s";
public static final String STACK_TAG_NAME = "stack";
public static final String EMC_VALUE_TAG_NAME = "emcValue";
public static final String EMC_VALUE_COMPONENT_ORDINAL_TEMPLATE = "componentOrdinal_%s";
}