From 2789d10176fe533d6d80f3805be3eb3a9f732b74 Mon Sep 17 00:00:00 2001 From: Leonardo Santos Date: Sun, 21 May 2017 14:18:18 -0300 Subject: [PATCH] Add 'status' --- README.md | 3 +++ dist/mc-player-counter.js | 22 +++++++++++++++++----- dist/mc-player-counter.min.js | 2 +- examples/index.html | 1 + package.json | 2 +- src/mc-player-counter.js | 16 ++++++++++++++-- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3cdfe06..c5c5115 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,9 @@ Displays the number of online players of your Minecraft Server in your site with In HTML, should be prefixed with `data-playercounter-`. E.g (`data-playercounter-ip`) +You can also display the server status by adding the attribute `data-playercounter-status`. It will display "online" or "offline". +See [example](examples/index.html#L11) + ## Demo - https://cdn.rawgit.com/leonardosnt/mc-player-counter/master/examples/index.html diff --git a/dist/mc-player-counter.js b/dist/mc-player-counter.js index ff07e95..b2f0162 100644 --- a/dist/mc-player-counter.js +++ b/dist/mc-player-counter.js @@ -52,11 +52,23 @@ var PlayerCounter = function () { var FORMAT_REGEX = /{\b(online|max)\b}/ig; var data = JSON.parse(request.responseText); - var text = _this.format.replace(FORMAT_REGEX, function (match, group) { - return data.players[group]; - }); - - _this.element.innerHTML = text; + var displayStatus = _this.element.getAttribute('data-playercounter-status'); + + // Display server status. + // offline/online + if (displayStatus !== null) { + _this.element.innerText = data.status ? 'online' : 'offline'; + return; + } + + // Display online players + // Make sure server is online + if (data.status) { + var text = _this.format.replace(FORMAT_REGEX, function (match, group) { + return data.players[group]; + }); + _this.element.innerHTML = text; + } }; request.open('GET', 'https://mcapi.ca/query/' + this.ip + '/players'); request.send(); diff --git a/dist/mc-player-counter.min.js b/dist/mc-player-counter.min.js index 7fba631..4292ef2 100644 --- a/dist/mc-player-counter.min.js +++ b/dist/mc-player-counter.min.js @@ -4,4 +4,4 @@ * Copyright (C) 2017 leonardosnt * Licensed under the MIT License. See LICENSE file in the project root for full license information. */ -!function(){"use strict";function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var t=function(){function e(e,t){for(var r=0;r

Players on hypixel.net: 0

Players on play.cubecraft.net: 0

+

Mineplex is currently ...

\ No newline at end of file diff --git a/package.json b/package.json index c77f09a..6cf78e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mc-player-counter", - "version": "1.0.0", + "version": "1.1.0", "author": "leonardosnt", "license": "MIT", "repository": { diff --git a/src/mc-player-counter.js b/src/mc-player-counter.js index b914de8..e6c83d9 100644 --- a/src/mc-player-counter.js +++ b/src/mc-player-counter.js @@ -34,9 +34,21 @@ class PlayerCounter { const FORMAT_REGEX = /{\b(online|max)\b}/ig; const data = JSON.parse(request.responseText); - const text = this.format.replace(FORMAT_REGEX, (match, group) => data.players[group]); + const displayStatus = this.element.getAttribute('data-playercounter-status'); - this.element.innerHTML = text; + // Display server status. + // offline/online + if (displayStatus !== null) { + this.element.innerText = data.status ? 'online' : 'offline'; + return; + } + + // Display online players + // Make sure server is online + if (data.status) { + const text = this.format.replace(FORMAT_REGEX, (match, group) => data.players[group]); + this.element.innerHTML = text; + } }; request.open('GET', `https://mcapi.ca/query/${this.ip}/players`); request.send();