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

Public Member Functions

 __construct ($stream, ?Server &$server=null)
 
 handleInitialPacket ()
 
 disconnect ($reason=null)
 
 startPacket ($packet)
 
 getHost ()
 
 sendEncryptionRequest ($private_key)
 
 handleEncryptionResponse ($private_key, callable $callback)
 
 finishLogin (UUID $uuid, Counter $eidCounter, int $compression_threshold=256)
 
 teleport (Point3D $pos, ?float $yaw=null, ?float $pitch=null, int $deadline=3)
 
 rotate (int $yaw, int $pitch)
 
 getEyePosition ()
 
 getUnitVector ()
 
 generateChunkQueue ()
 
 unloadChunks ()
 
 getChunk ()
 
 getWorld ()
 
 getServer ()
 
 sendAdminBroadcast ($message, string $permission="everything")
 
 sendMessage ($message, int $position=ChatPosition::SYSTEM)
 
 hasPermission (string $permission)
 
 setGamemode (int $gamemode, bool $abilities=true)
 
 sendAbilities ()
 
 setAbilitiesFromGamemode (int $gamemode)
 
 getName ()
 
 hasPosition ()
 
 getPosition ()
 
 hasServer ()
 
- 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)
 
- Public Member Functions inherited from Phpcraft\Command\CommandSender
 sendMessage ($message)
 

Public Attributes

 $hostname
 
 $hostport
 
 $join_specs = []
 
 $username
 
 $uuid
 
 $config
 
 $next_heartbeat = 0
 
 $disconnect_after = 0
 
 $downstream
 
 $convert_packets
 
 $eid
 
 $downstream_eid
 
 $world = "world"
 
 $pos
 
 $chunk_x
 
 $chunk_z
 
 $yaw = 0
 
 $pitch = 0
 
 $tpidCounter
 
 $tp_confirm_deadline = 0
 
 $on_ground = false
 
 $render_distance = 8
 
 $chunks = []
 
 $chunk_queue = []
 
 $gamemode = Gamemode::SURVIVAL
 
 $entityMetadata
 
 $invulnerable = false
 
 $flying = false
 
 $can_fly = false
 
 $instant_breaking = false
 
 $fly_speed = 0.05
 
 $walk_speed = 0.1
 
- 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 Member Functions inherited from Phpcraft\Connection
static varInt ($value)
 
- Static Public Attributes inherited from Phpcraft\Connection
static $zero
 
static $pow2 = []
 

Detailed Description

A server-to-client connection.

Constructor & Destructor Documentation

◆ __construct()

Phpcraft\ClientConnection::__construct (   $stream,
?Server $server = null 
)

After this, you should call ClientConnection::handleInitialPacket().

Parameters
resource$stream
Server | null$server

Member Function Documentation

◆ disconnect()

Phpcraft\ClientConnection::disconnect (   $reason = null)

Disconnects the client with an optional reason.

Parameters
array | string | null | ChatComponent$reasonThe reason for the disconnect.
Returns
void

◆ finishLogin()

Phpcraft\ClientConnection::finishLogin ( UUID  $uuid,
Counter  $eidCounter,
int  $compression_threshold = 256 
)

Sets the compression threshold and finishes the login.

Parameters
UUID$uuidThe UUID of the client.
Counter$eidCounterThe server's Counter to assign an entity ID to the client.
int$compression_thresholdThe amount of bytes a packet needs to have before it is compressed. Use -1 to disable compression. Compression will always be disabled on loopback connections.
Returns
ClientConnection $this
Exceptions
IOException

◆ generateChunkQueue()

Phpcraft\ClientConnection::generateChunkQueue ( )
Returns
void
Since
0.5

◆ getChunk()

Phpcraft\ClientConnection::getChunk ( )

Returns the chunk the client is standing in or null if there's no applicable world.

Returns
Chunk|null

◆ getEyePosition()

Phpcraft\ClientConnection::getEyePosition ( )

◆ getHost()

Phpcraft\ClientConnection::getHost ( )

Returns the host the client had connected to, e.g. localhost:25565. Note that SRV records are pre-connection redirects, so if _minecraft._tcp.example.com points to mc.example.com which is an A or AAAA record, this will return mc.example.com:25565.

Returns
string

◆ getName()

