Archived
0

добавлена возможность указывать произвольные слои блоков

This commit is contained in:
2018-08-16 14:28:35 +03:00
parent 39d7872d6a
commit d7c0a7078f
4 changed files with 56 additions and 10 deletions

View File

@@ -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<String> listOfLayers) {
List<BlockType> 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);
}
}

View File

@@ -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();
}

View File

@@ -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<BlockType> layersBlock;
private Reference<World> refWorld;
public SimpleChunkSection(List<BlockType> 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;
}
}

View File

@@ -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) {