Archived
0

грузим чанки при входе

This commit is contained in:
2021-05-09 18:47:51 +03:00
parent 20791ed881
commit 04316d9cbd
5 changed files with 70 additions and 13 deletions

View File

@@ -9,4 +9,16 @@ public class Location {
private double x; private double x;
private double y; private double y;
private double z; private double z;
public int getIntX() {
return (int) x;
}
public int getIntZ() {
return (int) z;
}
public Location toChunkXZ() {
return new Location(this.getIntX() >> 4, 0d, this.getIntZ() >> 4);
}
} }

View File

@@ -15,7 +15,8 @@ import mc.protocol.packets.server.*;
import mc.protocol.serializer.TextSerializer; import mc.protocol.serializer.TextSerializer;
import mc.protocol.utils.Difficulty; import mc.protocol.utils.Difficulty;
import mc.protocol.utils.GameMode; import mc.protocol.utils.GameMode;
import mc.protocol.utils.LevelType; import mc.protocol.world.Chunk;
import mc.protocol.world.World;
import mc.server.config.Config; import mc.server.config.Config;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@@ -34,6 +35,7 @@ public class PacketHandler {
private final Random random = new Random(System.currentTimeMillis()); private final Random random = new Random(System.currentTimeMillis());
private final Config config; private final Config config;
private final World world;
public void onHandshake(ConnectionContext context, HandshakePacket packet) { public void onHandshake(ConnectionContext context, HandshakePacket packet) {
context.setState(packet.getNextState()); context.setState(packet.getNextState());
@@ -75,6 +77,7 @@ public class PacketHandler {
context.sendNow(response); context.sendNow(response);
} }
@SuppressWarnings("java:S2589")
public void onLoginStart(ConnectionContext context, LoginStartPacket loginStartPacket) { public void onLoginStart(ConnectionContext context, LoginStartPacket loginStartPacket) {
var loginSuccessPacket = new LoginSuccessPacket(); var loginSuccessPacket = new LoginSuccessPacket();
loginSuccessPacket.setUuid(UUID.randomUUID()); loginSuccessPacket.setUuid(UUID.randomUUID());
@@ -88,14 +91,12 @@ public class PacketHandler {
joinGamePacket.setGameMode(GameMode.SURVIVAL); joinGamePacket.setGameMode(GameMode.SURVIVAL);
joinGamePacket.setDimension(0/*Overworld*/); joinGamePacket.setDimension(0/*Overworld*/);
joinGamePacket.setDifficulty(Difficulty.PEACEFUL); joinGamePacket.setDifficulty(Difficulty.PEACEFUL);
joinGamePacket.setLevelType(LevelType.FLAT); joinGamePacket.setLevelType(world.getLevelType());
context.send(joinGamePacket); context.send(joinGamePacket);
Location spawnLocation = new Location(7d, 130d, 7d);
var spawnPositionPacket = new SpawnPositionPacket(); var spawnPositionPacket = new SpawnPositionPacket();
spawnPositionPacket.setSpawn(spawnLocation); spawnPositionPacket.setSpawn(world.getSpawn());
context.send(spawnPositionPacket); context.send(spawnPositionPacket);
@@ -111,14 +112,38 @@ public class PacketHandler {
context.flushSending(); context.flushSending();
var chunkDataPacket = new ChunkDataPacket(); Location chunkLocation = world.getSpawn().toChunkXZ();
chunkDataPacket.setX(0); Chunk chunk = world.getChunk(chunkLocation.getIntX(), chunkLocation.getIntZ());
chunkDataPacket.setZ(0);
context.sendNow(chunkDataPacket); var chunkDataPacket = new ChunkDataPacket();
chunkDataPacket.setX(chunk.getX());
chunkDataPacket.setZ(chunk.getZ());
context.send(chunkDataPacket);
for (int i = 1; i <= config.world().viewDistance(); i++) {
int minX = chunkLocation.getIntX() - i;
int minZ = chunkLocation.getIntZ() - i;
int maxX = chunkLocation.getIntX() + i;
int maxZ = chunkLocation.getIntZ() + i;
for (int z = minZ; z <= maxZ; z++) {
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);
context.send(chunkDataPacket);
}
}
}
}
context.flushSending();
var playerPositionAndLookPacket = new SPlayerPositionAndLookPacket(); var playerPositionAndLookPacket = new SPlayerPositionAndLookPacket();
playerPositionAndLookPacket.setPosition(spawnLocation); playerPositionAndLookPacket.setPosition(world.getSpawn());
playerPositionAndLookPacket.setLook(new Look(0f, 0f)); playerPositionAndLookPacket.setLook(new Look(0f, 0f));
playerPositionAndLookPacket.setTeleportId(random.nextInt()); playerPositionAndLookPacket.setTeleportId(random.nextInt());

View File

@@ -2,6 +2,7 @@ package mc.server.di;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import mc.protocol.world.World;
import mc.server.PacketHandler; import mc.server.PacketHandler;
import mc.server.config.Config; import mc.server.config.Config;
@@ -9,7 +10,7 @@ import mc.server.config.Config;
public class PacketHandlerModule { public class PacketHandlerModule {
@Provides @Provides
public PacketHandler providePacketHandler(Config config) { public PacketHandler providePacketHandler(Config config, World world) {
return new PacketHandler(config); return new PacketHandler(config, world);
} }
} }

View File

@@ -1,12 +1,14 @@
package mc.server.di; package mc.server.di;
import dagger.Component; import dagger.Component;
import mc.protocol.di.ServerScope;
import mc.server.PacketHandler; import mc.server.PacketHandler;
import mc.server.config.Config; import mc.server.config.Config;
@Component(modules = { @Component(modules = {
ConfigModule.class, PacketHandlerModule.class ConfigModule.class, PacketHandlerModule.class, WorldModule.class
}) })
@ServerScope
public interface ServerComponent { public interface ServerComponent {
Config getConfig(); Config getConfig();

View File

@@ -0,0 +1,17 @@
package mc.server.di;
import dagger.Module;
import dagger.Provides;
import mc.protocol.di.ServerScope;
import mc.protocol.world.World;
import mc.server.world.VoidWorld;
@Module
public class WorldModule {
@Provides
@ServerScope
public World provideWorld() {
return new VoidWorld();
}
}