fix a whole bunch of codefactor issues
This commit is contained in:
parent
fa27b476e7
commit
16c7b5ddcc
9 changed files with 61 additions and 60 deletions
|
@ -6,7 +6,7 @@ enum LogType {
|
||||||
INFO,
|
INFO,
|
||||||
WARN,
|
WARN,
|
||||||
ERROR
|
ERROR
|
||||||
};
|
}
|
||||||
|
|
||||||
const LogTags = {
|
const LogTags = {
|
||||||
INFO: chalk.bgGreen(chalk.black(" INFO ")),
|
INFO: chalk.bgGreen(chalk.black(" INFO ")),
|
||||||
|
|
2
index.ts
2
index.ts
|
@ -3,4 +3,4 @@ import { readFileSync } from "fs";
|
||||||
import { MinecraftServer } from "./server/MinecraftServer";
|
import { MinecraftServer } from "./server/MinecraftServer";
|
||||||
const config:Config = JSON.parse(readFileSync("./config.json").toString()) as Config;
|
const config:Config = JSON.parse(readFileSync("./config.json").toString()) as Config;
|
||||||
|
|
||||||
const mcServer = new MinecraftServer(config);
|
new MinecraftServer(config);
|
|
@ -1,5 +1,4 @@
|
||||||
import { FunkyArray } from "../funkyArray";
|
import { FunkyArray } from "../funkyArray";
|
||||||
import { Block } from "./blocks/Block";
|
|
||||||
import { Player } from "./entities/Player";
|
import { Player } from "./entities/Player";
|
||||||
import { World } from "./World";
|
import { World } from "./World";
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ export class MPClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
handlePacketPlayer(packet:PacketPlayer) {
|
handlePacketPlayer(packet:PacketPlayer) {
|
||||||
// TODO
|
this.entity.onGround = packet.onGround;
|
||||||
}
|
}
|
||||||
|
|
||||||
handlePacketPlayerPosition(packet:PacketPlayerPosition) {
|
handlePacketPlayerPosition(packet:PacketPlayerPosition) {
|
||||||
|
|
|
@ -11,12 +11,8 @@ import { PacketKeepAlive } from "./packets/KeepAlive";
|
||||||
import { PacketLoginRequest } from "./packets/LoginRequest";
|
import { PacketLoginRequest } from "./packets/LoginRequest";
|
||||||
import { PacketDisconnectKick } from "./packets/DisconnectKick";
|
import { PacketDisconnectKick } from "./packets/DisconnectKick";
|
||||||
import { Player } from "./entities/Player";
|
import { Player } from "./entities/Player";
|
||||||
import { PacketTimeUpdate } from "./packets/TimeUpdate";
|
|
||||||
import { PacketSpawnPosition } from "./packets/SpawnPosition";
|
import { PacketSpawnPosition } from "./packets/SpawnPosition";
|
||||||
import { Chunk } from "./Chunk";
|
|
||||||
import { PacketMapChunk } from "./packets/MapChunk";
|
|
||||||
import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook";
|
import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook";
|
||||||
import { PacketPreChunk } from "./packets/PreChunk";
|
|
||||||
import { PacketChat } from "./packets/Chat";
|
import { PacketChat } from "./packets/Chat";
|
||||||
|
|
||||||
export class MinecraftServer {
|
export class MinecraftServer {
|
||||||
|
@ -25,10 +21,9 @@ export class MinecraftServer {
|
||||||
private static readonly TICK_RATE_MS = 1000 / MinecraftServer.TICK_RATE;
|
private static readonly TICK_RATE_MS = 1000 / MinecraftServer.TICK_RATE;
|
||||||
private readonly keepalivePacket = new PacketKeepAlive().writeData();
|
private readonly keepalivePacket = new PacketKeepAlive().writeData();
|
||||||
|
|
||||||
private totalClients:number = 0;
|
|
||||||
private config:Config;
|
private config:Config;
|
||||||
private server:Server;
|
private server:Server;
|
||||||
private serverClock:NodeJS.Timer;
|
private readonly serverClock:NodeJS.Timer;
|
||||||
private tickCounter:number = 0;
|
private tickCounter:number = 0;
|
||||||
private clients:FunkyArray<string, MPClient>;
|
private clients:FunkyArray<string, MPClient>;
|
||||||
private worlds:FunkyArray<number, World>;
|
private worlds:FunkyArray<number, World>;
|
||||||
|
@ -85,7 +80,7 @@ export class MinecraftServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.worlds.forEach(world => {
|
this.worlds.forEach(world => {
|
||||||
world.tick(this.tickCounter);
|
world.tick();
|
||||||
});
|
});
|
||||||
this.tickCounter++;
|
this.tickCounter++;
|
||||||
}, MinecraftServer.TICK_RATE_MS);
|
}, 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) {
|
onConnection(socket:Socket) {
|
||||||
let mpClient:MPClient;
|
let mpClient:MPClient;
|
||||||
|
const setMPClient = (mpclient:MPClient) => {
|
||||||
|
mpClient = mpclient;
|
||||||
|
}
|
||||||
|
|
||||||
const playerDisconnect = (err:Error) => {
|
const playerDisconnect = (err:Error) => {
|
||||||
mpClient.entity.world.removeEntity(mpClient.entity);
|
mpClient.entity.world.removeEntity(mpClient.entity);
|
||||||
this.clients.remove(mpClient.entity.username);
|
this.clients.remove(mpClient.entity.username);
|
||||||
this.sendToAllClients(new PacketChat(`\u00a7e${mpClient.entity.username} left the game`).writeData());
|
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("close", playerDisconnect.bind(this));
|
||||||
socket.on("error", playerDisconnect.bind(this));
|
socket.on("error", playerDisconnect.bind(this));
|
||||||
|
@ -123,45 +161,10 @@ export class MinecraftServer {
|
||||||
|
|
||||||
const packetId = reader.readUByte();
|
const packetId = reader.readUByte();
|
||||||
switch (packetId) {
|
switch (packetId) {
|
||||||
// Handle timeouts at some point, idk.
|
// TODO: Handle timeouts at some point, idk.
|
||||||
case Packets.KeepAlive:
|
case Packets.KeepAlive: break;
|
||||||
break;
|
case Packets.LoginRequest: this.handleLoginRequest(reader, socket, setMPClient.bind(this)); break;
|
||||||
|
case Packets.Handshake: this.handleHandshake(reader, socket); 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;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
import { Console } from "../console";
|
|
||||||
import { FunkyArray } from "../funkyArray";
|
import { FunkyArray } from "../funkyArray";
|
||||||
import { Chunk } from "./Chunk";
|
import { Chunk } from "./Chunk";
|
||||||
import { IEntity } from "./entities/IEntity";
|
import { IEntity } from "./entities/IEntity";
|
||||||
import { Player } from "./entities/Player";
|
import { Player } from "./entities/Player";
|
||||||
import { FlatGenerator } from "./generators/Flat";
|
//import { FlatGenerator } from "./generators/Flat";
|
||||||
import { HillyGenerator } from "./generators/Hilly";
|
import { HillyGenerator } from "./generators/Hilly";
|
||||||
import { IGenerator } from "./generators/IGenerator";
|
import { IGenerator } from "./generators/IGenerator";
|
||||||
|
|
||||||
|
@ -26,7 +25,7 @@ export class World {
|
||||||
// TODO: getChunkByCoordPair failed in here during removeEntity, figure out why.
|
// TODO: getChunkByCoordPair failed in here during removeEntity, figure out why.
|
||||||
public removeEntity(entity:IEntity) {
|
public removeEntity(entity:IEntity) {
|
||||||
if (entity instanceof Player) {
|
if (entity instanceof Player) {
|
||||||
for (let coordPair of entity.loadedChunks) {
|
for (const coordPair of entity.loadedChunks) {
|
||||||
const chunk = this.getChunkByCoordPair(coordPair);
|
const chunk = this.getChunkByCoordPair(coordPair);
|
||||||
chunk.playersInChunk.remove(entity.entityId);
|
chunk.playersInChunk.remove(entity.entityId);
|
||||||
|
|
||||||
|
@ -68,11 +67,11 @@ export class World {
|
||||||
this.chunks.remove(coordPair);
|
this.chunks.remove(coordPair);
|
||||||
}
|
}
|
||||||
|
|
||||||
public tick(tickCount:number) {
|
public tick() {
|
||||||
this.entites.forEach(entity => {
|
this.entites.forEach(entity => {
|
||||||
if (entity instanceof Player) {
|
if (entity instanceof Player) {
|
||||||
if (entity.justUnloaded.length > 0) {
|
if (entity.justUnloaded.length > 0) {
|
||||||
for (let coordPair of entity.justUnloaded) {
|
for (const coordPair of entity.justUnloaded) {
|
||||||
const chunkToUnload = this.getChunkByCoordPair(coordPair);
|
const chunkToUnload = this.getChunkByCoordPair(coordPair);
|
||||||
chunkToUnload.playersInChunk.remove(entity.entityId);
|
chunkToUnload.playersInChunk.remove(entity.entityId);
|
||||||
if (chunkToUnload.playersInChunk.length === 0) {
|
if (chunkToUnload.playersInChunk.length === 0) {
|
||||||
|
|
|
@ -4,12 +4,14 @@ import { Entity } from "./Entity";
|
||||||
export class EntityLiving extends Entity {
|
export class EntityLiving extends Entity {
|
||||||
public yaw:number;
|
public yaw:number;
|
||||||
public pitch:number;
|
public pitch:number;
|
||||||
|
public onGround:boolean;
|
||||||
|
|
||||||
public constructor(world:World) {
|
public constructor(world:World) {
|
||||||
super(world);
|
super(world);
|
||||||
|
|
||||||
this.yaw = 0;
|
this.yaw = 0;
|
||||||
this.pitch = 0;
|
this.pitch = 0;
|
||||||
|
this.onGround = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
onTick() {
|
onTick() {
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
import { FunkyArray } from "../../funkyArray";
|
|
||||||
import { Chunk } from "../Chunk";
|
import { Chunk } from "../Chunk";
|
||||||
import { MPClient } from "../MPClient";
|
import { MPClient } from "../MPClient";
|
||||||
import { MinecraftServer } from "../MinecraftServer";
|
import { MinecraftServer } from "../MinecraftServer";
|
||||||
import { World } from "../World";
|
import { World } from "../World";
|
||||||
import { PacketMapChunk } from "../packets/MapChunk";
|
import { PacketMapChunk } from "../packets/MapChunk";
|
||||||
import { EntityLiving } from "./EntityLiving";
|
import { EntityLiving } from "./EntityLiving";
|
||||||
import { Entity } from "./Entity";
|
|
||||||
import { Socket } from "net";
|
|
||||||
import { PacketPreChunk } from "../packets/PreChunk";
|
import { PacketPreChunk } from "../packets/PreChunk";
|
||||||
|
|
||||||
export class Player extends EntityLiving {
|
export class Player extends EntityLiving {
|
||||||
|
@ -64,7 +61,7 @@ export class Player extends EntityLiving {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mark any unaccounted chunks for unload
|
// Mark any unaccounted chunks for unload
|
||||||
for (let coordPair of this.loadedChunks) {
|
for (const coordPair of this.loadedChunks) {
|
||||||
if (!currentLoads.includes(coordPair)) {
|
if (!currentLoads.includes(coordPair)) {
|
||||||
this.justUnloaded.push(coordPair);
|
this.justUnloaded.push(coordPair);
|
||||||
const chunkToUnload = this.world.getChunkByCoordPair(coordPair);
|
const chunkToUnload = this.world.getChunkByCoordPair(coordPair);
|
||||||
|
|
|
@ -35,7 +35,8 @@ export class HillyGenerator implements IGenerator {
|
||||||
// This is good enough (and fast enough) for what is needed here.
|
// This is good enough (and fast enough) for what is needed here.
|
||||||
private mulberry32(a:number) {
|
private mulberry32(a:number) {
|
||||||
return function() {
|
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 = Math.imul(t ^ t >>> 15, t | 1);
|
||||||
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
||||||
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
||||||
|
|
Loading…
Reference in a new issue