Skip to content

Repository files navigation

An open-source way to run Minecraft easily on linux using Express & Websockets.

CodeFactorMaintainabilitynpm bundle sizenpm


Cloud Core is a highly configurable Minecraft Java wrapper, it uses child_process, express, websocket and many other modules to expose your Minecraft Java server to the web.

  • HTTP Api for sending commands, requesting the last 100 lines of console and getting the server usage statistics (cpu & ram).
  • Websocket support to receive console lines, send commands and start / stop the server.
  • A fully automated backup system that backs up your whole server on a bi-weekly / bi-monthly basis.
  • Highly customizable with events, and functions to interact with the server from plain javascript.
  • Highly secured, only letting users with a certain password connect.
  • Logs every command sent into the Minecraft latest log with an optional user field to log a command by a user.

Install

Firstly make sure you have the following dependencies:

  • Java 8 or above.
  • NodeJS 12 or above.
  • Access to SSH on your host.

Install via NPM...

npm install cloud-core-server

To start a server, you need to create a javascript file in the root of your server.

Include Cloud Core into this script by entering this line:

const CloudCore = require("cloud-core-server");

Then make a new instance of CloudCore:

const server = new CloudCore(options);

You need to configure most of the options, download the example-start.js file to see an example of what you might find, all options and default values are listed here:

core:

OptionAboutDefault Value
pathPath to minecraft server where the .jar file is located."./"
prefixPath to add at the start of the java command, this is useful for switching java version for specified servers. E.g. "/usr/lib/jvm/jdk-16.0.1/bin/" to use java 16. MAKE SURE STRING ENDS WITH /""
jarMain java file for the server."server.jar"
authorizationMain authorization code for any request."hackme"
argsArray of java flags, including ram and other options. (Minecraft Jar Flags)['-Xmx2G', '-Xms1G']
portMinecraft server port.25565

core.backups:

OptionAboutDefault Value
enabledDo you want automatic backups?false
timeHow much time do you want per backup? Can be only "weekly" OR "monthly""weekly"
directoryWhere do you want to save any backups?"./backup/"

remote:

OptionAboutDefault Value
bindIP Address to bind the webserver to."0.0.0.0"
portPort to bind the webserver to.35565

Running

Start your server by installing pm2 and running this command:

pm2 start (your start javascript file) -n (server name)

Your server will now be started. You can use pm2 log (server name) to view the server log or if there are any errors.

Be careful when you are stopping your server. If you stop your server via pm2 stop (server name) your server can be shut down incorrectly. Please first stop your server through the web, then stop it via pm2.

You can also start multiple servers using pm2.

Accessing

HTTP GET request to any URL to get the last 100 lines of the console.

HTTP POST request to any URL to send a command.

Open websocket to any URL.

Start & stop.

Make sure you follow authentication rules.

Also have a look at proxying your webserver to ensure security.

Getting CPU & RAM Usage.

Get console

$context = [
"http" => [
"method" => "GET",
"header" => "Authorization: <auth code here>"
]
];
$console = file_get_contents("http://localhost:35565/", false, stream_context_create($context))

Post command

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost:35565/");
curl_setopt($ch, CURLOPT_POST, 1);
$authHeaders = array(
"Authorization: <auth code here>",
"Content-type: application/json"
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $authHeaders);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array("command" => "command here", "user" => "optional user here")));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);

Websocket

webSocket=newWebSocket('ws://localhost:35565','auth code here');webSocket.onmessage=(message)=>{letline=message.data;console.log(line)};webSocket.send(JSON.stringify({"action": "command","command": "command here","user": "optional user here"}))

Important Note: You must have a ping/pong event every ~10 seconds to keep the websocket connection alive. To do this put ping for the action field. The server will return a pong in raw text. I advise you to have an if statement to make sure you filter it out.

Starting & Stopping

In the websocket, specify the action field as either: start, stop, restart or kill.

In HTTP, specify the command field as either start, stop or restart.

Note: You can still pass the user field.

Authentication

All requests include a Authorization header with the auth code you set in the start.js file.

Pass the authentication code in the protocall for websockets. See Above

Proxying the Server

It is highly recommended that you proxy your webserver as this does not support https at the moment. Here are some articles on how you can do that.

Setting up NGINX as your proxy server with NodeJS apps

How to Setup Apache As Frontend Proxy for Node.js

Usage HTTP API

Make a GET request to /usage to return a json array of the server's current CPU load and ram.

$context = [
"http" => [
"method" => "GET",
"header" => "Authorization: <auth code here>"
]
];
$usage = file_get_contents("http://localhost:35565/usage", false, stream_context_create($context))
$usage = json_decode($usage, true);

API

Events

Methods

Events

Register an event by doing:

server.on("event name",(params)=>{// do stuff})
Event NameDescriptionParameters
readyReady to start server.
commandWhen a command is executed from either the web or websocket.command
consoleWhen the server returns a console line. NOTE: If you emit this event, it will return whatever text you pass to the websocket.line
startWhen the server is started.
startedWhen the server has finshed starting.
stoppingWhen server tries to stop.
stopWhen server fully stops.
backupWhen server is backed up, includes filepath to zip file.path

Methods

Method NameDescriptionParameters
server.start()Starts server.
server.stop()Tries to stop server and executes callback when fully stopped.Callback Function
server.send()Sends a command to the server.Command to send
server.backup()Backs up the server reguardless of having backups enabled.
server.wsServerAccess websocket server Websocket Module.
server.httpServerReturns running express server
server.minecraftServerReturns child process spawn running the server. (Advanced Users Only)

Help

To get help feel free to message me on discord Chezzer#6969.

About

☁ An open-source way to run Minecraft easily on linux using JavaScript.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Packages

Contributors

Languages