Mama said save my work. I'm gonna save my work!

This commit is contained in:
Pahimar 2016-05-16 22:22:12 -04:00
parent 926ab181da
commit 06b4801d55
2 changed files with 52 additions and 55 deletions

View file

@ -792,9 +792,10 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
JsonArray jsonEnergyValueRegistry = new JsonArray(); JsonArray jsonEnergyValueRegistry = new JsonArray();
for (WrappedStack wrappedStack : energyValueRegistry.stackMappings.keySet()) if (energyValueRegistry != null && energyValueRegistry.stackMappings != null) {
{ for (WrappedStack wrappedStack : energyValueRegistry.stackMappings.keySet()) {
jsonEnergyValueRegistry.add(EnergyValueStackMapping.jsonSerializer.toJsonTree(new EnergyValueStackMapping(wrappedStack, energyValueRegistry.stackMappings.get(wrappedStack)))); jsonEnergyValueRegistry.add(EnergyValueStackMapping.jsonSerializer.toJsonTree(new EnergyValueStackMapping(wrappedStack, energyValueRegistry.stackMappings.get(wrappedStack))));
}
} }
return jsonEnergyValueRegistry; return jsonEnergyValueRegistry;

View file

@ -10,12 +10,10 @@ import com.pahimar.ee3.handler.ConfigurationHandler;
import com.pahimar.ee3.util.LogHelper; import com.pahimar.ee3.util.LogHelper;
import com.pahimar.ee3.util.SerializationHelper; import com.pahimar.ee3.util.SerializationHelper;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import net.minecraft.init.Items;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.FluidContainerRegistry; import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.OreDictionary;
import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.Marker;
@ -30,11 +28,11 @@ public class NewEnergyValueRegistry {
public static final NewEnergyValueRegistry INSTANCE = new NewEnergyValueRegistry(); public static final NewEnergyValueRegistry INSTANCE = new NewEnergyValueRegistry();
private ImmutableSortedMap<WrappedStack, EnergyValue> stackMap; private ImmutableSortedMap<WrappedStack, EnergyValue> stackValueMap;
private ImmutableSortedMap<EnergyValue, List<WrappedStack>> valueMap; private ImmutableSortedMap<EnergyValue, List<WrappedStack>> valueStackMap;
private final Map<WrappedStack, EnergyValue> preCalculationValueMap; private final Map<WrappedStack, EnergyValue> preCalculationStackValueMap;
private final Map<WrappedStack, EnergyValue> postCalculationValueMap; private final Map<WrappedStack, EnergyValue> postCalculationStackValueMap;
private transient SortedSet<WrappedStack> uncomputedStacks; private transient SortedSet<WrappedStack> uncomputedStacks;
public static File energyValuesDirectory; public static File energyValuesDirectory;
@ -47,20 +45,10 @@ public class NewEnergyValueRegistry {
private NewEnergyValueRegistry() { private NewEnergyValueRegistry() {
ImmutableSortedMap.Builder<WrappedStack, EnergyValue> stackMapBuilder = ImmutableSortedMap.naturalOrder(); ImmutableSortedMap.Builder<WrappedStack, EnergyValue> stackMapBuilder = ImmutableSortedMap.naturalOrder();
stackMap = stackMapBuilder.build(); stackValueMap = stackMapBuilder.build();
preCalculationValueMap = new TreeMap<>(); preCalculationStackValueMap = new TreeMap<>();
postCalculationValueMap = new TreeMap<>(); postCalculationStackValueMap = new TreeMap<>();
// Loading up some dummy values for testing serialization
preCalculationValueMap.put(WrappedStack.wrap(Items.apple), new EnergyValue(1));
preCalculationValueMap.put(WrappedStack.wrap(Items.arrow), new EnergyValue(2));
preCalculationValueMap.put(WrappedStack.wrap(Items.baked_potato), new EnergyValue(3));
preCalculationValueMap.put(WrappedStack.wrap(Items.bed), new EnergyValue(4));
preCalculationValueMap.put(WrappedStack.wrap(new OreStack("oreIron")), new EnergyValue(5));
preCalculationValueMap.put(WrappedStack.wrap(new FluidStack(FluidRegistry.WATER, 500)), new EnergyValue(6));
preCalculationValueMap.put(WrappedStack.wrap(new ItemStack(Items.carrot, 1, 1)), new EnergyValue(7));
preCalculationValueMap.put(WrappedStack.wrap(new ItemStack(Items.chainmail_boots, 1, 2)), new EnergyValue(8));
} }
/** /**
@ -318,7 +306,7 @@ public class NewEnergyValueRegistry {
* @return an {@link ImmutableMap} containing the current energy value mappings * @return an {@link ImmutableMap} containing the current energy value mappings
*/ */
public ImmutableMap<WrappedStack, EnergyValue> getEnergyValues() { public ImmutableMap<WrappedStack, EnergyValue> getEnergyValues() {
return stackMap; return stackValueMap;
} }
/** /**
@ -326,8 +314,8 @@ public class NewEnergyValueRegistry {
* *
* @return a {link Map} containing the pre-calculation energy value mappings * @return a {link Map} containing the pre-calculation energy value mappings
*/ */
public Map<WrappedStack, EnergyValue> getPreCalculationValueMap() { public Map<WrappedStack, EnergyValue> getPreCalculationStackValueMap() {
return preCalculationValueMap; return preCalculationStackValueMap;
} }
/** /**
@ -335,8 +323,8 @@ public class NewEnergyValueRegistry {
* *
* @return a {@link Map} containing the post-calculation energy value mappings * @return a {@link Map} containing the post-calculation energy value mappings
*/ */
public Map<WrappedStack, EnergyValue> getPostCalculationValueMap() { public Map<WrappedStack, EnergyValue> getPostCalculationStackValueMap() {
return postCalculationValueMap; return postCalculationStackValueMap;
} }
/** /**
@ -380,7 +368,7 @@ public class NewEnergyValueRegistry {
* @return an {@link EnergyValue} if there is one to be found, null otherwise * @return an {@link EnergyValue} if there is one to be found, null otherwise
*/ */
public EnergyValue getEnergyValue(Object object, boolean strict) { public EnergyValue getEnergyValue(Object object, boolean strict) {
return getEnergyValue(stackMap, object, strict); return getEnergyValue(stackValueMap, object, strict);
} }
/** /**
@ -424,10 +412,10 @@ public class NewEnergyValueRegistry {
List stacksInRange = new ArrayList<WrappedStack>(); List stacksInRange = new ArrayList<WrappedStack>();
if (valueMap != null) { if (valueStackMap != null) {
SortedMap<EnergyValue, List<WrappedStack>> tailMap = valueMap.tailMap(start); SortedMap<EnergyValue, List<WrappedStack>> tailMap = valueStackMap.tailMap(start);
SortedMap<EnergyValue, List<WrappedStack>> headMap = valueMap.headMap(finish); SortedMap<EnergyValue, List<WrappedStack>> headMap = valueStackMap.headMap(finish);
SortedMap<EnergyValue, List<WrappedStack>> smallerMap; SortedMap<EnergyValue, List<WrappedStack>> smallerMap;
SortedMap<EnergyValue, List<WrappedStack>> biggerMap; SortedMap<EnergyValue, List<WrappedStack>> biggerMap;
@ -445,7 +433,7 @@ public class NewEnergyValueRegistry {
for (EnergyValue value : smallerMap.keySet()) { for (EnergyValue value : smallerMap.keySet()) {
if (biggerMap.containsKey(value)) { if (biggerMap.containsKey(value)) {
for (WrappedStack wrappedStack : valueMap.get(value)) { for (WrappedStack wrappedStack : valueStackMap.get(value)) {
if (wrappedStack.getWrappedObject() instanceof ItemStack || wrappedStack.getWrappedObject() instanceof FluidStack) { if (wrappedStack.getWrappedObject() instanceof ItemStack || wrappedStack.getWrappedObject() instanceof FluidStack) {
stacksInRange.add(wrappedStack.getWrappedObject()); stacksInRange.add(wrappedStack.getWrappedObject());
} }
@ -495,7 +483,7 @@ public class NewEnergyValueRegistry {
if (phase == Phase.PRE_CALCULATION) { if (phase == Phase.PRE_CALCULATION) {
if (!FMLCommonHandler.instance().bus().post(new EnergyValueEvent.SetEnergyValueEvent(wrappedStack, factoredEnergyValue, Phase.PRE_CALCULATION))) { if (!FMLCommonHandler.instance().bus().post(new EnergyValueEvent.SetEnergyValueEvent(wrappedStack, factoredEnergyValue, Phase.PRE_CALCULATION))) {
preCalculationValueMap.put(wrappedStack, factoredEnergyValue); preCalculationStackValueMap.put(wrappedStack, factoredEnergyValue);
if (doRegenValues) { if (doRegenValues) {
compute(); compute();
@ -504,12 +492,12 @@ public class NewEnergyValueRegistry {
} }
else if (!FMLCommonHandler.instance().bus().post(new EnergyValueEvent.SetEnergyValueEvent(wrappedStack, factoredEnergyValue, Phase.POST_CALCULATION))) { else if (!FMLCommonHandler.instance().bus().post(new EnergyValueEvent.SetEnergyValueEvent(wrappedStack, factoredEnergyValue, Phase.POST_CALCULATION))) {
TreeMap<WrappedStack, EnergyValue> valueMap = new TreeMap<>(stackMap); TreeMap<WrappedStack, EnergyValue> valueMap = new TreeMap<>(stackValueMap);
valueMap.put(wrappedStack, energyValue); valueMap.put(wrappedStack, energyValue);
ImmutableSortedMap.Builder<WrappedStack, EnergyValue> stackMappingsBuilder = ImmutableSortedMap.naturalOrder(); ImmutableSortedMap.Builder<WrappedStack, EnergyValue> stackMappingsBuilder = ImmutableSortedMap.naturalOrder();
stackMap = stackMappingsBuilder.putAll(valueMap).build(); stackValueMap = stackMappingsBuilder.putAll(valueMap).build();
postCalculationValueMap.put(wrappedStack, factoredEnergyValue); postCalculationStackValueMap.put(wrappedStack, factoredEnergyValue);
} }
} }
} }
@ -524,28 +512,29 @@ public class NewEnergyValueRegistry {
uncomputedStacks = new TreeSet<>(); uncomputedStacks = new TreeSet<>();
// Add in all pre-calculation energy value mappings // Add in all pre-calculation energy value mappings
preCalculationValueMap.keySet().stream() preCalculationStackValueMap.keySet().stream()
.filter(wrappedStack -> wrappedStack != null && wrappedStack.getWrappedObject() != null && preCalculationValueMap.get(wrappedStack) != null) .filter(wrappedStack -> wrappedStack != null && wrappedStack.getWrappedObject() != null && preCalculationStackValueMap.get(wrappedStack) != null)
.forEach(wrappedStack -> stackValueMap.put(wrappedStack, preCalculationValueMap.get(wrappedStack))); .forEach(wrappedStack -> stackValueMap.put(wrappedStack, preCalculationStackValueMap.get(wrappedStack)));
// Calculate values from the known methods to create items, and the pre-calculation value mappings // Calculate values from the known methods to create items, and the pre-calculation value mappings
calculateStackMap(); calculateStackValueMap(stackValueMap);
// Add in all post-calculation energy value mappings // Add in all post-calculation energy value mappings
postCalculationValueMap.keySet().stream() postCalculationStackValueMap.keySet().stream()
.filter(wrappedStack -> wrappedStack != null && wrappedStack.getWrappedObject() != null && postCalculationValueMap.get(wrappedStack) != null) .filter(wrappedStack -> wrappedStack != null && wrappedStack.getWrappedObject() != null && postCalculationStackValueMap.get(wrappedStack) != null)
.forEach(wrappedStack -> stackValueMap.put(wrappedStack, postCalculationValueMap.get(wrappedStack))); .forEach(wrappedStack -> stackValueMap.put(wrappedStack, postCalculationStackValueMap.get(wrappedStack)));
// Bake the final calculated energy value map // Bake the final calculated energy value maps
ImmutableSortedMap.Builder<WrappedStack, EnergyValue> stackMappingsBuilder = ImmutableSortedMap.naturalOrder(); ImmutableSortedMap.Builder<WrappedStack, EnergyValue> stackMappingsBuilder = ImmutableSortedMap.naturalOrder();
stackMappingsBuilder.putAll(stackValueMap); stackMappingsBuilder.putAll(stackValueMap);
stackMap = stackMappingsBuilder.build(); this.stackValueMap = stackMappingsBuilder.build();
calculateValueStackMap();
// Save the results to disk // Save the results to disk
save(); save();
} }
private void calculateStackMap() { private void calculateStackValueMap(Map<WrappedStack, EnergyValue> stackValueMap) {
Map<WrappedStack, EnergyValue> computedMap; Map<WrappedStack, EnergyValue> computedMap;
int passNumber, passComputed, totalComputed; int passNumber, passComputed, totalComputed;
@ -562,8 +551,14 @@ public class NewEnergyValueRegistry {
if (firstPass) { if (firstPass) {
firstPass = false; firstPass = false;
} }
/**
* FIXME PRIORITY NUMBER 1
* @see EnergyValueRegistry#computeStackMappings(Map, int)
*/
// computedMap = computeFromInputs(stackValueMap);
long passDuration = System.nanoTime() - passStartTime; long passDuration = System.nanoTime() - passStartTime;
if (ConfigurationHandler.Settings.energyValueDebugLoggingEnabled) { if (ConfigurationHandler.Settings.energyValueDebugLoggingEnabled) {
LogHelper.info(ENERGY_VALUE_MARKER, "Pass {}: Calculated {} values for objects in {} ns", passNumber, passComputed, passDuration); LogHelper.info(ENERGY_VALUE_MARKER, "Pass {}: Calculated {} values for objects in {} ns", passNumber, passComputed, passDuration);
} }
@ -572,7 +567,7 @@ public class NewEnergyValueRegistry {
LogHelper.info(ENERGY_VALUE_MARKER, "Finished dynamic value calculation (calculated {} values for objects in {} ns)", totalComputed, endingTime); LogHelper.info(ENERGY_VALUE_MARKER, "Finished dynamic value calculation (calculated {} values for objects in {} ns)", totalComputed, endingTime);
} }
private void calculateValueMap() { private void calculateValueStackMap() {
SortedMap<EnergyValue, List<WrappedStack>> tempValueMap = new TreeMap<>(); SortedMap<EnergyValue, List<WrappedStack>> tempValueMap = new TreeMap<>();
@ -594,7 +589,7 @@ public class NewEnergyValueRegistry {
} }
} }
} }
valueMap = ImmutableSortedMap.copyOf(tempValueMap); valueStackMap = ImmutableSortedMap.copyOf(tempValueMap);
} }
/** /**
@ -602,9 +597,9 @@ public class NewEnergyValueRegistry {
*/ */
public void save() { public void save() {
writeToJsonFile(stackMap, energyValuesFile); writeToJsonFile(stackValueMap, energyValuesFile);
writeToJsonFile(preCalculationValueMap, preCalculationValuesFile); writeToJsonFile(preCalculationStackValueMap, preCalculationValuesFile);
writeToJsonFile(postCalculationValueMap, postCalculationValuesFile); writeToJsonFile(postCalculationStackValueMap, postCalculationValuesFile);
} }
/** /**
@ -616,13 +611,13 @@ public class NewEnergyValueRegistry {
public void load() { public void load() {
try { try {
preCalculationValueMap.putAll(readFromJsonFile(preCalculationValuesFile)); preCalculationStackValueMap.putAll(readFromJsonFile(preCalculationValuesFile));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// TODO Log that no pre-calculation values were loaded from file because file wasn't found // TODO Log that no pre-calculation values were loaded from file because file wasn't found
} }
try { try {
postCalculationValueMap.putAll(readFromJsonFile(postCalculationValuesFile)); postCalculationStackValueMap.putAll(readFromJsonFile(postCalculationValuesFile));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// TODO Log that no post-calculation values were loaded from file because file wasn't found // TODO Log that no post-calculation values were loaded from file because file wasn't found
} }
@ -630,7 +625,8 @@ public class NewEnergyValueRegistry {
try { try {
ImmutableSortedMap.Builder<WrappedStack, EnergyValue> stackMapBuilder = ImmutableSortedMap.naturalOrder(); ImmutableSortedMap.Builder<WrappedStack, EnergyValue> stackMapBuilder = ImmutableSortedMap.naturalOrder();
stackMapBuilder.putAll(readFromJsonFile(energyValuesFile)); stackMapBuilder.putAll(readFromJsonFile(energyValuesFile));
stackMap = stackMapBuilder.build(); stackValueMap = stackMapBuilder.build();
calculateValueStackMap();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
LogHelper.warn("No calculated energy value file found, regenerating"); // TODO Better log message LogHelper.warn("No calculated energy value file found, regenerating"); // TODO Better log message
compute(); compute();