Phpcraft
Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
Phpcraft\ServerConnection Class Reference
Inheritance diagram for Phpcraft\ServerConnection:
Inheritance graph
[legend]
Collaboration diagram for Phpcraft\ServerConnection:
Collaboration graph
[legend]

Public Member Functions

 __construct ($stream, int $protocol_version)
 
 sendHandshake (string $hostname, int $port, int $next_state, array $join_specs=[])
 
 login (Account $account)
 
 startPacket ($packet)
 
 getName ()
 
 sendMessage ($message)
 
 sendAdminBroadcast ($message, string $permission="everything")
 
 hasPermission (string $permission)
 
 hasPosition ()
 
 getPosition ()
 
 hasServer ()
 
 getServer ()
 
- Public Member Functions inherited from Phpcraft\Connection
 __construct (int $protocol_version=-1, $stream=null)
 
 isOpen ()
 
 setReadBuffer (string $buffer)
 
 hasRemainingData ()
 
 getRemainingData ()
 
 writeChat (ChatComponent $value)
 
 writeString (string $value)
 
 writeRaw (string $value)
 
 writeFloat (float $value)
 
 writePosition (Point3D $pos)
 
 writeGMP ($value, int $bytes, int $bits, bool $signed, int $gmp_export_options=GMP_MSW_FIRST|GMP_BIG_ENDIAN)
 
 writeLong ($value)
 
 writePrecisePosition (Point3D $pos)
 
 writeDouble (float $value)
 
 writeFixedPointPosition (Point3D $pos)
 
 writeInt ($value)
 
 writeSlot (Slot $slot)
 
 writeBoolean (bool $value)
 
 writeShort ($value)
 
 writeVarInt ($value)
 
 writeByte (int $value)
 
 writeUnsignedShort ($value)
 
 writeUnsignedByte (int $value)
 
 writeAngle (float $value)
 
 writeUUID (UUID $uuid)
 
 send (bool $raw=false)
 
 close ()
 
 readRawPacket (float $timeout=3.000, int $bytes=0)
 
 getRemoteAddress ()
 
 readPacket (float $timeout=3.000)
 
 readVarInt ()
 
 readUnsignedByte ()
 
 readAngle ()
 
 readByte ()
 
 readChat ()
 
 readString (int $maxLength=32767, int $minLength=-1)
 
 readPosition ()
 
 readGMP (int $bytes, int $bits, bool $signed, int $gmp_import_options=GMP_MSW_FIRST|GMP_BIG_ENDIAN)
 
 readPrecisePosition ()
 
 readDouble ()
 
 readFixedPointPosition ()
 
 readInt ()
 
 readUUID ()
 
 readSlot (bool $additional_processing=true)
 
 readBoolean ()
 
 readShort ()
 
 readNBT (int $type=0)
 
 readRaw (int $bytes)
 
 readLong ()
 
 readFloat ()
 
 readUnsignedShort ()
 
 ignoreBytes (int $bytes)
 

Static Public Member Functions

static connect (string $hostname, int $port, ?int $protocol_version=null, ?int $handshake_next_state=null, float $timeout=3.000)
 
static toAddress (string $address, ?int $protocol_version=null, ?int $handshake_next_state=null, float $timeout=3.000)
 
static negotiateProtocolVersion (string $hostname, int $port, float $timeout=3.000)
 
static resolveAddress (string $address)
 
static getStatus (string $hostname, int $port=25565, float $timeout=3.000, int $method=ServerConnection::METHOD_ALL)
 
- Static Public Member Functions inherited from Phpcraft\Connection
static varInt ($value)
 

Public Attributes

const METHOD_ALL = 0
 
const METHOD_MODERN = 1
 
const METHOD_LEGACY = 2
 
 $username
 
 $uuid
 
 $pos
 
- Public Attributes inherited from Phpcraft\Connection
const STATE_HANDSHAKE = 0
 
const STATE_STATUS = 1
 
const STATE_LOGIN = 2
 
const STATE_PLAY = 3
 
const LENIENCY_LENIENT = 1
 
const LENIENCY_VANILLA = 0
 
const LENIENCY_STRICT = -1
 
 $protocol_version
 
 $stream
 
 $compression_threshold = -1
 
 $state
 
 $write_buffer = ""
 
 $lenient = false
 
 $leniency = self::LENIENCY_VANILLA
 
 $read_buffer = ""
 
 $read_buffer_offset = 0
 

Additional Inherited Members

- Static Public Attributes inherited from Phpcraft\Connection
static $zero
 
static $pow2 = []
 

Detailed Description

A client-to-server connection.

Constructor & Destructor Documentation

◆ __construct()

Phpcraft\ServerConnection::__construct (   $stream,
int  $protocol_version 
)
Parameters
resource$streamA stream created by fsockopen.
int$protocol_version

Member Function Documentation

◆ connect()

static Phpcraft\ServerConnection::connect ( string  $hostname,
int  $port,
?int  $protocol_version = null,
?int  $handshake_next_state = null,
float  $timeout = 3.000 
)
static
Parameters
string$hostname
int$port
null | int$protocol_versionIf null, server will be list pinged to get a protocol version.
null | int$handshake_next_stateIf null, no handshake will be sent. Otherway, this may be Connection::STATE_STATUS for list ping or Connection::STATE_LOGIN for login to play.
float$timeoutTimeout in seconds. Note that in case of $protocol_version === null, two connections will be opened using the same timeout.
Returns
ServerConnection
Exceptions
IOException
Since
0.5.19

◆ getName()

Phpcraft\ServerConnection::getName ( )
Returns
string

Implements Phpcraft\Command\CommandSender.

