Events
Please see the PMMP Docs for explanation on how events works. This section currently only explains an example on how to create an event. This will be updated in the future.
Creating an Event Listener Class
Create a new file named EventListener.php
inside the directory where your Main class file is located at. Then open the file and add this code (replace the namespace with your plugin's namespace):
<?php
namespace YourPluginName\YourName;
use pocketmine\event\Listener;
class EventListener implements Listener {
}
Creating an Event Handling Method
We're going to use PlayerMoveEvent
as an example.
First, import the PlayerMoveEvent
class:
use pocketmine\event\player\PlayerMoveEvent;
Second, add a method with PlayerMoveEvent $event
as its argument. You can use any method name you like, but the method must be public and must return void
This time we're going to use onPlayerMove
:
public function onPlayerMove(PlayerMoveEvent $event): void
{
}
You can set the event priority when multiple handlers from different plugins needs to execute at the same time. For more information, please visit the PMMP Docs page.
As of April 13, the documentation is incomplete, so if you want to know more about it, please ask in PMMP's Discord server.
Handling the Event
In this example, we want to print the player's origin and target location in the player's chat, and cancel the event to prevent the player from moving.
Before that, we need to check if the event has been cancelled by another plugin. If it's indeed has been cancelled, we'd like to stop the event handling. To do that, add this code into the method we've just created:
if($event->isCancelled()) { // Checks if the event has been cancelled by another plugin
return; // Stops the event handling
}
Now we can do what we wanted safely. Please add this code under the code we've just added:
$player = $event->getPlayer(); // Saves the player instance as a variable to make the code a bit cleaner
$player->sendMessage("From: " . $event->getFrom()); // This should call the Location->__toString() method
$player->sendMessage("To: " . $event->getTo());
$event->cancel(); // Cancels the event to prevent the player from moving
After doing the three sections above, your EventListener
class should look like these:
<?php
namespace YourPluginName\YourName;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerMoveEvent;
class EventListener implements Listener{
public function onPlayerMove(PlayerMoveEvent $event): void{
if($event->isCancelled()){ // Checks if the event has been cancelled by another plugin
return; // Stops the event handling
}
$player = $event->getPlayer(); // Saves the player instance as a variable to make the code a bit cleaner
$player->sendMessage("From: " . $event->getFrom()); // This should call the Location->__toString() method
$player->sendMessage("To: " . $event->getTo());
$event->cancel(); // Cancels the event to prevent the player from moving
}
}
If not, please check again what you may have missed before continuing.
Registering the Event Listener Class
To make our EventListener
class work, we need to register it in our Main class, specifically while our plugin is enabling (that is, in the onEnable
method).
To do that, please open your Main class, then add this code in your onEnable
method:
$this->getServer()->getPluginManager()->registerEvents(new EventListener(), $this);
This time we don't need to import the EventListener
class, because it's located in the same directory as our Main class. Please always keep this in mind.
Congratulations, you've created your first event listener class and handler. Now you're ready to test it!