From dc9e3512e71cb2b4dbd4eabc137d4c082d1f05f1 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Thu, 16 Aug 2018 00:08:26 +0300 Subject: [PATCH] refactory world and clean classes --- build.gradle | 10 +-- core/src/main/java/mc/core/Direction.java | 8 --- core/src/main/java/mc/core/Location.java | 16 +++-- .../mc/core/serialization/IChunkReader.java | 10 --- .../serialization/IRegionReaderWriter.java | 12 ---- .../main/java/mc/core/world/IWorldType.java | 6 -- core/src/main/java/mc/core/world/Region.java | 43 ------------- core/src/main/java/mc/core/world/World.java | 54 ++-------------- .../java/mc/core/world/WorldGenerator.java | 6 -- .../main/java/mc/core/world/WorldType.java | 21 +++---- .../mc/core/world/block/AbstractBlock.java | 16 ++--- .../main/java/mc/core/world/block/Block.java | 43 +------------ .../mc/core/world/block/BlockFactory.java | 23 +++---- .../java/mc/core/world/block/BlockType.java | 36 ++++++----- .../main/java/mc/core/world/chunk/Chunk.java | 17 ++--- .../java/mc/core/world/chunk/ChunkLoader.java | 27 -------- .../mc/core/world/chunk/ChunkSection.java | 36 +++-------- ...ationTest.java => TestEntityLocation.java} | 62 ++----------------- .../main/java/mc/world/flat/FlatWorld.java | 59 +++--------------- .../main/java/mc/world/flat/SimpleChunk.java | 61 ++++++++++++++++++ .../mc/world/flat/SimpleChunkSection.java | 25 ++------ .../proto_1_12_2/packets/ChunkDataPacket.java | 27 +++++--- .../netty/PlayerEventListener.java | 5 +- .../netty/handlers/LoginHandler.java | 2 +- 24 files changed, 175 insertions(+), 450 deletions(-) delete mode 100644 core/src/main/java/mc/core/Direction.java delete mode 100644 core/src/main/java/mc/core/serialization/IChunkReader.java delete mode 100644 core/src/main/java/mc/core/serialization/IRegionReaderWriter.java delete mode 100644 core/src/main/java/mc/core/world/IWorldType.java delete mode 100644 core/src/main/java/mc/core/world/Region.java delete mode 100644 core/src/main/java/mc/core/world/WorldGenerator.java delete mode 100644 core/src/main/java/mc/core/world/chunk/ChunkLoader.java rename core/src/test/java/mc/core/{EntityLocationTest.java => TestEntityLocation.java} (51%) create mode 100644 flat_world/src/main/java/mc/world/flat/SimpleChunk.java diff --git a/build.gradle b/build.gradle index a657d38..6db4f4a 100644 --- a/build.gradle +++ b/build.gradle @@ -59,13 +59,13 @@ task runApp(type: JavaExec) { classpath += prj.sourceSets.main.runtimeClasspath } /* Uncomment, if your Log Implements are folder '{workDir}/log-impl' */ - //classpath += files(fileTree(dir: new File(workingDir, "log-impl"))) + classpath += files(fileTree(dir: new File(workingDir, "log-impl"))) /* Uncomment, if you used VM args */ - //jvmArgs = [ - // "-DspringConfig=app.xml", - // "-Dlog4j.configurationFile=log4j2.xml" - //] + jvmArgs = [ + "-DspringConfig=spring-flat.xml", + "-Dlog4j.configurationFile=log4j2.xml" + ] ignoreExitValue = true } diff --git a/core/src/main/java/mc/core/Direction.java b/core/src/main/java/mc/core/Direction.java deleted file mode 100644 index 74e4630..0000000 --- a/core/src/main/java/mc/core/Direction.java +++ /dev/null @@ -1,8 +0,0 @@ -package mc.core; - -public enum Direction { - NORTH, - EAST, - WEST, - SOUTH -} diff --git a/core/src/main/java/mc/core/Location.java b/core/src/main/java/mc/core/Location.java index aba8ba3..afb3582 100644 --- a/core/src/main/java/mc/core/Location.java +++ b/core/src/main/java/mc/core/Location.java @@ -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)); } } diff --git a/core/src/main/java/mc/core/serialization/IChunkReader.java b/core/src/main/java/mc/core/serialization/IChunkReader.java deleted file mode 100644 index ad11c5a..0000000 --- a/core/src/main/java/mc/core/serialization/IChunkReader.java +++ /dev/null @@ -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; -} diff --git a/core/src/main/java/mc/core/serialization/IRegionReaderWriter.java b/core/src/main/java/mc/core/serialization/IRegionReaderWriter.java deleted file mode 100644 index 76a7023..0000000 --- a/core/src/main/java/mc/core/serialization/IRegionReaderWriter.java +++ /dev/null @@ -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; -} diff --git a/core/src/main/java/mc/core/world/IWorldType.java b/core/src/main/java/mc/core/world/IWorldType.java deleted file mode 100644 index 64d5643..0000000 --- a/core/src/main/java/mc/core/world/IWorldType.java +++ /dev/null @@ -1,6 +0,0 @@ -package mc.core.world; - -public interface IWorldType { - String name(); - String description(); -} diff --git a/core/src/main/java/mc/core/world/Region.java b/core/src/main/java/mc/core/world/Region.java deleted file mode 100644 index 970aace..0000000 --- a/core/src/main/java/mc/core/world/Region.java +++ /dev/null @@ -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 chunkSerializer, IRegionReaderWriter regionReaderWritter) throws IOException; -} diff --git a/core/src/main/java/mc/core/world/World.java b/core/src/main/java/mc/core/world/World.java index e112aed..20bf437 100644 --- a/core/src/main/java/mc/core/world/World.java +++ b/core/src/main/java/mc/core/world/World.java @@ -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); } diff --git a/core/src/main/java/mc/core/world/WorldGenerator.java b/core/src/main/java/mc/core/world/WorldGenerator.java deleted file mode 100644 index 817012c..0000000 --- a/core/src/main/java/mc/core/world/WorldGenerator.java +++ /dev/null @@ -1,6 +0,0 @@ -package mc.core.world; - -public interface WorldGenerator { - - Region generateRegion (int x, int z, World world); -} diff --git a/core/src/main/java/mc/core/world/WorldType.java b/core/src/main/java/mc/core/world/WorldType.java index 73533fa..86f2680 100644 --- a/core/src/main/java/mc/core/world/WorldType.java +++ b/core/src/main/java/mc/core/world/WorldType.java @@ -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; } diff --git a/core/src/main/java/mc/core/world/block/AbstractBlock.java b/core/src/main/java/mc/core/world/block/AbstractBlock.java index 5d0572c..92fb3f2 100644 --- a/core/src/main/java/mc/core/world/block/AbstractBlock.java +++ b/core/src/main/java/mc/core/world/block/AbstractBlock.java @@ -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> 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 diff --git a/core/src/main/java/mc/core/world/block/Block.java b/core/src/main/java/mc/core/world/block/Block.java index a140ab0..d1a392f 100644 --- a/core/src/main/java/mc/core/world/block/Block.java +++ b/core/src/main/java/mc/core/world/block/Block.java @@ -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(); - } diff --git a/core/src/main/java/mc/core/world/block/BlockFactory.java b/core/src/main/java/mc/core/world/block/BlockFactory.java index 1c80548..caceb58 100644 --- a/core/src/main/java/mc/core/world/block/BlockFactory.java +++ b/core/src/main/java/mc/core/world/block/BlockFactory.java @@ -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)); } } } diff --git a/core/src/main/java/mc/core/world/block/BlockType.java b/core/src/main/java/mc/core/world/block/BlockType.java index 7adb32a..2b3bd1f 100644 --- a/core/src/main/java/mc/core/world/block/BlockType.java +++ b/core/src/main/java/mc/core/world/block/BlockType.java @@ -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 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; } diff --git a/core/src/main/java/mc/core/world/chunk/Chunk.java b/core/src/main/java/mc/core/world/chunk/Chunk.java index 2a03406..9436ccf 100644 --- a/core/src/main/java/mc/core/world/chunk/Chunk.java +++ b/core/src/main/java/mc/core/world/chunk/Chunk.java @@ -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); } diff --git a/core/src/main/java/mc/core/world/chunk/ChunkLoader.java b/core/src/main/java/mc/core/world/chunk/ChunkLoader.java deleted file mode 100644 index 0462773..0000000 --- a/core/src/main/java/mc/core/world/chunk/ChunkLoader.java +++ /dev/null @@ -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 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); -} diff --git a/core/src/main/java/mc/core/world/chunk/ChunkSection.java b/core/src/main/java/mc/core/world/chunk/ChunkSection.java index a70169e..97433d2 100644 --- a/core/src/main/java/mc/core/world/chunk/ChunkSection.java +++ b/core/src/main/java/mc/core/world/chunk/ChunkSection.java @@ -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(); } diff --git a/core/src/test/java/mc/core/EntityLocationTest.java b/core/src/test/java/mc/core/TestEntityLocation.java similarity index 51% rename from core/src/test/java/mc/core/EntityLocationTest.java rename to core/src/test/java/mc/core/TestEntityLocation.java index 7adfc7f..344991d 100644 --- a/core/src/test/java/mc/core/EntityLocationTest.java +++ b/core/src/test/java/mc/core/TestEntityLocation.java @@ -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> tagStream() { - return null; - } }; EntityLocation firstLocation = new EntityLocation(10, 20, 30, 40, 50, dummyWorld); diff --git a/flat_world/src/main/java/mc/world/flat/FlatWorld.java b/flat_world/src/main/java/mc/world/flat/FlatWorld.java index 0fd787c..6ec745f 100644 --- a/flat_world/src/main/java/mc/world/flat/FlatWorld.java +++ b/flat_world/src/main/java/mc/world/flat/FlatWorld.java @@ -4,38 +4,21 @@ */ package mc.world.flat; -import com.flowpowered.nbt.Tag; import lombok.Getter; -import lombok.Setter; import lombok.extern.slf4j.Slf4j; import mc.core.EntityLocation; -import mc.core.world.IWorldType; -import mc.core.world.Region; import mc.core.world.World; import mc.core.world.WorldType; +import mc.core.world.chunk.Chunk; import mc.core.world.chunk.ChunkSection; -import java.util.UUID; -import java.util.stream.Stream; - @Slf4j public class FlatWorld implements World { - @Getter - @Setter - private UUID worldId = UUID.fromString("00000000-0000-0000-C000-000000000046"); - @Getter - @Setter - private String name; - + private final WorldType worldType = WorldType.FLAT; private EntityLocation spawn; private ChunkSection chunkSection = new SimpleChunkSection(); - @Override - public IWorldType getWorldType() { - return WorldType.GENERAL; - } - @Override public EntityLocation getSpawn() { if (this.spawn == null) { @@ -53,42 +36,14 @@ public class FlatWorld implements World { } @Override - public ChunkSection getChunk(int x, int y, int z) { - return chunkSection; + public Chunk getChunk(int x, int z) { + Chunk chunk = new SimpleChunk(x, z, this); + chunk.setChunkSection(0, chunkSection); + return chunk; } @Override - public void setChunk(int x, int y, int z, ChunkSection chunkSection) { - throw new UnsupportedOperationException(); - } - - @Override - public Region getRegion(int x, int z) { - return null; - } - - @Override - public void setRegion(int x, int z, Region region) { - throw new UnsupportedOperationException(); - } - - @Override - public int getSeed() { - return 0; - } - - @Override - public Tag getTag(String name) { - throw new UnsupportedOperationException(); - } - - @Override - public void setTag(Tag tag) { - throw new UnsupportedOperationException(); - } - - @Override - public Stream> tagStream() { + public void setChunk(int x, int z, Chunk chunk) { throw new UnsupportedOperationException(); } } diff --git a/flat_world/src/main/java/mc/world/flat/SimpleChunk.java b/flat_world/src/main/java/mc/world/flat/SimpleChunk.java new file mode 100644 index 0000000..21cafb7 --- /dev/null +++ b/flat_world/src/main/java/mc/world/flat/SimpleChunk.java @@ -0,0 +1,61 @@ +package mc.world.flat; + +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import mc.core.world.Biome; +import mc.core.world.World; +import mc.core.world.chunk.Chunk; +import mc.core.world.chunk.ChunkSection; + +import java.lang.ref.Reference; +import java.lang.ref.WeakReference; + +@Slf4j +public class SimpleChunk implements Chunk { + @Getter + private int x, z; + private Reference refWorld; + private ChunkSection chunkSection; + private final Biome biome = Biome.PLAINS; + + public SimpleChunk(int x, int z, World world) { + this.x = x; + this.z = z; + setWorld(world); + } + + @Override + public ChunkSection getChunkSection(int height) { + return chunkSection; + } + + @Override + public void setChunkSection(int height, ChunkSection chunkSection) { + this.chunkSection = chunkSection; + } + + @Override + public Biome getBiome(int localX, int localZ) { + return biome; + } + + @Override + public void setBiome(int localX, int localZ, Biome biome) { + // ignore + } + + @Override + public World getWorld() { + if (refWorld.get() == null) { + log.error("World unloaded?"); + return null; + } else { + return refWorld.get(); + } + } + + @Override + public void setWorld(World world) { + this.refWorld = new WeakReference<>(world); + } +} diff --git a/flat_world/src/main/java/mc/world/flat/SimpleChunkSection.java b/flat_world/src/main/java/mc/world/flat/SimpleChunkSection.java index 73e47f1..c70ec87 100644 --- a/flat_world/src/main/java/mc/world/flat/SimpleChunkSection.java +++ b/flat_world/src/main/java/mc/world/flat/SimpleChunkSection.java @@ -4,8 +4,6 @@ */ package mc.world.flat; -import mc.core.world.Biome; -import mc.core.world.Region; import mc.core.world.World; import mc.core.world.block.Block; import mc.core.world.block.BlockFactory; @@ -34,16 +32,6 @@ public class SimpleChunkSection implements ChunkSection { } - @Override - public Biome getBiome(int x, int z) { - return Biome.PLAINS; - } - - @Override - public void setBiome(int x, int z, Biome biome) { - - } - @Override public int getX() { return 0; @@ -68,15 +56,10 @@ public class SimpleChunkSection implements ChunkSection { public Block getBlock(int x, int y, int z) { BlockFactory blockFactory = new BlockFactory(); - if (y == 0) return blockFactory.create(BlockType.BEDROCK, 0); - else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, 0); - else if (y == 3) return blockFactory.create(BlockType.GRASS, 0); - else return blockFactory.create(BlockType.AIR, 0); - } - - @Override - public Region getRegion() { - return null; + if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z, getWorld()); + else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, x, y, z, getWorld()); + else if (y == 3) return blockFactory.create(BlockType.GRASS, x, y, z, getWorld()); + else return blockFactory.create(BlockType.AIR, x, y, z, getWorld()); } @Override diff --git a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/ChunkDataPacket.java b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/ChunkDataPacket.java index ff94732..2dd9414 100644 --- a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/ChunkDataPacket.java +++ b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/ChunkDataPacket.java @@ -12,6 +12,8 @@ import mc.core.network.NetOutputStream; import mc.core.network.SCPacket; import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream; import mc.core.world.block.Block; +import mc.core.world.block.BlockType; +import mc.core.world.chunk.Chunk; import mc.core.world.chunk.ChunkSection; import java.util.ArrayList; @@ -75,11 +77,11 @@ public class ChunkDataPacket implements SCPacket { private int z; @Setter private boolean initChunk = true; // "Ground-Up Continuous" - @Getter - private List chunks = new ArrayList<>(); + @Setter + private Chunk chunk; - private int serializeBlockState(int id, int meta) { - return (id << 4) | meta; + private int serializeBlockState(BlockType blockType) { + return (blockType.getId() << 4) | blockType.getMeta(); } @Override @@ -91,9 +93,14 @@ public class ChunkDataPacket implements SCPacket { final ByteArrayOutputNetStream data = new ByteArrayOutputNetStream(); int dataItems = 0; - final int airBlockPalette = serializeBlockState(0, 0); + final int airBlockPalette = serializeBlockState(BlockType.AIR); + + for (int h = 0; h < 1/*потому что у нас пока только единичная сейция*/; h++) { + ChunkSection chunkSection = chunk.getChunkSection(h); + if (chunkSection == null) { + continue; + } - for (ChunkSection chunk : chunks) { final List palette = new ArrayList<>(); palette.add(airBlockPalette); final ByteArrayOutputNetStream dataArray = new ByteArrayOutputNetStream(); @@ -112,8 +119,8 @@ public class ChunkDataPacket implements SCPacket { for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { for (int x = 0; x < 16; x++) { - Block block = chunk.getBlock(x, y, z); - int blockState = serializeBlockState(block.getId(), block.getMeta()); + Block block = chunkSection.getBlock(x, y, z); + int blockState = serializeBlockState(block.getBlockType()); int currentIndexPaletteBlock; if (!palette.contains(blockState)) { @@ -138,12 +145,12 @@ public class ChunkDataPacket implements SCPacket { if (idxHalfByte == 0) { blockLightCompacted = block.getLight(); - skyLightCompacted = chunk.getSkyLight(x, y, z); + skyLightCompacted = chunkSection.getSkyLight(x, y, z); idxHalfByte++; } else { blockLightCompacted = (blockLightCompacted << 4) | block.getLight(); blockLight.writeByte(blockLightCompacted); - skyLightCompacted = (skyLightCompacted << 4) | chunk.getSkyLight(x, y, z); + skyLightCompacted = (skyLightCompacted << 4) | chunkSection.getSkyLight(x, y, z); skyLight.writeByte(skyLightCompacted); idxHalfByte = 0; } diff --git a/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/PlayerEventListener.java b/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/PlayerEventListener.java index 199c93a..63415cd 100644 --- a/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/PlayerEventListener.java +++ b/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/PlayerEventListener.java @@ -8,6 +8,7 @@ import mc.core.network.proto_1_12_2.TeleportManager; import mc.core.network.proto_1_12_2.packets.ChunkDataPacket; import mc.core.network.proto_1_12_2.packets.PlayerPositionAndLookPacket; import mc.core.utils.CompactedCoords; +import mc.core.world.chunk.Chunk; import mc.core.world.chunk.ChunkSection; @Slf4j @@ -29,13 +30,13 @@ class PlayerEventListener { for(Integer compressXZ : event.getNeedLoadChunks()) { int[] xz = CompactedCoords.uncompressXZ(compressXZ); - ChunkSection chunkSection = event.getPlayer().getLocation().getWorld().getChunk(xz[0], 0, xz[1]); + Chunk chunk = event.getPlayer().getLocation().getWorld().getChunk(xz[0], xz[1]); ChunkDataPacket packet = new ChunkDataPacket(); packet.setX(xz[0]); packet.setZ(xz[1]); packet.setInitChunk(true); - packet.getChunks().add(chunkSection); + packet.setChunk(chunk); event.getPlayer().getChannel().write(packet); } diff --git a/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/LoginHandler.java b/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/LoginHandler.java index bce94e5..b2a0a76 100644 --- a/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/LoginHandler.java +++ b/proto_1.12.2_netty/src/main/java/mc/core/network/proto_1_12_2/netty/handlers/LoginHandler.java @@ -85,7 +85,7 @@ public class LoginHandler extends AbstractStateHandler implements LoginStateHand ChunkDataPacket pkt8 = new ChunkDataPacket(); pkt8.setX(0); pkt8.setZ(0); - pkt8.getChunks().add(world.getChunk(0, 0,0)); + pkt8.setChunk(world.getChunk(0, 0)); pkt8.setInitChunk(true); channel.writeAndFlush(pkt8); player.getLoadedChunks().add(CompactedCoords.compressXZ(0, 0));