Skip to content

Commit

Permalink
feat: add webAllowRestart parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
vincss committed Nov 26, 2023
1 parent 9764466 commit aa509c0
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ via [Unofficial pterodactyl-installer](https://github.com/vilhelmprytz/pterodact
| `webStopOnStart` | Stop the web-server when minecraft starts | false |
| `webServeDynmap` | true or an absolute path to enable it, by default it will serve './plugins/dynmap/web/'. You can specify an absolute path to serve instead or an url to redirect to. [How use with dynamp](./wiki/Use-internal-SSS-WebServer-for-dynmap) | false |
| `webSubPath` | Set the path to the Web-GUI if serving from behind a reverse proxy | "" |
| `webAllowRestart` | Add a button in the web-ui to restart the minecraft server proxy | false |
| `startMinecraft` | Start the Minecraft Server (false to disable) | true |
| `minecraftCommand` | The command used to start the server | "java -jar paper.jar nogui" |
| `preventStop` | Prevent the user to stop the server (trought web-server or cli). | false |
Expand Down Expand Up @@ -151,6 +152,8 @@ Give a ⭐️ if you like the project!
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/vincss)

## 📜 ChangeLog
- 1.8.0 - 1.20.1 :
- add webAllowRestart parameter
- 1.7.1 - 1.20.1 :
- Run mcsleeperstarter as a non-root user within the docker container
- 1.7.0 - 1.20.1 :
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mcsleepingserverstarter",
"version": "1.7.1",
"version": "1.8.0",
"description": "Sleeps until someone connects",
"main": "build/sleepingServerStarter.js",
"bin": "build/sleepingServerStarter.js",
Expand Down
6 changes: 3 additions & 3 deletions sleepingSettings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ serverOnlineMode: true # like in the server.properties, to check the licence of
maxPlayers: 20

webPort: 8080 # 0 to disable web hosting, on a positive number a web page it will serve
webStopOnStart: true
# webStopOnStart: true
# webServeDynmap: true # true or false to enable, by default it will serve ./plugins/dynmap/web/. You can specify an absolute path to serve instead or an url to redirect to.
webAllowRestart: true
# webAllowRestart: true

startMinecraft: true # false to disable
minecraftCommand: "java -jar paper.jar nogui" # or paper.jar or whatever flavor you like
Expand All @@ -21,7 +21,7 @@ minecraftCommand: "java -jar paper.jar nogui" # or paper.jar or whatever flavor

# discordWebhookUrl: "https://discord.com/api/webhooks/REPLACE_ME"

# blackListedAddress: ["149.102.143.151", "132.145.71.44", "193.35.18.165"]
# blackListedAddress: ["149.102.143.151", "132.145.71.44", "45.128.232.206", "193.35.18."]
# whiteListedNames: ["vincss"]

# Use a custom icon 64x64 png converted using https://www.base64-image.de/
Expand Down
21 changes: 17 additions & 4 deletions src/sleepingContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class SleepingContainer implements ISleepingServer {
discord?: SleepingDiscord;

isClosing = false;
restartAsked?: boolean = false;

constructor(callBack: (settings: Settings) => void) {
this.logger = getLogger();
Expand All @@ -33,7 +34,7 @@ export class SleepingContainer implements ISleepingServer {

init = async (isThisTheBeginning = false) => {
if (isThisTheBeginning || this.settings.webStopOnStart) {
if (this.settings.webPort > 0) {
if (this.settings.webPort) {
this.webServer = new SleepingWeb(
this.settings,
this.playerConnectionCallBack,
Expand Down Expand Up @@ -69,7 +70,7 @@ export class SleepingContainer implements ISleepingServer {
`----------- [v${version}] Starting Minecraft : ${this.settings.minecraftCommand} ----------- `
);

if (this.settings.webPort > 0 && !this.settings.webStopOnStart) {
if (this.settings.webPort && !this.settings.webStopOnStart) {
const cmdArgs = this.settings.minecraftCommand.split(" ");
const exec = cmdArgs.splice(0, 1)[0];

Expand All @@ -96,14 +97,15 @@ export class SleepingContainer implements ISleepingServer {
}
};

killMinecraft = () => {
killMinecraft = (restartAsked?:boolean) => {
if (this.settings.preventStop) {
this.logger.info(`[Container] killMinecraft: preventStop is set.`);
return;
}

if (platform() !== "win32") {
this.mcProcess?.kill();
this.restartAsked = restartAsked;
} else {
this.logger.info(
`[Container] Not killing server:${platform()}, signals are not working well on Windows`
Expand Down Expand Up @@ -164,11 +166,22 @@ export class SleepingContainer implements ISleepingServer {
MC_TIMEOUT / 1000
} secs)...`
);

setTimeout(async () => {

if(this.restartAsked) {
this.restartAsked = false;
this.logger.info(
`[Container] Restart asked. Launching MC...`
);
this.startMinecraft(onMcClosed);
return;
}

this.reloadSettings();
this.logger.info("[Container] ...Too late !...");
await this.init();
}, MC_TIMEOUT); // restart server
}, MC_TIMEOUT); // restart sss server
};

this.startMinecraft(onMcClosed);
Expand Down
7 changes: 2 additions & 5 deletions src/sleepingSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export type Settings = {
maxPlayers: number;
loginMessage: string;
serverOnlineMode: boolean;
webPort: number;
webStopOnStart: boolean;
webPort?: number;
webStopOnStart?: boolean;
webServeDynmap?: boolean | string;
webSubPath?: string;
webAllowRestart?: boolean;
Expand All @@ -42,9 +42,6 @@ export const DefaultSettings: Settings = {
loginMessage: "...Waking server up, come back in a minute...",
serverOnlineMode: true,

webPort: 0, // 0 to disable web hosting
webStopOnStart: false,

startMinecraft: true, // false to disable
minecraftCommand: "java -jar paper.jar nogui",
version: false,
Expand Down
2 changes: 1 addition & 1 deletion src/sleepingWeb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class SleepingWeb implements ISleepingServer {
)} Restart server`
);

this.sleepingContainer.killMinecraft();
this.sleepingContainer.killMinecraft(true);
})

this.app.get(`${this.webPath}/status`, async (req, res) => {
Expand Down

0 comments on commit aa509c0

Please sign in to comment.