I have just started learning a little bit of pocketmine coding with the help of internet and I came across a issue that I don't know how to make different file for different commmand like I am creating a core so it have a few commmand of different categories and I don't want to make my main file a mess. I have seen the bedwars plugin but can't able to understand because there commmand are little bit confusing that make overall file confusing for me. Thanks in advance
It goes something like this, you just need a class extending Command.php and to register use the Server.php function getCommandMap()->registerCommand($prefix, $class); PHP: class MyCommand extends Command{///Implement __construct to define Command name, aliases....public function execute(CommandSender $sender, string $label, array $args) : bool{}}class Main extends PluginBase{public function onEnable(){$this->getServer()->getCommandMap()->registerCommand("test", new MyCommand("test"));}}
You can create commands within multiple files. I do it with construct. ex. PHP: public function __construct(){ parent::__construct("command_name", "description", "usage", ['aliasses']); $this->setPermission("permission.name"); # (If permission is needed)}/** * @param CommandSender $sender * @param string $commandLabel * @param array $args */public function execute(CommandSender $sender, string $commandLabel, array $args){ # Your code here || (What will the command execute?) #ex. $sender->sendMessage(TextFormat::GREEN . "Sup man ");} Then we need to register our commands via command map in the loader/main class. PHP: public function onEnable(): void { $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getServer()->getCommandMap()->registerAll("CoreCommands", [ new MyNameOfCommandFileOne(), new MyNameOfCommandFileTwo(), new MyNameOfCommandFIleThree() # No comma on the last one || (This will error) # Not the literal command name^, the file name works for me. ]); Then your command should work.