◆ getPosition()

Phpcraft\ServerConnection::getPosition ( )

◆ getServer()

Phpcraft\ServerConnection::getServer ( )

Available in accordance with the CommandSender interface.

Returns
Server|null null

Implements Phpcraft\Command\CommandSender.

◆ getStatus()

static Phpcraft\ServerConnection::getStatus ( string  $hostname,
int  $port = 25565,
float  $timeout = 3.000,
int  $method = ServerConnection::METHOD_ALL 
)
static

Returns the server list ping as multi-dimensional array with the addition of the "ping" value which is in seconds. In an error case, an empty array is returned. Here's an example:

[
  "version" => [
    "name" => "1.12.2",
    "protocol" => 340
  ],
  "players" => [
    "online" => 1,
    "max" => 20,
    "sample" => [
      [
        "name" => "timmyRS",
        "id" => "e0603b59-2edc-45f7-acc7-b0cccd6656e1"
      ]
    ]
  ],
  "description" => [
    "text" => "A Minecraft Server"
  ],
  "favicon" => "data:image/png;base64,<data>",
  "ping" => 0.068003177642822
]

Note that a server might not present all of these values, so always check with isset or array_key_exists first. description should always be a valid chat component.

Parameters
string$hostname
int$port
float$timeoutThe amount of seconds to wait for a response with each method.
int$method
Returns
array
Exceptions
IOException
Since
0.5.19

◆ hasPermission()

Phpcraft\ServerConnection::hasPermission ( string  $permission)
Parameters
string$permission
Returns
bool

Implements Phpcraft\Command\CommandSender.

◆ hasPosition()

Phpcraft\ServerConnection::hasPosition ( )
Returns
bool

Implements Phpcraft\Command\CommandSender.

◆ hasServer()

Phpcraft\ServerConnection::hasServer ( )

Available in accordance with the CommandSender interface.

Returns
bool false

Implements Phpcraft\Command\CommandSender.

◆ login()

Phpcraft\ServerConnection::login ( Account  $account)

Logs in to the server using the given account. This has to be called even when joining an offline mode server.

Parameters
Account$account
Returns
string|null The error message or null on success.
Exceptions
IOException

◆ negotiateProtocolVersion()

static Phpcraft\ServerConnection::negotiateProtocolVersion ( string  $hostname,
int  $port,
float  $timeout = 3.000 
)
static
Parameters
string$hostname
int$port
float$timeoutTimeout in seconds.
Returns
int
Exceptions
IOException

◆ resolveAddress()

static Phpcraft\ServerConnection::resolveAddress ( string  $address)
static

; Resolves the given address.

Parameters
string$addressThe server address, e.g. localhost
Returns
array{hostname:string,port:int} An array containing string "hostname" and int "port".
Since
0.5.19

◆ sendAdminBroadcast()

Phpcraft\ServerConnection::sendAdminBroadcast (   $message,
string  $permission = "everything" 
)

Prints a message to the console. Available in accordance with the CommandSender interface.

Parameters
array | string | null | ChatComponent$message
string$permission
Returns
void

Implements Phpcraft\Command\CommandSender.

◆ sendHandshake()

Phpcraft\ServerConnection::sendHandshake ( string  $hostname,
int  $port,
int  $next_state,
array  $join_specs = [] 
)

Sends a handshake to the server. If $next_state is 2, you should call ServerConnection::login() after this, even when joining an offline mode server.

Parameters
string$hostname
int$port
int$next_stateMay be Connection::STATE_STATUS for list ping or Connection::STATE_LOGIN for login to play.
array<string>$join_specs Additional data to provide, e.g. "FML" is in this array for Forge clients.
Returns
ServerConnection $this
Exceptions
IOException

◆ sendMessage()

Phpcraft\ServerConnection::sendMessage (   $message)

Prints a message to the console. Available in accordance with the CommandSender interface.

Parameters
array | string | null | ChatComponent$message
Returns
void

Implements Phpcraft\Command\CommandSender.

◆ startPacket()

Phpcraft\ServerConnection::startPacket (   $packet)

Clears the write buffer and starts a new packet.

Parameters
string | integer$packetThe name or ID of the new packet.
Returns
Connection $this

Reimplemented from Phpcraft\Connection.

◆ toAddress()

static Phpcraft\ServerConnection::toAddress ( string  $address,
?int  $protocol_version = null,
?int  $handshake_next_state = null,
float  $timeout = 3.000 
)
static
Parameters
string$address
null | int$protocol_versionIf null, server will be list pinged to get a protocol version.
null | int$handshake_next_stateIf null, no handshake will be sent. Otherway, this may be Connection::STATE_STATUS for list ping or Connection::STATE_LOGIN for login to play.
float$timeoutTimeout in seconds. Note that in case of $protocol_version === null, two connections will be opened using the same timeout.
Returns
ServerConnection
Exceptions
IOException
Since
0.5.19

Member Data Documentation

◆ $pos

Point3D Phpcraft\ServerConnection::$pos

Our position on the server.

◆ $username

string null Phpcraft\ServerConnection::$username

The username assigned to us by the server after login. Null before that.

◆ $uuid

UUID null Phpcraft\ServerConnection::$uuid

The UUID assigned to us by the server after login. Null before that.

◆ METHOD_ALL

const Phpcraft\ServerConnection::METHOD_ALL = 0

Modern list ping. Legacy if that fails.

◆ METHOD_LEGACY

const Phpcraft\ServerConnection::METHOD_LEGACY = 2

◆ METHOD_MODERN

const Phpcraft\ServerConnection::METHOD_MODERN = 1