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