From 7efb66ec7ff46636f5f791a3e9178305d58a43a3 Mon Sep 17 00:00:00 2001 From: Pahimar Date: Sun, 15 May 2016 10:31:10 -0400 Subject: [PATCH] Fixing up DynEMC calculations. Sorely needed, and still a work in progress --- .../api/exchange/IEnergyValueProvider.java | 6 +- .../ee3/exchange/EnergyValueRegistry.java | 9 +- .../ee3/exchange/NewEnergyValueRegistry.java | 117 ++++++++++++++++-- .../pahimar/ee3/util/EnergyValueHelper.java | 39 +++++- .../pahimar/ee3/util/SerializationHelper.java | 66 ---------- .../serialize/WrappedStackSerializer.java | 1 + 6 files changed, 152 insertions(+), 86 deletions(-) diff --git a/src/main/java/com/pahimar/ee3/api/exchange/IEnergyValueProvider.java b/src/main/java/com/pahimar/ee3/api/exchange/IEnergyValueProvider.java index f3baeecf..d1d047b1 100644 --- a/src/main/java/com/pahimar/ee3/api/exchange/IEnergyValueProvider.java +++ b/src/main/java/com/pahimar/ee3/api/exchange/IEnergyValueProvider.java @@ -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); } diff --git a/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java b/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java index 09a4f75c..289b2aa9 100644 --- a/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java +++ b/src/main/java/com/pahimar/ee3/exchange/EnergyValueRegistry.java @@ -81,13 +81,13 @@ public class EnergyValueRegistry implements JsonSerializer, { 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, 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, } else if (!strict) { - if (wrappedObject instanceof ItemStack) - { + if (wrappedObject instanceof ItemStack) { EnergyValue lowestValue = null; ItemStack wrappedItemStack = (ItemStack) wrappedObject; diff --git a/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java b/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java index c390c6b5..f3075b14 100644 --- a/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java +++ b/src/main/java/com/pahimar/ee3/exchange/NewEnergyValueRegistry.java @@ -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 * isPreCalculationAssignment 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 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 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 readFromJsonFile(File file) throws FileNotFoundException { + + Map 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; + } } diff --git a/src/main/java/com/pahimar/ee3/util/EnergyValueHelper.java b/src/main/java/com/pahimar/ee3/util/EnergyValueHelper.java index f2309e2e..759ff9fd 100644 --- a/src/main/java/com/pahimar/ee3/util/EnergyValueHelper.java +++ b/src/main/java/com/pahimar/ee3/util/EnergyValueHelper.java @@ -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 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 stackValueMappings, WrappedStack outputStack, List inputStacks) { float computedValue = 0f; diff --git a/src/main/java/com/pahimar/ee3/util/SerializationHelper.java b/src/main/java/com/pahimar/ee3/util/SerializationHelper.java index 35b321c2..5ecc1af4 100644 --- a/src/main/java/com/pahimar/ee3/util/SerializationHelper.java +++ b/src/main/java/com/pahimar/ee3/util/SerializationHelper.java @@ -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 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 readFromJsonFile(File file) throws FileNotFoundException { - - Map 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 energyValueMap) { File energyValuesDataDirectory = new File(FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getSaveHandler().getWorldDirectory(), "data" + File.separator + Reference.LOWERCASE_MOD_ID + File.separator + "energyvalues"); diff --git a/src/main/java/com/pahimar/ee3/util/serialize/WrappedStackSerializer.java b/src/main/java/com/pahimar/ee3/util/serialize/WrappedStackSerializer.java index 5144e2d7..5d42c204 100644 --- a/src/main/java/com/pahimar/ee3/util/serialize/WrappedStackSerializer.java +++ b/src/main/java/com/pahimar/ee3/util/serialize/WrappedStackSerializer.java @@ -18,6 +18,7 @@ public class WrappedStackSerializer implements JsonSerializer, 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()) {