World info saving
This commit is contained in:
@@ -6,6 +6,7 @@ public final class WorldConstants {
|
||||
|
||||
public static final String CHUNK_FILE_NAME_TEMPLATE = "chunk_{0}_{1}_{2}.dat";
|
||||
public static final String BIOME_FILE_NAME_TEMPLATE = "biomes.dat";
|
||||
public static final String WORLD_INFO_FILE_NAME_TEMPLATE = "world.dat";
|
||||
public static final String REGION_FILE_NAME_TEMPLATE = "r.{0}.{1}";
|
||||
|
||||
public static final int WORLD_MIN_HEIGHT = 36;
|
||||
|
||||
@@ -8,6 +8,7 @@ import mc.core.world.*;
|
||||
import mc.world.generated_world.region.RegionImpl;
|
||||
import mc.world.generated_world.serialization.ChunkSerializer;
|
||||
import mc.world.generated_world.serialization.RegionReaderWriter;
|
||||
import mc.world.generated_world.serialization.WorldReaderWriter;
|
||||
import mc.world.generated_world.world.CubicWorld;
|
||||
import mc.world.generated_world.world.Temperature;
|
||||
import mc.world.generated_world.world.Wetness;
|
||||
@@ -27,6 +28,7 @@ public class SeedBasedWorldGenerator implements WorldGenerator {
|
||||
World world = new CubicWorld(UUID.fromString("00000000-0000-0000-C000-000000000046"), 2626949);
|
||||
Region region = worldGenerator.generateRegion(0, 0, world);
|
||||
region.save(new ChunkSerializer(), new RegionReaderWriter(new File("worlds", world.getWorldId().toString())));
|
||||
new WorldReaderWriter(new File("worlds")).writeWorldInfo(world);
|
||||
/*worldGenerator.generateRegion(1, 0, world);
|
||||
worldGenerator.generateRegion(-1, 0, world);
|
||||
worldGenerator.generateRegion(0, 1, world);
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package mc.world.generated_world.serialization;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import mc.core.WarpPosition;
|
||||
import mc.core.world.World;
|
||||
import mc.world.generated_world.world.CubicWorld;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.UUID;
|
||||
|
||||
import static mc.world.generated_world.WorldConstants.WORLD_INFO_FILE_NAME_TEMPLATE;
|
||||
|
||||
@Slf4j
|
||||
public class WorldReaderWriter {
|
||||
private final File worldsFolder;
|
||||
|
||||
public WorldReaderWriter(File worldsFolder) {
|
||||
this.worldsFolder = worldsFolder;
|
||||
}
|
||||
|
||||
public World readWorld (UUID uuid) throws IOException {
|
||||
World world = null;
|
||||
File worldFolder = new File(worldsFolder, uuid.toString());
|
||||
if (!worldFolder.exists()) {
|
||||
throw new FileNotFoundException("World folder is not exist");
|
||||
}
|
||||
File worldInfoFile = new File(worldFolder, WORLD_INFO_FILE_NAME_TEMPLATE);
|
||||
WorldInfo worldInfo;
|
||||
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(worldInfoFile))) {
|
||||
worldInfo = (WorldInfo) ois.readObject();
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.error("Error occurred while reading world info file", e);
|
||||
return null;
|
||||
}
|
||||
world = new CubicWorld(uuid, worldInfo.getSeed());
|
||||
world.setSpawn(worldInfo.getSpawn());
|
||||
world.setName(worldInfo.getName());
|
||||
return world;
|
||||
}
|
||||
|
||||
public void writeWorldInfo (World world) throws IOException {
|
||||
File worldFolder = new File(worldsFolder, world.getWorldId().toString());
|
||||
worldFolder.mkdirs();
|
||||
File worldInfoFile = new File(worldFolder, WORLD_INFO_FILE_NAME_TEMPLATE);
|
||||
WorldInfo worldInfo = new WorldInfo();
|
||||
worldInfo.setName(world.getName());
|
||||
worldInfo.setSeed(world.getSeed());
|
||||
worldInfo.setSpawn(world.getSpawn());
|
||||
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(worldInfoFile))) {
|
||||
oos.writeObject(worldInfo);
|
||||
oos.flush();
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class WorldInfo implements Serializable {
|
||||
private WarpPosition spawn;
|
||||
private String name;
|
||||
private int seed;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,10 @@ package mc.world.generated_world.world;
|
||||
|
||||
import com.flowpowered.nbt.Tag;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import mc.core.Location;
|
||||
import mc.core.WarpPosition;
|
||||
import mc.core.player.Look;
|
||||
import mc.core.world.*;
|
||||
import mc.world.generated_world.chunk.InMemoryCacheChunkLoader;
|
||||
|
||||
@@ -15,10 +18,12 @@ public class CubicWorld implements World {
|
||||
@Getter
|
||||
private final UUID worldId;
|
||||
private final int seed;
|
||||
private volatile Location spawnLocation;
|
||||
private volatile WarpPosition warpPosition;
|
||||
private final transient Object spawnLocationLock = new Object();
|
||||
private final transient ChunkLoader chunkLoader;
|
||||
private final Map<String, Tag<?>> nbtTagMap = new HashMap<>();
|
||||
@Getter@Setter
|
||||
private String name;
|
||||
|
||||
public CubicWorld(UUID worldId, int seed) {
|
||||
this.worldId = worldId;
|
||||
@@ -50,21 +55,21 @@ public class CubicWorld implements World {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getSpawn() {
|
||||
if (spawnLocation == null) {
|
||||
public WarpPosition getSpawn() {
|
||||
if (warpPosition == null) {
|
||||
synchronized (spawnLocationLock) {
|
||||
if (spawnLocation == null) {
|
||||
spawnLocation = Location.startPointLocation();
|
||||
if (warpPosition == null) {
|
||||
warpPosition = new WarpPosition(Location.startPointLocation(), new Look(0,0));
|
||||
}
|
||||
}
|
||||
}
|
||||
return spawnLocation;
|
||||
return warpPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSpawn(Location location) {
|
||||
public void setSpawn(WarpPosition warpPosition) {
|
||||
synchronized (spawnLocationLock) {
|
||||
this.spawnLocation = location;
|
||||
this.warpPosition = warpPosition;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user