Skip to content

Commit

Permalink
r50
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolu1990 committed Jun 24, 2015
1 parent 6be39c9 commit f4e7c1a
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 138 deletions.
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 @@ -18,7 +18,7 @@ public final class Version {
* Unique version code. Must be a String for annotations. Used to check if a
* new version if available. Each update must increment this number.
*/
public final static String REVISION = "48";
public final static String REVISION = "50";

public final static String getVersionName() {
return String.format("BETA-%d.%d r%s", MAJOR, MINOR, REVISION);
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/mods/eln/node/NodeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.*;
import java.util.Map.Entry;
import java.util.Random;

import mods.eln.misc.Coordonate;
import mods.eln.misc.Direction;
Expand Down Expand Up @@ -140,6 +135,8 @@ public NodeBase getRandomNode() {
return nodes.get(rand.nextInt(nodes.size()));
}



public void loadFromNbt(NBTTagCompound nbt) {
ArrayList<NodeBase> addedNode = new ArrayList<NodeBase>();
for (Object o : Utils.getTags(nbt))
Expand All @@ -152,7 +149,6 @@ public void loadFromNbt(NBTTagCompound nbt) {
addNode(node);
addedNode.add(node);
node.initializeFromNBT();

} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -167,9 +163,6 @@ public void saveToNbt(NBTTagCompound nbt, int dim) {
int nodeCounter = 0;
ArrayList<NodeBase> nodesCopy = new ArrayList<NodeBase>();
nodesCopy.addAll(nodes);
System.out.println("WORLD SAVE " + dim);
System.out.flush();

for (NodeBase node : nodesCopy)
{
try {
Expand All @@ -184,6 +177,7 @@ public void saveToNbt(NBTTagCompound nbt, int dim) {
}

}

}

public void clear() {
Expand All @@ -192,10 +186,8 @@ public void clear() {
}

public void unload(int dimensionId) {
Iterator<NodeBase> i = nodes.iterator();
System.out.println("WORLD UNLOAD " +dimensionId);
System.out.flush();

Iterator<NodeBase> i = nodes.iterator();
while (i.hasNext()) {
NodeBase n = i.next();
if (n.coordonate.dimention == dimensionId) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/mods/eln/server/ConsoleListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import mods.eln.misc.Color;
import mods.eln.misc.LangFileParser;
import mods.eln.misc.Utils;
import mods.eln.sim.Simulator;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;

import java.util.EmptyStackException;

public class ConsoleListener extends CommandBase {

@Override
Expand Down Expand Up @@ -72,6 +75,7 @@ else if (retCode == LangFileParser.RetStatus.ERR__PARSING_ERROR)
else
icommandsender.addChatMessage(new ChatComponentText(Color.COLOR_DARK_YELLOW+"Unknown status returned."));
}
//Eln.simulator.pleaseCrash = true;
}

private void printBooleanResult(ICommandSender icommandsender, Boolean ack){
Expand Down
260 changes: 137 additions & 123 deletions src/main/java/mods/eln/server/ServerEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent;
import li.cil.oc.api.network.Node;
import mods.eln.Eln;
import mods.eln.misc.Coordonate;
import mods.eln.misc.Utils;
Expand All @@ -20,135 +21,148 @@
import net.minecraftforge.event.world.WorldEvent.Unload;
import org.lwjgl.Sys;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.*;
import java.util.HashSet;
import java.util.LinkedList;

public class ServerEventListener {

LinkedList<EntityLightningBolt> lightningListNext = new LinkedList<EntityLightningBolt>();
LinkedList<EntityLightningBolt> lightningList = new LinkedList<EntityLightningBolt>();

public ServerEventListener(){
MinecraftForge.EVENT_BUS.register(this);
FMLCommonHandler.instance().bus().register(this);
}

@SubscribeEvent
public void tick(ServerTickEvent event) {
if (event.phase != Phase.END) return;

lightningList = lightningListNext;
lightningListNext = new LinkedList<EntityLightningBolt>();
}

@SubscribeEvent
public void onNewEntity(EntityConstructing event) {
if (event.entity instanceof EntityLightningBolt) {
lightningListNext.add((EntityLightningBolt)event.entity);
}
}

public void clear() {
lightningList.clear();
}

public double getLightningClosestTo(Coordonate c) {
double best = 10000000;
for (EntityLightningBolt l : lightningList) {
if (c.world() != l.worldObj) continue;
double d = l.getDistance(c.x, c.y, c.z);
if (d < best) best = d;
}
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;
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
public void onWorldUnload(Unload e) {
if (e.world.isRemote) return;
NodeManager.instance.unload(e.world.provider.dimensionId);
Eln.ghostManager.unload(e.world.provider.dimensionId);
}

@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) {
}
}
public ServerEventListener() {
MinecraftForge.EVENT_BUS.register(this);
FMLCommonHandler.instance().bus().register(this);
}

@SubscribeEvent
public void tick(ServerTickEvent event) {
if (event.phase != Phase.END) return;

lightningList = lightningListNext;
lightningListNext = new LinkedList<EntityLightningBolt>();
}

@SubscribeEvent
public void onNewEntity(EntityConstructing event) {
if (event.entity instanceof EntityLightningBolt) {
lightningListNext.add((EntityLightningBolt) event.entity);
}
}

public void clear() {
lightningList.clear();
}

public double getLightningClosestTo(Coordonate c) {
double best = 10000000;
for (EntityLightningBolt l : lightningList) {
if (c.world() != l.worldObj) continue;
double d = l.getDistance(c.x, c.y, c.z);
if (d < best) best = d;
}
return best;
}


public String getEaWorldSaveName(World w) {
return Utils.getMapFolder() + "data/electricalAgeWorld" + w.provider.dimensionId + ".dat";
}
public HashSet<Integer> loadedWorlds = new HashSet<Integer>();
@SubscribeEvent
public void onWorldLoad(Load e) {
if (e.world.isRemote) return;
loadedWorlds.add(e.world.provider.dimensionId);
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
public void onWorldUnload(Unload e) {
if (e.world.isRemote) return;
loadedWorlds.remove(e.world.provider.dimensionId);
try {
NodeManager.instance.unload(e.world.provider.dimensionId);
Eln.ghostManager.unload(e.world.provider.dimensionId);
} catch (Exception ex) {
ex.printStackTrace();
}

}

@SubscribeEvent
public void onWorldSave(Save e) {
if (e.world.isRemote) return;
if(!loadedWorlds.contains(e.world.provider.dimensionId)) {
//System.out.println("I hate you minecraft");
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) {
e.printStackTrace();
}
try {
Eln.ghostManager.loadFromNBT(nbt.getCompoundTag("ghost"));
} catch (Exception e) {
e.printStackTrace();
}
}

public static void writeToEaWorldNBT(NBTTagCompound nbt, int dim) {
try {
NodeManager.instance.saveToNbt(Utils.newNbtTagCompund(nbt, "nodes"), dim);
} catch (Exception e) {
e.printStackTrace();
}
try {
Eln.ghostManager.saveToNBT(Utils.newNbtTagCompund(nbt, "ghost"), dim);
} catch (Exception e) {
e.printStackTrace();
}

}

}
3 changes: 2 additions & 1 deletion src/main/java/mods/eln/sim/Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,11 @@ public void removeAllThermalSlowProcess(ArrayList<IProcess> process) {

// private ArrayList<Double> conectionSerialConductance = new ArrayList<Double>();

public boolean pleaseCrash = false;
@SubscribeEvent
public void tick(ServerTickEvent event) {
if (event.phase != Phase.START) return;

if(pleaseCrash) throw new StackOverflowError();
long stackStart;

long startTime = System.nanoTime();
Expand Down

0 comments on commit f4e7c1a

Please sign in to comment.