Phpcraft\ClientConnection::getName ( )
Returns
string

Implements Phpcraft\Command\CommandSender.

◆ getPosition()

Phpcraft\ClientConnection::getPosition ( )
Returns
Point3D|null

Implements Phpcraft\Command\CommandSender.

◆ getServer()

Phpcraft\ClientConnection::getServer ( )
Returns
Server|null

Implements Phpcraft\Command\CommandSender.

◆ getUnitVector()

Phpcraft\ClientConnection::getUnitVector ( )

Returns a unit vector goin in the direction the client is looking.

Returns
Point3D

◆ getWorld()

Phpcraft\ClientConnection::getWorld ( )

Returns a World that you can directly interact with or null if not applicable.

Returns
World|null

◆ handleEncryptionResponse()

Phpcraft\ClientConnection::handleEncryptionResponse (   $private_key,
callable  $callback 
)

Reads an encryption response packet and starts asynchronous authentication with Mojang. This requires ClientConnection::$username to be set. In case of an error, the client is disconnected and the callback is called with false. Should the authentication with Mojang finish successfully, the callback is called with an array like this as argument:

[
  "id" => "11111111222233334444555555555555",
  "name" => "Notch",
  "properties" => [
    [
      "name" => "textures",
      "value" => "<base64 string>",
      "signature" => "<base64 string; signed data using Yggdrasil's private key>"
    ]
  ]
]
Parameters
resource$private_keyYour OpenSSL private key resource.
callable$callback
Returns
boolean
Exceptions
IOException

◆ handleInitialPacket()

Phpcraft\ClientConnection::handleInitialPacket ( )

Deals with the first packet the client has sent. This function deals with the handshake or legacy list ping packet. Errors will cause the connection to be closed.

Returns
int Status: 0 = The client is yet to present an initial packet. 1 = Handshake was successfully read; use Connection::$state to see if the client wants to get the status (1) or login to play (2). 2 = A legacy list ping packet has been received. 3 = An error occured and the connection has been closed.

◆ hasPermission()

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

Implements Phpcraft\Command\CommandSender.

◆ hasPosition()

Phpcraft\ClientConnection::hasPosition ( )
Returns
bool

Implements Phpcraft\Command\CommandSender.

◆ hasServer()

Phpcraft\ClientConnection::hasServer ( )

Available in accordance with the CommandSender interface.

Returns
bool true

Implements Phpcraft\Command\CommandSender.

◆ rotate()

Phpcraft\ClientConnection::rotate ( int  $yaw,
int  $pitch 
)

Changes the client's rotation.

Parameters
int$yaw
int$pitch
Returns
ClientConnection $this
Exceptions
IOException

◆ sendAbilities()

Phpcraft\ClientConnection::sendAbilities ( )

◆ sendAdminBroadcast()

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

Sends a message to the client and "[{$this-&gt;username}: $message]" to the server console and players with the given permission.

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

Implements Phpcraft\Command\CommandSender.

◆ sendEncryptionRequest()

Phpcraft\ClientConnection::sendEncryptionRequest (   $private_key)

Sends an Encryption Request Packet.

Parameters
resource$private_keyYour OpenSSL private key resource.
Returns
ClientConnection $this
Exceptions
IOException

◆ sendMessage()

Phpcraft\ClientConnection::sendMessage (   $message,
int  $position = ChatPosition::SYSTEM 
)

Sends a chat message to the client.

Parameters
array | string | null | ChatComponent$message
int$position
Returns
void
Exceptions
IOException

◆ setAbilitiesFromGamemode()

Phpcraft\ClientConnection::setAbilitiesFromGamemode ( int  $gamemode)

Sets the client's abilities according to the given gamemode.

Parameters
int$gamemode
Returns
ClientConnection $this
See also
ClientConnection::sendAbilities
ClientConnection::setGamemode

◆ setGamemode()

Phpcraft\ClientConnection::setGamemode ( int  $gamemode,
bool  $abilities = true 
)

Sets the client's gamemode and adjusts their abilities accordingly.

Parameters
int$gamemode
bool$abilitiesIf false, abilities will not be touched.
Returns
ClientConnection $this
Exceptions
Exception

◆ startPacket()

Phpcraft\ClientConnection::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.

◆ teleport()

