Archived
0

World info saving

This commit is contained in:
Forwolk
2018-08-02 13:14:59 +03:00
parent a9e6378101
commit 94e32a6921
9 changed files with 112 additions and 16 deletions

View File

@@ -7,9 +7,11 @@ package mc.core;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
@AllArgsConstructor
@Data
public class Location {
public class Location implements Serializable{
private double x, y, z;
public static Location copyOf(Location location) {

View File

@@ -0,0 +1,14 @@
package mc.core;
import lombok.AllArgsConstructor;
import lombok.Data;
import mc.core.player.Look;
import java.io.Serializable;
@Data
@AllArgsConstructor
public class WarpPosition implements Serializable {
private Location location;
private Look look;
}

View File

@@ -7,9 +7,11 @@ package mc.core.player;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
@Data
@AllArgsConstructor
public class Look {
public class Look implements Serializable{
private float yaw, pitch;
public void set(Look look) {

View File

@@ -5,6 +5,7 @@
package mc.core.world;
import mc.core.Location;
import mc.core.WarpPosition;
import mc.core.nbt.Taggable;
import java.io.Serializable;
@@ -45,8 +46,8 @@ public interface World extends Taggable, Serializable{
UUID getWorldId();
IWorldType getWorldType();
Location getSpawn();
void setSpawn(Location location);
WarpPosition getSpawn();
void setSpawn(WarpPosition location);
Chunk getChunk(int x, int y, int z);
void setChunk(int x, int y, int z, Chunk chunk);
@@ -55,4 +56,7 @@ public interface World extends Taggable, Serializable{
void setRegion(int x, int z, Region region);
int getSeed();
String getName();
void setName(String name);
}

View File

@@ -8,6 +8,8 @@ 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 java.util.UUID;
@@ -17,10 +19,12 @@ public class FlatWorld implements World {
@Getter@Setter
private UUID worldId = UUID.fromString("00000000-0000-0000-C000-000000000046");
@Getter@Setter
private String name;
@Getter
@Setter
private Location spawn = new Location(0, 6, 0);
private WarpPosition spawn = new WarpPosition(new Location(0, 6, 0), new Look(0, 0));
private Chunk chunk = new SimpleChunk();
@Override

View File

@@ -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;

View File

@@ -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);

View File

@@ -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;
}
}

View File

@@ -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;
}
}