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.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import java.io.Serializable;
@AllArgsConstructor @AllArgsConstructor
@Data @Data
public class Location { public class Location implements Serializable{
private double x, y, z; private double x, y, z;
public static Location copyOf(Location location) { 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.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import java.io.Serializable;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
public class Look { public class Look implements Serializable{
private float yaw, pitch; private float yaw, pitch;
public void set(Look look) { public void set(Look look) {

View File

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

View File

@@ -8,6 +8,8 @@ import com.flowpowered.nbt.Tag;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import mc.core.Location; import mc.core.Location;
import mc.core.WarpPosition;
import mc.core.player.Look;
import mc.core.world.*; import mc.core.world.*;
import java.util.UUID; import java.util.UUID;
@@ -17,10 +19,12 @@ public class FlatWorld implements World {
@Getter@Setter @Getter@Setter
private UUID worldId = UUID.fromString("00000000-0000-0000-C000-000000000046"); private UUID worldId = UUID.fromString("00000000-0000-0000-C000-000000000046");
@Getter@Setter
private String name;
@Getter @Getter
@Setter @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(); private Chunk chunk = new SimpleChunk();
@Override @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 CHUNK_FILE_NAME_TEMPLATE = "chunk_{0}_{1}_{2}.dat";
public static final String BIOME_FILE_NAME_TEMPLATE = "biomes.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 String REGION_FILE_NAME_TEMPLATE = "r.{0}.{1}";
public static final int WORLD_MIN_HEIGHT = 36; 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.region.RegionImpl;
import mc.world.generated_world.serialization.ChunkSerializer; import mc.world.generated_world.serialization.ChunkSerializer;
import mc.world.generated_world.serialization.RegionReaderWriter; 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.CubicWorld;
import mc.world.generated_world.world.Temperature; import mc.world.generated_world.world.Temperature;
import mc.world.generated_world.world.Wetness; 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); World world = new CubicWorld(UUID.fromString("00000000-0000-0000-C000-000000000046"), 2626949);
Region region = worldGenerator.generateRegion(0, 0, world); Region region = worldGenerator.generateRegion(0, 0, world);
region.save(new ChunkSerializer(), new RegionReaderWriter(new File("worlds", world.getWorldId().toString()))); 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(-1, 0, world); worldGenerator.generateRegion(-1, 0, world);
worldGenerator.generateRegion(0, 1, 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 com.flowpowered.nbt.Tag;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import mc.core.Location; import mc.core.Location;
import mc.core.WarpPosition;
import mc.core.player.Look;
import mc.core.world.*; import mc.core.world.*;
import mc.world.generated_world.chunk.InMemoryCacheChunkLoader; import mc.world.generated_world.chunk.InMemoryCacheChunkLoader;
@@ -15,10 +18,12 @@ public class CubicWorld implements World {
@Getter @Getter
private final UUID worldId; private final UUID worldId;
private final int seed; private final int seed;
private volatile Location spawnLocation; private volatile WarpPosition warpPosition;
private final transient Object spawnLocationLock = new Object(); private final transient Object spawnLocationLock = new Object();
private final transient ChunkLoader chunkLoader; private final transient ChunkLoader chunkLoader;
private final Map<String, Tag<?>> nbtTagMap = new HashMap<>(); private final Map<String, Tag<?>> nbtTagMap = new HashMap<>();
@Getter@Setter
private String name;
public CubicWorld(UUID worldId, int seed) { public CubicWorld(UUID worldId, int seed) {
this.worldId = worldId; this.worldId = worldId;
@@ -50,21 +55,21 @@ public class CubicWorld implements World {
} }
@Override @Override
public Location getSpawn() { public WarpPosition getSpawn() {
if (spawnLocation == null) { if (warpPosition == null) {
synchronized (spawnLocationLock) { synchronized (spawnLocationLock) {
if (spawnLocation == null) { if (warpPosition == null) {
spawnLocation = Location.startPointLocation(); warpPosition = new WarpPosition(Location.startPointLocation(), new Look(0,0));
} }
} }
} }
return spawnLocation; return warpPosition;
} }
@Override @Override
public void setSpawn(Location location) { public void setSpawn(WarpPosition warpPosition) {
synchronized (spawnLocationLock) { synchronized (spawnLocationLock) {
this.spawnLocation = location; this.warpPosition = warpPosition;
} }
} }