Phpcraft\ClientConnection::teleport ( Point3D  $pos,
?float  $yaw = null,
?float  $pitch = null,
int  $deadline = 3 
)

Teleports the client to the given position, and optionally, changes their rotation.

Parameters
Point3D$pos
float | null$yaw
float | null$pitch
int$deadline
Returns
ClientConnection $this
Exceptions
IOException

◆ unloadChunks()

Phpcraft\ClientConnection::unloadChunks ( )
Returns
void
Exceptions
IOException
Since
0.5.9

Member Data Documentation

◆ $can_fly

boolean Phpcraft\ClientConnection::$can_fly = false

◆ $chunk_queue

array Phpcraft\ClientConnection::$chunk_queue = []

◆ $chunk_x

int null Phpcraft\ClientConnection::$chunk_x

◆ $chunk_z

int null Phpcraft\ClientConnection::$chunk_z

◆ $chunks

array Phpcraft\ClientConnection::$chunks = []

◆ $config

ClientConfiguration Phpcraft\ClientConnection::$config

◆ $convert_packets

bool Phpcraft\ClientConnection::$convert_packets

Used by proxy servers to store if the client is incompatible with the downstream server and needs packets to be converted.

◆ $disconnect_after

float Phpcraft\ClientConnection::$disconnect_after = 0

This variable is for servers to keep track of how long clients have to answer keep alive packets.

◆ $downstream

ServerConnection Phpcraft\ClientConnection::$downstream

Used by proxy servers to store the downstream connection instance.

◆ $downstream_eid

GMP Phpcraft\ClientConnection::$downstream_eid

The downstream entity ID. Used only on proxy servers.

◆ $eid

GMP Phpcraft\ClientConnection::$eid

The client's entity ID.

◆ $entityMetadata

Player null Phpcraft\ClientConnection::$entityMetadata

◆ $fly_speed

float Phpcraft\ClientConnection::$fly_speed = 0.05

◆ $flying

boolean Phpcraft\ClientConnection::$flying = false
See also
ClientConnection::sendAbilities
ServerFlyChangeEvent

◆ $gamemode

int Phpcraft\ClientConnection::$gamemode = Gamemode::SURVIVAL

◆ $hostname

string Phpcraft\ClientConnection::$hostname

The hostname the client had connected to.

See also
ClientConnection::getHost
ClientConnection::handleInitialPacket

◆ $hostport

int Phpcraft\ClientConnection::$hostport

The port the client had connected to.

See also
ClientConnection::handleInitialPacket
ClientConnection::getHost

◆ $instant_breaking

boolean Phpcraft\ClientConnection::$instant_breaking = false

◆ $invulnerable

boolean Phpcraft\ClientConnection::$invulnerable = false

◆ $join_specs

array< string > Phpcraft\ClientConnection::$join_specs = []

Additional data the client had provided in the handshake. E.g., "FML" is in this array for Forge clients.

◆ $next_heartbeat

float Phpcraft\ClientConnection::$next_heartbeat = 0

This variable is for servers to keep track of when to send the next keep alive packet to clients.

◆ $on_ground

boolean Phpcraft\ClientConnection::$on_ground = false
See also
ServerOnGroundChangeEvent

◆ $pitch

float Phpcraft\ClientConnection::$pitch = 0

The client's rotation on the Y axis, -90 to 90.

◆ $pos

Point3D Phpcraft\ClientConnection::$pos

The client's position in the world they are in.

◆ $render_distance

int Phpcraft\ClientConnection::$render_distance = 8

◆ $tp_confirm_deadline

float Phpcraft\ClientConnection::$tp_confirm_deadline = 0

◆ $tpidCounter

Counter Phpcraft\ClientConnection::$tpidCounter

◆ $username

string Phpcraft\ClientConnection::$username

The client's in-game name.

◆ $uuid

UUID Phpcraft\ClientConnection::$uuid

The UUID of the client.

◆ $walk_speed

float Phpcraft\ClientConnection::$walk_speed = 0.1

◆ $world

string Phpcraft\ClientConnection::$world = "world"

The name of the world the client is in.

Since
0.5.6

◆ $yaw

float Phpcraft\ClientConnection::$yaw = 0

The client's rotation on the X axis, 0 to 359.9.