Banner

Coding Programming PocketMine

Tutorial: How to Create a Plugin for PocketMine using Php

  1. Introduction
  2. Preparation
  3. Basic Plugin Structure
  4. Main Plugin
  5. Command Personalization
  6. Events and Listener
  7. Plugin Configuration
  8. Most Important Part
  9. Plugin Publication
  10. Additional Resources

Introduction

PocketMine-MP is server software for Minecraft: Bedrock Edition, downloaded in PHP. This tutorial walks you through creating a plugin for PocketMine 5.0.0 and later versions.

Preparation

Condition

PocketMine-MP installation

  1. Download the last version of PocketMine-MP from there Official Website.
  2. Follow the instructions to install PocketMine in your operating system.

Basic Plugin Structure

A PocketMine-MP plugin consisting of various archives, but the most important is plugin.yml, which describes the plugin, and a PHP archive containing p logic.

Carpet Structure

MyFirstPlugin/
├── src/
│ └── MyFirstPlugin/
│ └── Main.php
└── plugin.yml
view raw cmd hosted with ❤ by GitHub

Primer Plugin

Archive plugin.yml

name: MyFirstPlugin
icon: icon.png
main: MyFirstPlugin\Main
version: 1.0.0
api: 5.0.0
author: TuNombre
description: Mi primer plugin para PocketMine-MP 5.0.0
view raw plugin.yml hosted with ❤ by GitHub

Archive Main.php

<?php
namespace MyFirstPlugin;
use pocketmine\plugin\PluginBase;
class Main extends PluginBase {
public function onEnable(): void {
$this->getLogger()->info("¡Hola, mundo! Mi primer plugin ha sido habilitado.");
}
}
view raw Main.php hosted with ❤ by GitHub

Command Personalization

To add personalized commands, you must specify them plugin.yml and create logic in PHP archive.

Modification plugin.yml

commands:
hello:
description: Saluda al jugador
usage: "/hello"
view raw plugin.yml hosted with ❤ by GitHub

Modification Main.php

<?php
namespace MyFirstPlugin;
use pocketmine\plugin\PluginBase;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\player\Player;
class Main extends PluginBase {
public function onEnable(): void {
$this->getLogger()->info("¡Hola, mundo! Mi primer plugin ha sido habilitado.");
}
public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool {
if ($command->getName() === "hello") {
if ($sender instanceof Player) {
$sender->sendMessage("¡Hola, " . $sender->getName() . "!");
return true;
} else {
$sender->sendMessage("Este comando solo puede ser usado en el juego.");
return false;
}
}
return false;
}
}
view raw Main.php hosted with ❤ by GitHub

Events and Listener

These events allow the plugin to customize actions on the server.

Example: Events and Listener

<?php
namespace MyFirstPlugin;
use pocketmine\plugin\PluginBase;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;
class Main extends PluginBase implements Listener {
public function onEnable(): void {
$this->getLogger()->info("¡Hola, mundo! Mi primer plugin ha sido habilitado.");
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
public function onPlayerJoin(PlayerJoinEvent $event): void {
$player = $event->getPlayer();
$player->sendMessage("¡Bienvenido, " . $player->getName() . " al servidor!");
}
}
view raw Main.php hosted with ❤ by GitHub

Plugin Configuration

To allow plugin configuration, you can create an archive config.yml.

Archive config.yml

mensaje_bienvenida: "¡Bienvenido al servidor, %player%!"
view raw config.yml hosted with ❤ by GitHub

Modification Main.php

<?php
namespace MyFirstPlugin;
use pocketmine\plugin\PluginBase;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;
class Main extends PluginBase implements Listener {
public function onEnable(): void {
$this->saveDefaultConfig();
$this->getLogger()->info("¡Hola, mundo! Mi primer plugin ha sido habilitado.");
$this->getServer()->getPluginManager()->registerEvents($this, $this);
}
public function onPlayerJoin(PlayerJoinEvent $event): void {
$player = $event->getPlayer();
$message = str_replace("%player%", $player->getName(), $this->getConfig()->get("mensaje_bienvenida"));
$player->sendMessage($message);
}
}
view raw Main.php hosted with ❤ by GitHub

Most Important Part

Publishing plugins

Adding Resources

Join our Discord community and ask! Go to Discord