forked from jzitnik/twodcraft
47 lines
1.1 KiB
Java
47 lines
1.1 KiB
Java
package cz.jzitnik.game;
|
|
|
|
import java.io.*;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
|
public class GameSaver {
|
|
public void save(Game game) {
|
|
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("world.ser"))) {
|
|
out.writeObject(game);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
// TODO: This will need rewrite
|
|
public static Game load() {
|
|
log.info("Loading game");
|
|
File file = new File("world.ser");
|
|
|
|
if (!file.isFile()) {
|
|
return new Game();
|
|
}
|
|
|
|
try {
|
|
log.info("Loading game from save file");
|
|
FileInputStream fileIn = new FileInputStream("world.ser");
|
|
|
|
ObjectInputStream in = new ObjectInputStream(fileIn);
|
|
|
|
// Read the object from the file
|
|
Object object = in.readObject();
|
|
|
|
Game game = (Game) object;
|
|
|
|
in.close();
|
|
fileIn.close();
|
|
|
|
return game;
|
|
} catch (IOException | ClassNotFoundException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
|
|
}
|
|
}
|