Skip to content
PEMapModder edited this page Jan 18, 2016 · 12 revisions

Developer documentation

Add extra registration fields

  1. Create a class that implements the HereAuth\User\Registration\RegistrationStep interface.
  2. Create a constructor that accepts a HereAuth\User\User as argument and stores it in a class property.
  3. Implement the getMessage() method, which returns a string containing the message to send to the player when the player:
  4. Implement the onSubmit() method. It accepts a string argument that is exactly what the player inputted, and returns a boolean (true/false) to indicate whether the value is accepted by you. If false is returned, the player will have to type the value again. If you return null (or if you return nothing), it will be casted to boolean to become false. Therefore, always return a value, true or false. You can know which player this is about using the User you stored in the constructor.
  5. Create an event handler for HereAuth\Event\HereAuthRegistrationCreationEvent.
  6. In the event, use the $event->getRegistration()->addStep() method to add a new instance of the class you just created above. Developers SHOULD use a different instance for every time the event is triggered. You can get the User required from the $event->getUser() function.

Example:

use pocketmine\event\Listener;

use HereAuth\Event\HereAuthRegistrationCreationEvent;
use HereAuth\User\User;
use HereAuth\User\Registration\RegistrationStep;

class MyRegStep implements RegistrationStep{
    private $user;

    public function __construct(User $user){
        $this->user = $user;
    }

    public function getMessage(){
        return "Please enter your email.";
    }

    public function onSubmit($addr){
        if(!preg_match('/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z.]{2,4}$/', $addr)){
            $this->user->getPlayer()->sendMessage("Invalid email!");
            return false;
        }
        mail($addr, "You have been registered!", "Hello " . $this->user->getPlayer->getName() . ",\r\n" .
                "You have been registered with HereAuth on the following server:\r\n" .
                Utils::getIP() . ":" . $this->user->getMain()->getServer()->getPort() . "\r\nHave fun!");
        return true;
    }
}

class MyEventListener implements Listener{
    public function onRegistrationCreate(HereAuthRegistrationCreationEvent $event){
        $event->getRegistration()->addStep(new MyRegStep($event->getUser()));
    }
}

You can adjust the order of this field from other plugins, through adjusting the event handler priority in the documentation comment before the event handler (PocketMine built-in feature):

class MyEventListener implements Listener{
    /**
     * @priority LOW
     */
    public function onRegistrationCreate(HereAuthRegistrationCreationEvent $event){

As you might already know, there are 6 event priorities, being dispatched in this subsequent order: LOWEST -> LOW -> NORMAL -> HIGH -> HIGHEST -> MONITOR. However, you SHOULD NOT handle it at the MONITOR level so that event handlers after your one can see the step you added! As the PocketMine documentation says:

Event is listened to purely for monitoring the outcome of an event. No modifications to the event should be made under this priority

Another point to note is that password input (and password confirm) as well as the custom registration steps defined in config.yml must come before steps added in events.

Handle events

Event name (**** in `HereAuth\Event\HereAuth****Event``) Description Can be cancelled?
Authentication When a player auto-authed, registered or logged in with password, or when the player joined without registering (only for servers where ForceRegister is false)
RegistrationCreation When a user starts registering, this event is triggered so that other plugins can add registration steps to it
Login When a user had registered before and AutoAuth or password auth matched
Registration When a user has registered
Clone this wiki locally