Overview
The PluginBase class is the foundation for all PocketMine-MP plugins. Every plugin must extend this class and implement its lifecycle methods.
Creating a Plugin
<?php
namespace MyPlugin;
use pocketmine\plugin\PluginBase;
class Main extends PluginBase {
protected function onLoad() : void {
$this->getLogger()->info("Plugin loading...");
}
protected function onEnable() : void {
$this->getLogger()->info("Plugin enabled!");
}
protected function onDisable() : void {
$this->getLogger()->info("Plugin disabled!");
}
}
Lifecycle Methods
onLoad()
Called when the plugin is loaded, before onEnable().
protected function onLoad() : void
Use this method to:
- Register custom generators
- Register custom entities
- Perform early initialization
Example:
protected function onLoad() : void {
GeneratorManager::getInstance()->addGenerator(
CustomGenerator::class,
"custom",
fn() => null
);
}
onEnable()
Called when the plugin is enabled.
protected function onEnable() : void
Use this method to:
- Register event listeners
- Schedule tasks
- Load configuration
- Register commands
Example:
protected function onEnable() : void {
$this->getServer()->getPluginManager()->registerEvents(
new MyListener($this),
$this
);
$this->saveDefaultConfig();
$this->getScheduler()->scheduleRepeatingTask(
new MyTask(),
20 // Run every second
);
}
onDisable()
Called when the plugin is disabled.
protected function onDisable() : void
Use this method to:
- Save data
- Close database connections
- Cancel tasks
- Clean up resources
Example:
protected function onDisable() : void {
$this->saveConfig();
$this->database->close();
}
Core Methods
getServer()
Gets the server instance.
final public function getServer() : Server
Returns: Server - The server instance
Example:
$server = $this->getServer();
$players = $server->getOnlinePlayers();
getLogger()
Gets the plugin’s logger.
getLogger() : \AttachableLogger
public function getLogger() : \AttachableLogger
Returns: AttachableLogger - Plugin logger instance
Example:
$this->getLogger()->info("Information message");
$this->getLogger()->warning("Warning message");
$this->getLogger()->error("Error message");
$this->getLogger()->debug("Debug message");
getName()
Gets the plugin name from plugin.yml.
final public function getName() : string
Returns: string - Plugin name
getDescription()
Gets the plugin description object.
getDescription() : PluginDescription
final public function getDescription() : PluginDescription
Returns: PluginDescription - Plugin metadata
Example:
$desc = $this->getDescription();
$version = $desc->getVersion();
$authors = $desc->getAuthors();
getDataFolder()
Gets the plugin’s data folder path.
final public function getDataFolder() : string
Returns: string - Absolute path to plugin data folder
Example:
$dataFolder = $this->getDataFolder();
$dbPath = $dataFolder . "database.db";
isEnabled()
Checks if the plugin is enabled.
final public function isEnabled() : bool
Returns: bool - True if plugin is enabled
isDisabled()
Checks if the plugin is disabled.
final public function isDisabled() : bool
Returns: bool - True if plugin is disabled
Configuration
getConfig()
Gets the plugin’s config.yml as a Config object.
public function getConfig() : Config
Returns: Config - Configuration object
Example:
$config = $this->getConfig();
$maxPlayers = $config->get("max-players", 10);
$enabled = $config->get("enabled", true);
saveConfig()
Saves the current config to disk.
public function saveConfig() : void
Example:
$config = $this->getConfig();
$config->set("last-save", time());
$this->saveConfig();
saveDefaultConfig()
Saves the default config.yml from the plugin resources.
saveDefaultConfig() : bool
public function saveDefaultConfig() : bool
Returns: bool - True if config was saved, false if already exists
Example:
protected function onEnable() : void {
$this->saveDefaultConfig();
$config = $this->getConfig();
}
reloadConfig()
Reloads the config from disk.
public function reloadConfig() : void
Example:
public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool {
if($command->getName() === "myreload") {
$this->reloadConfig();
$sender->sendMessage("Config reloaded!");
return true;
}
return false;
}
Resources
saveResource()
Saves a resource file from the plugin to the data folder.
saveResource(string $filename, bool $replace = false) : bool
public function saveResource(string $filename, bool $replace = false) : bool
Name of the resource file in the resources folder
Whether to overwrite existing file (default: false)
Returns: bool - True if resource was saved successfully
Example:
protected function onEnable() : void {
$this->saveResource("config.yml");
$this->saveResource("messages.yml");
$this->saveResource("data.json", true); // Replace if exists
}
getResource()
Gets a resource from the plugin as a file handle.
getResource(string $filename)
public function getResource(string $filename)
Returns: resource|null - File handle or null if not found
Note: Deprecated. Use getResourcePath() with standard PHP functions instead.
getResourcePath()
Gets the full path to a resource file.
getResourcePath(string $filename) : string
public function getResourcePath(string $filename) : string
Returns: string - Full path to resource file
Example:
$path = $this->getResourcePath("data.json");
$data = json_decode(file_get_contents($path), true);
getResources()
Gets all resources in the plugin.
public function getResources() : array
Returns: SplFileInfo[] - Array of resource file info objects
Scheduler
getScheduler()
Gets the plugin’s task scheduler.
getScheduler() : TaskScheduler
public function getScheduler() : TaskScheduler
Returns: TaskScheduler - The plugin’s scheduler
Example:
$scheduler = $this->getScheduler();
// Schedule immediate task
$scheduler->scheduleTask(new MyTask());
// Schedule delayed task (5 seconds)
$scheduler->scheduleDelayedTask(new MyTask(), 20 * 5);
// Schedule repeating task (every 1 second)
$scheduler->scheduleRepeatingTask(new MyTask(), 20);
// Schedule delayed repeating task
$scheduler->scheduleDelayedRepeatingTask(new MyTask(), 20 * 5, 20);
Commands
getCommand()
Gets a command registered by this plugin.
public function getCommand(string $name)
Returns: Command|null - Command object if found and owned by this plugin
Example:
$command = $this->getCommand("mycommand");
if($command !== null) {
$command->setPermission("myplugin.admin");
}
onCommand()
Handles command execution. Override this to handle commands defined in plugin.yml.
onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool
public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool
Returns: bool - True if command was handled successfully
Example:
public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool {
switch($command->getName()) {
case "heal":
if($sender instanceof Player) {
$sender->setHealth($sender->getMaxHealth());
$sender->sendMessage("You have been healed!");
return true;
}
$sender->sendMessage("This command can only be used in-game");
return false;
case "broadcast":
if(count($args) === 0) {
return false; // Shows usage
}
$message = implode(" ", $args);
$this->getServer()->broadcastMessage($message);
return true;
}
return false;
}
Complete Example
<?php
namespace MyPlugin;
use pocketmine\plugin\PluginBase;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\player\Player;
use pocketmine\scheduler\Task;
class Main extends PluginBase implements Listener {
protected function onEnable() : void {
// Save default config
$this->saveDefaultConfig();
// Register event listener
$this->getServer()->getPluginManager()->registerEvents($this, $this);
// Schedule repeating task
$this->getScheduler()->scheduleRepeatingTask(
new class($this) extends Task {
public function __construct(private Main $plugin) {}
public function onRun() : void {
$this->plugin->getLogger()->debug("Task running...");
}
},
20 * 60 // Every minute
);
$this->getLogger()->info("Plugin enabled!");
}
public function onPlayerJoin(PlayerJoinEvent $event) : void {
$player = $event->getPlayer();
$message = $this->getConfig()->get("join-message", "Welcome {player}!");
$message = str_replace("{player}", $player->getName(), $message);
$player->sendMessage($message);
}
public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool {
if($command->getName() === "myplugin") {
$sender->sendMessage("MyPlugin v" . $this->getDescription()->getVersion());
return true;
}
return false;
}
protected function onDisable() : void {
$this->saveConfig();
$this->getLogger()->info("Plugin disabled!");
}
}