<commit_before>/*
 * While not entirely a direct port, this file is heavily based on 
 */

package net.justdave.mcstatus;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.InputMismatchException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;

public class MinecraftServer extends Object {
	private static final String TAG = MinecraftServer.class.getSimpleName();
	private static final int THUMBNAIL_SIZE = 64;

	private String serverName = "My Minecraft Server";
	private String serverAddress = "localhost";
	private int queryPort = 25565; // the default minecraft query port
	private Socket socket = null;
	private String serverData;
	private JSONObject serverJSON = new JSONObject();

	public MinecraftServer() { // default to localhost:25565
		setDescription("Loading...");
	}

	public MinecraftServer(String name, String address) throws URISyntaxException {
        serverName = name;
		Log.i(TAG, "new MincraftServer(".concat(address).concat(")"));
		URI uri = new URI("my://" + address);
		serverAddress = uri.getHost();
		if (uri.getPort() > 0) {
			queryPort = uri.getPort();
		}
		setDescription("Loading...");
	}

	public void setDescription(String msg) {
		try {
			serverJSON.put("description", msg);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public String serverAddress() {
		StringBuilder result = new StringBuilder();
		result.append(serverAddress);
		if (queryPort != 25565) { // if it's not the default minecraft port
			result.append(":");
			result.append(queryPort);
		}
		return result.toString();
	}

	public String serverName() {
		return serverName;
	}

	public int maxPlayers() {
		JSONObject players = serverJSON.optJSONObject("players");
		if (players == null) {
			return 0;
		}
		return serverJSON.optJSONObject("players").optInt("max");
	}

	public int onlinePlayers() {
		JSONObject players = serverJSON.optJSONObject("players");
		if (players == null) {
			return 0;
		}
		return serverJSON.optJSONObject("players").optInt("online");
	}

	public ArrayList<String> playerList() {
		ArrayList<String> result = new ArrayList<String>();
		result.clear();
		JSONObject players = serverJSON.optJSONObject("players");
		if (players == null) {
			return result;
		}
		JSONArray sample = players.optJSONArray("sample");
		if (sample == null) {
			return result;
		}
		int pos = 0;
		while (pos < sample.length()) {
			JSONObject entry = sample.optJSONObject(pos++);
			String username = entry.optString("name");
			result.add(username);
		}
		Log.i(TAG, "playerList() returning ".concat(result.toString()));
		return result;
	}

	public String serverVersion() {
		JSONObject version = serverJSON.optJSONObject("version");
		if (version == null) {
			return "";
		}
		return version.optString("name");
	}

	public String description() {
		StringBuilder result = new StringBuilder();
		String desc = serverJSON.optString("description");
		result.append("<body style='background-color: transparent; color: white; margin: 0; padding: 0;'><span>");
		int curChar = 0;
		String color = "";
		String decoration = "";
		while (curChar < desc.length()) {
			char theChar = desc.charAt(curChar++);
			if (theChar == '§') {
				char code = desc.charAt(curChar++);
				switch (code) {
				case '0':
					color = "#000000";
					decoration = "";
					break;
				case '1':
					color = "#0000aa";
					decoration = "";
					break;
				case '2':
					color = "#00aa00";
					decoration = "";
					break;<commit_msg>Fixes #9: Check for the description being a JSON object and grab the text field from it if it is.<commit_after>/*
 * While not entirely a direct port, this file is heavily based on 
 */

package net.justdave.mcstatus;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.InputMismatchException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;

public class MinecraftServer extends Object {
	private static final String TAG = MinecraftServer.class.getSimpleName();
	private static final int THUMBNAIL_SIZE = 64;

	private String serverName = "My Minecraft Server";
	private String serverAddress = "localhost";
	private int queryPort = 25565; // the default minecraft query port
	private Socket socket = null;
	private String serverData;
	private JSONObject serverJSON = new JSONObject();

	public MinecraftServer() { // default to localhost:25565
		setDescription("Loading...");
	}

	public MinecraftServer(String name, String address) throws URISyntaxException {
        serverName = name;
		Log.i(TAG, "new MincraftServer(".concat(address).concat(")"));
		URI uri = new URI("my://" + address);
		serverAddress = uri.getHost();
		if (uri.getPort() > 0) {
			queryPort = uri.getPort();
		}
		setDescription("Loading...");
	}

	public void setDescription(String msg) {
		try {
			serverJSON.put("description", msg);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	public String serverAddress() {
		StringBuilder result = new StringBuilder();
		result.append(serverAddress);
		if (queryPort != 25565) { // if it's not the default minecraft port
			result.append(":");
			result.append(queryPort);
		}
		return result.toString();
	}

	public String serverName() {
		return serverName;
	}

	public int maxPlayers() {
		JSONObject players = serverJSON.optJSONObject("players");
		if (players == null) {
			return 0;
		}
		return serverJSON.optJSONObject("players").optInt("max");
	}

	public int onlinePlayers() {
		JSONObject players = serverJSON.optJSONObject("players");
		if (players == null) {
			return 0;
		}
		return serverJSON.optJSONObject("players").optInt("online");
	}

	public ArrayList<String> playerList() {
		ArrayList<String> result = new ArrayList<String>();
		result.clear();
		JSONObject players = serverJSON.optJSONObject("players");
		if (players == null) {
			return result;
		}
		JSONArray sample = players.optJSONArray("sample");
		if (sample == null) {
			return result;
		}
		int pos = 0;
		while (pos < sample.length()) {
			JSONObject entry = sample.optJSONObject(pos++);
			String username = entry.optString("name");
			result.add(username);
		}
		Log.i(TAG, "playerList() returning ".concat(result.toString()));
		return result;
	}

	public String serverVersion() {
		JSONObject version = serverJSON.optJSONObject("version");
		if (version == null) {
			return "";
		}
		return version.optString("name");
	}

	public String description() {
		StringBuilder result = new StringBuilder();
		String desc = serverJSON.optString("description");
		if (serverJSON.optJSONObject("description") != null) {
            desc = serverJSON.optJSONObject("description").optString("text");
        }
		result.append("<body style='background-color: transparent; color: white; margin: 0; padding: 0;'><span>");
		int curChar = 0;
		String color = "";
		String decoration = "";
		while (curChar < desc.length()) {
			char theChar = desc.charAt(curChar++);
			if (theChar == '§') {
				char code = desc.charAt(curChar++);
				switch (code) {
				case '0':
					color = "#000000";
					decoration = "";
					break;
				case '1':
					color = "#0000aa";
					decoration = "";
					break;
				case '2':
					color = "#00aa00";
					decoration = "";
					break;
{'fraction_non_alphanumeric': 0.13366645846346034, 'fraction_numerical': 0.01136789506558401, 'mean_word_length': 4.057485786481364, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5105550', 'n_tokens_mistral': 2754, 'n_tokens_neox': 2383, 'n_words': 641}