Skip to content

ServerAuth API

Flavius12 edited this page Aug 29, 2015 · 14 revisions

Table of Contents

Basic Tutorial

1. Define the plugin dependency in plugin.yml (you can check if ServerAuth is installed in different ways):

depend: [ServerAuth]

2. Include ServerAuth API and ServerAuth Events in your php code:

//PocketMine Event Listener
use pocketmine\event\Listener;
//ServerAuth API
use ServerAuth\ServerAuth;
//ServerAuth Events
use ServerAuth\Events\ServerAuthRegisterEvent;

3. Create the class:

class Example extends PluginBase implements Listener {
}

4. Check if ServerAuth API is compatible (insert this code in onEnable() function)

if(ServerAuth::getAPI()->getAPIVersion() == "(used API version)"){
            //API compatible
            //Register Events
            $this->getServer()->getPluginManager()->registerEvents($this, $this);
        }else{
            //API not compatible
            $this->getPluginLoader()->disablePlugin($this);
        }
  }

5. Handle a ServerAuth event (in this tutorial we will handle the ServerAuthRegisterEvent):

public function onRegister(ServerAuthRegisterEvent $event){
  $player = $event->getPlayer();
  $player->sendMessage("You are now registered, " . $player->getName());
}

6. Call the API function:

ServerAuth::getAPI()->api_function();

ServerAuth API Events

ServerAuthAuthenticateEvent:

This event is handled when a player log into his account.

Event functions are:

Get Player:
Player getPlayer()

Description:
Get event player.
Return:
The event player

ServerAuthDeauthenticateEvent:

This event is handled when a player log out from his account.

Event functions are:

Get Player:
Player getPlayer()

Description:
Get event player.
Return:
The event player

ServerAuthPasswordChangeEvent:

This event is handled when a player changes his account password.

Event functions are:

Get Player:
Player getPlayer()

Description:
Get event player.
Return:
The event player

Get Password:
string getPassword()

Description:
Get the new password.
Return:
The new password

ServerAuthRegisterEvent:

This event is handled when a player registers an account.

Event functions are:

Get Player:
Player getPlayer()

Description:
Get event player.
Return:
The event player

Get Password:
string getPassword()

Description:
Get password (it can be hashed or not).
Return:
The password

ServerAuthUnregisterEvent:

This event is handled when a player unregisters his account.

Event functions are:

Get Player:
Player getPlayer()

Description:
Get event player.
Return:
The event player

ServerAuth API constants

ERR_USER_NOT_REGISTERED

Description:
User not registered
Value:
0

SUCCESS

Description:
Success
Value:
1

ERR_WRONG_PASSWORD

Description:
Wrong password error
Value:
2

ERR_USER_NOT_AUTHENTICATED

Description:
User not authenticated error
Value:
3

ERR_USER_ALREADY_AUTHENTICATED

Description:
User already authenticated error
Value:
4

ERR_USER_ALREADY_REGISTERED

Description:
User already registered error
Value:
5

ERR_PASSWORD_TOO_SHORT

Description:
Password too short error
Value:
6

ERR_PASSWORD_TOO_LONG

Description:
Password too long error
Value:
7

ERR_MAX_IP_REACHED

Description:
Max number of same IPs reached error
Value:
8

ERR_GENERIC

Description:
A generic error
Value:
9

ServerAuth API Functions

Get Version:

string getVersion()

Description:
Get ServerAuth plugin version
Return:
plugin version

Get API Version:

string getAPIVersion()

Description:
Get the ServerAuth API version.
Return:
plugin API version

Get Database:

mysqli|boolean getDatabase()

Description:
Get ServerAuth MySQLi instance.
Return:
mysqli on success
false on MySQL connection failure

Get Database Configuration:

array getDatabaseConfig()

Description:
Get ServerAuth MySQL configuration array.
Return:
array

Get Data Provider:

boolean getDataProvider()

Description:
Get ServerAuth data provider.
Return:
true if ServerAuth is using MySQL
false if ServerAuth is using YAML config

Check if register messages are enabled:

boolean areRegisterMessagesEnabled()

Description:
Check if register messages are enabled.
Return:
boolean

Enable\Disable register messages:

function enableRegisterMessages($bool)

Description:
Enable\Disable register messages.
Parameters:
$bool true to enable them or false to disable them

Check if login messages are enabled:

boolean areLoginMessagesEnabled()

Description:
Check if login messages are enabled.
Return:
boolean

Enable\Disable login messages:

function enableLoginMessages($bool)

Description:
Enable\Disable login messages.
Parameters:
$bool true to enable them or false to disable them

Get Player data

array|int getPlayerData($player)

Description:
Get player data.
Parameters:
$player The player name
Return:
The array of player data on SUCCESS, otherwise the current error

Get password hash:

string getPasswordHash()

Description:
Get password hash.
Return:
The password hash algorithm

Get language data:

Config getLanguage($language)

Description:
Get language data.
Parameters:
$language The language file name
Return:
A pocketmine\utils\Config instance containing language data

Get the ServerAuth language specified in config:

Config getConfigLanguage()

Description:
Get the ServerAuth language specified in config.
Return:
A pocketmine\utils\Config instance containing the current language data

Check if a player is registered:

boolean|int isPlayerRegistered($player)

Description:
Check if a player is registered.
Parameters:
$player The player name
Return:
true or false on SUCCESS, otherwise the current error

Check if a player is authenticated:

boolean isPlayerAuthenticated(Player $player)

Description:
Check if a player is authenticated.
Parameters:
$player The player instance
Return:
boolean

Register a player to ServerAuth:

boolean|int registerPlayer(Player $player, $password)

Description:
Register a player to ServerAuth.
Parameters:
$player The player instance
$password The password
Return:
true on SUCCESS, otherwise the current error

Unregister a player:

boolean|int unregisterPlayer(Player $player)

Description:
Unregister a player.
Parameters:
$player The player instance
Return:
true or false on SUCCESS or false if the player is not registered, otherwise the current error

Authenticate a player:

boolean|int authenticatePlayer(Player $player, $password, $hash = true)

Description:
Authenticate a player.
Parameters:
$player The player instance
$password The password
$hash true to hash the password
Return:
true on SUCCESS, otherwise the current error

Deauthenticate a player:

boolean|int deauthenticatePlayer(Player $player)

Description:
Deauthenticate a player.
Parameters:
$player The player instance
Return:
true on SUCCESS, otherwise the current error

Change player password:

int|boolean changePlayerPassword(Player $player, $new_password)

Description:
Change player password.
Parameters:
$player The player instance
$new_password The new password
Return:
true on SUCCESS or false if the player is not registered, otherwise the current status

Clone this wiki locally