Initial work on IDSU manager
This commit is contained in:
parent
d5a1494784
commit
b0fdd92b8d
2 changed files with 147 additions and 0 deletions
|
@ -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");
|
||||
|
|
144
src/main/java/techreborn/tiles/iesu/IDSUManager.java
Normal file
144
src/main/java/techreborn/tiles/iesu/IDSUManager.java
Normal file
|
@ -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<IDSUWorldSaveData> worldData = new ArrayList<IDSUWorldSaveData>();
|
||||
|
||||
@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<Integer, IDSUValueSaveData> idsuValues = new HashMap<Integer, IDSUValueSaveData>();
|
||||
|
||||
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<Map<Integer, IDSUValueSaveData>>() { }.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;
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue