From abb3e44c689c18b7c61e1a84432a1494b980412c Mon Sep 17 00:00:00 2001 From: Dolu1990 Date: Sun, 14 Jun 2015 12:33:08 +0100 Subject: [PATCH] 1.10 r47 fix save bugs --- src/main/java/mods/eln/misc/Utils.java | 8 +- src/main/java/mods/eln/misc/Version.java | 2 +- src/main/java/mods/eln/node/NodeManager.java | 5 + .../java/mods/eln/server/ElnWorldStorage.java | 91 +++++++++---------- .../mods/eln/server/ServerEventListener.java | 77 +++++++++++++++- 5 files changed, 129 insertions(+), 54 deletions(-) diff --git a/src/main/java/mods/eln/misc/Utils.java b/src/main/java/mods/eln/misc/Utils.java index 0131f91ec..f4a244148 100644 --- a/src/main/java/mods/eln/misc/Utils.java +++ b/src/main/java/mods/eln/misc/Utils.java @@ -1270,11 +1270,15 @@ public static NBTTagCompound newNbtTagCompund(NBTTagCompound nbt, String string) nbt.setTag(string, cmp); return cmp; } + public static String getMapFolder() { + MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance(); + String savesAt = !server.isDedicatedServer() ? "saves/" : ""; + return savesAt + server.getFolderName() + "/"; + } public static File getMapFile(String name) { MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance(); - String savesAt = !server.isDedicatedServer() ? "saves/" : ""; - File f = server.getFile(savesAt + server.getFolderName() + "/" + name); + File f = server.getFile(getMapFolder() + name); return f; } diff --git a/src/main/java/mods/eln/misc/Version.java b/src/main/java/mods/eln/misc/Version.java index c32a80cd3..e66d4a68e 100644 --- a/src/main/java/mods/eln/misc/Version.java +++ b/src/main/java/mods/eln/misc/Version.java @@ -12,7 +12,7 @@ public final class Version { public final static int MAJOR = 1; /** Minor version code. */ - public final static int MINOR = 9; + public final static int MINOR = 10; /** * Unique version code. Must be a String for annotations. Used to check if a diff --git a/src/main/java/mods/eln/node/NodeManager.java b/src/main/java/mods/eln/node/NodeManager.java index f093f7128..e7d6f1c33 100644 --- a/src/main/java/mods/eln/node/NodeManager.java +++ b/src/main/java/mods/eln/node/NodeManager.java @@ -199,4 +199,9 @@ public void unload(int dimensionId) { } } + + + + + } diff --git a/src/main/java/mods/eln/server/ElnWorldStorage.java b/src/main/java/mods/eln/server/ElnWorldStorage.java index bfa38590e..363a1bdef 100644 --- a/src/main/java/mods/eln/server/ElnWorldStorage.java +++ b/src/main/java/mods/eln/server/ElnWorldStorage.java @@ -3,64 +3,55 @@ import mods.eln.Eln; import mods.eln.misc.Utils; import mods.eln.node.NodeManager; +import net.minecraft.nbt.CompressedStreamTools; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import net.minecraft.world.WorldSavedData; import net.minecraft.world.storage.MapStorage; +import java.io.File; +import java.io.FileOutputStream; + public class ElnWorldStorage extends WorldSavedData { - private int dim; + private int dim; final static String key = "eln.worldStorage"; - public ElnWorldStorage(String str) { - super(str); - } - - public static ElnWorldStorage forWorld(World world) { - // Retrieves the MyWorldData instance for the given world, creating it if necessary - MapStorage storage = world.perWorldStorage; - int dim = world.provider.dimensionId; - ElnWorldStorage result = (ElnWorldStorage) storage.loadData(ElnWorldStorage.class, key + dim); - - if (result == null) { - result = new ElnWorldStorage(key + dim); - result.dim = dim; - storage.setData(key + dim, result); - } - return result; - } - - @Override - public void readFromNBT(NBTTagCompound nbt) { - dim = nbt.getInteger("dim"); - try { - NodeManager.instance.loadFromNbt(nbt.getCompoundTag("nodes")); - } catch (Exception e) { - } - try { - Eln.ghostManager.loadFromNBT(nbt.getCompoundTag("ghost")); - } catch (Exception e) { - } - } - - @Override - public void writeToNBT(NBTTagCompound nbt) { - nbt.setInteger("dim", dim); - try { - NodeManager.instance.saveToNbt(Utils.newNbtTagCompund(nbt, "nodes"), dim); - } catch (Exception e) { - } - try { - Eln.ghostManager.saveToNBT(Utils.newNbtTagCompund(nbt, "ghost"), dim); - } catch (Exception e) { - } - - } - - @Override - public boolean isDirty() { - return true; - } + public ElnWorldStorage(String str) { + super(str); + } + + public static ElnWorldStorage forWorld(World world) { + // Retrieves the MyWorldData instance for the given world, creating it if necessary + MapStorage storage = world.perWorldStorage; + int dim = world.provider.dimensionId; + ElnWorldStorage result = (ElnWorldStorage) storage.loadData(ElnWorldStorage.class, key + dim); + if (result == null) { + result = (ElnWorldStorage) storage.loadData(ElnWorldStorage.class, key + dim + "back"); + } + if (result == null) { + result = new ElnWorldStorage(key + dim); + result.dim = dim; + storage.setData(key + dim, result); + } + return result; + } + + @Override + public void readFromNBT(NBTTagCompound nbt) { + dim = nbt.getInteger("dim"); + ServerEventListener.readFromEaWorldNBT(nbt); + } + + @Override + public void writeToNBT(NBTTagCompound nbt) { + nbt.setInteger("dim", dim); + ServerEventListener.writeToEaWorldNBT(nbt,dim); + } + + @Override + public boolean isDirty() { + return true; + } } diff --git a/src/main/java/mods/eln/server/ServerEventListener.java b/src/main/java/mods/eln/server/ServerEventListener.java index 6729218bb..466fda606 100644 --- a/src/main/java/mods/eln/server/ServerEventListener.java +++ b/src/main/java/mods/eln/server/ServerEventListener.java @@ -6,14 +6,22 @@ import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent; import mods.eln.Eln; import mods.eln.misc.Coordonate; +import mods.eln.misc.Utils; import mods.eln.node.NodeManager; import net.minecraft.entity.effect.EntityLightningBolt; +import net.minecraft.nbt.CompressedStreamTools; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.world.World; import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.common.util.Constants; import net.minecraftforge.event.entity.EntityEvent.EntityConstructing; import net.minecraftforge.event.world.WorldEvent.Load; import net.minecraftforge.event.world.WorldEvent.Save; import net.minecraftforge.event.world.WorldEvent.Unload; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.util.LinkedList; public class ServerEventListener { @@ -55,10 +63,29 @@ public double getLightningClosestTo(Coordonate c) { return best; } + + public String getEaWorldSaveName(World w){ + return Utils.getMapFolder() + "data/electricalAgeWorld" + w.provider.dimensionId +".dat"; + } + @SubscribeEvent public void onWorldLoad(Load e) { if (e.world.isRemote) return; - ElnWorldStorage storage = ElnWorldStorage.forWorld(e.world); + try { + FileInputStream fileStream = new FileInputStream(getEaWorldSaveName(e.world)); + NBTTagCompound nbt = CompressedStreamTools.readCompressed(fileStream); + readFromEaWorldNBT(nbt); + fileStream.close(); + } catch (Exception ex) { + try{ + FileInputStream fileStream = new FileInputStream(getEaWorldSaveName(e.world)+"back"); + NBTTagCompound nbt = CompressedStreamTools.readCompressed(fileStream); + readFromEaWorldNBT(nbt); + fileStream.close(); + } catch (Exception ex2) { + ElnWorldStorage storage = ElnWorldStorage.forWorld(e.world); + } + } } @SubscribeEvent @@ -71,8 +98,56 @@ public void onWorldUnload(Unload e) { @SubscribeEvent public void onWorldSave(Save e) { if (e.world.isRemote) return; + try { + NBTTagCompound nbt = new NBTTagCompound(); + writeToEaWorldNBT(nbt,e.world.provider.dimensionId); + + File oldBackup = new File(getEaWorldSaveName(e.world)+"back"); + if(oldBackup.exists()){ + oldBackup.delete(); + } + + File oldSave = new File(getEaWorldSaveName(e.world)); + if(oldSave.exists()){ + oldSave.renameTo(oldBackup); + } + + FileOutputStream fileStream = new FileOutputStream(getEaWorldSaveName(e.world)); + CompressedStreamTools.writeCompressed(nbt, fileStream); + fileStream.flush(); + fileStream.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + //ElnWorldStorage storage = ElnWorldStorage.forWorld(e.world); int idx = 0; idx++; } + + + + + public static void readFromEaWorldNBT(NBTTagCompound nbt) { + try { + NodeManager.instance.loadFromNbt(nbt.getCompoundTag("nodes")); + } catch (Exception e) { + } + try { + Eln.ghostManager.loadFromNBT(nbt.getCompoundTag("ghost")); + } catch (Exception e) { + } + } + + public static void writeToEaWorldNBT(NBTTagCompound nbt,int dim) { + try { + NodeManager.instance.saveToNbt(Utils.newNbtTagCompund(nbt, "nodes"), dim); + } catch (Exception e) { + } + try { + Eln.ghostManager.saveToNBT(Utils.newNbtTagCompund(nbt, "ghost"), dim); + } catch (Exception e) { + } + } + }