Fixing up DynEMC calculations. Sorely needed, and still a work in progress

This commit is contained in:
Pahimar 2016-05-15 10:31:10 -04:00
parent 22c8dee145
commit 7efb66ec7f
6 changed files with 152 additions and 86 deletions

View file

@ -2,7 +2,7 @@ package com.pahimar.ee3.api.exchange;
import net.minecraft.item.ItemStack;
public interface IEnergyValueProvider
{
public abstract EnergyValue getEnergyValue(ItemStack itemStack);
public interface IEnergyValueProvider {
EnergyValue getEnergyValue(ItemStack itemStack);
}

View file

@ -81,13 +81,13 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
{
if (factoredEnergyValue.compareTo(preCalculationMappings.get(factoredWrappedStack)) < 0)
{
LogHelper.trace(PRE_CALC_MARKER, "[{}] Mod with ID '{}' set a pre-calculation energy value of {} for object {}", LoaderHelper.getLoaderState(), Loader.instance().activeModContainer().getModId(), energyValue, wrappedStack);
LogHelper.trace(PRE_CALC_MARKER, "[{}] Mod with ID '{}' setEnergyValue a pre-calculation energy value of {} for object {}", LoaderHelper.getLoaderState(), Loader.instance().activeModContainer().getModId(), energyValue, wrappedStack);
preCalculationMappings.put(factoredWrappedStack, factoredEnergyValue);
}
}
else
{
LogHelper.trace(PRE_CALC_MARKER, "[{}] Mod with ID '{}' set a pre-calculation energy value of {} for object {}", LoaderHelper.getLoaderState(), Loader.instance().activeModContainer().getModId(), energyValue, wrappedStack);
LogHelper.trace(PRE_CALC_MARKER, "[{}] Mod with ID '{}' setEnergyValue a pre-calculation energy value of {} for object {}", LoaderHelper.getLoaderState(), Loader.instance().activeModContainer().getModId(), energyValue, wrappedStack);
preCalculationMappings.put(factoredWrappedStack, factoredEnergyValue);
}
}
@ -115,7 +115,7 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
WrappedStack factoredWrappedStack = WrappedStack.wrap(wrappedStack, 1);
EnergyValue factoredEnergyValue = EnergyValueHelper.factor(energyValue, wrappedStack.getStackSize());
LogHelper.trace(POST_CALC_MARKER, "[{}] Mod with ID '{}' set a post-calculation energy value of {} for object {}", LoaderHelper.getLoaderState(), Loader.instance().activeModContainer().getModId(), energyValue, wrappedStack);
LogHelper.trace(POST_CALC_MARKER, "[{}] Mod with ID '{}' setEnergyValue a post-calculation energy value of {} for object {}", LoaderHelper.getLoaderState(), Loader.instance().activeModContainer().getModId(), energyValue, wrappedStack);
postCalculationMappings.put(factoredWrappedStack, factoredEnergyValue);
}
}
@ -208,8 +208,7 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
}
else if (!strict)
{
if (wrappedObject instanceof ItemStack)
{
if (wrappedObject instanceof ItemStack) {
EnergyValue lowestValue = null;
ItemStack wrappedItemStack = (ItemStack) wrappedObject;

View file

@ -2,19 +2,20 @@ package com.pahimar.ee3.exchange;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.gson.JsonParseException;
import com.pahimar.ee3.api.exchange.EnergyValue;
import com.pahimar.ee3.util.EnergyValueHelper;
import com.pahimar.ee3.util.LogHelper;
import com.pahimar.ee3.util.SerializationHelper;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.*;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeMap;
@ -83,19 +84,51 @@ public class NewEnergyValueRegistry {
return postCalculationValueMap;
}
public boolean hasEnergyValue(Object object) {
return hasEnergyValue(object, false);
}
public boolean hasEnergyValue(Object object, boolean strict) {
// TODO This
return false;
}
public EnergyValue getEnergyValue(Object object) {
return getEnergyValue(object, false);
}
public EnergyValue getEnergyValue(Object object, boolean strict) {
// TODO This
return null;
}
/**
* Sets an {@link EnergyValue} for the provided {@link Object} (if it can be wrapped in a {@link WrappedStack}.
* Depending on whether or not this is a pre-calculation value assignment it's also possible for the calculated
* energy value map to be recomputed to take into account the new mapping.
*
* @param object the object the energy value is being assigned for
* @param energyValue the energy value being set on the object
* @param energyValue the energy value being setEnergyValue on the object
* @param isPreCalculationAssignment whether or not the calculated energy value assignment is a pre-calculation
* value assignment or not
*/
public void setEnergyValue(Object object, EnergyValue energyValue, boolean isPreCalculationAssignment) {
setEnergyValue(object, energyValue, isPreCalculationAssignment, false);
}
/**
* Sets an {@link EnergyValue} for the provided {@link Object} (if it can be wrapped in a {@link WrappedStack}.
* Depending on whether or not this is a pre-calculation value assignment it's also possible for the calculated
* energy value map to be recomputed to take into account the new mapping.
*
* @param object the object the energy value is being assigned for
* @param energyValue the energy value being setEnergyValue on the object
* @param isPreCalculationAssignment whether or not the calculated energy value assignment is a pre-calculation
* value assignment or not
* @param doRegenValues whether or not the energy value map needs recomputing. Only an option if
* <code>isPreCalculationAssignment</code> is true
*/
public void set(Object object, EnergyValue energyValue, boolean isPreCalculationAssignment, boolean doRegenValues) {
public void setEnergyValue(Object object, EnergyValue energyValue, boolean isPreCalculationAssignment, boolean doRegenValues) {
if (WrappedStack.canBeWrapped(object) && energyValue != null && Float.compare(energyValue.getValue(), 0f) > 0) {
@ -185,9 +218,9 @@ public class NewEnergyValueRegistry {
* Saves the pre-calculation, post-calculation, and calculated energy value maps to disk
*/
public void save() {
SerializationHelper.writeToJsonFile(energyValueMap, energyValuesFile);
SerializationHelper.writeToJsonFile(preCalculationValueMap, preCalculationValuesFile);
SerializationHelper.writeToJsonFile(postCalculationValueMap, postCalculationValuesFile);
writeToJsonFile(energyValueMap, energyValuesFile);
writeToJsonFile(preCalculationValueMap, preCalculationValuesFile);
writeToJsonFile(postCalculationValueMap, postCalculationValuesFile);
}
/**
@ -199,24 +232,88 @@ public class NewEnergyValueRegistry {
public void load() {
try {
preCalculationValueMap.putAll(SerializationHelper.readFromJsonFile(preCalculationValuesFile));
preCalculationValueMap.putAll(readFromJsonFile(preCalculationValuesFile));
} catch (FileNotFoundException e) {
// TODO Log that no pre-calculation values were loaded from file because file wasn't found
}
try {
postCalculationValueMap.putAll(SerializationHelper.readFromJsonFile(postCalculationValuesFile));
postCalculationValueMap.putAll(readFromJsonFile(postCalculationValuesFile));
} catch (FileNotFoundException e) {
// TODO Log that no post-calculation values were loaded from file because file wasn't found
}
try {
ImmutableSortedMap.Builder<WrappedStack, EnergyValue> energyValueMapBuilder = ImmutableSortedMap.naturalOrder();
energyValueMapBuilder.putAll(SerializationHelper.readFromJsonFile(energyValuesFile));
energyValueMapBuilder.putAll(readFromJsonFile(energyValuesFile));
energyValueMap = energyValueMapBuilder.build();
} catch (FileNotFoundException e) {
LogHelper.warn("No calculated energy value file found, regenerating"); // TODO Better log message
compute();
}
}
/**
* @see net.minecraft.nbt.CompressedStreamTools#safeWrite(NBTTagCompound, File)
*/
private static void writeToJsonFile(Map<WrappedStack, EnergyValue> valueMap, File file) {
File tempFile = new File(file.getAbsolutePath() + "_tmp");
if (tempFile.exists()) {
tempFile.delete();
}
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(tempFile))) {
bufferedWriter.write(SerializationHelper.GSON.toJson(valueMap, SerializationHelper.ENERGY_VALUE_MAP_TYPE));
bufferedWriter.close();
}
catch (IOException exception) {
exception.printStackTrace(); // TODO Better logging of the exception
}
if (file.exists()) {
file.delete();
}
if (file.exists()) {
LogHelper.warn("Failed to delete " + file);
}
else {
tempFile.renameTo(file);
}
}
private static Map<WrappedStack, EnergyValue> readFromJsonFile(File file) throws FileNotFoundException {
Map<WrappedStack, EnergyValue> valueMap = new TreeMap<>();
StringBuilder jsonStringBuilder = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
jsonStringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
jsonStringBuilder.append(line);
}
}
catch (IOException exception) {
if (exception instanceof FileNotFoundException) {
throw (FileNotFoundException) exception;
}
else {
exception.printStackTrace(); // TODO Better logging of the exception (other)
}
}
try {
valueMap = SerializationHelper.GSON.fromJson(jsonStringBuilder.toString(), SerializationHelper.ENERGY_VALUE_MAP_TYPE);
}
catch (JsonParseException exception) {
// TODO Better logging of the exception (failed parsing so no values loaded)
}
return valueMap;
}
}

View file

@ -1,6 +1,7 @@
package com.pahimar.ee3.util;
import com.pahimar.ee3.api.exchange.EnergyValue;
import com.pahimar.ee3.api.exchange.IEnergyValueProvider;
import com.pahimar.ee3.exchange.EnergyValueRegistry;
import com.pahimar.ee3.exchange.OreStack;
import com.pahimar.ee3.exchange.WrappedStack;
@ -12,8 +13,42 @@ import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
public class EnergyValueHelper
{
public class EnergyValueHelper {
// FIXME Priority Number 1 here
public static EnergyValue getEnergyValue(Map<WrappedStack, EnergyValue> valueMap, Object object, boolean strict) {
/**
* Priority of checking goes
* 1 - IEnergyValueProvider implementation
* 2 - Exact match
* 3 - If the object is an ItemStack, various checks in the OreDictionary
* THINK ON THIS BECAUSE THIS LOGIC COULD BE FAULTY FOR COMPUTATION PURPOSES
* 1 - Does the parent OreStack have a non-null value
* 2 - Do all sibling members have the same non-null value
* 3 - Does there exist a wildcard value mapping with a value
* 4 - If the object is an OreStack, check all child ItemStacks to see if they have the same (not null) value
*/
if (WrappedStack.canBeWrapped(object)) {
if (object instanceof ItemStack && ((ItemStack) object).getItem() instanceof IEnergyValueProvider && !strict) {
}
else if (valueMap != null) {
WrappedStack wrappedObject = WrappedStack.wrap(object, 1);
if (valueMap.containsKey(wrappedObject)) {
return valueMap.get(wrappedObject);
}
else if (!strict) {
}
}
}
return null;
}
public static EnergyValue computeEnergyValueFromRecipe(Map<WrappedStack, EnergyValue> stackValueMappings, WrappedStack outputStack, List<WrappedStack> inputStacks)
{
float computedValue = 0f;

View file

@ -4,7 +4,6 @@ import com.google.common.io.Files;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.pahimar.ee3.api.exchange.EnergyValue;
@ -19,7 +18,6 @@ import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ModContainer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.FluidStack;
import org.apache.commons.codec.digest.DigestUtils;
@ -187,70 +185,6 @@ public class SerializationHelper {
return energyValueStackMap;
}
/**
* @see net.minecraft.nbt.CompressedStreamTools#safeWrite(NBTTagCompound, File)
*/
public static void writeToJsonFile(Map<WrappedStack, EnergyValue> valueMap, File file) {
File tempFile = new File(file.getAbsolutePath() + "_tmp");
if (tempFile.exists()) {
tempFile.delete();
}
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(tempFile))) {
bufferedWriter.write(SerializationHelper.GSON.toJson(valueMap, ENERGY_VALUE_MAP_TYPE));
bufferedWriter.close();
}
catch (IOException exception) {
exception.printStackTrace(); // TODO Better logging of the exception
}
if (file.exists()) {
file.delete();
}
if (file.exists()) {
LogHelper.warn("Failed to delete " + file);
}
else {
tempFile.renameTo(file);
}
}
public static Map<WrappedStack, EnergyValue> readFromJsonFile(File file) throws FileNotFoundException {
Map<WrappedStack, EnergyValue> valueMap = new TreeMap<>();
StringBuilder jsonStringBuilder = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
jsonStringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
jsonStringBuilder.append(line);
}
}
catch (IOException exception) {
if (exception instanceof FileNotFoundException) {
throw (FileNotFoundException) exception;
}
else {
exception.printStackTrace(); // TODO Better logging of the exception (other)
}
}
try {
valueMap = SerializationHelper.GSON.fromJson(jsonStringBuilder.toString(), ENERGY_VALUE_MAP_TYPE);
}
catch (JsonParseException exception) {
// TODO Better logging of the exception (failed parsing so no values loaded)
}
return valueMap;
}
public static void writeEnergyValueStackMapToJsonFile(String fileName, Map<WrappedStack, EnergyValue> energyValueMap)
{
File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues");

View file

@ -18,6 +18,7 @@ public class WrappedStackSerializer implements JsonSerializer<WrappedStack>, Jso
private static final String TYPE_FLUIDSTACK = "fluidstack";
@Override
// TODO Update deserialize to match up with new serialize method implementation
public WrappedStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonObject()) {