More serialization work, looking a lot cleaner than before but definitely still some improvements possible
This commit is contained in:
parent
0e9f732a0b
commit
c8404fd6ca
|
@ -31,7 +31,6 @@ import cpw.mods.fml.common.network.NetworkRegistry;
|
|||
import cpw.mods.fml.common.registry.GameRegistry;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
@Mod(modid = Reference.MOD_ID, name = Reference.MOD_NAME, certificateFingerprint = Reference.FINGERPRINT, version = Reference.MOD_VERSION, dependencies = Reference.DEPENDENCIES, guiFactory = Reference.GUI_FACTORY_CLASS)
|
||||
public class EquivalentExchange3
|
||||
|
@ -121,13 +120,8 @@ public class EquivalentExchange3
|
|||
CachedOreDictionary.getInstance();
|
||||
Abilities.initNotLearnables();
|
||||
|
||||
NewEnergyValueRegistry.INSTANCE.load();
|
||||
|
||||
try {
|
||||
NewEnergyValueRegistry.INSTANCE.save();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
NewEnergyValueRegistry.INSTANCE.load();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
|
|
@ -22,6 +22,10 @@ public final class EnergyValue implements Comparable<EnergyValue>, JsonDeseriali
|
|||
this.energyValue = energyValue;
|
||||
}
|
||||
|
||||
public EnergyValue(Number energyValue) {
|
||||
this.energyValue = energyValue.floatValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object)
|
||||
{
|
||||
|
@ -96,7 +100,6 @@ public final class EnergyValue implements Comparable<EnergyValue>, JsonDeseriali
|
|||
* @param jsonEnergyValue Json encoded String representing a EmcValue object
|
||||
* @return The EmcValue that was encoded as json, or null if a valid EmcValue could not be decoded from given String
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static EnergyValue createFromJson(String jsonEnergyValue)
|
||||
{
|
||||
try
|
||||
|
@ -182,4 +185,11 @@ public final class EnergyValue implements Comparable<EnergyValue>, JsonDeseriali
|
|||
|
||||
return jsonEnergyValue;
|
||||
}
|
||||
|
||||
public enum Phase {
|
||||
PRE_CALCULATION,
|
||||
POST_CALCULATION,
|
||||
RUNTIME,
|
||||
ALL
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,7 +75,7 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
|
|||
if (wrappedStack.getStackSize() > 0)
|
||||
{
|
||||
WrappedStack factoredWrappedStack = WrappedStack.wrap(wrappedStack, 1);
|
||||
EnergyValue factoredEnergyValue = EnergyValueHelper.factorEnergyValue(energyValue, wrappedStack.getStackSize());
|
||||
EnergyValue factoredEnergyValue = EnergyValueHelper.factor(energyValue, wrappedStack.getStackSize());
|
||||
|
||||
if (preCalculationMappings.containsKey(factoredWrappedStack))
|
||||
{
|
||||
|
@ -113,7 +113,7 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
|
|||
if (wrappedStack.getStackSize() > 0)
|
||||
{
|
||||
WrappedStack factoredWrappedStack = WrappedStack.wrap(wrappedStack, 1);
|
||||
EnergyValue factoredEnergyValue = EnergyValueHelper.factorEnergyValue(energyValue, wrappedStack.getStackSize());
|
||||
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);
|
||||
postCalculationMappings.put(factoredWrappedStack, factoredEnergyValue);
|
||||
|
@ -416,7 +416,7 @@ public class EnergyValueRegistry implements JsonSerializer<EnergyValueRegistry>,
|
|||
{
|
||||
if (computedStackValues.get(keyStack) != null && Float.compare(computedStackValues.get(keyStack).getValue(), 0f) > 0)
|
||||
{
|
||||
factoredExchangeEnergyValue = EnergyValueHelper.factorEnergyValue(computedStackValues.get(keyStack), keyStack.getStackSize());
|
||||
factoredExchangeEnergyValue = EnergyValueHelper.factor(computedStackValues.get(keyStack), keyStack.getStackSize());
|
||||
factoredKeyStack = WrappedStack.wrap(keyStack, 1);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,96 +1,124 @@
|
|||
package com.pahimar.ee3.exchange;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSortedMap;
|
||||
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.nbt.NBTTagCompound;
|
||||
import net.minecraft.item.ItemStack;
|
||||
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.*;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class NewEnergyValueRegistry {
|
||||
|
||||
public static final Marker ENERGY_VALUE_MARKER = MarkerManager.getMarker("EE3_ENERGY_VALUE", LogHelper.MOD_MARKER);
|
||||
public static final NewEnergyValueRegistry INSTANCE = new NewEnergyValueRegistry();
|
||||
|
||||
public final Map<WrappedStack, EnergyValue> preCalculationMappings;
|
||||
public final Map<WrappedStack, EnergyValue> postCalculationMappings;
|
||||
private ImmutableSortedMap<WrappedStack, EnergyValue> energyValueMap;
|
||||
private final Map<WrappedStack, EnergyValue> preCalculationValueMap;
|
||||
private final Map<WrappedStack, EnergyValue> postCalculationValueMap;
|
||||
|
||||
public static File energyValuesDataDirectory;
|
||||
public static File energyValuesDataFile;
|
||||
public static File energyValuesDirectory;
|
||||
public static File energyValuesFile;
|
||||
public static File preCalculationValuesFile;
|
||||
public static File postCalculationValuesFile;
|
||||
|
||||
public static final Marker ENERGY_VALUE_MARKER = MarkerManager.getMarker("EE3_ENERGY_VALUE", LogHelper.MOD_MARKER);
|
||||
|
||||
private NewEnergyValueRegistry() {
|
||||
|
||||
preCalculationMappings = new TreeMap<>();
|
||||
ImmutableSortedMap.Builder<WrappedStack, EnergyValue> energyValueMapBuilder = ImmutableSortedMap.naturalOrder();
|
||||
energyValueMap = energyValueMapBuilder.build();
|
||||
|
||||
preCalculationValueMap = new TreeMap<>();
|
||||
postCalculationValueMap = new TreeMap<>();
|
||||
|
||||
// Loading up some dummy values for testing serialization
|
||||
preCalculationMappings.put(WrappedStack.wrap(Items.apple), new EnergyValue(1));
|
||||
preCalculationMappings.put(WrappedStack.wrap(Items.arrow), new EnergyValue(2));
|
||||
preCalculationMappings.put(WrappedStack.wrap(Items.baked_potato), new EnergyValue(3));
|
||||
preCalculationMappings.put(WrappedStack.wrap(Items.bed), new EnergyValue(4));
|
||||
preCalculationMappings.put(WrappedStack.wrap(new OreStack("oreIron")), new EnergyValue(5));
|
||||
|
||||
postCalculationMappings = new TreeMap<>();
|
||||
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));
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
return SerializationHelper.GSON.toJson(this);
|
||||
public ImmutableMap<WrappedStack, EnergyValue> getEnergyValues() {
|
||||
return energyValueMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see net.minecraft.nbt.CompressedStreamTools#safeWrite(NBTTagCompound, File)
|
||||
*/
|
||||
public void save() throws IOException {
|
||||
|
||||
File file = energyValuesDataFile;
|
||||
File tempFile = new File(file.getAbsolutePath() + "_tmp");
|
||||
|
||||
if (tempFile.exists()) {
|
||||
tempFile.delete();
|
||||
public Map<WrappedStack, EnergyValue> getPreCalculationValueMap() {
|
||||
return preCalculationValueMap;
|
||||
}
|
||||
|
||||
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(tempFile));
|
||||
bufferedWriter.write(this.toJson());
|
||||
bufferedWriter.close();
|
||||
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
public Map<WrappedStack, EnergyValue> getPostCalculationValueMap() {
|
||||
return postCalculationValueMap;
|
||||
}
|
||||
|
||||
if (file.exists()) {
|
||||
throw new IOException("Failed to delete " + file);
|
||||
public void set(Object object, EnergyValue energyValue, boolean isPreCalculationAssignment) {
|
||||
|
||||
if (WrappedStack.canBeWrapped(object) && energyValue != null && Float.compare(energyValue.getValue(), 0f) > 0) {
|
||||
|
||||
WrappedStack wrappedStack = WrappedStack.wrap(object, 1);
|
||||
EnergyValue factoredEnergyValue = EnergyValueHelper.factor(energyValue, wrappedStack.getStackSize());
|
||||
|
||||
if (isPreCalculationAssignment) {
|
||||
if (preCalculationValueMap.containsKey(wrappedStack) && factoredEnergyValue.compareTo(preCalculationValueMap.get(wrappedStack)) < 0) {
|
||||
preCalculationValueMap.put(wrappedStack, factoredEnergyValue);
|
||||
}
|
||||
else {
|
||||
tempFile.renameTo(file);
|
||||
preCalculationValueMap.put(wrappedStack, factoredEnergyValue);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (postCalculationValueMap.containsKey(wrappedStack) && factoredEnergyValue.compareTo(postCalculationValueMap.get(wrappedStack)) < 0) {
|
||||
postCalculationValueMap.put(wrappedStack, factoredEnergyValue);
|
||||
}
|
||||
else {
|
||||
postCalculationValueMap.put(wrappedStack, factoredEnergyValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
SerializationHelper.writeToJsonFile(energyValueMap, energyValuesFile);
|
||||
SerializationHelper.writeToJsonFile(preCalculationValueMap, preCalculationValuesFile);
|
||||
SerializationHelper.writeToJsonFile(postCalculationValueMap, postCalculationValuesFile);
|
||||
}
|
||||
|
||||
public void load() {
|
||||
|
||||
File file = energyValuesDataFile;
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
|
||||
|
||||
stringBuilder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
stringBuilder.append(line);
|
||||
}
|
||||
try {
|
||||
preCalculationValueMap.clear();
|
||||
preCalculationValueMap.putAll(SerializationHelper.readFromJsonFile(preCalculationValuesFile));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
// TODO Log that no pre-calculation values were loaded from file because file wasn't found
|
||||
}
|
||||
String jsonRegistryString = stringBuilder.toString();
|
||||
LogHelper.info(jsonRegistryString);
|
||||
|
||||
NewEnergyValueRegistry newEnergyValueRegistry = SerializationHelper.GSON.fromJson(jsonRegistryString, NewEnergyValueRegistry.class);
|
||||
try {
|
||||
postCalculationValueMap.clear();
|
||||
postCalculationValueMap.putAll(SerializationHelper.readFromJsonFile(postCalculationValuesFile));
|
||||
} catch (FileNotFoundException e) {
|
||||
// TODO Log that no pre-calculation values were loaded from file because file wasn't found
|
||||
}
|
||||
|
||||
LogHelper.info(newEnergyValueRegistry);
|
||||
try {
|
||||
ImmutableSortedMap.Builder<WrappedStack, EnergyValue> energyValueMapBuilder = ImmutableSortedMap.naturalOrder();
|
||||
energyValueMapBuilder.putAll(SerializationHelper.readFromJsonFile(energyValuesFile));
|
||||
energyValueMap = energyValueMapBuilder.build();
|
||||
} catch (FileNotFoundException e) {
|
||||
LogHelper.warn("No calculated energy value file found, regenerating");
|
||||
// TODO Gen new values from the loaded pre/post value maps
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -515,10 +515,11 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
|
|||
if (wrappedStack instanceof ItemStack)
|
||||
{
|
||||
ItemStack itemStack = (ItemStack) wrappedStack;
|
||||
String unlocalizedName = "";
|
||||
String unlocalizedName;
|
||||
try
|
||||
{
|
||||
unlocalizedName = itemStack.getUnlocalizedName();
|
||||
// unlocalizedName = itemStack.getUnlocalizedName();
|
||||
unlocalizedName = Item.itemRegistry.getNameForObject(itemStack.getItem());
|
||||
}
|
||||
catch (ArrayIndexOutOfBoundsException e)
|
||||
{
|
||||
|
@ -537,12 +538,12 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
|
|||
else if (wrappedStack instanceof OreStack)
|
||||
{
|
||||
OreStack oreStack = (OreStack) wrappedStack;
|
||||
return String.format("%sxoreStack.%s", stackSize, oreStack.oreName);
|
||||
return String.format("%sxoreStack[%s]", stackSize, oreStack.oreName);
|
||||
}
|
||||
else if (wrappedStack instanceof FluidStack)
|
||||
{
|
||||
FluidStack fluidStack = (FluidStack) wrappedStack;
|
||||
return String.format("%sxfluidStack.%s", stackSize, fluidStack.getFluid().getName());
|
||||
return String.format("%sxfluidStack[%s]", stackSize, fluidStack.getFluid().getName());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -18,77 +18,6 @@ public class Comparators
|
|||
}
|
||||
};
|
||||
|
||||
public static Comparator<ItemStack> idComparator = new Comparator<ItemStack>()
|
||||
{
|
||||
public int compare(ItemStack itemStack1, ItemStack itemStack2)
|
||||
{
|
||||
if (itemStack1 != null && itemStack2 != null)
|
||||
{
|
||||
// Sort on itemID
|
||||
if (Item.getIdFromItem(itemStack1.getItem()) - Item.getIdFromItem(itemStack2.getItem()) == 0)
|
||||
{
|
||||
// Sort on item
|
||||
if (itemStack1.getItem() == itemStack2.getItem())
|
||||
{
|
||||
// Then sort on meta
|
||||
if ((itemStack1.getItemDamage() == itemStack2.getItemDamage()) || itemStack1.getItemDamage() == OreDictionary.WILDCARD_VALUE || itemStack2.getItemDamage() == OreDictionary.WILDCARD_VALUE)
|
||||
{
|
||||
// Then sort on NBT
|
||||
if (itemStack1.hasTagCompound() && itemStack2.hasTagCompound())
|
||||
{
|
||||
// Then sort on stack size
|
||||
if (ItemStack.areItemStackTagsEqual(itemStack1, itemStack2))
|
||||
{
|
||||
return (itemStack1.stackSize - itemStack2.stackSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (itemStack1.getTagCompound().hashCode() - itemStack2.getTagCompound().hashCode());
|
||||
}
|
||||
}
|
||||
else if (!(itemStack1.hasTagCompound()) && itemStack2.hasTagCompound())
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (itemStack1.hasTagCompound() && !(itemStack2.hasTagCompound()))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (itemStack1.stackSize - itemStack2.stackSize);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return (itemStack1.getItemDamage() - itemStack2.getItemDamage());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return itemStack1.getItem().getUnlocalizedName(itemStack1).compareToIgnoreCase(itemStack2.getItem().getUnlocalizedName(itemStack2));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Item.getIdFromItem(itemStack1.getItem()) - Item.getIdFromItem(itemStack2.getItem());
|
||||
}
|
||||
}
|
||||
else if (itemStack1 != null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (itemStack2 != null)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static Comparator<ItemStack> reverseIdComparator = new Comparator<ItemStack>()
|
||||
{
|
||||
@Override
|
||||
|
@ -98,6 +27,65 @@ public class Comparators
|
|||
}
|
||||
};
|
||||
|
||||
public static Comparator<ItemStack> idComparator = new Comparator<ItemStack>() {
|
||||
|
||||
@Override
|
||||
public int compare(ItemStack itemStack1, ItemStack itemStack2) {
|
||||
|
||||
if (itemStack1 != null && itemStack2 != null) {
|
||||
|
||||
String name1 = Item.itemRegistry.getNameForObject(itemStack1.getItem());
|
||||
String name2 = Item.itemRegistry.getNameForObject(itemStack2.getItem());
|
||||
|
||||
if (name1.equalsIgnoreCase(name2)) {
|
||||
// Sort on item
|
||||
if (itemStack1.getItem() == itemStack2.getItem()) {
|
||||
// Then sort on meta
|
||||
if ((itemStack1.getItemDamage() == itemStack2.getItemDamage()) || itemStack1.getItemDamage() == OreDictionary.WILDCARD_VALUE || itemStack2.getItemDamage() == OreDictionary.WILDCARD_VALUE) {
|
||||
// Then sort on NBT
|
||||
if (itemStack1.hasTagCompound() && itemStack2.hasTagCompound()) {
|
||||
// Then sort on stack size
|
||||
if (ItemStack.areItemStackTagsEqual(itemStack1, itemStack2)) {
|
||||
return (itemStack1.stackSize - itemStack2.stackSize);
|
||||
}
|
||||
else {
|
||||
return (itemStack1.getTagCompound().hashCode() - itemStack2.getTagCompound().hashCode());
|
||||
}
|
||||
}
|
||||
else if (!(itemStack1.hasTagCompound()) && itemStack2.hasTagCompound()) {
|
||||
return -1;
|
||||
}
|
||||
else if (itemStack1.hasTagCompound() && !(itemStack2.hasTagCompound())) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return (itemStack1.stackSize - itemStack2.stackSize);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return (itemStack1.getItemDamage() - itemStack2.getItemDamage());
|
||||
}
|
||||
}
|
||||
else {
|
||||
return itemStack1.getItem().getUnlocalizedName(itemStack1).compareToIgnoreCase(itemStack2.getItem().getUnlocalizedName(itemStack2));
|
||||
}
|
||||
}
|
||||
else {
|
||||
return name1.compareToIgnoreCase(name2);
|
||||
}
|
||||
}
|
||||
else if (itemStack1 != null) {
|
||||
return -1;
|
||||
}
|
||||
else if (itemStack2 != null) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static Comparator<ItemStack> displayNameComparator = new Comparator<ItemStack>()
|
||||
{
|
||||
public int compare(ItemStack itemStack1, ItemStack itemStack2)
|
||||
|
|
|
@ -6,11 +6,13 @@ import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
|||
import java.io.File;
|
||||
|
||||
public class Files {
|
||||
|
||||
public static final String ENERGY_VALUES_JSON = "energy-values.json";
|
||||
public static final String PRE_CALCULATION_ENERGY_VALUES = "pre-calculation-energy-values.json";
|
||||
public static final String POST_CALCULATION_ENERGY_VALUES = "post-calculation-energy-values.json";
|
||||
|
||||
public static final String TEMPLATE_JSON_FILE = "template.json";
|
||||
public static final String ABILITIES_JSON_FILE = "abilities.json";
|
||||
public static final String ENERGY_VALUES_JSON = "energy-values.json";
|
||||
|
||||
public static File dataDirectory;
|
||||
|
||||
|
@ -24,9 +26,11 @@ public class Files {
|
|||
|
||||
dataDirectory = new File(event.getModConfigurationDirectory().getParentFile(), "data" + File.separator + Reference.LOWERCASE_MOD_ID);
|
||||
|
||||
NewEnergyValueRegistry.energyValuesDataDirectory = new File(dataDirectory, "energy-values");
|
||||
NewEnergyValueRegistry.energyValuesDataDirectory.mkdirs();
|
||||
NewEnergyValueRegistry.energyValuesDataFile = new File(NewEnergyValueRegistry.energyValuesDataDirectory, ENERGY_VALUES_JSON);
|
||||
NewEnergyValueRegistry.energyValuesDirectory = new File(dataDirectory, "energy-values");
|
||||
NewEnergyValueRegistry.energyValuesDirectory.mkdirs();
|
||||
NewEnergyValueRegistry.energyValuesFile = new File(NewEnergyValueRegistry.energyValuesDirectory, ENERGY_VALUES_JSON);
|
||||
NewEnergyValueRegistry.preCalculationValuesFile = new File(NewEnergyValueRegistry.energyValuesDirectory, PRE_CALCULATION_ENERGY_VALUES);
|
||||
NewEnergyValueRegistry.postCalculationValuesFile = new File(NewEnergyValueRegistry.energyValuesDirectory, POST_CALCULATION_ENERGY_VALUES);
|
||||
|
||||
abilitiesDataDirectory = new File(dataDirectory, "abilities");
|
||||
abilitiesDataDirectory.mkdirs();
|
||||
|
|
|
@ -99,15 +99,15 @@ public class EnergyValueHelper
|
|||
}
|
||||
}
|
||||
|
||||
return factorEnergyValue(new EnergyValue(computedValue), outputStack.getStackSize());
|
||||
return factor(new EnergyValue(computedValue), outputStack.getStackSize());
|
||||
}
|
||||
|
||||
public static EnergyValue factorEnergyValue(EnergyValue energyValue, int factor)
|
||||
public static EnergyValue factor(EnergyValue energyValue, int factor)
|
||||
{
|
||||
return factorEnergyValue(energyValue, (float) factor);
|
||||
return factor(energyValue, (float) factor);
|
||||
}
|
||||
|
||||
public static EnergyValue factorEnergyValue(EnergyValue energyValue, float factor)
|
||||
public static EnergyValue factor(EnergyValue energyValue, float factor)
|
||||
{
|
||||
if ((Float.compare(factor, 0f) != 0) && (energyValue != null))
|
||||
{
|
||||
|
|
|
@ -1,12 +1,17 @@
|
|||
package com.pahimar.ee3.util;
|
||||
|
||||
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;
|
||||
import com.pahimar.ee3.exchange.*;
|
||||
import com.pahimar.ee3.exchange.EnergyValueRegistry;
|
||||
import com.pahimar.ee3.exchange.EnergyValueStackMapping;
|
||||
import com.pahimar.ee3.exchange.OreStack;
|
||||
import com.pahimar.ee3.exchange.WrappedStack;
|
||||
import com.pahimar.ee3.knowledge.TransmutationKnowledge;
|
||||
import com.pahimar.ee3.reference.Reference;
|
||||
import com.pahimar.ee3.util.serialize.*;
|
||||
|
@ -14,14 +19,17 @@ 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;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
|
||||
public class SerializationHelper {
|
||||
|
||||
public static final Type ENERGY_VALUE_MAP_TYPE = new TypeToken<Map<WrappedStack, EnergyValue>>(){}.getType();
|
||||
public static final Gson GSON = new GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.enableComplexMapKeySerialization()
|
||||
|
@ -29,7 +37,7 @@ public class SerializationHelper {
|
|||
.registerTypeAdapter(OreStack.class, new OreStackSerializer())
|
||||
.registerTypeAdapter(FluidStack.class, new FluidStackSerializer())
|
||||
.registerTypeAdapter(WrappedStack.class, new WrappedStackSerializer())
|
||||
.registerTypeAdapter(NewEnergyValueRegistry.class, new NewEnergyValueRegistrySerializer())
|
||||
.registerTypeAdapter(ENERGY_VALUE_MAP_TYPE, new EnergyValueMapSerializer())
|
||||
.create();
|
||||
|
||||
private static File instanceDataDirectory;
|
||||
|
@ -179,6 +187,70 @@ 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");
|
||||
|
|
|
@ -0,0 +1,98 @@
|
|||
package com.pahimar.ee3.util.serialize;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.pahimar.ee3.api.exchange.EnergyValue;
|
||||
import com.pahimar.ee3.exchange.OreStack;
|
||||
import com.pahimar.ee3.exchange.WrappedStack;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class EnergyValueMapSerializer implements JsonSerializer<Map<WrappedStack, EnergyValue>>, JsonDeserializer<Map<WrappedStack, EnergyValue>> {
|
||||
|
||||
private static final String ENERGY_VALUE = "energyValue";
|
||||
private static final String TYPE_ITEM_STACK = "itemstack";
|
||||
private static final String TYPE_ORE_STACK = "orestack";
|
||||
private static final String TYPE_FLUID_STACK = "fluidstack";
|
||||
|
||||
@Override
|
||||
public Map<WrappedStack, EnergyValue> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
|
||||
Map<WrappedStack, EnergyValue> valueMap = new TreeMap<>();
|
||||
|
||||
if (json.isJsonArray()) {
|
||||
|
||||
JsonArray jsonArray = json.getAsJsonArray();
|
||||
Iterator<JsonElement> jsonArrayIterator = jsonArray.iterator();
|
||||
|
||||
while (jsonArrayIterator.hasNext()) {
|
||||
|
||||
JsonElement jsonElement = jsonArrayIterator.next();
|
||||
if (validateValueMapping(jsonElement)) {
|
||||
|
||||
JsonObject jsonValueMapping = jsonElement.getAsJsonObject();
|
||||
|
||||
WrappedStack wrappedStack = null;
|
||||
EnergyValue energyValue = new EnergyValue(jsonValueMapping.get(ENERGY_VALUE).getAsNumber());
|
||||
|
||||
if (jsonValueMapping.has(TYPE_ITEM_STACK)) {
|
||||
wrappedStack = WrappedStack.wrap(context.deserialize(jsonValueMapping.get(TYPE_ITEM_STACK), ItemStack.class));
|
||||
}
|
||||
else if (jsonValueMapping.has(TYPE_ORE_STACK)) {
|
||||
wrappedStack = WrappedStack.wrap(context.deserialize(jsonValueMapping.get(TYPE_ORE_STACK), OreStack.class));
|
||||
}
|
||||
else if (jsonValueMapping.has(TYPE_FLUID_STACK)) {
|
||||
wrappedStack = WrappedStack.wrap(context.deserialize(jsonValueMapping.get(TYPE_FLUID_STACK), FluidStack.class));
|
||||
}
|
||||
|
||||
if (wrappedStack != null && energyValue != null) {
|
||||
valueMap.put(wrappedStack, energyValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return valueMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(Map<WrappedStack, EnergyValue> src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
|
||||
if (src != null) {
|
||||
for (WrappedStack wrappedStack : src.keySet()) {
|
||||
JsonObject jsonMapping = new JsonObject();
|
||||
jsonMapping.add(wrappedStack.getWrappedObject().getClass().getSimpleName().toLowerCase(), context.serialize(wrappedStack.getWrappedObject()));
|
||||
jsonMapping.addProperty(ENERGY_VALUE, src.get(wrappedStack).getValue());
|
||||
jsonArray.add(jsonMapping);
|
||||
}
|
||||
}
|
||||
|
||||
return jsonArray;
|
||||
}
|
||||
|
||||
/***
|
||||
* Validates whether or not the provided {@link JsonElement} is a valid json encoded {@link WrappedStack}:{@link EnergyValue} mapping
|
||||
*
|
||||
* @param jsonElement the {@link JsonElement} being validated
|
||||
* @return true if jsonElement is a valid json encoded {@link WrappedStack}:{@link EnergyValue} mapping, false otherwise
|
||||
*/
|
||||
private static boolean validateValueMapping(JsonElement jsonElement) {
|
||||
|
||||
if (jsonElement.isJsonObject()) {
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
if (jsonObject.entrySet().size() == 2 && jsonObject.has(ENERGY_VALUE) && jsonObject.get(ENERGY_VALUE).isJsonPrimitive()) {
|
||||
return (jsonObject.has(TYPE_ITEM_STACK) && jsonObject.get(TYPE_ITEM_STACK).isJsonObject()) ||
|
||||
(jsonObject.has(TYPE_ORE_STACK) && jsonObject.get(TYPE_ORE_STACK).isJsonObject()) ||
|
||||
(jsonObject.has(TYPE_FLUID_STACK) && jsonObject.get(TYPE_FLUID_STACK).isJsonObject());
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -26,7 +26,7 @@ public class FluidStackSerializer implements JsonSerializer<FluidStack>, JsonDes
|
|||
NBTTagCompound tagCompound = null;
|
||||
|
||||
try {
|
||||
if (jsonObject.get(NAME).getAsJsonPrimitive().isString()) {
|
||||
if (jsonObject.has(NAME) && jsonObject.get(NAME).getAsJsonPrimitive().isString()) {
|
||||
name = jsonObject.get(NAME).getAsString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ public class ItemStackSerializer implements JsonSerializer<ItemStack>, JsonDeser
|
|||
NBTTagCompound tagCompound = null;
|
||||
|
||||
try {
|
||||
if (jsonObject.get(NAME).getAsJsonPrimitive().isString()) {
|
||||
if (jsonObject.has(NAME) && jsonObject.get(NAME).getAsJsonPrimitive().isString()) {
|
||||
name = jsonObject.get(NAME).getAsString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
package com.pahimar.ee3.util.serialize;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.pahimar.ee3.api.exchange.EnergyValue;
|
||||
import com.pahimar.ee3.exchange.NewEnergyValueRegistry;
|
||||
import com.pahimar.ee3.exchange.WrappedStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
public class NewEnergyValueRegistrySerializer implements JsonSerializer<NewEnergyValueRegistry>, JsonDeserializer<NewEnergyValueRegistry> {
|
||||
|
||||
private static final String PRE_CALCULATION_ASSIGNMENTS = "pre_calculation_assignments";
|
||||
private static final String POST_CALCULATION_ASSIGNMENTS = "post_calculation_assignments";
|
||||
private static final String ENERGY_VALUE = "energyValue";
|
||||
|
||||
@Override
|
||||
public NewEnergyValueRegistry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
|
||||
if (json.isJsonObject()) {
|
||||
|
||||
JsonObject jsonObject = json.getAsJsonObject();
|
||||
|
||||
if (jsonObject.has(PRE_CALCULATION_ASSIGNMENTS) && jsonObject.get(PRE_CALCULATION_ASSIGNMENTS).isJsonArray()) {
|
||||
JsonArray preCalculationArray = jsonObject.get(PRE_CALCULATION_ASSIGNMENTS).getAsJsonArray();
|
||||
|
||||
Iterator<JsonElement> iterator = preCalculationArray.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
JsonElement jsonElement = iterator.next();
|
||||
if (jsonElement.isJsonObject()) {
|
||||
JsonObject jsonMapping = jsonElement.getAsJsonObject();
|
||||
// TODO This
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (jsonObject.has(POST_CALCULATION_ASSIGNMENTS) && jsonObject.get(POST_CALCULATION_ASSIGNMENTS).isJsonArray()) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(NewEnergyValueRegistry src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
|
||||
if (src != null) {
|
||||
JsonObject registryObject = new JsonObject();
|
||||
|
||||
registryObject.add(PRE_CALCULATION_ASSIGNMENTS, serialize(src.preCalculationMappings, context));
|
||||
registryObject.add(POST_CALCULATION_ASSIGNMENTS, serialize(src.postCalculationMappings, context));
|
||||
|
||||
return registryObject;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static JsonArray serialize(Map<WrappedStack, EnergyValue> valueMap, JsonSerializationContext context) {
|
||||
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
|
||||
for (WrappedStack wrappedStack : valueMap.keySet()) {
|
||||
JsonObject jsonMapping = new JsonObject();
|
||||
jsonMapping.add(wrappedStack.getWrappedObject().getClass().getSimpleName().toLowerCase(), context.serialize(wrappedStack.getWrappedObject()));
|
||||
jsonMapping.addProperty(ENERGY_VALUE, valueMap.get(wrappedStack).getValue());
|
||||
jsonArray.add(jsonMapping);
|
||||
}
|
||||
|
||||
return jsonArray;
|
||||
}
|
||||
}
|
|
@ -16,7 +16,7 @@ public class OreStackSerializer implements JsonSerializer<OreStack>, JsonDeseria
|
|||
JsonObject jsonObject = (JsonObject) json;
|
||||
|
||||
try {
|
||||
if (jsonObject.get(NAME).getAsJsonPrimitive().isString()) {
|
||||
if (jsonObject.has(NAME) && jsonObject.get(NAME).getAsJsonPrimitive().isString()) {
|
||||
String name = jsonObject.get(NAME).getAsString();
|
||||
return new OreStack(name);
|
||||
}
|
||||
|
|
|
@ -34,8 +34,6 @@ public class WrappedStackSerializer implements JsonSerializer<WrappedStack>, Jso
|
|||
catch (IllegalStateException exception) {
|
||||
}
|
||||
|
||||
// TODO COME BACK HERE AND MAKE THIS CLEANER FOR THE SAKE OF PACK DEVS AND USERS ALIKE
|
||||
|
||||
if (jsonObject.get(DATA).isJsonObject()) {
|
||||
JsonObject data = jsonObject.getAsJsonObject(DATA);
|
||||
|
||||
|
|
BIN
src/main/resources/assets/ee3/textures/arrays/transmutation.png
Normal file
BIN
src/main/resources/assets/ee3/textures/arrays/transmutation.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 134 KiB |
Loading…
Reference in a new issue