fix a whole bunch of codefactor issues

This commit is contained in:
Holly Stubbs 2023-04-09 04:47:23 +01:00
parent fa27b476e7
commit 16c7b5ddcc
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
9 changed files with 61 additions and 60 deletions

View file

@ -6,7 +6,7 @@ enum LogType {
INFO,
WARN,
ERROR
};
}
const LogTags = {
INFO: chalk.bgGreen(chalk.black(" INFO ")),

View file

@ -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);
new MinecraftServer(config);

View file

@ -1,5 +1,4 @@
import { FunkyArray } from "../funkyArray";
import { Block } from "./blocks/Block";
import { Player } from "./entities/Player";
import { World } from "./World";

View file

@ -37,7 +37,7 @@ export class MPClient {
}
handlePacketPlayer(packet:PacketPlayer) {
// TODO
this.entity.onGround = packet.onGround;
}
handlePacketPlayerPosition(packet:PacketPlayerPosition) {

View file

@ -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<string, MPClient>;
private worlds:FunkyArray<number, World>;
@ -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;
}
});
}

View file

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

View file

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

View file

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

View file

@ -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;