Skip to content

Commit f75720a

Browse files
committed
Added mcserver.js
As I have never made a javascript library before, I have used the structure that money.js has.
1 parent d8958fe commit f75720a

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
# mcserver-js
22
A JavaScript library for MC-API server queries
3+
4+
## Usage
5+
Coming Soon
6+
7+
## Credits
8+
Basic structure borrowed from [money.js / fx](https://github.com/openexchangerates/money.js), [underscore.js](https://github.com/jashkenas/underscore).

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "mcserver-js",
3+
"version": "1.0.0",
4+
"description": "JavaScript interface for MC-API.net server api",
5+
"keywords" : ["minecraft", "mc-api", "mcapi", "uuid", "mcserver.js", "mcserverjs", "mcserver-js", "server", "ping", "favicon"],
6+
"main": "src/mcserver.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/MC-API/mcserver-js.git"
13+
},
14+
"bugs": {
15+
"url": "https://github.com/MC-API/mcserver-js/issues"
16+
},
17+
"license" : "MIT",
18+
"homepage": "https://github.com/MC-API/mcserver-js"
19+
}

src/mcserver.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*!
2+
* mcserver-js
3+
*
4+
* JavaScript library for interaction with mc-api.net server api
5+
*
6+
* Freely distributable under the MIT license.
7+
* Portions of mcserver-js are inspired by or borrowed from money.js and underscore.js
8+
*/
9+
(function(root) {
10+
var mcserver = {};
11+
mcserver.region = 'us';
12+
mcserver.ping = function(ip, port, cb) {
13+
if(typeof port == 'function') {
14+
cb = port;
15+
16+
if(ip.indexOf(':') >= 0) {
17+
port = parseInt(ip.substr(':')[1]);
18+
ip = ip.substr(':')[0];
19+
} else {
20+
port = 25565;
21+
}
22+
} else {// port is not a function(cb)
23+
if(ip.indexOf(':') >= 0) {
24+
port = parseInt(ip.substr(':')[1]);
25+
ip = ip.substr(':')[0];
26+
}
27+
}
28+
29+
if(typeof cb != 'function') {
30+
console.error('Undefined callback function for mcserver.ping');
31+
}
32+
33+
var request = new XMLHttpRequest();
34+
request.open('GET', 'https://' + this.region + '.mc-api.net/v3/server/info/' + ip + (port == 25565 ? '' : ':' + port), true);
35+
36+
request.onload = function() {
37+
if(this.status >= 200 && this.status < 400) {
38+
var data = JSON.parse(this.response);
39+
cb(data, null);
40+
} else {
41+
cb(null, this.response);
42+
}
43+
};
44+
45+
request.onerror = function(err) {
46+
cb(null, err.target.status);
47+
};
48+
49+
request.send();
50+
};
51+
52+
53+
/* --- Module Definition --- */
54+
55+
// Export the mcserver object for CommonJS. If being loaded as an AMD module, define it as such.
56+
// Otherwise, just add `mcs` to the global object
57+
if (typeof exports !== 'undefined') {
58+
if (typeof module !== 'undefined' && module.exports) {
59+
exports = module.exports = mcserver;
60+
}
61+
exports.mcserver = mcserver;
62+
} else if (typeof define === 'function' && define.amd) {
63+
// Return the library as an AMD module:
64+
define([], function() {
65+
return mcserver;
66+
});
67+
} else {
68+
// Use mcserver.noConflict to restore `mcserver` back to its original value before mcserver-js loaded.
69+
// Returns a reference to the library's `mcserver` object; e.g. `var mcs = mcserver.noConflict();`
70+
mcserver.noConflict = (function(previous) {
71+
return function() {
72+
// Reset the value of the root's `mcserver` variable:
73+
root.mcserver = previous;
74+
// Delete the noConflict function:
75+
mcserver.noConflict = undefined;
76+
// Return reference to the library to re-assign it:
77+
return mcserver;
78+
};
79+
})(root.mcserver);
80+
81+
// Declare `mcserver` on the root (global/window) object:
82+
root['mcserver'] = mcserver;
83+
}
84+
}(this));

0 commit comments

Comments
 (0)