Skip to content

Instantly share code, notes, and snippets.

@zbee
Last active August 29, 2015 13:56
Show Gist options
  • Save zbee/9339531 to your computer and use it in GitHub Desktop.
Save zbee/9339531 to your computer and use it in GitHub Desktop.
The Minecraft-API examples are rather derpy and not really working. I present you working ones.
CURRENT (BROKEN - It cannot possibly return a number of players from the API method that returns only true or false based on whether the server is up. The status method doesn't even return data.players)
var ip = "player.server.com";
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://minecraft-api.com/v1/status/?server=" + ip, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
data = JSON.parse(xhr.responseText);
if (data.status) {
alert("The server is Online with " + data.players.online + "/" + data.players.max + " players!");
} else {
alert("The server is Offline :[");
}
}
}
xhr.send();
If you replace line 6 (starts with "xhr.open") with this:
xhr.open("GET", "http://minecraft-api.com/v1/query/?server=" + ip, true);
It actually works
PHP is even pretty derpy. This is their example.
<?php
$data = json_decode( file_get_contents( 'http://minecraft-api.com/v1/get/?server=play.server.com' ), true );
if ( $data['status'] == true ) {
// This will only be displayed if the server up and kick'in
echo 'The server is Online with ' . $data['players']['online'] . '/' . $data['players']['max'] . ' players!';
} else {
// This will only be displayed if the server is offline.
echo 'The server is Offline! :[ Check back later.';
}
?>
It's using get. Which, for some reason exists. It returns slightly more info than status -which should be being used-, less than query and is fairly useless. Additionally, that's for returning players. You may note that the documentation details that the get method cannot return players.
So you must use query:
$data = json_decode( file_get_contents( 'http://minecraft-api.com/v1/query/?server=play.server.com' ), true );
I don't know Java too much but it's using get for returning server status, just superfluous, really. Just use status:
URL url = new URL("http://minecraft-api.com/v1/status/?server=" + server);
Again, I don't know Java, but I'm pretty sure that it -like unto all other languages- can't magically change what get returns to display players from it. Query must be used:
URL url = new URL("http://minecraft-api.com/v1/query/?server=" + server);
And, C# uses the player method, which the developer of MC-API seems to say doesn't even work. So, use this:
var Data = Client.DownloadString("http://minecraft-api.com/v1/query/?server=" + serverip);
This is probably a lot to take in, so I simplified it in the other files in this gist, thos are the working examples, all using the status or query methods of calling the API, since we like things to work.
I also made it so that it actually includes the <script> and <?php?> tags in those two languages, since that's how they'd be included realistically and there's no reason not to, honestly.
Again, I don't know Java or C# so, maybe it was done correctly the way it was (you know, maybe magic is real too), but I doubt it.
//Status of Server and Players on Server
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
private void getPlayers(string serverip) //p_serverip is your Server Address
{
WebClient Client = new WebClient();
var Data = Client.DownloadString("http://minecraft-api.com/v1/query/?server=" + serverip);
//JSON parse!
JObject JsonObject = JObject.Parse(Data);
string players = JsonObject["players"]["online"].ToString();
string players_max = JsonObject["players"]["max"].ToString();
}
//Status of Server
package org.javatest;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
public class Main
{
public static void main(String[] args)
{
HashMap JSON = pingServer("Your-Server.com");
if (JSON == null || JSON.get("status").equals("false") {
System.out.println("The server is offline :(");
} else {
System.out.println("The server is online :]");
}
}
public static HashMap pingServer(String server)
{
try {
URL url = new URL("http://minecraft-api.com/v1/status/?server=" + server);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
String data = reader.readLine();
Gson gson = new Gson();
return gson.fromJson(data, HashMap.class);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
//Players on Server
package org.javatest;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
public class Main
{
public static void main(String[] args)
{
HashMap JSON = pingServer("Your-Server.com");
if (JSON == null || JSON.get("error") != null) {
System.out.println("The server is offline :(");
} else {
LinkedTreeMap players = (LinkedTreeMap) JSON.get("players");
System.out.println("There are " + players.get("online") + " out of " + players.get("max") + " online!");
System.out.println("MoTD is (" + JSON.get("motd") + ")");
}
}
public static HashMap pingServer(String server)
{
try {
URL url = new URL("http://minecraft-api.com/v1/query/?server=" + server);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
String data = reader.readLine();
Gson gson = new Gson();
return gson.fromJson(data, HashMap.class);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
<!--Status of Server-->
<script type="text/javascript">
var ip = "player.server.com";
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://minecraft-api.com/v1/status/?server=" + ip, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
data = JSON.parse(xhr.responseText);
if (data.status) {
alert("The server is Online :]");
} else {
alert("The server is Offline :[");
}
}
}
xhr.send();
</script>
<!--Players on Server-->
<script type="text/javascript">
var ip = "player.server.com";
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://minecraft-api.com/v1/query/?server=" + ip, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
data = JSON.parse(xhr.responseText);
if (data.status) {
alert("The server is Online with " + data.players.online + "/" + data.players.max + " players!");
} else {
alert("The server is Offline :[");
}
}
}
xhr.send();
</script>
<!--Status of Server-->
<?php
$data = json_decode( file_get_contents( 'http://minecraft-api.com/v1/status/?server=play.server.com' ), true );
if ( $data['status'] == true ) {
// This will only be displayed if the server up and kick'in
echo 'The server is online:]';
} else {
// This will only be displayed if the server is offline.
echo 'The server is offline:[';
}
?>
<!--Players on Server-->
<?php
$data = json_decode( file_get_contents( 'http://minecraft-api.com/v1/query/?server=play.server.com' ), true );
if ( $data['status'] == true ) {
// This will only be displayed if the server up and kick'in
echo 'The server is Online with ' . $data['players']['online'] . '/' . $data['players']['max'] . ' players!';
} else {
// This will only be displayed if the server is offline.
echo 'The server is Offline! :[ Check back later.';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment