Merge branch 'develop' into dmitriymx/location-refactory
# Conflicts: # core/src/main/java/mc/core/Location.java # flat_world/src/main/java/mc/world/flat/FlatWorld.java # generated_world/src/main/java/mc/world/generated_world/world/CubicWorld.java # proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/LoginHandler.java
This commit is contained in:
@@ -6,18 +6,21 @@ package mc.core;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import mc.core.exception.ResourceUnloadedException;
|
||||
import mc.core.world.World;
|
||||
import mc.core.world.chunk.Chunk;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.ref.Reference;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class Location implements Serializable, Cloneable {
|
||||
@Getter
|
||||
@Setter
|
||||
private double x, y, z;
|
||||
private WeakReference<World> refWorld;
|
||||
private Reference<World> refWorld;
|
||||
|
||||
public Location(double x, double y, double z, World world) {
|
||||
public Location (double x, double y, double z, World world) {
|
||||
setXYZ(x, y, z);
|
||||
setWorld(world);
|
||||
}
|
||||
@@ -33,10 +36,16 @@ public class Location implements Serializable, Cloneable {
|
||||
}
|
||||
|
||||
public World getWorld() {
|
||||
return refWorld.get();
|
||||
if (refWorld == null) {
|
||||
return null;
|
||||
} else if (refWorld.get() == null) {
|
||||
throw new ResourceUnloadedException("You're trying to get unloaded world");
|
||||
} else {
|
||||
return refWorld.get();
|
||||
}
|
||||
}
|
||||
|
||||
public void setWorld(World world) {
|
||||
public void setWorld (World world) {
|
||||
this.refWorld = new WeakReference<>(world);
|
||||
}
|
||||
|
||||
@@ -52,11 +61,22 @@ public class Location implements Serializable, Cloneable {
|
||||
return (int) z;
|
||||
}
|
||||
|
||||
public Chunk getChunk() {
|
||||
World world;
|
||||
if ((world = getWorld()) == null) {
|
||||
return null;
|
||||
} else {
|
||||
return world.getRegion((int) (x / 256), (int) (z / 256))
|
||||
.getChunk((int) ((x % 256) / 16), (int) ((z % 256) / 16));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location clone() {
|
||||
try {
|
||||
return (Location) super.clone();
|
||||
} catch (CloneNotSupportedException e) { // такое в нашем случае вообще возможно?
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package mc.core.exception;
|
||||
|
||||
public abstract class McCoreUncheckedException extends RuntimeException {
|
||||
|
||||
public McCoreUncheckedException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public McCoreUncheckedException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package mc.core.exception;
|
||||
|
||||
public class ResourceUnloadedException extends McCoreUncheckedException {
|
||||
|
||||
public ResourceUnloadedException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package mc.core.serialization;
|
||||
|
||||
import mc.core.world.chunk.Chunk;
|
||||
import mc.core.world.ChunkSection;
|
||||
import mc.core.world.Region;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface IChunkReader {
|
||||
Chunk read (Region region, int x, int y, int z) throws IOException;
|
||||
ChunkSection read (Region region, int x, int y, int z) throws IOException;
|
||||
}
|
||||
|
||||
45
core/src/main/java/mc/core/world/ChunkSection.java
Normal file
45
core/src/main/java/mc/core/world/ChunkSection.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-15
|
||||
*/
|
||||
package mc.core.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);
|
||||
|
||||
int getX();
|
||||
int getY();
|
||||
int getZ();
|
||||
|
||||
void setBlock(Block block);
|
||||
Block getBlock(int x, int y, int z);
|
||||
|
||||
Region getRegion();
|
||||
World getWorld();
|
||||
}
|
||||
@@ -22,8 +22,13 @@ import java.io.Serializable;
|
||||
*
|
||||
*/
|
||||
public interface Region extends Serializable{
|
||||
Chunk getChunkAt(int x, int y, int z);
|
||||
void setChunk(int x, int y, int z, Chunk chunk);
|
||||
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();
|
||||
@@ -31,5 +36,7 @@ public interface Region extends Serializable{
|
||||
Biome getBiomeAt (int x, int z);
|
||||
void setBiome (int x, int z, Biome biome);
|
||||
|
||||
void save(Serializer<Chunk> chunkSerializer, IRegionReaderWriter regionReaderWritter) throws IOException;
|
||||
World getWorld();
|
||||
|
||||
void save(Serializer<ChunkSection> chunkSerializer, IRegionReaderWriter regionReaderWritter) throws IOException;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ package mc.core.world;
|
||||
|
||||
import mc.core.EntityLocation;
|
||||
import mc.core.nbt.Taggable;
|
||||
import mc.core.world.chunk.Chunk;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
@@ -49,8 +48,8 @@ public interface World extends Taggable, Serializable{
|
||||
EntityLocation getSpawn();
|
||||
void setSpawn(EntityLocation location);
|
||||
|
||||
Chunk getChunk(int x, int y, int z);
|
||||
void setChunk(int x, int y, int z, Chunk chunk);
|
||||
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);
|
||||
|
||||
@@ -1,46 +1,16 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-04-15
|
||||
*/
|
||||
package mc.core.world.chunk;
|
||||
|
||||
import mc.core.Location;
|
||||
import mc.core.world.Biome;
|
||||
import mc.core.world.block.Block;
|
||||
import mc.core.world.ChunkSection;
|
||||
import mc.core.world.Region;
|
||||
import mc.core.world.World;
|
||||
|
||||
import java.io.Serializable;
|
||||
public interface Chunk {
|
||||
|
||||
/**
|
||||
* 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 Chunk extends Serializable{
|
||||
Block getBlock(int x, int y, int z);
|
||||
default Block getBlock(Location location) {
|
||||
return getBlock(location.getBlockX(), location.getBlockY(), location.getBlockZ());
|
||||
}
|
||||
void setBlock(Block block);
|
||||
|
||||
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);
|
||||
World getWorld();
|
||||
ChunkSection getChunkSection(int height);
|
||||
ChunkSection setChunkSection(int height, ChunkSection chunkSection);
|
||||
Region getRegion();
|
||||
|
||||
int getX();
|
||||
int getY();
|
||||
int getZ();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package mc.core.world.chunk;
|
||||
|
||||
import mc.core.world.ChunkSection;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ChunkLoader {
|
||||
@@ -12,7 +14,7 @@ public interface ChunkLoader {
|
||||
* @param z chunk position
|
||||
* @return optional of chunk (nullable)
|
||||
*/
|
||||
Optional<Chunk> loadChunk (int x, int y, int z);
|
||||
Optional<ChunkSection> loadChunk (int x, int y, int z);
|
||||
|
||||
/**
|
||||
* Tries to load chunk like {@link #loadChunk(int, int, int)}
|
||||
@@ -23,5 +25,5 @@ public interface ChunkLoader {
|
||||
* @param z chunk position
|
||||
* @return chunk
|
||||
*/
|
||||
Chunk loadOrGenerateChunk (int x, int y, int z);
|
||||
ChunkSection loadOrGenerateChunk (int x, int y, int z);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user