Skip to content

Commit

Permalink
Merge pull request #219 from vincss/feature/stop_windows
Browse files Browse the repository at this point in the history
Feature/stop windows
  • Loading branch information
vincss authored Dec 14, 2023
2 parents 469078e + e9ec53c commit 92e4977
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Give a ⭐️ if you like the project!
- add useWhitelistFile parameter to switch to whitelist.json instead of whiteListedNames (thanks to spanasiuk)
- add useBlacklistFiles parameter to use banned-ips.json and banned-players.json (thanks to spanasiuk)
- add useNativeFiles parameter to use server.properties for getting serverPort, maxPlayers, serverOnlineMode, useWhitelistFile, and useBlacklistFiles properties (thanks to spanasiuk)
- change stdio streams on Windows when web-gui is used.
- 1.8.0 - 1.20.1 :
- add webAllowRestart parameter
- add minecraftAutostart parameter
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 23 additions & 10 deletions src/sleepingContainer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { ChildProcess, execSync, spawn } from "child_process";
import { type } from "os";
import { SleepingBedrock } from "./sleepingBedrock";
import { SleepingDiscord } from "./sleepingDiscord";
import { getMinecraftDirectory, isPortTaken, isAccessAllowed, ServerStatus } from "./sleepingHelper";
import {
getMinecraftDirectory,
isPortTaken,
isAccessAllowed,
ServerStatus,
} from "./sleepingHelper";
import { getLogger, LoggerType, version } from "./sleepingLogger";
import { SleepingMcJava } from "./sleepingMcJava";
import { ISleepingServer } from "./sleepingServerInterface";
import { getSettings, getAccessSettings, Settings, AccessFileSettings } from "./sleepingSettings";
import { Player, PlayerConnectionCallBackType } from "./sleepingTypes";
import { SleepingWeb } from "./sleepingWeb";

const isWindows = type().includes("Windows");

export class SleepingContainer implements ISleepingServer {
logger: LoggerType;
settings: Settings;
Expand All @@ -27,7 +35,7 @@ export class SleepingContainer implements ISleepingServer {
constructor(callBack: (settings: Settings) => void) {
this.logger = getLogger();
this.settings = getSettings();
this.accessSettings = getAccessSettings(this.settings)
this.accessSettings = getAccessSettings(this.settings);
callBack(this.settings);
}

Expand All @@ -45,9 +53,9 @@ export class SleepingContainer implements ISleepingServer {

if (this.settings.serverPort > 0) {
this.sleepingMcServer = new SleepingMcJava(
this.playerConnectionCallBack,
this.settings,
this.accessSettings
this.playerConnectionCallBack,
this.settings,
this.accessSettings
);
if (isThisTheBeginning && this.settings.minecraftAutostart) {
this.startMinecraft();
Expand All @@ -71,7 +79,7 @@ export class SleepingContainer implements ISleepingServer {

getSettings = () => {
return this.settings;
}
};

launchMinecraftProcess = async (onProcessClosed: () => void) => {
this.logger.info(
Expand All @@ -83,7 +91,8 @@ export class SleepingContainer implements ISleepingServer {
const exec = cmdArgs.splice(0, 1)[0];

this.mcProcess = spawn(exec, cmdArgs, {
stdio: "inherit",
// To receive stop from kill on Windows
stdio: isWindows ? ["overlapped", "inherit", "inherit"] : "inherit",
cwd: getMinecraftDirectory(this.settings),
});

Expand Down Expand Up @@ -111,7 +120,12 @@ export class SleepingContainer implements ISleepingServer {
return;
}

this.mcProcess?.kill();
if (isWindows) {
const result = this.mcProcess?.stdin?.write("stop\n");
this.logger.info(`[Container] killMinecraft`, result);
} else {
this.mcProcess?.kill();
}
this.restartAsked = restartAsked;
};

Expand All @@ -136,7 +150,6 @@ export class SleepingContainer implements ISleepingServer {
playerConnectionCallBack: PlayerConnectionCallBackType = async (
player: Player
) => {

const accessStatus = isAccessAllowed(player, this.settings, this.accessSettings);
if (!accessStatus.allowed) {
this.logger.info(`[Container] ${player}: ${accessStatus.reason}.`);
Expand Down Expand Up @@ -192,7 +205,7 @@ export class SleepingContainer implements ISleepingServer {

reloadSettings = () => {
this.settings = getSettings();
this.accessSettings = getAccessSettings(this.settings)
this.accessSettings = getAccessSettings(this.settings);
};

getStatus = async () => {
Expand Down

0 comments on commit 92e4977

Please sign in to comment.