Minecraft Query Class

Synopsis:

General Usage:

include('mcquery.class.php');
$q = new MCQuery('121.45.193.22');
$q->connect();
print_r($q->basic_status());
version 1.0.0
author Shannon Wynter (http://fremnet.net/contact)
copyright Copyright (c) 2013, Shannon Wynter (Fremnet)
license GPL 2.0 or greater
package Fremnet
subpackage Minecraft

 Methods

Construct

__construct(string $host, integer $port, integer $max_retries, integer $read_timeout) 

Parameters

$host

string

the host to connect to

$port

integer

set to null to take the default of 25565

$max_retries

integer

set to null to take the default of 3 retries

$read_timeout

integer

set to null to take the default read timeout of 2 seconds

Basic Status

basic_status() 

Retrieve basic server status

Example output Array ( [motd] => A Minecraft Server [gametype] => SMP [map] => world [numplayers] => 2 [maxplayers] => 20 [hostname] => 127.0.0.1 [port] => 25565 )

returns array associative array of name => value

Exceptions

\MCNotConnectedException
\MCNoChallengeException

Connect

connect() 

'Connect' the udp socket and initiate handshake

Exceptions

\Exception

Disconnect

disconnect() 

Disconnect the udp socket (is it ever really connected?)

Exceptions

\MCNotConnectedException

Full status

full_status() : array

Retrieve the full server status including player list

Example output Array ( [motd] => A Minecraft Server [gametype] => SMP [game_id] => MINECRAFT [version] => 1.4.7 [plugins] => [map] => world [numplayers] => 2 [maxplayers] => 20 [hostport] => 25565 [hostip] => 127.0.0.1 [players] => Array ( [0] => fredblogs [1] => maryblogs ) )

Exceptions

\MCNotConnectedException
\MCNoChallengeException

Returns

arrayassociative array of server data

Handshake

handshake() 

Perform the MCQuery protocol handshake described on dinnerbone's website

see \http://dinnerbone.com/blog/2011/10/14/minecraft-19-has-rcon-and-query/

Exceptions

\MCNotConnectedException

Array Mutate

array_mutate(array $array) : array

Quickly convert any given array of name, value into an associative array of name => value.

NB: Not really sutable for large arrays.

Parameters

$array

array

even [name, value] array

Exceptions

\LengthException

Returns

arrayassociative array of [name => value]

Read Packet

read_packet() : array

Really when hooked up to smart udp read it's going to read as much as it can, not just one packet.

Read and unpack the basic information from a MCQuery packet

Returns

arrayType of packet, Id of packet, Buffer

Smart UDP Read

smart_udp_read() : string

Face it, when it comes to reading UDP packets, PHP is rather dumb This gives us a slightly smarter udp read that waits for the socket to be unblocked and keeps reading until there's no more waiting data.

Exceptions

\MCNotConnectedException

Returns

string

Write packet

write_packet(integer $type, mixed $payload) 

Send a MCQuery packet to the server

$type would be PACKET_TYPE_STATUS or PACKET_TYPE_CHALLENGE

Parameters

$type

integer

type of packet to send

$payload

mixed

usually a string to send as a payload

 Properties

 

$challenge 
 

$host 
 

$id 
 

$max_retries 
 

$prot 
 

$read_timeout 
 

$retries 
 

$socket 

 Constants

 

Challenge packet

PACKET_TYPE_CHALLENGE 
 

Status packet

PACKET_TYPE_STATUS