Work on serializing/deserializing abilities to disk (cause why not give map makers more power?)

This commit is contained in:
pahimar 2015-02-07 23:12:25 -05:00
parent 100d2e7208
commit e8e18fede1
7 changed files with 197 additions and 121 deletions

View file

@ -73,7 +73,7 @@ public class EquivalentExchange3
EnergyValues.addDefaultEnergyValues();
Skills.addDefaultSkills();
Abilities.setOresNotLearnable();
}
@EventHandler
@ -130,6 +130,8 @@ public class EquivalentExchange3
TransmutationKnowledgeRegistry.getInstance().saveAll();
WorldEventHandler.hasInitilialized = false;
AbilityRegistry.getInstance().saveToFile(new File(SerializationHelper.getDataDirectory(), "abilities.json"));
}
public EnergyValueRegistry getEnergyValueRegistry()

View file

@ -2,136 +2,73 @@ package com.pahimar.ee3.api;
import com.pahimar.ee3.EquivalentExchange3;
import cpw.mods.fml.common.Mod;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
public final class AbilityRegistryProxy
{
@Mod.Instance("EE3")
private static Object ee3Mod;
public static boolean isLearnable(Block block)
{
return isLearnable(new ItemStack(block));
}
public static boolean isLearnable(Item item)
{
return isLearnable(new ItemStack(item));
}
public static boolean isLearnable(ItemStack itemStack)
public static boolean isLearnable(Object object)
{
init();
if (ee3Mod != null)
{
return EE3Wrapper.ee3mod.getAbilityRegistry().isLearnable(itemStack);
return EE3Wrapper.ee3mod.getAbilityRegistry().isLearnable(object);
}
return false;
}
public static void setAsLearnable(Block block)
{
setAsLearnable(new ItemStack(block));
}
public static void setAsLearnable(Item item)
{
setAsLearnable(new ItemStack(item));
}
public static void setAsLearnable(ItemStack itemStack)
public static void setAsLearnable(Object object)
{
init();
if (ee3Mod != null)
{
EE3Wrapper.ee3mod.getAbilityRegistry().setAsLearnable(itemStack);
EE3Wrapper.ee3mod.getAbilityRegistry().setAsLearnable(object);
}
}
public static void setAsNotLearnable(Block block)
{
setAsNotLearnable(new ItemStack(block));
}
public static void setAsNotLearnable(Item item)
{
setAsNotLearnable(new ItemStack(item));
}
public static void setAsNotLearnable(ItemStack itemStack)
public static void setAsNotLearnable(Object object)
{
init();
if (ee3Mod != null)
{
EE3Wrapper.ee3mod.getAbilityRegistry().setAsNotLearnable(itemStack);
EE3Wrapper.ee3mod.getAbilityRegistry().setAsNotLearnable(object);
}
}
public static boolean isRecoverable(Block block)
{
return isRecoverable(new ItemStack(block));
}
public static boolean isRecoverable(Item item)
{
return isRecoverable(new ItemStack(item));
}
public static boolean isRecoverable(ItemStack itemStack)
public static boolean isRecoverable(Object object)
{
init();
if (ee3Mod != null)
{
return EE3Wrapper.ee3mod.getAbilityRegistry().isRecoverable(itemStack);
return EE3Wrapper.ee3mod.getAbilityRegistry().isRecoverable(object);
}
return false;
}
public static void setAsRecoverable(Block block)
{
setAsRecoverable(new ItemStack(block));
}
public static void setAsRecoverable(Item item)
{
setAsRecoverable(new ItemStack(item));
}
public static void setAsRecoverable(ItemStack itemStack)
public static void setAsRecoverable(Object object)
{
init();
if (ee3Mod != null)
{
EE3Wrapper.ee3mod.getAbilityRegistry().setAsRecoverable(itemStack);
EE3Wrapper.ee3mod.getAbilityRegistry().setAsRecoverable(object);
}
}
public static void setAsNotRecoverable(Block block)
{
setAsNotRecoverable(new ItemStack(block));
}
public static void setAsNotRecoverable(Item item)
{
setAsNotRecoverable(new ItemStack(item));
}
public static void setAsNotRecoverable(ItemStack itemStack)
public static void setAsNotRecoverable(Object object)
{
init();
if (ee3Mod != null)
{
EE3Wrapper.ee3mod.getAbilityRegistry().setAsNotRecoverable(itemStack);
EE3Wrapper.ee3mod.getAbilityRegistry().setAsNotRecoverable(object);
}
}

View file

@ -66,11 +66,14 @@ public class OreStack implements Comparable<OreStack>
this.stackSize = stackSize;
}
// TODO Maybe this should return a List of OreStacks that match the OreDictionary entries this ItemStack belongs to
// Ponder and test - changing this could have massive ramifications on DynEV
// For now, this returns an OreStack for the first OreDictionary entry the ItemStack is associated with
public OreStack(ItemStack itemStack)
{
if (itemStack != null && OreDictionary.getOreIDs(itemStack).length > 0)
{
this.oreName = OreDictionary.getOreName(OreDictionary.getOreIDs(itemStack)[0]); // TODO Likely not ideal, revisit
this.oreName = OreDictionary.getOreName(OreDictionary.getOreIDs(itemStack)[0]);
this.stackSize = itemStack.stackSize;
}
}

