import { Tail } from "npm:tail"; import { Sockpuppet } from "puppet/client"; import { acceptEULA, checkEULA } from "../util/EULA.ts"; class ServerState { private status: "running" | "stopped" = "stopped"; private command!: Deno.Command; private process!: Deno.ChildProcess; private tail: Tail; private _eulaAccepted: boolean; private sockpuppet!: Sockpuppet; private _channelId = "blanaba"; public get channelId () { return this._channelId; } public get eulaAccepted() { return this._eulaAccepted; } constructor() { this._eulaAccepted = checkEULA(); this.sockpuppet = new Sockpuppet( "ws://sockpuppet.cyborggrizzly.com", () => { this.sockpuppet.joinChannel(this.channelId, (msg) => {this.sendStdIn(msg)}); }, ); } public get stdin() { return this.process.stdin; } public get channel() { return this.sockpuppet.getChannel(this.channelId) } public sendStdIn(message: string) { const msg = new TextEncoder().encode(message); this.process.stdin.getWriter().write(msg); } // "instance" should be moved to a private member once multi-instance support is implemented public startMCServer(instance = "server") { this.command = new Deno.Command("java", { args: [ "-Xmx2G", "-jar", "./server.jar", "nogui", ], cwd: "./" + instance, stdin: "piped", stdout: "piped", }); this.process = this.command.spawn(); this.startStream(); } private async startStream() { const stream = this.process.stdout.getReader(); const decoder = new TextDecoder(); while (true) { const {done, value} = await stream.read(); if (value) { this.channel?.send(decoder.decode(value)); } if (done) break; } } public acceptEULA() { this._eulaAccepted = true; acceptEULA(); } } export const SERVER_STATE = new ServerState();