From b0fdd92b8d6b6e13cb860e88b7016a9c4935c347 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Mon, 15 Jun 2015 20:09:16 +0100 Subject: [PATCH] Initial work on IDSU manager --- src/main/java/techreborn/Core.java | 3 + .../techreborn/tiles/iesu/IDSUManager.java | 144 ++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/main/java/techreborn/tiles/iesu/IDSUManager.java diff --git a/src/main/java/techreborn/Core.java b/src/main/java/techreborn/Core.java index 93ebee522..b8bbd2d3e 100644 --- a/src/main/java/techreborn/Core.java +++ b/src/main/java/techreborn/Core.java @@ -18,6 +18,7 @@ import techreborn.init.ModRecipes; import techreborn.lib.ModInfo; import techreborn.packets.PacketHandler; import techreborn.proxies.CommonProxy; +import techreborn.tiles.iesu.IDSUManager; import techreborn.util.LogHelper; import techreborn.world.TROreGen; import cpw.mods.fml.client.event.ConfigChangedEvent; @@ -88,6 +89,8 @@ public class Core { TRAchievements.init(); // Multiblock events MinecraftForge.EVENT_BUS.register(new MultiblockEventHandler()); + // IESU manager + MinecraftForge.EVENT_BUS.register(new IDSUManager()); FMLCommonHandler.instance().bus().register(new MultiblockServerTickHandler()); LogHelper.info("Initialization Complete"); diff --git a/src/main/java/techreborn/tiles/iesu/IDSUManager.java b/src/main/java/techreborn/tiles/iesu/IDSUManager.java new file mode 100644 index 000000000..c63d98793 --- /dev/null +++ b/src/main/java/techreborn/tiles/iesu/IDSUManager.java @@ -0,0 +1,144 @@ +package techreborn.tiles.iesu; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import cpw.mods.fml.common.eventhandler.EventPriority; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import net.minecraft.world.World; +import net.minecraft.world.storage.ISaveHandler; +import net.minecraftforge.event.world.WorldEvent; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + + +public class IDSUManager { + + + public static ArrayList worldData = new ArrayList(); + + @SubscribeEvent(priority = EventPriority.HIGHEST) + public void worldSave(WorldEvent.Save event){ + if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){ + for(IDSUWorldSaveData saveData : worldData){ + if(saveData.saveHandler == event.world.getSaveHandler()){ + saveData.save(); + } + } + } + } + + @SubscribeEvent(priority = EventPriority.HIGHEST) + public void worldLoad(WorldEvent.Load event){ + if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){ + for(IDSUWorldSaveData saveData : worldData){ + if(saveData.saveHandler == event.world.getSaveHandler()){ + saveData.load(); + return; + } + } + IDSUWorldSaveData worldSaveData = new IDSUWorldSaveData(event.world); + worldData.add(worldSaveData); + worldSaveData.load(); + } + } + + @SubscribeEvent(priority = EventPriority.LOWEST) + public void worldClosed(WorldEvent.Unload event){ + if(event.world != null && event.world.getSaveHandler() != null && event.world.getSaveHandler().getWorldDirectory() != null){ + for(IDSUWorldSaveData saveData : worldData){ + if(saveData.saveHandler == event.world.getSaveHandler()){ + saveData.save(); + } + } + } + worldData.clear(); + } + + + public class IDSUWorldSaveData { + + public HashMap idsuValues = new HashMap(); + + public World world; + + ISaveHandler saveHandler; + + File folder; + + File file; + + public IDSUWorldSaveData(World world) { + this.world = world; + this.saveHandler = world.getSaveHandler(); + folder = new File(saveHandler.getWorldDirectory(), "iesuData"); + file = new File(folder,"idsu.json"); + idsuValues.put(0, new IDSUValueSaveData()); + } + + public IDSUValueSaveData getSaves(int i){ + if(idsuValues.containsKey(i)){ + return idsuValues.get(i); + } + return null; + } + + public void load(){ + //TODO load values from file + if(!file.exists()){ + return; + } + try { + Gson gson = new Gson(); + BufferedReader reader = new BufferedReader(new FileReader(file)); + Type typeOfHashMap = new TypeToken>() { }.getType(); + idsuValues = gson.fromJson(reader, typeOfHashMap); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void save(){ + if(idsuValues.isEmpty()){ + return; + } + if(!file.exists()){ + if(!folder.exists()){ + folder.mkdirs(); + } + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + Gson gson = new Gson(); + String json = gson.toJson(idsuValues); + try { + FileWriter writer = new FileWriter(file); + writer.write(json); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + public class IDSUValueSaveData { + + + public int id = 1; + + + + } +}