This guide will explain how to split the Command from PluginBase, the reason behind this is to make the plugin cleaner and more readable.I am sick of people making cores with switch ($command->getName()) Note: If you literally have no idea about how PHP OOP works, don't follow this guide and keep developing like trash because you're not going to understand the methods explained in this post. Firstly first you would want to create your pluginbase class PHP: <?phpnamespace author\pluginname;use pocketmine\plugin\PluginBase;class MainClass extends PluginBase{ } then I will make the $map variable and register the command class PHP: $map = $this->getServer()->getCommandMap();$map->register($this->getName(), new CommandClass("test", $this)); Call this onEnable don't forget to import the command class later. if you want to register more then 1 command you can use the registerAll function. So I am going to create my command folder in the current directory and make a file called CommandClass Now you would want to extend Command and make a constructor with something like this PHP: class Commandclass extends Command implements PluginIdentifiableCommand{ /** * @var Plugin */ private $plugin; public function __construct(string $name, Plugin $plugin, string $description = "", ?string $usageMessage = null, array $aliases = []){ parent::__construct($name, $description, $usageMessage, $aliases); $this->plugin = $plugin; $this->setDescription("Tests"); }} The constructer is telling the server that the command name is $name which is what you are passing in when you call it Lets set a description called Tests Now you would want to inherit docs PHP: public function execute(CommandSender $sender, string $commandLabel, array $args){ $sender->sendMessage("Tada");} What I am doing here is to send a message called tada when you run the command PHP: public function getPlugin(): Plugin{ return $this->plugin;} now if you ever want to access something from your main class you can use the getPlugin function we have created Sorry if my explanation was very crappy since most of the time I let phpstorm inherit docs for me so feel free to ask questions relevant to the post.
Thank you for this guide! It definitely does look cleaner and more professional than the normal switch($cmd->getName(); For the ones who have a little more trouble understanding, do you mind if I make a repository on GitHub to show a better example of this method of creating commands?
If I want call A Function at Main like "Public function onForm($player)" I will write "$this->plugin->onForm()"?
You lack the knowledge to act as an expert on a given topic. You by no means are restricted from writing such guides, but next time without concrete evidence, don't bring up your humble opinions.
Not at all buddy. I was targetting the thread author. He claimed that one approach is better than another, while both work perfectly fine under required circumstances. Some plugins do not require such encapsulation, there is a blurry line between good and premature optimization. Take as an example /date plugin that informs the sender about current date (server). Do you really require another class instead of 8 lines of extra code?