-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
46 lines (38 loc) · 1.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { Config, ConfigLoader } from "./src/Config.js";
import Server from "./src/Server.js";
import LoginSuccessPacket from "./src/packet/server/LoginSuccessPacket.js";
import Connection from "./src/Connection.js";
const config: Config = await ConfigLoader.fromFile("config.json");
const server = new Server(config);
server.start();
server.on("listening", (port) => server.logger.info(`Listening on port ${port}`));
server.on("unknownPacket", (packet, conn) => {
server.logger.debug("Unknown packet", `{state=${Connection.State[conn.state]}}`, packet.dataBuffer);
});
server.on("packet", (packet, _conn) => {
server.logger.debug(packet.constructor.name, packet.data);
});
server.on("connection", (conn) => {
server.logger.debug("Connection", {
ip: conn.socket.remoteAddress,
port: conn.socket.remotePort
});
});
server.on("disconnect", (conn) => {
server.logger.debug("Disconnect", {
ip: conn.socket.remoteAddress,
port: conn.socket.remotePort
});
});
server.on("closed", () => {
server.logger.info("Server closed");
process.exit(0);
});
process.on("SIGINT", () => {
process.stdout.write("\x1b[2D"); // Move cursor 2 characters left (clears ^C)
if (server.isRunning) server.stop().then();
else process.exit(0);
});
server.on("packet.LoginPacket", (packet, conn) => {
new LoginSuccessPacket(packet.data.uuid ?? Buffer.from("OfflinePlayer:" + packet.data.username, "utf-8").toString("hex").slice(0, 32), packet.data.username).send(conn).then();
});