save file handling

pull/12/head
valoeghese 2020-02-28 09:18:14 +13:00
parent 8469a5c0ab
commit bd18284c40
1 changed files with 48 additions and 2 deletions

View File

@ -1,6 +1,10 @@
package com.github.halotroop.litecraft.save; package com.github.halotroop.litecraft.save;
import java.io.File; import java.io.*;
import com.github.halotroop.litecraft.world.Chunk;
import tk.valoeghese.sod.BinaryData;
public final class LitecraftSave public final class LitecraftSave
{ {
@ -19,9 +23,51 @@ public final class LitecraftSave
} }
this.file = saveFile; this.file = saveFile;
this.file.mkdirs();
} }
private final File file; private final File file;
public void saveChunk(Chunk chunk)
{
StringBuilder fileLocBuilder = new StringBuilder(this.file.getPath())
.append('/').append(chunk.chunkX)
.append('/').append(chunk.chunkZ);
File chunkDir = new File(fileLocBuilder.toString());
chunkDir.mkdirs();
File chunkFile = new File(fileLocBuilder.append('/').append(chunk.chunkY).append(".sod").toString());
try
{
chunkFile.createNewFile();
BinaryData data = new BinaryData();
chunk.write(data);
data.write(chunkFile);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public Chunk readChunk(int chunkX, int chunkY, int chunkZ)
{
File chunkFile = new File(new StringBuilder(this.file.getPath())
.append('/').append(chunkX)
.append('/').append(chunkZ)
.append('/').append(chunkY).append(".sod").toString());
if (chunkFile.isFile())
{
BinaryData data = BinaryData.read(chunkFile);
Chunk result = new Chunk(chunkX, chunkY, chunkZ);
result.read(data);
return result;
}
else return null;
}
private static final String SAVE_DIR = "./saves/"; private static final String SAVE_DIR = "./saves/";
} }