Lots of stuff in various stages of working
This commit is contained in:
parent
8c503ffec7
commit
f77b24278a
|
@ -7,6 +7,7 @@ import com.pahimar.ee3.command.CommandSyncValues;
|
|||
import com.pahimar.ee3.exchange.EnergyValueRegistry;
|
||||
import com.pahimar.ee3.handler.*;
|
||||
import com.pahimar.ee3.init.*;
|
||||
import com.pahimar.ee3.knowledge.KnowledgeRegistry;
|
||||
import com.pahimar.ee3.knowledge.SkillRegistry;
|
||||
import com.pahimar.ee3.network.PacketHandler;
|
||||
import com.pahimar.ee3.proxy.IProxy;
|
||||
|
@ -52,10 +53,13 @@ public class EquivalentExchange3
|
|||
@EventHandler
|
||||
public void onServerStarting(FMLServerStartingEvent event)
|
||||
{
|
||||
SerializationHelper.initModDataDirectories();
|
||||
|
||||
KnowledgeRegistry.getInstance();
|
||||
|
||||
event.registerServerCommand(new CommandSetValue());
|
||||
event.registerServerCommand(new CommandSetCurrentItemValue());
|
||||
event.registerServerCommand(new CommandSyncValues());
|
||||
SerializationHelper.initModDataDirectories();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
@ -127,6 +131,8 @@ public class EquivalentExchange3
|
|||
SerializationHelper.writeNBTToFile(SerializationHelper.getDataDirectory(), SerializationHelper.getModListMD5() + "." + Reference.MOD_ID.toLowerCase(), getEnergyValueRegistry());
|
||||
}
|
||||
|
||||
KnowledgeRegistry.getInstance().saveAll();
|
||||
|
||||
WorldEventHandler.hasInitilialized = false;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,26 @@
|
|||
package com.pahimar.ee3.exchange;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
|
||||
public class JsonItemStack
|
||||
{
|
||||
public String itemName;
|
||||
public int itemDamage;
|
||||
public NBTTagCompound nbtTagCompound;
|
||||
public NBTTagCompound itemNBTTagCompound;
|
||||
|
||||
public JsonItemStack() {
|
||||
this.itemName = null;
|
||||
this.itemDamage = 0;
|
||||
this.itemNBTTagCompound = null;
|
||||
}
|
||||
|
||||
public JsonItemStack(ItemStack itemStack) {
|
||||
this.itemName = Item.itemRegistry.getNameForObject(itemStack.getItem());
|
||||
this.itemDamage = itemStack.getItemDamage();
|
||||
if (itemStack.stackTagCompound != null) {
|
||||
this.itemNBTTagCompound = itemStack.getTagCompound();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -398,9 +398,9 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
|
|||
if (stackSize > 0 && item != null)
|
||||
{
|
||||
itemStack = new ItemStack(item, stackSize, jsonItemStack.itemDamage);
|
||||
if (jsonItemStack.nbtTagCompound != null)
|
||||
if (jsonItemStack.itemNBTTagCompound != null)
|
||||
{
|
||||
itemStack.stackTagCompound = jsonItemStack.nbtTagCompound;
|
||||
itemStack.stackTagCompound = jsonItemStack.itemNBTTagCompound;
|
||||
}
|
||||
}
|
||||
stackObject = itemStack;
|
||||
|
@ -475,7 +475,7 @@ public class WrappedStack implements Comparable<WrappedStack>, JsonDeserializer<
|
|||
jsonItemStack.itemDamage = ((ItemStack) wrappedStack.wrappedStack).getItemDamage();
|
||||
if (((ItemStack) wrappedStack.wrappedStack).stackTagCompound != null)
|
||||
{
|
||||
jsonItemStack.nbtTagCompound = ((ItemStack) wrappedStack.wrappedStack).stackTagCompound;
|
||||
jsonItemStack.itemNBTTagCompound = ((ItemStack) wrappedStack.wrappedStack).stackTagCompound;
|
||||
}
|
||||
jsonWrappedStack.add("data", gson.toJsonTree(jsonItemStack, JsonItemStack.class));
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ import java.io.FileInputStream;
|
|||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
// TODO Remove
|
||||
public class TransmutationKnowledgeHandler
|
||||
{
|
||||
public static File playerDataDirectory;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.pahimar.ee3.knowledge;
|
||||
|
||||
import com.pahimar.ee3.reference.Files;
|
||||
import com.pahimar.ee3.util.SerializationHelper;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
|
||||
|
@ -16,9 +17,11 @@ public class KnowledgeRegistry
|
|||
|
||||
private KnowledgeRegistry()
|
||||
{
|
||||
knowledgeDirectory = new File(SerializationHelper.getPlayerDataDirectory(), "knowledge");
|
||||
knowledgeDirectory = new File(SerializationHelper.getPlayerDataDirectory(), "knowledge" + File.separator + "transmutation");
|
||||
knowledgeDirectory.mkdirs();
|
||||
|
||||
loadTemplateKnowledgeFromDisk();
|
||||
|
||||
playerKnowledgeMap = new HashMap<UUID, TransmutationKnowledge>();
|
||||
}
|
||||
|
||||
|
@ -34,14 +37,24 @@ public class KnowledgeRegistry
|
|||
|
||||
public TransmutationKnowledge getTemplateKnowledge()
|
||||
{
|
||||
if (templateKnowledge == null)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return templateKnowledge;
|
||||
}
|
||||
|
||||
public void loadTemplateKnowledgeFromDisk() {
|
||||
File templateFile = new File(knowledgeDirectory, Files.TEMPLATE_JSON_FILE);
|
||||
|
||||
if (!templateFile.exists()) {
|
||||
templateKnowledge = new TransmutationKnowledge();
|
||||
SerializationHelper.writeTransmutationKnowledgeToFile(knowledgeDirectory, Files.TEMPLATE_JSON_FILE, templateKnowledge);
|
||||
} else {
|
||||
templateKnowledge = SerializationHelper.readTransmutationKnowledgeFromFile(knowledgeDirectory, Files.TEMPLATE_JSON_FILE);
|
||||
}
|
||||
}
|
||||
|
||||
public void saveTemplateKnowledgeToDisk() {
|
||||
SerializationHelper.writeTransmutationKnowledgeToFile(knowledgeDirectory, Files.TEMPLATE_JSON_FILE, templateKnowledge);
|
||||
}
|
||||
|
||||
public void loadPlayerFromDisk(EntityPlayer entityPlayer)
|
||||
{
|
||||
TransmutationKnowledge playerTransmutationKnowledge = new TransmutationKnowledge();
|
||||
|
@ -50,8 +63,7 @@ public class KnowledgeRegistry
|
|||
|
||||
if (playerKnowledgeFile.exists() && playerKnowledgeFile.isFile())
|
||||
{
|
||||
// TODO Load from disk as JSON and not as NBT
|
||||
playerTransmutationKnowledge = TransmutationKnowledge.readTransmutationKnowledgeFromNBT(SerializationHelper.readNBTFromFile(playerKnowledgeFile));
|
||||
playerTransmutationKnowledge = SerializationHelper.readTransmutationKnowledgeFromFile(knowledgeDirectory, entityPlayer.getUniqueID().toString() + ".json");
|
||||
}
|
||||
|
||||
playerKnowledgeMap.put(entityPlayer.getUniqueID(), playerTransmutationKnowledge);
|
||||
|
@ -59,7 +71,18 @@ public class KnowledgeRegistry
|
|||
|
||||
public void savePlayerKnowledgeToDisk(EntityPlayer entityPlayer)
|
||||
{
|
||||
// TODO Save to disk as JSON and not as NBT
|
||||
SerializationHelper.writeNBTToFile(knowledgeDirectory, entityPlayer.getUniqueID().toString() + ".json", playerKnowledgeMap.get(entityPlayer.getUniqueID()));
|
||||
if (playerKnowledgeMap.containsKey(entityPlayer.getUniqueID())) {
|
||||
SerializationHelper.writeTransmutationKnowledgeToFile(knowledgeDirectory, entityPlayer.getUniqueID().toString() + ".json", playerKnowledgeMap.get(entityPlayer.getUniqueID()));
|
||||
} else {
|
||||
SerializationHelper.writeTransmutationKnowledgeToFile(knowledgeDirectory, entityPlayer.getUniqueID().toString() + ".json", new TransmutationKnowledge());
|
||||
}
|
||||
}
|
||||
|
||||
public void saveAll() {
|
||||
saveTemplateKnowledgeToDisk();
|
||||
|
||||
for (UUID playerUUID : playerKnowledgeMap.keySet()) {
|
||||
SerializationHelper.writeTransmutationKnowledgeToFile(knowledgeDirectory, playerUUID.toString() + ".json", playerKnowledgeMap.get(playerUUID));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,25 @@
|
|||
package com.pahimar.ee3.knowledge;
|
||||
|
||||
import com.pahimar.ee3.exchange.EnergyValueRegistry;
|
||||
import com.pahimar.ee3.exchange.WrappedStack;
|
||||
import com.pahimar.ee3.handler.TransmutationKnowledgeHandler;
|
||||
import com.pahimar.ee3.reference.Settings;
|
||||
import com.pahimar.ee3.util.ItemHelper;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
// TODO Change up so that we only track what is not learnable, and what is not recoverable
|
||||
// Everything that has an EnergyValue is learnable and recoverable unless there is an entry for it in either of these lists
|
||||
public class SkillRegistry
|
||||
{
|
||||
private static SkillRegistry SkillRegistry = null;
|
||||
private SortedMap<ItemStack, Skill> transmutationSkills;
|
||||
private Set<WrappedStack> notLearnableSet;
|
||||
private Set<WrappedStack> notRecoverableSet;
|
||||
|
||||
private SkillRegistry()
|
||||
{
|
||||
|
@ -32,6 +39,8 @@ public class SkillRegistry
|
|||
private void init()
|
||||
{
|
||||
transmutationSkills = new TreeMap<ItemStack, Skill>(ItemHelper.comparator);
|
||||
notLearnableSet = new TreeSet<WrappedStack>();
|
||||
notRecoverableSet = new TreeSet<WrappedStack>();
|
||||
}
|
||||
|
||||
public void addSkill(ItemStack itemStack, boolean learnable, boolean recoverable)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
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.JsonItemStack;
|
||||
import com.pahimar.ee3.reference.Names;
|
||||
import com.pahimar.ee3.util.INBTTaggable;
|
||||
|
@ -11,6 +13,7 @@ import net.minecraft.item.ItemStack;
|
|||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
|
||||
|
@ -197,7 +200,7 @@ public class TransmutationKnowledge implements INBTTaggable, JsonSerializer<Tran
|
|||
{
|
||||
if (json.isJsonArray()) {
|
||||
JsonArray jsonArray = (JsonArray) json;
|
||||
Set<ItemStack> itemStackSet = new TreeSet<ItemStack>(ItemHelper.comparator);
|
||||
Set<ItemStack> itemStacks = new TreeSet<ItemStack>(ItemHelper.comparator);
|
||||
Iterator<JsonElement> iterator = jsonArray.iterator();
|
||||
|
||||
while (iterator.hasNext()) {
|
||||
|
@ -209,16 +212,18 @@ public class TransmutationKnowledge implements INBTTaggable, JsonSerializer<Tran
|
|||
Item item = (Item) Item.itemRegistry.getObject(jsonItemStack.itemName);
|
||||
if (item != null) {
|
||||
itemStack = new ItemStack(item, 1, jsonItemStack.itemDamage);
|
||||
if (jsonItemStack.nbtTagCompound != null) {
|
||||
itemStack.stackTagCompound = jsonItemStack.nbtTagCompound;
|
||||
if (jsonItemStack.itemNBTTagCompound != null) {
|
||||
itemStack.stackTagCompound = jsonItemStack.itemNBTTagCompound;
|
||||
}
|
||||
}
|
||||
|
||||
itemStackSet.add(itemStack);
|
||||
if (itemStack != null) {
|
||||
itemStacks.add(itemStack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new TransmutationKnowledge(itemStackSet);
|
||||
return new TransmutationKnowledge(itemStacks);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -227,21 +232,70 @@ public class TransmutationKnowledge implements INBTTaggable, JsonSerializer<Tran
|
|||
@Override
|
||||
public JsonElement serialize(TransmutationKnowledge transmutationKnowledge, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
Gson gson = new Gson();
|
||||
JsonArray jsonTransmutationKnowledge = new JsonArray();
|
||||
|
||||
for (ItemStack itemStack : transmutationKnowledge.getKnownTransmutations())
|
||||
{
|
||||
JsonItemStack jsonItemStack = new JsonItemStack();
|
||||
jsonItemStack.itemName = Item.itemRegistry.getNameForObject(itemStack.getItem());
|
||||
jsonItemStack.itemDamage = itemStack.getItemDamage();
|
||||
if (itemStack.stackTagCompound != null)
|
||||
{
|
||||
jsonItemStack.nbtTagCompound = itemStack.getTagCompound();
|
||||
}
|
||||
jsonTransmutationKnowledge.add(gson.toJsonTree(jsonItemStack));
|
||||
jsonTransmutationKnowledge.add(jsonSerializer.toJsonTree(new JsonItemStack(itemStack)));
|
||||
}
|
||||
|
||||
return jsonTransmutationKnowledge;
|
||||
}
|
||||
|
||||
public static void saveToFile(File file, TransmutationKnowledge transmutationKnowledge) {
|
||||
JsonWriter jsonWriter;
|
||||
|
||||
try {
|
||||
jsonWriter = new JsonWriter(new FileWriter(file));
|
||||
jsonWriter.setIndent(" ");
|
||||
jsonWriter.beginArray();
|
||||
|
||||
Iterator<ItemStack> iterator = transmutationKnowledge.getKnownTransmutations().iterator();
|
||||
|
||||
while (iterator.hasNext())
|
||||
{
|
||||
ItemStack itemStack = iterator.next();
|
||||
jsonSerializer.toJson(new JsonItemStack(itemStack), JsonItemStack.class, jsonWriter);
|
||||
}
|
||||
|
||||
jsonWriter.endArray();
|
||||
jsonWriter.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static TransmutationKnowledge readFromFile(File file) {
|
||||
Set<ItemStack> itemStacks = new TreeSet<ItemStack>(ItemHelper.comparator);
|
||||
JsonReader jsonReader;
|
||||
|
||||
try {
|
||||
jsonReader = new JsonReader(new FileReader(file));
|
||||
jsonReader.beginArray();
|
||||
while (jsonReader.hasNext()) {
|
||||
JsonItemStack jsonItemStack = jsonSerializer.fromJson(jsonReader, JsonItemStack.class);
|
||||
|
||||
ItemStack itemStack = null;
|
||||
Item item = (Item) Item.itemRegistry.getObject(jsonItemStack.itemName);
|
||||
if (item != null) {
|
||||
itemStack = new ItemStack(item, 1, jsonItemStack.itemDamage);
|
||||
if (jsonItemStack.itemNBTTagCompound != null) {
|
||||
itemStack.stackTagCompound = jsonItemStack.itemNBTTagCompound;
|
||||
}
|
||||
}
|
||||
|
||||
if (itemStack != null) {
|
||||
itemStacks.add(itemStack);
|
||||
}
|
||||
}
|
||||
jsonReader.endArray();
|
||||
jsonReader.close();
|
||||
} catch (FileNotFoundException ignored) {
|
||||
// NOOP
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return new TransmutationKnowledge(itemStacks);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.pahimar.ee3.reference;
|
|||
|
||||
public class Files
|
||||
{
|
||||
|
||||
public static final String PRE_ASSIGNED_ENERGY_VALUES = "pre-assigned-energy-values.json";
|
||||
public static final String POST_ASSIGNED_ENERGY_VALUES = "post-assigned-energy-values.json";
|
||||
public static final String TEMPLATE_JSON_FILE = "template.json";
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import com.google.gson.stream.JsonWriter;
|
|||
import com.pahimar.ee3.api.EnergyValue;
|
||||
import com.pahimar.ee3.exchange.EnergyValueStackMapping;
|
||||
import com.pahimar.ee3.exchange.WrappedStack;
|
||||
import com.pahimar.ee3.knowledge.TransmutationKnowledge;
|
||||
import com.pahimar.ee3.reference.Reference;
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import cpw.mods.fml.common.Loader;
|
||||
|
@ -126,10 +127,50 @@ public class SerializationHelper
|
|||
}
|
||||
} catch (Exception exception)
|
||||
{
|
||||
LogHelper.warn(String.format("Failed to save %s to file: %s%s", nbtTaggable.getTagLabel(), directory.getAbsolutePath(), fileName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static TransmutationKnowledge readTransmutationKnowledgeFromFile(File directory, String fileName) {
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs();
|
||||
}
|
||||
|
||||
return TransmutationKnowledge.readFromFile(new File(directory, fileName));
|
||||
}
|
||||
|
||||
public static void writeTransmutationKnowledgeToFile(File directory, String fileName, TransmutationKnowledge transmutationKnowledge) {
|
||||
writeTransmutationKnowledgeToFile(directory, fileName, transmutationKnowledge, false);
|
||||
}
|
||||
|
||||
public static void writeTransmutationKnowledgeToFile(File directory, String fileName, TransmutationKnowledge transmutationKnowledge, boolean verboseLogging) {
|
||||
if (directory != null && fileName != null) {
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs();
|
||||
}
|
||||
|
||||
if (transmutationKnowledge == null) {
|
||||
transmutationKnowledge = new TransmutationKnowledge();
|
||||
}
|
||||
|
||||
try {
|
||||
File file1 = new File(directory, fileName + ".tmp");
|
||||
File file2 = new File(directory, fileName);
|
||||
TransmutationKnowledge.saveToFile(file1, transmutationKnowledge);
|
||||
|
||||
if (file2.exists()) {
|
||||
file2.delete();
|
||||
}
|
||||
|
||||
file1.renameTo(file2);
|
||||
|
||||
if (verboseLogging)
|
||||
{
|
||||
LogHelper.warn(String.format("Failed to save %s to file: %s%s", nbtTaggable.getTagLabel(), directory.getAbsolutePath(), fileName));
|
||||
LogHelper.info(String.format("Successfully saved TransmutationKnowledge to file: %s", file2.getAbsolutePath()));
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
LogHelper.error(String.format("Failed to save TransmutationKnowledge to file: %s%s", directory.getAbsolutePath(), fileName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue