Archived
0

refactory world and clean classes

This commit is contained in:
2018-08-16 00:08:26 +03:00
parent 333150dd30
commit dc9e3512e7
24 changed files with 175 additions and 450 deletions

View File

@@ -1,8 +0,0 @@
package mc.core;
public enum Direction {
NORTH,
EAST,
WEST,
SOUTH
}

View File

@@ -11,11 +11,10 @@ import mc.core.world.World;
import mc.core.world.chunk.Chunk;
import mc.core.world.chunk.ChunkSection;
import java.io.Serializable;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
public class Location implements Serializable, Cloneable {
public class Location implements Cloneable {
@Getter
@Setter
private double x, y, z;
@@ -63,21 +62,20 @@ public class Location implements Serializable, Cloneable {
}
public Chunk getChunk() {
World world;
if ((world = getWorld()) == null) {
World world = getWorld();
if (world == null) {
return null;
} else {
return world.getRegion((int) (x / 256), (int) (z / 256))
.getChunk((int) ((x % 256) / 16), (int) ((z % 256) / 16));
return world.getChunk((int)(x / 16), (int)(z / 16));
}
}
public ChunkSection getChunkSection() {
World world;
if ((world = getWorld()) == null) {
Chunk chunk = getChunk();
if (chunk == null) {
return null;
} else {
return world.getChunk(getBlockX(), getBlockY(), getBlockZ());
return chunk.getChunkSection((int)(y / 16));
}
}

View File

@@ -1,10 +0,0 @@
package mc.core.serialization;
import mc.core.world.Region;
import mc.core.world.chunk.ChunkSection;
import java.io.IOException;
public interface IChunkReader {
ChunkSection read (Region region, int x, int y, int z) throws IOException;
}

View File

@@ -1,12 +0,0 @@
package mc.core.serialization;
import mc.core.world.Region;
import mc.core.world.World;
import java.io.IOException;
public interface IRegionReaderWriter {
Region read (int x, int z, World world) throws IOException;
void write (Region region) throws IOException;
}

View File

@@ -1,6 +0,0 @@
package mc.core.world;
public interface IWorldType {
String name();
String description();
}

View File

@@ -1,43 +0,0 @@
package mc.core.world;
import mc.core.serialization.IRegionReaderWriter;
import mc.core.serialization.Serializer;
import mc.core.world.chunk.Chunk;
import mc.core.world.chunk.ChunkSection;
import java.io.IOException;
import java.io.Serializable;
/**
* Simple world generation unit
* 16x16x16 chunks
*
*
* +-------------+----------------+------------+
* | param | range | bits |
* +-------------+----------------+------------+
* | biome_map | 256x256 0-128 | 524288 |
* +-------------+----------------+------------+
*
* Total: 524288 bits (64 Kb)
*
*/
public interface Region extends Serializable{
Chunk getChunk (int x, int z);
void setChunk(int x, int z, Chunk chunk);
@Deprecated
ChunkSection getChunkAt(int x, int y, int z);
@Deprecated
void setChunk(int x, int y, int z, ChunkSection chunkSection);
int getX();
int getZ();
Biome getBiomeAt (int x, int z);
void setBiome (int x, int z, Biome biome);
World getWorld();
void save(Serializer<ChunkSection> chunkSerializer, IRegionReaderWriter regionReaderWritter) throws IOException;
}

View File

@@ -5,58 +5,14 @@
package mc.core.world;
import mc.core.EntityLocation;
import mc.core.nbt.Taggable;
import mc.core.world.chunk.ChunkSection;
import mc.core.world.chunk.Chunk;
import java.io.Serializable;
import java.util.UUID;
/**
* WorldInfo
* +-------------+----------------+------------+
* | param | range | bits |
* +-------------+----------------+------------+
* | worldId | uuid | 128 |
* +-------------+----------------+------------+
* | worldName | string [0-64] | 512 |
* +-------------+----------------+------------+
* | spawnX | -524288:524287 | 20 |
* +-------------+----------------+------------+
* | spawnY | 0:255 | 8 |
* +-------------+----------------+------------+
* | spawnZ | -524288:524287 | 20 |
* +-------------+----------------+------------+
* | seed | long | 64 |
* +-------------+----------------+------------+
* | type | 0-255 | 8 |
* +-------------+----------------+------------+
*
* /worlds/
* --> []/world_uuid/
* --> world.dat
* --> []/r.X.Z/
* --> biomes.dat
* --> []chunk_x_y_z.dat
* --> entities.dat
* --> /playerdata/
* --> []player_uuid.dat
*/
public interface World extends Taggable, Serializable{
UUID getWorldId();
IWorldType getWorldType();
public interface World {
WorldType getWorldType();
EntityLocation getSpawn();
void setSpawn(EntityLocation location);
ChunkSection getChunk(int x, int y, int z);
void setChunk(int x, int y, int z, ChunkSection chunkSection);
Region getRegion(int x, int z);
void setRegion(int x, int z, Region region);
int getSeed();
String getName();
void setName(String name);
Chunk getChunk(int x, int z);
void setChunk(int x, int z, Chunk chunkSection);
}

View File

@@ -1,6 +0,0 @@
package mc.core.world;
public interface WorldGenerator {
Region generateRegion (int x, int z, World world);
}

View File

@@ -1,18 +1,13 @@
package mc.core.world;
public enum WorldType implements IWorldType {
GENERAL("Standard world type"),
NETHER ("Nether world type"),
END ("End world type");
import lombok.Getter;
import lombok.RequiredArgsConstructor;
private final String description;
@RequiredArgsConstructor
@Getter
public enum WorldType {
DEFAULT("default"),
FLAT("flat");
WorldType(String description) {
this.description = description;
}
@Override
public String description() {
return description;
}
private final String name;
}

View File

@@ -14,10 +14,7 @@ public abstract class AbstractBlock implements Block {
@Setter
private Location location;
@Getter
private int meta;
@Getter
@Setter
private int light = 0; //TODO need to know range of values
private int light = 15;
@Getter
private final BlockType blockType;
private final Map<String, Tag<?>> nbtTagsMap = new HashMap<>();
@@ -26,14 +23,11 @@ public abstract class AbstractBlock implements Block {
this.blockType = type;
}
protected AbstractBlock(BlockType type, int meta) {
this.blockType = type;
this.meta = meta;
}
@Override
public int getId() {
return blockType.getId();
public void setLight(int light) {
if (light > 15) this.light = 15;
else if (light < 0) this.light = 0;
else this.light = light;
}
@Override

View File

@@ -3,50 +3,9 @@ package mc.core.world.block;
import mc.core.Location;
import mc.core.nbt.Taggable;
import java.io.Serializable;
/**
* Serialization block info
*
* +------------+--------+------------+
* | param | range | bits |
* +------------+--------+------------+
* | id | 0:255 | 8 |
* +------------+--------+------------+
* | meta | 0:15 | 4 |
* +------------+--------+------------+
* | x | 0:15 | 4 |
* +------------+--------+------------+
* | y | 0:15 | 4 |
* +------------+--------+------------+
* | z | 0:15 | 4 |
* +------------+--------+------------+
*
* Total: 24 bits per block (3 bytes)
*
*/
public interface Block extends Taggable, Serializable{
/** Block id */
int getId();
/**
* Addition in 0-15
* F.e. 35:0 - white wool
* 35:15 - black wool
*/
int getMeta();
public interface Block extends Taggable{
int getLight();
void setLight(int light);
/**
* Getting block type
*/
BlockType getBlockType();
/** Block location */
Location getLocation();
}

View File

@@ -1,28 +1,19 @@
package mc.core.world.block;
import mc.core.Location;
import mc.core.world.World;
public class BlockFactory {
public Block create(BlockType blockType, int meta, int x, int y, int z) {
return new EmbeddedBlock(blockType, meta, x, y, z);
public Block create(BlockType blockType, int x, int y, int z, World world) {
return new EmbeddedBlock(blockType, x, y, z, world);
}
public Block create(BlockType blockType, int meta) {
return new EmbeddedBlock(blockType, meta, 0, 0, 0);
}
public Block create(BlockType blockType) {
return create(blockType, 0, 0, 0, 0);
}
/**
* For first-time generation
*/
/** For first-time generation */
private class EmbeddedBlock extends AbstractBlock {
EmbeddedBlock(BlockType type, int meta, int x, int y, int z) {
super(type, meta);
super.setLocation(new Location(x,y,z, null));
EmbeddedBlock(BlockType type, int x, int y, int z, World world) {
super(type);
setLocation(new Location(x,y,z, world));
}
}
}

View File

@@ -1,25 +1,31 @@
package mc.core.world.block;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.Arrays;
import java.util.stream.Stream;
@RequiredArgsConstructor
public enum BlockType {
AIR(0, "Air"),
STONE(1, "Stone"),
GRASS(2, "Grass"),
DIRT(3, "Dirt"),
BEDROCK(7, "Bedrock"),
WATER(8, "Water"),
SAND(12, "Sand"),
SNOW(32, "Snow");
AIR(0, 0),
STONE(1, 0),
GRASS(2, 0),
DIRT(3, 0),
BEDROCK(7, 0),
WATER(9, 0),
SAND(12, 0),
SNOW(78, 0);
public static BlockType getByIdMeta(int id, int meta) {
Stream<BlockType> stream = Arrays.stream(BlockType.values());
return stream.filter(blockType -> blockType.id == id && blockType.meta == meta)
.findFirst()
.orElse(BlockType.AIR);
}
@Getter
private final int id;
@Getter
private final String name;
BlockType(int id, String name) {
this.id = id;
this.name = name;
}
private final int meta;
}

View File

@@ -1,15 +1,18 @@
package mc.core.world.chunk;
import mc.core.world.Region;
import mc.core.world.Biome;
import mc.core.world.World;
public interface Chunk {
World getWorld();
ChunkSection getChunkSection(int height);
ChunkSection setChunkSection(int height, ChunkSection chunkSection);
Region getRegion();
int getX();
int getZ();
ChunkSection getChunkSection(int height);
void setChunkSection(int height, ChunkSection chunkSection);
Biome getBiome(int localX, int localZ);
void setBiome(int localX, int localZ, Biome biome);
World getWorld();
void setWorld(World world);
}

View File

@@ -1,27 +0,0 @@
package mc.core.world.chunk;
import java.util.Optional;
public interface ChunkLoader {
/**
* Loads chunk from cache. If chunk in cache doesn't exist, loads from file (or other storage)
*
* @param x chunk position
* @param y chunk position
* @param z chunk position
* @return optional of chunk (nullable)
*/
Optional<ChunkSection> loadChunk (int x, int y, int z);
/**
* Tries to load chunk like {@link #loadChunk(int, int, int)}
* If chunk doesn't exist, generates it with selected world generator
*
* @param x chunk position
* @param y chunk position
* @param z chunk position
* @return chunk
*/
ChunkSection loadOrGenerateChunk (int x, int y, int z);
}

View File

@@ -4,38 +4,11 @@
*/
package mc.core.world.chunk;
import mc.core.world.Biome;
import mc.core.world.Region;
import mc.core.world.World;
import mc.core.world.block.Block;
import java.io.Serializable;
/**
* Serialization chunk info
*
* +-------------+----------------+------------+
* | param | range | bits |
* +-------------+----------------+------------+
* | blocks | array | 24*count |
* +-------------+----------------+------------+
*
* Total: 24 * block_count bits (3 * block_count bytes)
* Max size: 12288 bytes (~12 Kb per chunk)
*
*/
/* 16x16x16 */
public interface ChunkSection extends Serializable{
int getSkyLight(int x, int y, int z);
void setSkyLight(int x, int y, int z, int lightLevel);
int getAddition(int x, int y, int z);
void setAddition(int x, int y, int z, int value);
Biome getBiome(int x, int z);
void setBiome(int x, int z, Biome biome);
public interface ChunkSection {
int getX();
int getY();
int getZ();
@@ -43,6 +16,11 @@ public interface ChunkSection extends Serializable{
void setBlock(Block block);
Block getBlock(int x, int y, int z);
Region getRegion();
int getSkyLight(int x, int y, int z);
void setSkyLight(int x, int y, int z, int lightLevel);
int getAddition(int x, int y, int z);
void setAddition(int x, int y, int z, int value);
World getWorld();
}

View File

@@ -1,27 +1,17 @@
package mc.core;
import com.flowpowered.nbt.Tag;
import mc.core.world.IWorldType;
import mc.core.world.Region;
import mc.core.world.World;
import mc.core.world.chunk.ChunkSection;
import mc.core.world.WorldType;
import mc.core.world.chunk.Chunk;
import org.junit.Assert;
import org.junit.Test;
import java.util.UUID;
import java.util.stream.Stream;
public class EntityLocationTest {
public class TestEntityLocation {
@Test
public void cloneTest() {
World dummyWorld = new World() {
@Override
public UUID getWorldId() {
return null;
}
@Override
public IWorldType getWorldType() {
public WorldType getWorldType() {
return null;
}
@@ -36,54 +26,14 @@ public class EntityLocationTest {
}
@Override
public ChunkSection getChunk(int x, int y, int z) {
public Chunk getChunk(int x, int z) {
return null;
}
@Override
public void setChunk(int x, int y, int z, ChunkSection chunkSection) {
public void setChunk(int x, int z, Chunk chunk) {
}
@Override
public Region getRegion(int x, int z) {
return null;
}
@Override
public void setRegion(int x, int z, Region region) {
}
@Override
public int getSeed() {
return 0;
}
@Override
public String getName() {
return null;
}
@Override
public void setName(String name) {
}
@Override
public Tag<?> getTag(String name) {
return null;
}
@Override
public void setTag(Tag<?> tag) {
}
@Override
public Stream<Tag<?>> tagStream() {
return null;
}
};
EntityLocation firstLocation = new EntityLocation(10, 20, 30, 40, 50, dummyWorld);