adds forge support, fixes some critical errors

This commit is contained in:
2023-10-08 13:38:00 -06:00
parent 6944cbb9f7
commit ad4629576c
21 changed files with 3484 additions and 132 deletions

View File

@@ -3,15 +3,26 @@ import { acceptEULA, checkEULA } from "../util/EULA.ts";
import { Loader } from "../types/mcgrizzconf.ts";
import { getConfFile, updateConfFile } from "../util/confFile.ts";
import { IS_BROWSER } from "$fresh/runtime.ts";
import { getServerStartCommands } from "../serverConfigs/start.ts";
type MCServerEvent = "message";
type MCServerEventCallback = (msg: string) => void;
type status = "running" | "stopped";
class ServerState {
private _status: "running" | "stopped" = "stopped";
private _status: status = "stopped";
public get status() {
return this._status;
}
private set status(s: status) {
this._status = s;
const statusEvent = new CustomEvent("serverstatuschange", {
detail: this._status,
});
globalThis.dispatchEvent(statusEvent);
}
private command!: Deno.Command;
private process!: Deno.ChildProcess;
@@ -48,7 +59,7 @@ class ServerState {
constructor() {
this._channelId = crypto.randomUUID();
const conf = getConfFile();
this._serverType = conf.loader;
this._serverVersion = conf.version;
@@ -68,7 +79,7 @@ class ServerState {
1;
}
} else {
console.log(msg)
console.log(msg);
this.sendStdIn(msg);
}
});
@@ -89,102 +100,78 @@ class ServerState {
}
// "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",
});
public async startMCServer(instance = "server") {
this.command = getServerStartCommands(this._serverType, instance);
this.channel?.send("clear");
this.channel?.send('clear');
this.process = this.command.spawn();
const { readable, writable } = new TransformStream();
readable.pipeTo(this.process.stdin);
this.stdin = writable.getWriter();
this._status = "running";
const statusEvent = new CustomEvent('serverstatuschange', {detail: this._status});
globalThis.dispatchEvent(statusEvent);
this.status = "running";
this.startStream();
// this.process.status.then(() => {
// this._status = "stopped";
// });
await this.process.status;
this.status = 'stopped';
}
private async startStream() {
const stream = this.process.stdout.getReader();
const decoder = new TextDecoder();
// const stream = this.process.stdout.getReader();
// const decoder = new TextDecoder();
while (true) {
const { done, value } = await stream.read();
if (value) {
const line = decoder.decode(value);
this.channel?.send(line);
const stdoutMsg = new CustomEvent('stdoutmsg', {
detail: line
})
globalThis.dispatchEvent(stdoutMsg);
}
if (done) break;
}
// while (true) {
// const { done, value } = await stream.read();
// if (value) {
// const line = decoder.decode(value);
// this.channel?.send(line);
// const stdoutMsg = new CustomEvent("stdoutmsg", {
// detail: line,
// });
// globalThis.dispatchEvent(stdoutMsg);
// }
// if (done) break;
// }
// await this.process.stdout.pipeThrough(new TextDecoderStream()).pipeTo(
// new WritableStream({
// write: (chunk) => {
// this.channel?.send(chunk);
// const stdoutMsg = new CustomEvent("stdoutmsg", {
// detail: chunk,
// });
// globalThis.dispatchEvent(stdoutMsg);
// },
// }),
// );
await this.process.stdout.pipeThrough(new TextDecoderStream()).pipeTo(
new WritableStream({
write: (chunk) => {
this.channel?.send(chunk);
const stdoutMsg = new CustomEvent("stdoutmsg", {
detail: chunk,
});
globalThis.dispatchEvent(stdoutMsg);
},
}),
);
}
public gracefullyStopMCServer() {
this._status = "stopped";
this.status = "stopped";
this.sendStdIn("stop");
const statusEvent = new CustomEvent('serverstatuschange', {detail: this._status});
globalThis.dispatchEvent(statusEvent);
}
public forceStopMCServer() {
this._status = "stopped";
this.status = "stopped";
this.process.kill();
const statusEvent = new CustomEvent('serverstatuschange', {detail: this._status});
globalThis.dispatchEvent(statusEvent);
}
public async restartMCServer() {
if (this.status === "running") {
await this.sendStdIn("stop");
const statusEvent = new CustomEvent('serverstatuschange', {detail: 'restarting'});
const statusEvent = new CustomEvent("serverstatuschange", {
detail: "restarting",
});
globalThis.dispatchEvent(statusEvent);
// while (true) {
await this.process.status;
// }
}
const statusEvent = new CustomEvent('serverstatuschange', {detail: this._status});
globalThis.dispatchEvent(statusEvent);
this.startMCServer();
}