View file

@ -17,7 +17,7 @@ import java.util.List;
public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<WrappedStack>, JsonSerializer<WrappedStack>
{
private static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(WrappedStack.class, new WrappedStack()).create();
public static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(WrappedStack.class, new WrappedStack()).create();
private final String objectType;
private final Object wrappedStack;
@ -267,27 +267,27 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
{
NBTTagCompound wrappedItemTagCompound = new NBTTagCompound();
((ItemStack) wrappedStack.getWrappedStack()).writeToNBT(wrappedItemTagCompound);
wrappedStackTagCompound.setInteger("objectType", 0);
wrappedStackTagCompound.setTag("wrappedStack", wrappedItemTagCompound);
wrappedStackTagCompound.setInteger("stackSize", wrappedStack.getStackSize());
wrappedStackTagCompound.setInteger("wrappedStack_type", 0);
wrappedStackTagCompound.setTag("wrappedStack_data", wrappedItemTagCompound);
wrappedStackTagCompound.setInteger("wrappedStack_stackSize", wrappedStack.getStackSize());
return wrappedStackTagCompound;
}
else if (wrappedStack.getWrappedStack() instanceof OreStack)
{
NBTTagCompound wrappedOreTagCompound = new NBTTagCompound();
((OreStack) wrappedStack.getWrappedStack()).writeToNBT(wrappedOreTagCompound);
wrappedStackTagCompound.setInteger("objectType", 1);
wrappedStackTagCompound.setTag("wrappedStack", wrappedOreTagCompound);
wrappedStackTagCompound.setInteger("stackSize", wrappedStack.getStackSize());
wrappedStackTagCompound.setInteger("wrappedStack_type", 1);
wrappedStackTagCompound.setTag("wrappedStack_data", wrappedOreTagCompound);
wrappedStackTagCompound.setInteger("wrappedStack_stackSize", wrappedStack.getStackSize());
return wrappedStackTagCompound;
}
else if (wrappedStack.getWrappedStack() instanceof FluidStack)
{
NBTTagCompound wrappedFluidTagCompound = new NBTTagCompound();
((FluidStack) wrappedStack.getWrappedStack()).writeToNBT(wrappedFluidTagCompound);
wrappedStackTagCompound.setInteger("objectType", 2);
wrappedStackTagCompound.setTag("wrappedStack", wrappedFluidTagCompound);
wrappedStackTagCompound.setInteger("stackSize", wrappedStack.getStackSize());
wrappedStackTagCompound.setInteger("wrappedStack_type", 2);
wrappedStackTagCompound.setTag("wrappedStack_data", wrappedFluidTagCompound);
wrappedStackTagCompound.setInteger("wrappedStack_stackSize", wrappedStack.getStackSize());
return wrappedStackTagCompound;
}
}
@ -297,24 +297,24 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
public static WrappedStack fromNBTTagCompound(NBTTagCompound nbtTagCompound)
{
if (nbtTagCompound.hasKey("objectType") && nbtTagCompound.hasKey("wrappedStack") && nbtTagCompound.hasKey("stackSize"))
if (nbtTagCompound.hasKey("wrappedStack_type") && nbtTagCompound.hasKey("wrappedStack_data") && nbtTagCompound.hasKey("wrappedStack_stackSize"))
{
int objectType = nbtTagCompound.getInteger("objectType");
int stackSize = nbtTagCompound.getInteger("stackSize");
int objectType = nbtTagCompound.getInteger("wrappedStack_type");
int stackSize = nbtTagCompound.getInteger("wrappedStack_stackSize");
if (objectType == 0)
{
ItemStack itemStack = ItemStack.loadItemStackFromNBT(nbtTagCompound.getCompoundTag("wrappedStack"));
ItemStack itemStack = ItemStack.loadItemStackFromNBT(nbtTagCompound.getCompoundTag("wrappedStack_data"));
return new WrappedStack(itemStack, stackSize);
}
else if (objectType == 1)
{
OreStack oreStack = OreStack.loadOreStackFromNBT(nbtTagCompound.getCompoundTag("wrappedStack"));
OreStack oreStack = OreStack.loadOreStackFromNBT(nbtTagCompound.getCompoundTag("wrappedStack_data"));
return new WrappedStack(oreStack, stackSize);
}
else if (objectType == 2)
{
FluidStack fluidStack = FluidStack.loadFluidStackFromNBT(nbtTagCompound.getCompoundTag("wrappedStack"));
FluidStack fluidStack = FluidStack.loadFluidStackFromNBT(nbtTagCompound.getCompoundTag("wrappedStack_data"));
return new WrappedStack(fluidStack, stackSize);
}
else
@ -372,27 +372,27 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
{
JsonObject jsonWrappedStack = (JsonObject) jsonElement;
int stackSize = -1;
int stackSize = 1;
String objectType = null;
Object stackObject = null;
if (jsonWrappedStack.get("type") != null)
if (jsonWrappedStack.get("wrappedStack_type") != null)
{
objectType = jsonWrappedStack.get("type").getAsString();
objectType = jsonWrappedStack.get("wrappedStack_type").getAsString();
}
if (jsonWrappedStack.get("stackSize") != null)
if (jsonWrappedStack.get("wrappedStack_stackSize") != null)
{
stackSize = jsonWrappedStack.get("stackSize").getAsInt();
stackSize = jsonWrappedStack.get("wrappedStack_stackSize").getAsInt();
}
if (jsonWrappedStack.get("data") != null && !jsonWrappedStack.get("data").isJsonPrimitive())
if (jsonWrappedStack.get("wrappedStack_data") != null && !jsonWrappedStack.get("wrappedStack_data").isJsonPrimitive())
{
if (objectType != null)
{
if (objectType.equalsIgnoreCase("ItemStack"))
{
JsonItemStack jsonItemStack = jsonSerializer.fromJson(jsonWrappedStack.get("data"), JsonItemStack.class);
JsonItemStack jsonItemStack = jsonSerializer.fromJson(jsonWrappedStack.get("wrappedStack_data"), JsonItemStack.class);
ItemStack itemStack = null;
Item item = (Item) Item.itemRegistry.getObject(jsonItemStack.itemName);
if (stackSize > 0 && item != null)
@ -407,7 +407,7 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
}
else if (objectType.equalsIgnoreCase("OreStack"))
{
OreStack oreStack = jsonSerializer.fromJson(jsonWrappedStack.get("data"), OreStack.class);
OreStack oreStack = jsonSerializer.fromJson(jsonWrappedStack.get("wrappedStack_data"), OreStack.class);
if (stackSize > 0)
{
@ -417,7 +417,7 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
}
else if (objectType.equalsIgnoreCase("FluidStack"))
{
FluidStack fluidStack = jsonSerializer.fromJson(jsonWrappedStack.get("data"), FluidStack.class);
FluidStack fluidStack = jsonSerializer.fromJson(jsonWrappedStack.get("wrappedStack_data"), FluidStack.class);
if (stackSize > 0)
{
@ -465,8 +465,8 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
Gson gson = new Gson();
jsonWrappedStack.addProperty("type", wrappedStack.objectType);
jsonWrappedStack.addProperty("stackSize", wrappedStack.stackSize);
jsonWrappedStack.addProperty("wrappedStack_type", wrappedStack.objectType);
jsonWrappedStack.addProperty("wrappedStack_stackSize", wrappedStack.stackSize);
if (wrappedStack.wrappedStack instanceof ItemStack)
{
@ -477,15 +477,15 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
{
jsonItemStack.itemNBTTagCompound = ((ItemStack) wrappedStack.wrappedStack).stackTagCompound;
}
jsonWrappedStack.add("data", gson.toJsonTree(jsonItemStack, JsonItemStack.class));
jsonWrappedStack.add("wrappedStack_data", gson.toJsonTree(jsonItemStack, JsonItemStack.class));
}
else if (wrappedStack.wrappedStack instanceof OreStack)
{
jsonWrappedStack.add("data", gson.toJsonTree(wrappedStack.wrappedStack, OreStack.class));
jsonWrappedStack.add("wrappedStack_data", gson.toJsonTree(wrappedStack.wrappedStack, OreStack.class));
}
else if (wrappedStack.wrappedStack instanceof FluidStack)
{
jsonWrappedStack.add("data", gson.toJsonTree(wrappedStack.wrappedStack, FluidStack.class));
jsonWrappedStack.add("wrappedStack_data", gson.toJsonTree(wrappedStack.wrappedStack, FluidStack.class));
}
return jsonWrappedStack;

View file

@ -1,21 +1,18 @@
package com.pahimar.ee3.init;
import com.pahimar.ee3.api.AbilityRegistryProxy;
import net.minecraft.item.ItemStack;
import com.pahimar.ee3.exchange.OreStack;
import net.minecraftforge.oredict.OreDictionary;
public class Skills
public class Abilities
{
public static void addDefaultSkills()
public static void setOresNotLearnable()
{
for (String oreName : OreDictionary.getOreNames())
{
if (oreName.startsWith("ore"))
{
for (ItemStack oreStack : OreDictionary.getOres(oreName))
{
AbilityRegistryProxy.setAsNotLearnable(oreStack);
}
AbilityRegistryProxy.setAsNotLearnable(new OreStack(oreName));
}
}
}

View file

@ -1,30 +1,39 @@
package com.pahimar.ee3.knowledge;
import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.pahimar.ee3.exchange.WrappedStack;
import java.io.*;
import java.lang.reflect.Type;
import java.util.Set;
import java.util.TreeSet;
// TODO Serialize the sets to separate JSON for map makers, cause you know, I love them
public class AbilityRegistry
public class AbilityRegistry implements JsonSerializer<AbilityRegistry>, JsonDeserializer<AbilityRegistry>
{
private static AbilityRegistry AbilityRegistry = null;
private static final Gson jsonSerializer = (new GsonBuilder()).setPrettyPrinting().registerTypeAdapter(AbilityRegistry.class, new AbilityRegistry()).create();
private static AbilityRegistry abilityRegistry = null;
private Set<WrappedStack> notLearnableSet;
private Set<WrappedStack> notRecoverableSet;
private AbilityRegistry()
{
notLearnableSet = new TreeSet<WrappedStack>();
notRecoverableSet = new TreeSet<WrappedStack>();
}
public static AbilityRegistry getInstance()
{
if (AbilityRegistry == null)
if (abilityRegistry == null)
{
AbilityRegistry = new AbilityRegistry();
AbilityRegistry.init();
abilityRegistry = new AbilityRegistry();
abilityRegistry.init();
}
return AbilityRegistry;
return abilityRegistry;
}
private void init()
@ -33,6 +42,11 @@ public class AbilityRegistry
notRecoverableSet = new TreeSet<WrappedStack>();
}
public Set<WrappedStack> getNotLearnableStacks()
{
return this.notLearnableSet;
}
public boolean isLearnable(Object object)
{
if (WrappedStack.canBeWrapped(object))
@ -60,6 +74,11 @@ public class AbilityRegistry
}
}
public Set<WrappedStack> getNotRecoverableSet()
{
return this.notRecoverableSet;
}
public boolean isRecoverable(Object object)
{
if (WrappedStack.canBeWrapped(object))
@ -85,4 +104,123 @@ public class AbilityRegistry
notRecoverableSet.add(new WrappedStack(object));
}
}
@Override
public String toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Not Learnables");
for (WrappedStack wrappedStack : notLearnableSet)
{
stringBuilder.append(wrappedStack);
}
stringBuilder.append("Not Recoverables");
for (WrappedStack wrappedStack : notRecoverableSet)
{
stringBuilder.append(wrappedStack);
}
return stringBuilder.toString();
}
@Override
public AbilityRegistry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
if (json.isJsonObject())
{
JsonObject jsonObject = (JsonObject) json;
Set<WrappedStack> notLearnableStacks = new TreeSet<WrappedStack>();
Set<WrappedStack> notRecoverableStacks = new TreeSet<WrappedStack>();
if (jsonObject.has("notLearnable") && jsonObject.get("notLearnable").isJsonArray())
{
JsonArray jsonArray = (JsonArray) jsonObject.get("notLearnable");
// TODO Pick up here in the morning
}
if (jsonObject.has("notRecoverable") && jsonObject.get("notRecoverable").isJsonArray())
{
JsonArray jsonArray = (JsonArray) jsonObject.get("notRecoverable");
}
}
return null;
}
@Override
public JsonElement serialize(AbilityRegistry abilityRegistry, Type typeOfSrc, JsonSerializationContext context)
{
JsonObject jsonAbilityRegistry = new JsonObject();
JsonArray notLearnables = new JsonArray();
for (WrappedStack wrappedStack : abilityRegistry.getNotLearnableStacks())
{
notLearnables.add(WrappedStack.jsonSerializer.toJsonTree(wrappedStack));
}
jsonAbilityRegistry.add("notLearnable", notLearnables);
JsonArray notRecoverables = new JsonArray();
for (WrappedStack wrappedStack : abilityRegistry.getNotRecoverableSet())
{
notRecoverables.add(WrappedStack.jsonSerializer.toJsonTree(wrappedStack));
}
jsonAbilityRegistry.add("notRecoverable", notRecoverables);
return jsonAbilityRegistry;
}
public void saveToFile(File file)
{
JsonWriter jsonWriter;
try
{
jsonWriter = new JsonWriter(new FileWriter(file));
jsonWriter.setIndent(" ");
jsonSerializer.toJson(this, AbilityRegistry.class, jsonWriter);
jsonWriter.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void loadFromFile(File file)
{
JsonReader jsonReader;
try
{
jsonReader = new JsonReader(new FileReader(file));
AbilityRegistry abilityRegistry1 = jsonSerializer.fromJson(jsonReader, AbilityRegistry.class);
jsonReader.close();
for (WrappedStack wrappedStack : abilityRegistry1.getNotLearnableStacks())
{
if (!this.notLearnableSet.contains(wrappedStack))
{
this.notLearnableSet.add(wrappedStack);
}
}
for (WrappedStack wrappedStack : abilityRegistry1.getNotRecoverableSet())
{
if (!this.notRecoverableSet.contains(wrappedStack))
{
this.notRecoverableSet.add(wrappedStack);
}
}
}
catch (FileNotFoundException ignored)
{
// NOOP
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

View file

@ -10,7 +10,6 @@ import java.util.HashMap;
import java.util.Set;
import java.util.UUID;
// TODO Unload players from disk if they have changes made to their knowledge while they are not online
public class TransmutationKnowledgeRegistry
{
private static TransmutationKnowledgeRegistry transmutationKnowledgeRegistry = null;