From d7c0a7078f2988148e70f8452cb8d6e8215bf660 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Thu, 16 Aug 2018 14:28:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D1=83=D0=BA=D0=B0=D0=B7=D1=8B=D0=B2=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D0=BF=D1=80=D0=BE=D0=B8=D0=B7=D0=B2=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D1=8B=D0=B5=20=D1=81=D0=BB=D0=BE=D0=B8=20=D0=B1?= =?UTF-8?q?=D0=BB=D0=BE=D0=BA=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/mc/world/flat/FlatWorld.java | 27 +++++++++++++++- .../main/java/mc/world/flat/SimpleChunk.java | 3 +- .../mc/world/flat/SimpleChunkSection.java | 31 ++++++++++++++----- .../proto_1_12_2/packets/ChunkDataPacket.java | 5 ++- 4 files changed, 56 insertions(+), 10 deletions(-) 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 6ec745f..f687e85 100644 --- a/flat_world/src/main/java/mc/world/flat/FlatWorld.java +++ b/flat_world/src/main/java/mc/world/flat/FlatWorld.java @@ -9,15 +9,19 @@ import lombok.extern.slf4j.Slf4j; import mc.core.EntityLocation; import mc.core.world.World; import mc.core.world.WorldType; +import mc.core.world.block.BlockType; import mc.core.world.chunk.Chunk; import mc.core.world.chunk.ChunkSection; +import java.util.ArrayList; +import java.util.List; + @Slf4j public class FlatWorld implements World { @Getter private final WorldType worldType = WorldType.FLAT; private EntityLocation spawn; - private ChunkSection chunkSection = new SimpleChunkSection(); + private ChunkSection chunkSection; @Override public EntityLocation getSpawn() { @@ -46,4 +50,25 @@ public class FlatWorld implements World { public void setChunk(int x, int z, Chunk chunk) { throw new UnsupportedOperationException(); } + + public void setLayersBlock(List listOfLayers) { + List layoutsBlock = new ArrayList<>(); + + for (String value : listOfLayers) { + String[] splitValue = value.split(";"); + + BlockType blockType; + try { + blockType = BlockType.valueOf(splitValue[1]); + } catch (IllegalArgumentException e) { + continue; + } + + for (int i = 0; i < Integer.parseInt(splitValue[0]); i++) { + layoutsBlock.add(blockType); + } + } + + this.chunkSection = new SimpleChunkSection(layoutsBlock, this); + } } diff --git a/flat_world/src/main/java/mc/world/flat/SimpleChunk.java b/flat_world/src/main/java/mc/world/flat/SimpleChunk.java index 5208c33..13d81f6 100644 --- a/flat_world/src/main/java/mc/world/flat/SimpleChunk.java +++ b/flat_world/src/main/java/mc/world/flat/SimpleChunk.java @@ -2,6 +2,7 @@ package mc.world.flat; import lombok.Getter; import lombok.extern.slf4j.Slf4j; +import mc.core.exception.ResourceUnloadedException; import mc.core.world.Biome; import mc.core.world.World; import mc.core.world.chunk.Chunk; @@ -47,7 +48,7 @@ public class SimpleChunk implements Chunk { @Override public World getWorld() { if (refWorld.get() == null) { - throw new WorldUnloadedException(); + throw new ResourceUnloadedException("World unloaded"); } else { return refWorld.get(); } 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 4db9cf5..7b3265e 100644 --- a/flat_world/src/main/java/mc/world/flat/SimpleChunkSection.java +++ b/flat_world/src/main/java/mc/world/flat/SimpleChunkSection.java @@ -4,18 +4,28 @@ */ package mc.world.flat; +import mc.core.exception.ResourceUnloadedException; import mc.core.world.Biome; import mc.core.world.World; import mc.core.world.block.Block; import mc.core.world.block.BlockFactory; import mc.core.world.block.BlockType; -import mc.core.world.chunk.Chunk; import mc.core.world.chunk.ChunkSection; import java.lang.ref.Reference; import java.lang.ref.WeakReference; +import java.util.List; public class SimpleChunkSection implements ChunkSection { + private final BlockFactory blockFactory = new BlockFactory(); + private final List layersBlock; + private Reference refWorld; + + public SimpleChunkSection(List layersBlock, World world) { + this.layersBlock = layersBlock; + this.refWorld = new WeakReference<>(world); + } + @Override public int getSkyLight(int x, int y, int z) { if (y <= 3) return 0; @@ -61,16 +71,23 @@ public class SimpleChunkSection implements ChunkSection { @Override public Block getBlock(int x, int y, int z) { - BlockFactory blockFactory = new BlockFactory(); + if (y >= layersBlock.size()) { + return blockFactory.create(BlockType.AIR, x, y, z, getWorld()); + } - 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()); + BlockType blockType = layersBlock.get(y); + if (blockType == null) return blockFactory.create(BlockType.AIR, x, y, z, getWorld()); + + return blockFactory.create(blockType, x, y, z, getWorld()); } @Override public World getWorld() { - throw new UnsupportedOperationException(); + World world = refWorld.get(); + if (world == null) { + throw new ResourceUnloadedException("World unloaded"); + } + + return world; } } 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 b3d6cd7..6600db6 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 @@ -102,6 +102,7 @@ public class ChunkDataPacket implements SCPacket { netStream.writeInt(z); // Chunk Y netStream.writeBoolean(initChunk); // Init Chunk + int maxH = 0; if (sectionList == null && chunk != null) { int bitMask = 0; for (int h = 15; h >= 0; h--) { @@ -109,6 +110,7 @@ public class ChunkDataPacket implements SCPacket { ChunkSection chunkSection = chunk.getChunkSection(h); if (chunkSection != null && chunkSection.getY() == h) { bitMask |= 0x01; + maxH++; } else { bitMask |= 0x00; } @@ -123,6 +125,7 @@ public class ChunkDataPacket implements SCPacket { ChunkSection chunkSection = sectionList.get(i); if (chunkSection != null && chunkSection.getY() == h) { bitMask |= 0x01; + maxH++; } else { bitMask |= 0x00; } @@ -138,7 +141,7 @@ public class ChunkDataPacket implements SCPacket { int dataItems = 0; final int airBlockPalette = serializeBlockState(BlockType.AIR); - for (int h = 0; h < 16; h++) { + for (int h = 0; h < maxH; h++) { ChunkSection chunkSection = null; if (chunk != null) {