Skip to content

Commit

Permalink
1.10 r47 fix save bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolu1990 committed Jun 14, 2015
1 parent 4782663 commit abb3e44
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 54 deletions.
8 changes: 6 additions & 2 deletions src/main/java/mods/eln/misc/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mods/eln/misc/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/mods/eln/node/NodeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,9 @@ public void unload(int dimensionId) {
}
}






}
91 changes: 41 additions & 50 deletions src/main/java/mods/eln/server/ElnWorldStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
77 changes: 76 additions & 1 deletion src/main/java/mods/eln/server/ServerEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
}
}

}

0 comments on commit abb3e44

Please sign in to comment.