Archived
0

первый удачный алгоритм загрузки мира

This commit is contained in:
2021-06-21 00:22:55 +03:00
parent bb3f0bbdcb
commit 06be69f3e4
11 changed files with 457 additions and 79 deletions

View File

@@ -99,8 +99,7 @@ public class ProcessorLogin implements PacketProcessor {
Chunk chunk = world.getChunk((int) chunkLocation.getX(), (int) chunkLocation.getZ());
var chunkDataPacket = new ChunkDataPacket();
chunkDataPacket.setX(chunk.getX());
chunkDataPacket.setZ(chunk.getZ());
chunkDataPacket.setChunk(chunk);
player.getCtx().write(chunkDataPacket);
@@ -113,8 +112,7 @@ public class ProcessorLogin implements PacketProcessor {
for (int x = minX; x <= maxX; x++) {
if ((z == minZ || z == maxZ) || (x == minX || x == maxX)) {
chunkDataPacket = new ChunkDataPacket();
chunkDataPacket.setX(x);
chunkDataPacket.setZ(z);
chunkDataPacket.setChunk(world.getChunk(x, z));
player.getCtx().write(chunkDataPacket);
}
}

View File

@@ -0,0 +1,33 @@
package mc.server.world;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import mc.protocol.model.Location;
import mc.protocol.world.Block;
@RequiredArgsConstructor
public class AirBlock implements Block {
@Getter
private final Location location;
@Override
public int getId() {
return 0;
}
@Override
public int getMeta() {
return 0;
}
@Override
public int getLight() {
return 0;
}
@Override
public void setLight(int value) {
throw new UnsupportedOperationException();
}
}

View File

@@ -2,10 +2,25 @@ package mc.server.world;
import lombok.Data;
import mc.protocol.world.Chunk;
import mc.protocol.world.ChunkSection;
import java.util.HashMap;
import java.util.Map;
@Data
public class VoidChunk implements Chunk {
private final int x;
private final int z;
private final Map<Integer, VoidChunkSection> sections = new HashMap<>();
@Override
public ChunkSection getSection(int height) {
return sections.computeIfAbsent(height, VoidChunkSection::new);
}
@Override
public byte getBiome(int x, int z) {
return 127; // 127 | 7F | minecraft:void | The Void
}
}

View File

@@ -0,0 +1,29 @@
package mc.server.world;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import mc.protocol.model.Location;
import mc.protocol.world.Block;
import mc.protocol.world.ChunkSection;
import java.util.HashMap;
import java.util.Map;
@RequiredArgsConstructor
@Getter
public class VoidChunkSection implements ChunkSection {
private final int y;
private final Map<Location, AirBlock> blocks = new HashMap<>();
@Override
public Block getBlock(int x, int y, int z) {
return blocks.computeIfAbsent(new Location().set(x, y, z), AirBlock::new);
}
@Override
public int getSkyLight(int x, int y, int z) {
return 0;
}
}

View File

@@ -1,13 +1,16 @@
package mc.server.world;
import mc.protocol.model.Location;
import mc.protocol.pool.ProtocolObjectPool;
import mc.protocol.utils.LevelType;
import mc.protocol.utils.Table;
import mc.protocol.world.Chunk;
import mc.protocol.world.World;
public class VoidWorld implements World {
private static final Location spawn = new Location().set(7d, 130d, 7d);
private static final Location spawn = ProtocolObjectPool.getLocationPool().borrowObject().set(7d, 130d, 7d);
private final Table<Integer, Integer, VoidChunk> chunkTable = new Table<>();
@Override
public LevelType getLevelType() {
@@ -21,6 +24,12 @@ public class VoidWorld implements World {
@Override
public Chunk getChunk(int x, int z) {
return new VoidChunk(x, z);
VoidChunk chunk = chunkTable.getColumnAndRow(x, z);
if (chunk == null) {
chunk = new VoidChunk(x, z);
chunkTable.put(x, z, chunk);
}
return chunk;
}
}