UNPKG

observermc

Version:

A Node.js wrapper and API for multiple Minecraft Servers

262 lines (261 loc) 10.5 kB
"use strict"; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MinecraftServer = exports.Status = void 0; var MinecraftServerWhitelist_1 = require("./MinecraftServerWhitelist"); var MinecraftServerEvents_1 = require("./MinecraftServerEvents"); var core_1 = require("@scriptserver/core"); var util_1 = require("../util"); var MinecraftServerProperties_1 = require("./MinecraftServerProperties"); var Status; (function (Status) { Status["Offline"] = "offline"; Status["Starting"] = "starting"; Status["Online"] = "online"; Status["Stopping"] = "stopping"; })(Status = exports.Status || (exports.Status = {})); var MinecraftServer = /** @class */ (function () { function MinecraftServer(options) { /** Checks if the stop timeout is enabled */ this._stopTimeoutEnabled = true; /** Checks if the timeout was arleady cleared */ this._stopTimeoutCleared = true; /** Locks the start to prevent starting the server more than once */ this._startlock = false; /** Events that the server emits */ this._events = { status: function (code) { }, joined: function (username) { }, leaved: function (username) { }, line: function (line) { }, rconRunning: function () { }, any: function (event, data) { }, }; /** Online player list */ this._online = []; /** Server status */ this._status = Status.Offline; /** Server stop timeout in minutes */ this._timeout = 0; // mins this._config = options; // Save options // Setup timeout this._stopTimeoutEnabled = !!options.autostop; this._timeout = options.autostop ? options.autostop : 0; // Event parser this._eventParser = new MinecraftServerEvents_1.MinecraftServerEventParser(options.type, options.events); // Setup server properties this._properties = new MinecraftServerProperties_1.MinecraftServerProperties(options.path); // Setup whitelist this._whitelist = new MinecraftServerWhitelist_1.MinecraftServerWhitelist(options.path, this._properties.get("online_mode")); // Setup script server var scriptServerOptions = { javaServer: { path: options.path, jar: options.jar, args: options.args, pipeStdout: false, flavorSpecific: { default: { startedRegExp: RegExp(this._eventParser.triggers.online), stoppedRegExp: RegExp(this._eventParser.triggers.offline), }, }, }, }; if (options.rcon) { scriptServerOptions["rconConnection"] = options.rcon; } this._scriptServer = new core_1.ScriptServer(scriptServerOptions); } /** Stars the stop timeout */ MinecraftServer.prototype._startTimeout = function (reason) { var _this = this; reason = reason || ""; if (this._stopTimeoutCleared) { this._stopTimeout = setTimeout(function () { util_1.Logger.log("[" + _this.name + "] Timeout! Stopping the server..."); _this.stop(); }, this._timeout); util_1.Logger.log("[" + this.name + "] Timeout started! (" + reason + ")"); this._stopTimeoutCleared = false; return; } util_1.Logger.error("[" + this.name + "] Timeout already started! (" + reason + ")"); }; /** Clears the stop timeout */ MinecraftServer.prototype._clearTimeout = function (reason) { reason = reason || ""; if (!this._stopTimeoutCleared) { if (this._stopTimeout) clearTimeout(this._stopTimeout); this._stopTimeoutCleared = true; util_1.Logger.log("[" + this.name + "] Timeout stopped! (" + reason + ")"); return; } util_1.Logger.error("[" + this.name + "] Timeout already cleared! (" + reason + ")"); }; /** Event emitted everytime the server status changes */ MinecraftServer.prototype.onStatusChanged = function (callback) { this._events.status = callback; }; /** Event emitted everytime a user joins */ MinecraftServer.prototype.onUserJoined = function (callback) { this._events.joined = callback; }; /** Event emitted everytime a user leaves */ MinecraftServer.prototype.onUserLeaved = function (callback) { this._events.leaved = callback; }; /** Event emitted for every line in the console output */ MinecraftServer.prototype.onConsoleLine = function (callback) { this._events.line = callback; }; /** Event emitted when the rcon is running */ MinecraftServer.prototype.onRconRunning = function (callback) { this._events.rconRunning = callback; }; /** Event emitted on any event */ MinecraftServer.prototype.onEvent = function (callback) { this._events.any = callback; }; /** Starts the server */ MinecraftServer.prototype.start = function () { var _this = this; return new Promise(function (resolve, reject) { if (_this._status !== Status.Offline || _this._startlock) { util_1.Logger.error("[" + _this.name + "] Could not start server!"); resolve(false); return; } _this._startlock = true; _this._scriptServer.javaServer.on("console", _this.onConsole.bind(_this)); _this._scriptServer.start(); resolve(true); }); }; MinecraftServer.prototype.onConsole = function (line) { this._events.line(line); var e = this._eventParser.parse(line); this._events.any(e.event, e.data); if (e.event === "starting") { this._setStatus(Status.Starting); } if (e.event === "online") { // Server online event this._setStatus(Status.Online); this._startlock = false; if (this._stopTimeoutEnabled) { this._startTimeout("Server started"); } } else if (e.event === "rconRunning") { // Rcon Running event util_1.Logger.log("[" + this.name + "] Rcon running"); this._events.rconRunning(); } else if (e.event === "login") { // Player login event this._online = __spreadArrays(this._online, [e.data.user]); this._events.joined(e.data.user); if (this._online.length === 1) { this._clearTimeout("Player joined"); } } else if (e.event === "logout") { // Player logout event this._online.splice(this._online.indexOf(e.data.user), 1); this._events.leaved(e.data.user); if (this._online.length === 0 && this._stopTimeoutEnabled) { this._startTimeout("Last player logout"); } } else if (e.event === "stopping") { // Server stopping event this._setStatus(Status.Stopping); this._online = []; } else if (e.event === "offline") { // Server stopped event this._scriptServer.stop(); // Stops RCON and Process this._setStatus(Status.Offline); } }; /** Send a console command */ MinecraftServer.prototype.console = function (command) { if (this._status !== Status.Online) { util_1.Logger.error("[" + this.name + "] Server not online. Could not send command '/" + command + "'"); return false; } util_1.Logger.log("Sending command '/" + command + "'"); this._scriptServer.javaServer.send(command); return true; }; /** Stops the server */ MinecraftServer.prototype.stop = function () { if (this._status !== Status.Online) { util_1.Logger.error("[" + this.name + "] Server not online. Could not stop"); return false; } this._scriptServer.javaServer.send("stop"); this._clearTimeout("Stop server"); return true; }; Object.defineProperty(MinecraftServer.prototype, "status", { /** Server status */ get: function () { return this._status; }, enumerable: false, configurable: true }); Object.defineProperty(MinecraftServer.prototype, "onlinePlayers", { /** Online player list */ get: function () { return this._online.slice(); }, enumerable: false, configurable: true }); /** Sets the server status */ MinecraftServer.prototype._setStatus = function (status) { this._status = status; this._events.status(status); this._events.any("status", status); }; Object.defineProperty(MinecraftServer.prototype, "name", { get: function () { return this._config.name; }, enumerable: false, configurable: true }); Object.defineProperty(MinecraftServer.prototype, "config", { get: function () { return this._config; }, enumerable: false, configurable: true }); Object.defineProperty(MinecraftServer.prototype, "whitelist", { get: function () { return this._whitelist; }, enumerable: false, configurable: true }); Object.defineProperty(MinecraftServer.prototype, "properties", { get: function () { return this._properties; }, enumerable: false, configurable: true }); return MinecraftServer; }()); exports.MinecraftServer = MinecraftServer;