From 16c7b5ddcc327f2f1f420020781c37ee493353b5 Mon Sep 17 00:00:00 2001 From: Holly Date: Sun, 9 Apr 2023 04:47:23 +0100 Subject: [PATCH] fix a whole bunch of codefactor issues --- console.ts | 2 +- index.ts | 2 +- server/Chunk.ts | 1 - server/MPClient.ts | 2 +- server/MinecraftServer.ts | 95 +++++++++++++++++---------------- server/World.ts | 9 ++-- server/entities/EntityLiving.ts | 2 + server/entities/Player.ts | 5 +- server/generators/Hilly.ts | 3 +- 9 files changed, 61 insertions(+), 60 deletions(-) diff --git a/console.ts b/console.ts index def4934..9cfb141 100644 --- a/console.ts +++ b/console.ts @@ -6,7 +6,7 @@ enum LogType { INFO, WARN, ERROR -}; +} const LogTags = { INFO: chalk.bgGreen(chalk.black(" INFO ")), diff --git a/index.ts b/index.ts index 95f4938..594bd06 100644 --- a/index.ts +++ b/index.ts @@ -3,4 +3,4 @@ import { readFileSync } from "fs"; import { MinecraftServer } from "./server/MinecraftServer"; const config:Config = JSON.parse(readFileSync("./config.json").toString()) as Config; -const mcServer = new MinecraftServer(config); \ No newline at end of file +new MinecraftServer(config); \ No newline at end of file diff --git a/server/Chunk.ts b/server/Chunk.ts index 97e19d3..69b44f0 100644 --- a/server/Chunk.ts +++ b/server/Chunk.ts @@ -1,5 +1,4 @@ import { FunkyArray } from "../funkyArray"; -import { Block } from "./blocks/Block"; import { Player } from "./entities/Player"; import { World } from "./World"; diff --git a/server/MPClient.ts b/server/MPClient.ts index 0576ce8..1ab85da 100644 --- a/server/MPClient.ts +++ b/server/MPClient.ts @@ -37,7 +37,7 @@ export class MPClient { } handlePacketPlayer(packet:PacketPlayer) { - // TODO + this.entity.onGround = packet.onGround; } handlePacketPlayerPosition(packet:PacketPlayerPosition) { diff --git a/server/MinecraftServer.ts b/server/MinecraftServer.ts index 76f8377..c903b9e 100644 --- a/server/MinecraftServer.ts +++ b/server/MinecraftServer.ts @@ -11,12 +11,8 @@ import { PacketKeepAlive } from "./packets/KeepAlive"; import { PacketLoginRequest } from "./packets/LoginRequest"; import { PacketDisconnectKick } from "./packets/DisconnectKick"; import { Player } from "./entities/Player"; -import { PacketTimeUpdate } from "./packets/TimeUpdate"; import { PacketSpawnPosition } from "./packets/SpawnPosition"; -import { Chunk } from "./Chunk"; -import { PacketMapChunk } from "./packets/MapChunk"; import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook"; -import { PacketPreChunk } from "./packets/PreChunk"; import { PacketChat } from "./packets/Chat"; export class MinecraftServer { @@ -25,10 +21,9 @@ export class MinecraftServer { private static readonly TICK_RATE_MS = 1000 / MinecraftServer.TICK_RATE; private readonly keepalivePacket = new PacketKeepAlive().writeData(); - private totalClients:number = 0; private config:Config; private server:Server; - private serverClock:NodeJS.Timer; + private readonly serverClock:NodeJS.Timer; private tickCounter:number = 0; private clients:FunkyArray; private worlds:FunkyArray; @@ -85,7 +80,7 @@ export class MinecraftServer { } this.worlds.forEach(world => { - world.tick(this.tickCounter); + world.tick(); }); this.tickCounter++; }, MinecraftServer.TICK_RATE_MS); @@ -101,13 +96,56 @@ export class MinecraftServer { }); } + handleLoginRequest(reader:Reader, socket:Socket, setMPClient:(mpclient:MPClient) => void) { + const loginPacket = new PacketLoginRequest().readData(reader); + if (loginPacket.protocolVersion !== MinecraftServer.PROTOCOL_VERSION) { + if (loginPacket.protocolVersion > MinecraftServer.PROTOCOL_VERSION) { + socket.write(new PacketDisconnectKick("Outdated server!").writeData()); + } else { + socket.write(new PacketDisconnectKick("Outdated or modded client!").writeData()); + } + return; + } + + const world = this.worlds.get(0); + if (world instanceof World) { + const clientEntity = new Player(this, world, loginPacket.username); + world.addEntity(clientEntity); + + const client = new MPClient(socket, clientEntity); + setMPClient(client); + clientEntity.mpClient = client; + this.clients.set(loginPacket.username, client); + + this.sendToAllClients(new PacketChat(`\u00a7e${loginPacket.username} joined the game`).writeData()); + + socket.write(new PacketLoginRequest(clientEntity.entityId, "", 0, 0).writeData()); + socket.write(new PacketSpawnPosition(8, 64, 8).writeData()); + + socket.write(new PacketPlayerPositionLook(8, 70, 70.62, 8, 0, 0, false).writeData()); + } else { + socket.write(new PacketDisconnectKick("Failed to find world to put player in.").writeData()); + } + } + + handleHandshake(reader:Reader, socket:Socket) { + const handshakePacket = new PacketHandshake().readData(reader); + socket.write(handshakePacket.writeData()); + } + onConnection(socket:Socket) { let mpClient:MPClient; + const setMPClient = (mpclient:MPClient) => { + mpClient = mpclient; + } const playerDisconnect = (err:Error) => { mpClient.entity.world.removeEntity(mpClient.entity); this.clients.remove(mpClient.entity.username); this.sendToAllClients(new PacketChat(`\u00a7e${mpClient.entity.username} left the game`).writeData()); + if (typeof(err) !== "boolean") { + Console.printError(`Client disconnected with error: ${err.message}`); + } } socket.on("close", playerDisconnect.bind(this)); socket.on("error", playerDisconnect.bind(this)); @@ -123,45 +161,10 @@ export class MinecraftServer { const packetId = reader.readUByte(); switch (packetId) { - // Handle timeouts at some point, idk. - case Packets.KeepAlive: - break; - - case Packets.LoginRequest: - const loginPacket = new PacketLoginRequest().readData(reader); - if (loginPacket.protocolVersion !== MinecraftServer.PROTOCOL_VERSION) { - if (loginPacket.protocolVersion > MinecraftServer.PROTOCOL_VERSION) { - socket.write(new PacketDisconnectKick("Outdated server!").writeData()); - } else { - socket.write(new PacketDisconnectKick("Outdated or modded client!").writeData()); - } - return; - } - - const world = this.worlds.get(0); - if (world instanceof World) { - const clientEntity = new Player(this, world, loginPacket.username); - world.addEntity(clientEntity); - - const client = mpClient = new MPClient(socket, clientEntity); - clientEntity.mpClient = client; - this.clients.set(loginPacket.username, client); - - this.sendToAllClients(new PacketChat(`\u00a7e${loginPacket.username} joined the game`).writeData()); - - socket.write(new PacketLoginRequest(clientEntity.entityId, "", 0, 0).writeData()); - socket.write(new PacketSpawnPosition(8, 64, 8).writeData()); - - socket.write(new PacketPlayerPositionLook(8, 70, 70.62, 8, 0, 0, false).writeData()); - } else { - socket.write(new PacketDisconnectKick("Failed to find world to put player in.").writeData()); - } - break; - - case Packets.Handshake: - const handshakePacket = new PacketHandshake().readData(reader); - socket.write(handshakePacket.writeData()); - break; + // TODO: Handle timeouts at some point, idk. + case Packets.KeepAlive: break; + case Packets.LoginRequest: this.handleLoginRequest(reader, socket, setMPClient.bind(this)); break; + case Packets.Handshake: this.handleHandshake(reader, socket); break; } }); } diff --git a/server/World.ts b/server/World.ts index 63455ad..051f9ea 100644 --- a/server/World.ts +++ b/server/World.ts @@ -1,9 +1,8 @@ -import { Console } from "../console"; import { FunkyArray } from "../funkyArray"; import { Chunk } from "./Chunk"; import { IEntity } from "./entities/IEntity"; import { Player } from "./entities/Player"; -import { FlatGenerator } from "./generators/Flat"; +//import { FlatGenerator } from "./generators/Flat"; import { HillyGenerator } from "./generators/Hilly"; import { IGenerator } from "./generators/IGenerator"; @@ -26,7 +25,7 @@ export class World { // TODO: getChunkByCoordPair failed in here during removeEntity, figure out why. public removeEntity(entity:IEntity) { if (entity instanceof Player) { - for (let coordPair of entity.loadedChunks) { + for (const coordPair of entity.loadedChunks) { const chunk = this.getChunkByCoordPair(coordPair); chunk.playersInChunk.remove(entity.entityId); @@ -68,11 +67,11 @@ export class World { this.chunks.remove(coordPair); } - public tick(tickCount:number) { + public tick() { this.entites.forEach(entity => { if (entity instanceof Player) { if (entity.justUnloaded.length > 0) { - for (let coordPair of entity.justUnloaded) { + for (const coordPair of entity.justUnloaded) { const chunkToUnload = this.getChunkByCoordPair(coordPair); chunkToUnload.playersInChunk.remove(entity.entityId); if (chunkToUnload.playersInChunk.length === 0) { diff --git a/server/entities/EntityLiving.ts b/server/entities/EntityLiving.ts index 0a8152f..c5dfe3c 100644 --- a/server/entities/EntityLiving.ts +++ b/server/entities/EntityLiving.ts @@ -4,12 +4,14 @@ import { Entity } from "./Entity"; export class EntityLiving extends Entity { public yaw:number; public pitch:number; + public onGround:boolean; public constructor(world:World) { super(world); this.yaw = 0; this.pitch = 0; + this.onGround = false; } onTick() { diff --git a/server/entities/Player.ts b/server/entities/Player.ts index 6edc893..42a963a 100644 --- a/server/entities/Player.ts +++ b/server/entities/Player.ts @@ -1,12 +1,9 @@ -import { FunkyArray } from "../../funkyArray"; import { Chunk } from "../Chunk"; import { MPClient } from "../MPClient"; import { MinecraftServer } from "../MinecraftServer"; import { World } from "../World"; import { PacketMapChunk } from "../packets/MapChunk"; import { EntityLiving } from "./EntityLiving"; -import { Entity } from "./Entity"; -import { Socket } from "net"; import { PacketPreChunk } from "../packets/PreChunk"; export class Player extends EntityLiving { @@ -64,7 +61,7 @@ export class Player extends EntityLiving { } // Mark any unaccounted chunks for unload - for (let coordPair of this.loadedChunks) { + for (const coordPair of this.loadedChunks) { if (!currentLoads.includes(coordPair)) { this.justUnloaded.push(coordPair); const chunkToUnload = this.world.getChunkByCoordPair(coordPair); diff --git a/server/generators/Hilly.ts b/server/generators/Hilly.ts index e8177b1..3ed9d53 100644 --- a/server/generators/Hilly.ts +++ b/server/generators/Hilly.ts @@ -35,7 +35,8 @@ export class HillyGenerator implements IGenerator { // This is good enough (and fast enough) for what is needed here. private mulberry32(a:number) { return function() { - var t = a += 0x6D2B79F5; + // TODO: Determine if "a" is needed + let t = a += 0x6D2B79F5; t = Math.imul(t ^ t >>> 15, t | 1); t ^= t + Math.imul(t ^ t >>> 7, t | 61); return ((t ^ t >>> 14) >>> 0) / 4294967296;