Skip to content

Commit

Permalink
use mcapi.us since gameapis.net has shut down
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardosnt committed Feb 2, 2019
1 parent a6e54b3 commit d8b271c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.vscode
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# mc-player-counter

Displays the number of online players of your Minecraft Server in your site with 2 lines of HTML.
Displays the number of online players in your Minecraft Server using 2 lines of HTML.

## Options
- refreshRate - Rate that the counter will refresh.
- refreshRate - The rate that the counter will refresh (1m by default. Note that https://mcapi.us/ has a 5 minute cache.)
- format - Format that the counter will be displayed
- `{max}` - Maximum players
- `{online}` - Online players
- ip - Server IP. E.g (`mc.hypixel.net`), with port (`mc.hypixel.net:25565`)
- element - Element that the counter will be rendered in.

In HTML, should be prefixed with `data-playercounter-`. E.g (`data-playercounter-ip`)
In HTML, the attributes 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".
It's also possible to display the server status by adding the attribute `data-playercounter-status`. It will display "online" or "offline".
See [example](examples/index.html#L12)

## Demo
- https://cdn.rawgit.com/leonardosnt/mc-player-counter/1.1.0/examples/index.html
- https://cdn.rawgit.com/leonardosnt/mc-player-counter/master/examples/index.html

## Usage:

Expand All @@ -26,25 +26,25 @@ HTML
<html>
<head>
<!-- ... -->
<script src="https://cdn.jsdelivr.net/gh/leonardosnt/mc-player-counter@1.1.0/dist/mc-player-counter.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/leonardosnt/mc-player-counter/dist/mc-player-counter.min.js"></script>
</head>
<body>
There are <span data-playercounter-ip="my.server.ip">0</span> players online on my server.
</body>
</html>
```

JS (Optional)
JS ("API")
```javascript
new PlayerCounter({
element: element,
ip: 'server ip',
format: '{online}/{max}' // default {online}
refreshRate: 1000 // default 5s (5000)
refreshRate: 60 * 1000 // default 1m
});
```

## License

Copyright (C) 2017 leonardosnt <leonrdsnt@gmail.com>
Copyright (C) 2017 leonardosnt <leonrdsnt@gmail.com>
Licensed under the MIT License. See LICENSE file in the project root for full license information.
37 changes: 20 additions & 17 deletions dist/mc-player-counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ var PlayerCounter = function () {
function PlayerCounter(_ref) {
var ip = _ref.ip,
element = _ref.element,
_ref$format = _ref.format,
format = _ref$format === undefined ? '{online}' : _ref$format,
_ref$refreshRate = _ref.refreshRate,
refreshRate = _ref$refreshRate === undefined ? 5e3 : _ref$refreshRate;
format = _ref.format,
refreshRate = _ref.refreshRate;

_classCallCheck(this, PlayerCounter);

if (ip == undefined) {
format = format || '{online}';
refreshRate = refreshRate || 60 * 1000;

if (!ip) {
throw TypeError('ip cannot be null or undefined');
}

if (element == undefined) {
throw TypeError('element cannot be null or undefiend');
if (!element) {
throw TypeError('element cannot be null or undefined');
}

this.ip = ip;
Expand All @@ -51,26 +52,28 @@ var PlayerCounter = function () {
if (request.readyState !== 4 || request.status !== 200) return;

var FORMAT_REGEX = /{\b(online|max)\b}/ig;
var data = JSON.parse(request.responseText);
var response = JSON.parse(request.responseText);
var displayStatus = _this.element.getAttribute('data-playercounter-status');

// Display server status.
// offline/online
if (displayStatus !== null) {
_this.element.innerText = data.status ? 'online' : 'offline';
_this.element.innerText = response.online ? '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];
if (response.online) {
_this.element.innerHTML = _this.format.replace(FORMAT_REGEX, function (_, group) {
return (
// Change 'online' to 'now' to keep backward compatibility
response.players[group === 'online' ? 'now' : group]
);
});
_this.element.innerHTML = text;
}
};
request.open('GET', 'https://use.gameapis.net/mc/query/players/' + this.ip);
request.open('GET', 'http://mcapi.us/server/status?ip=' + this.ip);
request.send();
}
}]);
Expand All @@ -86,9 +89,9 @@ var onDomLoad = function onDomLoad() {

new PlayerCounter({
element: element,
ip: element.getAttribute('data-playercounter-ip') || undefined,
format: element.getAttribute('data-playercounter-format') || undefined,
refreshRate: element.getAttribute('data-playercounter-refreshRate') || undefined
ip: element.getAttribute('data-playercounter-ip'),
format: element.getAttribute('data-playercounter-format'),
refreshRate: element.getAttribute('data-playercounter-refreshRate')
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion dist/mc-player-counter.min.js

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": "mc-player-counter",
"version": "1.1.0",
"version": "1.1.3",
"author": "leonardosnt",
"license": "MIT",
"repository": {
Expand Down
31 changes: 18 additions & 13 deletions src/mc-player-counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
*/

class PlayerCounter {
constructor({ ip, element, format = '{online}' , refreshRate = 5e3 }) {
if (ip == undefined) {
constructor({ ip, element, format, refreshRate }) {
format = format || '{online}';
refreshRate = refreshRate || 60 * 1000;

if (!ip) {
throw TypeError('ip cannot be null or undefined');
}

if (element == undefined) {
throw TypeError('element cannot be null or undefiend');
if (!element) {
throw TypeError('element cannot be null or undefined');
}

this.ip = ip;
Expand All @@ -33,24 +36,26 @@ class PlayerCounter {
if (request.readyState !== 4 || request.status !== 200) return;

const FORMAT_REGEX = /{\b(online|max)\b}/ig;
const data = JSON.parse(request.responseText);
const response = JSON.parse(request.responseText);
const displayStatus = this.element.getAttribute('data-playercounter-status');

// Display server status.
// offline/online
if (displayStatus !== null) {
this.element.innerText = data.status ? 'online' : 'offline';
this.element.innerText = response.online ? '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;
if (response.online) {
this.element.innerHTML = this.format.replace(FORMAT_REGEX, (_, group) => (
// Change 'online' to 'now' to keep backward compatibility
response.players[group === 'online' ? 'now' : group])
);
}
};
request.open('GET', `https://use.gameapis.net/mc/query/players/${this.ip}`);
request.open('GET', `http://mcapi.us/server/status?ip=${this.ip}`);
request.send();
}
}
Expand All @@ -63,9 +68,9 @@ const onDomLoad = function() {

new PlayerCounter({
element: element,
ip: element.getAttribute('data-playercounter-ip') || undefined,
format: element.getAttribute('data-playercounter-format') || undefined,
refreshRate: element.getAttribute('data-playercounter-refreshRate') || undefined
ip: element.getAttribute('data-playercounter-ip'),
format: element.getAttribute('data-playercounter-format'),
refreshRate: element.getAttribute('data-playercounter-refreshRate')
});
}
};
Expand Down

0 comments on commit d8b271c

Please sign in to comment.