vincss / mcsleepingserverstarter

Put your minecraft server to rest, while SleepingServerStarter is watching !
https://www.spigotmc.org/threads/put-your-server-to-rest-with-sleepingserverstarter.108828/
256 stars 47 forks source link

npm package and API? #75

Closed jojomatik closed 2 years ago

jojomatik commented 2 years ago

Hi @vincss, @iwa and everyone else interested :)

This issue is more of a question but could eventually lead to a feature request:

For my project jojomatik/blockcluster - a minecraft server manager for the webbrowser - I was wondering how I could wake up servers if they have been stopped (https://github.com/jojomatik/blockcluster/issues/279). I was originally thinking of a basic web page with a button where users are able to start the server.

Now I've stumbled across this project. Using the minecraft protocol to wake up servers is definitely way more elegant than a webpage. Is it possible to use this project programmatically in my project (typescript)? Would you be able to publish this project as a npm package?

Or would you rather suggest using PrismarineJS/node-minecraft-protocol directly in my project? Either way, would you be interested to collaborate on implementing a solution to this in my project, as you've clearly already put a lot of thought into this and propably have a way better understanding of the quirks that may arise? This might help me not wasting time on reinventing the wheel :) My project ultimately aims to make it as easy and graphically pleasing as possible to host a minecraft server.

More details on my intended use-case can be found in https://github.com/jojomatik/blockcluster/issues/279.

Either way, thank you very much for your time. You've already helped a lot with the idea of using the minecraft protocol alone :)

vincss commented 2 years ago

Hi ! Maybe what you are trying to acheive is something similar to the pufferpanel : https://github.com/vincss/mcsleepingserverstarter/blob/master/docs/pufferpanel.json

To use SSS with your web app, you can wake up and get the status of the server trought the web api : Enable the settings : webPort

Then you can : POST on /wakeup to wake it up GET on /status to know if the server is Sleeping/Running

https://github.com/vincss/mcsleepingserverstarter/blob/master/src/sleepingWeb.ts

jojomatik commented 2 years ago

Thank you for your quick response and the ideas.

I think I need to be clearer on what type of integration I'd like to have, I want the user to be able to implement this with as little effort as possible. In my head these are the steps:

  1. Create a minecraft server as usual
  2. Open blockcluster
  3. Flip a switch thats labeled "auto-sleep and wake-up"
  4. Start the server once, and everything is handled by blockcluster and its integration with this project from now on

If I understand this project correctly, to do this, I could indeed download the necessary files automatically from this repository and modify my start button to run this project instead of the server directly. Currently the start button invokes the command java -jar server.jar [...] and I could change that to ./mcsleepingserverstarter-linux.

But what, in my opinion, would be a better solution would be to be able to use it programatically. E.g. like this:

import sleepingserver from "sleepingserverstarter";

function putServerToSleep() {
  mcserver.stop();
  sleepingserver.listen(port, "message", [...]).then((event) => {
    if(event.type === "wakeup") {
      mcserver.start();
    }
  });
}

where the "then"-part is executed as soon as a player connects. putServerToSleep is a function that I'd call if no player is connected for a certain amount of time.

The question is if this library is able (or will be able in the future) to supply such an API (or similar) or if I should rather use PrismarineJS/node-minecraft-protocol directly?

vincss commented 2 years ago

For the stop part you can use : https://github.com/vincss/mcEmptyServerStopper

It was not my plan to provide it as a library for now. It shouldn't be too hard, I will see what I can do in the next few days.

vincss commented 2 years ago

Hi ! I created and pushed the lib : https://www.npmjs.com/package/mcsleepingserverstarter/v/1.1.5-0 (Use the 1.1.5-0, the other is not working.)

Here is a small example of how to use it : https://github.com/vincss/mcsleepingserverstarter_client/blob/master/src/index.ts

Can you give it a try ?

jojomatik commented 2 years ago

Hi, sorry for getting back to you this late, but I'm currently involved in a lot of projects

Thank you for making the npm package, I will try to test it as soon as I find time for this :)

jojomatik commented 2 years ago

I've just had a small look at the package, I think we might be able to get it working, but the package would have to fulfill multiple additional requirements:

  1. As the settings would be received from my webinterface/ the general server config I would need a way to add those to the default settings. I think you could export the default settings or add another function - similar to getSettings() - that however doesn't try load anything from a file, instead taking an partial settings object and merging it with the default.
  2. I can't rely on the commands minecraftCommand and minecraftWorkingDirectory. I need to start the minecraft server from my own backend, to be able to attach to the console, read the resource usage etc, therefore I would need an argument minecraftCallback: () => Promise<boolean> which would be called instead of the minecraft command. In this argument I could pass a callback function that would in turn run the relevant functions in my code.

Also, I'd like to know how customizable the log messages are? I do understand, that they are useful if this tool is only monitoring one server at a time, but if I load it programatically for a lot of servers, the log might become kind of bloated from all of these messages.

I don't know how much time it would be to implement these requests, but I think it might actually be more useful if I access the protocol directly? Otherwise this might increase the size and complexity of this library without improving a lot, to most of the users? What do you think?

vincss commented 2 years ago

Indeed, I think the only thing you need from this piece of software is the https://www.npmjs.com/package/minecraft-protocol I don't see the added value of SleepingServerStarter in your case.

jojomatik commented 2 years ago

Thanks for the work/ consideration you put into this and for the honest feedback :)

If it's okay for you (and I don't forget) I might still credit your project as inspiration for this feature, when I finally get to implementing this feature in my project.

I might get back to you, if any questions arise when it comes to edge cases to get your opinion on how to handle these, as you've already put a lot of thought into this.

Again: Thank you very much for the inspiration and have a nice day :)