Archived
0

сериализация полного чанка

This commit is contained in:
2021-06-27 18:54:30 +03:00
parent 9ed6db2484
commit c1854c8f73
18 changed files with 203 additions and 190 deletions

View File

@@ -67,7 +67,7 @@ public class Main {
//endregion
ServerComponent serverComponent = DaggerServerComponent.builder()
.processorModule(new ScenarioModule(configComponent.getConfig()))
.scenarioModule(new ScenarioModule(configComponent.getConfig()))
.build();
serverComponent.getPacketScenarios().forEach(scenario -> scenario.setup(serverComponent.getProtocolHandlersBus()));

View File

@@ -3,7 +3,7 @@ package mc.server.di;
import dagger.Module;
import dagger.Provides;
import mc.protocol.world.World;
import mc.server.world.VoidWorld;
import mc.server.world.SomeWorld;
import javax.inject.Singleton;
@@ -13,6 +13,6 @@ public class WorldModule {
@Provides
@Singleton
World provideWorld() {
return new VoidWorld();
return new SomeWorld();
}
}

View File

@@ -37,7 +37,7 @@ public class ScenarioLogin implements PacketScenario {
}
private void login(ChannelHandlerContext ctx, LoginStartPacket packet) {
Player player = playerManager.create(ctx, packet.getName(), GameMode.SURVIVAL, world.getSpawn());
Player player = playerManager.create(ctx, packet.getName(), GameMode.CREATIVE, world.getSpawn());
ctx.channel().attr(ServetAttributes.PLAYER).set(player);
sendLoginSuccess(player);
@@ -85,7 +85,7 @@ public class ScenarioLogin implements PacketScenario {
var playerAbilitiesPacket = new PlayerAbilitiesPacket();
playerAbilitiesPacket.setCatFly(true);
playerAbilitiesPacket.setFlying(true);
playerAbilitiesPacket.setCreativeMode(false);
playerAbilitiesPacket.setCreativeMode(true);
playerAbilitiesPacket.setInvulnerable(true);
playerAbilitiesPacket.setFieldOfView(0.0f);
playerAbilitiesPacket.setFlyingSpeed(0.05f);
@@ -96,7 +96,8 @@ public class ScenarioLogin implements PacketScenario {
@SuppressWarnings("java:S2589")
private void sendWorldData(Player player) {
Location chunkLocation = LocationUtils.toChunkXZ(player.getLocation());
Chunk chunk = world.getChunk((int) chunkLocation.getX(), (int) chunkLocation.getZ());
// Chunk chunk = world.getChunk((int) chunkLocation.getX(), (int) chunkLocation.getZ());
Chunk chunk = world.getChunk(-50, -16);
var chunkDataPacket = new ChunkDataPacket();
chunkDataPacket.setChunk(chunk);
@@ -112,8 +113,11 @@ public class ScenarioLogin implements PacketScenario {
for (int x = minX; x <= maxX; x++) {
if ((z == minZ || z == maxZ) || (x == minX || x == maxX)) {
chunkDataPacket = new ChunkDataPacket();
chunkDataPacket.setChunk(world.getChunk(x, z));
player.getCtx().write(chunkDataPacket);
chunk = world.getChunk(x, z);
if (chunk != null) {
chunkDataPacket.setChunk(chunk);
player.getCtx().write(chunkDataPacket);
}
}
}
}

View File

@@ -1,33 +0,0 @@
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

@@ -0,0 +1,14 @@
package mc.server.world;
import lombok.Data;
import mc.protocol.model.Location;
import mc.protocol.world.Block;
@Data
public class SomeBlock implements Block {
private int id;
private int meta;
private int light;
private Location location;
}

View File

@@ -8,19 +8,19 @@ import java.util.HashMap;
import java.util.Map;
@Data
public class VoidChunk implements Chunk {
public class SomeChunk implements Chunk {
private final int x;
private final int z;
private final Map<Integer, VoidChunkSection> sections = new HashMap<>();
private final Map<Integer, ChunkSection> sections = new HashMap<>();
@Override
public ChunkSection getSection(int height) {
return sections.computeIfAbsent(height, VoidChunkSection::new);
return sections.computeIfAbsent(height, SomeChunkSection::new);
}
@Override
public byte getBiome(int x, int z) {
return 127; // 127 | 7F | minecraft:void | The Void
return 0; //ocean
}
}

View File

@@ -0,0 +1,56 @@
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 SomeChunkSection implements ChunkSection {
private final int y;
private final Map<Location, Block> blocks = new HashMap<>();
@Override
public Block getBlock(int x, int y, int z) {
if (this.y == 15) {
if (y == 0) {
return blocks.computeIfAbsent(new Location().set(x, y, z), location -> {
SomeBlock block = new SomeBlock();
block.setLocation(location);
block.setId(3);
return block;
});
} else {
return blocks.computeIfAbsent(new Location().set(x, y, z), location -> {
SomeBlock block = new SomeBlock();
block.setLocation(location);
block.setId(0);
return block;
});
}
} else {
return blocks.computeIfAbsent(new Location().set(x, y, z), location -> {
SomeBlock block = new SomeBlock();
block.setLocation(location);
block.setId(1);
return block;
});
}
}
@Override
public int getSkyLight(int x, int y, int z) {
if (this.y == 15 && y != 0) {
return 15;
} else {
return 0;
}
}
}

View File

@@ -3,14 +3,14 @@ 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;
import mc.utils.Table;
public class VoidWorld implements World {
public class SomeWorld implements World {
private static final Location spawn = ProtocolObjectPool.getLocationPool().borrowObject().set(7d, 130d, 7d);
private final Table<Integer, Integer, VoidChunk> chunkTable = new Table<>();
private static final Location spawn = ProtocolObjectPool.getLocationPool().borrowObject().set(-790d, 256d, -263d + 16d);
private final Table<Integer, Integer, Chunk> chunkTable = new Table<>();
@Override
public LevelType getLevelType() {
@@ -19,14 +19,18 @@ public class VoidWorld implements World {
@Override
public Location getSpawn() {
return VoidWorld.spawn;
return SomeWorld.spawn;
}
@Override
public Chunk getChunk(int x, int z) {
VoidChunk chunk = chunkTable.getColumnAndRow(x, z);
if (x != -50 && z != -16) {
return null;
}
Chunk chunk = chunkTable.getColumnAndRow(x, z);
if (chunk == null) {
chunk = new VoidChunk(x, z);
chunk = new SomeChunk(x, z);
chunkTable.put(x, z, chunk);
}

View File

@@ -1,29 +0,0 @@
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

@@ -23,5 +23,5 @@ icon {
}
world {
view-distance: 1
view-distance: 0
}

View File

@@ -8,8 +8,16 @@
</layout>
</appender>
<!--<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>out.log</file>
<encoder>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%35.35logger{34}] &#45;&#45; %msg%n</Pattern>
</encoder>
</appender>-->
<root level="info">
<appender-ref ref="CONSOLE"/>
<!--<appender-ref ref="FILE"/>-->
</root>
<logger name="LAUNCHER" level="debug" additivity="false">