Hi, I would like to know if there is a way so that, when a player writes a command, a message is broadcasted with the name of the player in front. eg: JeanCharles: /afk -- JeanCharles is now AFK. I tried this code : PHP: public function onCommand(CommandSender $sender, PlayerCommandPreprocessEvent $event, Command $cmd, $label, array $args) { $sender->sendMessage(TextFormat::GREEN . "Vous etes desormais AFK."); $sender = $event->getPlayer(); $name = $sender->getName(); $this->getServer()->broadcastMessage("$name est desormais AFK."); return false;} but it dosen't work. Error message in crashdump : PHP: Error: Declaration of zakoussemc\afkplugin\Main::eek:nCommand(pocketmine\command\CommandSender $sender, pocketmine\event\player\PlayerCommandPreprocessEvent $event, pocketmine\command\Command $cmd, $label, array $args) must be compatible with pocketmine\command\CommandExecutor::eek:nCommand(pocketmine\command\CommandSender $sender, pocketmine\command\Command $command, $label, array $args) Full code of Main.php : PHP: <?phpnamespace zakoussemc\afkplugin;use pocketmine\command\Command;use pocketmine\command\CommandExecutor;use pocketmine\command\CommandSender;use pocketmine\Player;use pocketmine\plugin\PluginBase;use pocketmine\Server;use pocketmine\utils\Config;use pocketmine\utils\TextFormat;use pocketmine\event\Listener;use pocketmine\event\player\PlayerCommandPreprocessEvent;class Main extends PluginBase implements Listener {public function onEnable() { @mkdir($this->getDataFolder()); $this->saveDefaultConfig(); $this->reloadConfig(); $this->getLogger()->info(TextFormat::RED . "its alive"); $this->getServer()->getPluginManager()->registerEvents($this,$this); }public function translateColors($symbol, $message) { $message = str_replace($symbol."0", TextFormat::BLACK, $message); $message = str_replace($symbol."1", TextFormat::DARK_BLUE, $message); $message = str_replace($symbol."2", TextFormat::DARK_GREEN, $message); $message = str_replace($symbol."3", TextFormat::DARK_AQUA, $message); $message = str_replace($symbol."4", TextFormat::DARK_RED, $message); $message = str_replace($symbol."5", TextFormat::DARK_PURPLE, $message); $message = str_replace($symbol."6", TextFormat::GOLD, $message); $message = str_replace($symbol."7", TextFormat::GRAY, $message); $message = str_replace($symbol."8", TextFormat::DARK_GRAY, $message); $message = str_replace($symbol."9", TextFormat::BLUE, $message); $message = str_replace($symbol."a", TextFormat::GREEN, $message); $message = str_replace($symbol."b", TextFormat::AQUA, $message); $message = str_replace($symbol."c", TextFormat::RED, $message); $message = str_replace($symbol."d", TextFormat::LIGHT_PURPLE, $message); $message = str_replace($symbol."e", TextFormat::YELLOW, $message); $message = str_replace($symbol."f", TextFormat::WHITE, $message); $message = str_replace($symbol."k", TextFormat::OBFUSCATED, $message); $message = str_replace($symbol."l", TextFormat::BOLD, $message); $message = str_replace($symbol."m", TextFormat::STRIKETHROUGH, $message); $message = str_replace($symbol."n", TextFormat::UNDERLINE, $message); $message = str_replace($symbol."o", TextFormat::ITALIC, $message); $message = str_replace($symbol."r", TextFormat::RESET, $message); return $message; } // Acces aux permissions.private function access(CommandSender $sender, $permission) { if($sender->hasPermission($permission)) return true; $sender->sendMessage("Vous n'avez pas la permission d'utiliser cette commande."); return false;}private function inGame(CommandSender $sender,$msg = true) { if ($sender instanceof Player) return true; if ($msg) $sender->sendMessage("Vous pouvez seulement utiliser cette commande en jeu."); return false;}public function onCommand(CommandSender $sender, PlayerCommandPreprocessEvent $event, Command $cmd, $label, array $args) { $sender->sendMessage(TextFormat::GREEN . "Vous etes desormais AFK."); $sender = $event->getPlayer(); $name = $sender->getName(); $this->getServer()->broadcastMessage("$name est desormais AFK."); return false;}} Thanks!
I changed the code a bit, 2 spaces were not useful PHP: <?phpnamespace zakoussemc\afkplugin;use pocketmine\command\Command;use pocketmine\command\CommandExecutor;use pocketmine\command\CommandSender;use pocketmine\Player;use pocketmine\plugin\PluginBase;use pocketmine\Server;use pocketmine\utils\Config;use pocketmine\utils\TextFormat;use pocketmine\event\Listener;use pocketmine\event\player\PlayerCommandPreprocessEvent;class Main extends PluginBase implements Listener {public function onEnable() { @mkdir($this->getDataFolder()); $this->saveDefaultConfig(); $this->reloadConfig(); $this->getLogger()->info(TextFormat::RED . "its alive"); $this->getServer()->getPluginManager()->registerEvents($this,$this); }public function translateColors($symbol, $message) { $message = str_replace($symbol."0", TextFormat::BLACK, $message); $message = str_replace($symbol."1", TextFormat::DARK_BLUE, $message); $message = str_replace($symbol."2", TextFormat::DARK_GREEN, $message); $message = str_replace($symbol."3", TextFormat::DARK_AQUA, $message); $message = str_replace($symbol."4", TextFormat::DARK_RED, $message); $message = str_replace($symbol."5", TextFormat::DARK_PURPLE, $message); $message = str_replace($symbol."6", TextFormat::GOLD, $message); $message = str_replace($symbol."7", TextFormat::GRAY, $message); $message = str_replace($symbol."8", TextFormat::DARK_GRAY, $message); $message = str_replace($symbol."9", TextFormat::BLUE, $message); $message = str_replace($symbol."a", TextFormat::GREEN, $message); $message = str_replace($symbol."b", TextFormat::AQUA, $message); $message = str_replace($symbol."c", TextFormat::RED, $message); $message = str_replace($symbol."d", TextFormat::LIGHT_PURPLE, $message); $message = str_replace($symbol."e", TextFormat::YELLOW, $message); $message = str_replace($symbol."f", TextFormat::WHITE, $message); $message = str_replace($symbol."k", TextFormat::OBFUSCATED, $message); $message = str_replace($symbol."l", TextFormat::BOLD, $message); $message = str_replace($symbol."m", TextFormat::STRIKETHROUGH, $message); $message = str_replace($symbol."n", TextFormat::UNDERLINE, $message); $message = str_replace($symbol."o", TextFormat::ITALIC, $message); $message = str_replace($symbol."r", TextFormat::RESET, $message); return $message; } // Acces aux permissions.private function access(CommandSender $sender, $permission) { if($sender->hasPermission($permission)) return true; $sender->sendMessage("Vous n'avez pas la permission d'utiliser cette commande."); return false;}private function inGame(CommandSender $sender,$msg = true) { if ($sender instanceof Player) return true; if ($msg) $sender->sendMessage("Vous pouvez seulement utiliser cette commande en jeu."); return false;}public function onCommand(CommandSender $sender,PlayerCommandPreprocessEvent $event,Command $cmd,$label,array $args) { $sender->sendMessage(TextFormat::GREEN . "Vous etes desormais AFK."); $sender = $event->getPlayer(); $name = $sender->getName(); $this->getServer()->broadcastMessage("$name est desormais AFK."); return false;}}
Why do you need $sender = $event->getPlayer()? Just check if $sender is instanceof Player and then use it. PHP: public function onCommand(CommandSender $sender, PlayerCommandPreprocessEvent $event, Command $cmd, $label, array $args) { becomes PHP: public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) {
So instead of PHP: public function onCommand(CommandSender $sender,PlayerCommandPreprocessEvent $event,Command $cmd,$label,array $args) { $sender->sendMessage(TextFormat::GREEN . "Vous etes desormais AFK."); $sender = $event->getPlayer(); $name = $sender->getName(); $this->getServer()->broadcastMessage("$name est desormais AFK."); return false;} I put it like this ? : PHP: public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) { $sender->sendMessage(TextFormat::GREEN . "Vous etes desormais AFK."); if ($sender instanceof Player) return true; $this->getServer()->broadcastMessage("$name est desormais AFK."); return false;}
No. $name would not be defined. Think about it. If the $sender is a player, you want it to broadcast his name. What you are doing here is checking if the sender is a player, return (leave) the function without broadcasting.
Learn PHP. Honestly, even if I give you the code you will just copy and paste it... PHP: public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) { $sender->sendMessage(TextFormat::GREEN . "Vous etes desormais AFK."); if (!($sender instanceof Player)) return true; $this->getServer()->broadcastMessage("{$sender->getName()} est desormais AFK."); return true;}
I'm trying to learn it, but I can't find any courses in french, I'm french not english and it's hard for me to understand everything that's writen...
Hm... I am pretty sure there are basic syntax tutorials in French. Then learn by doing some hands on. PocketMine plugin development is/ not a good place to start with.
It's not a matter of thinking, it's about reading docs, going through the source or using an IDE. You can't just think of a new function/method name and have it do exactly what you want it to do. Please learn PHP before you post anything in the plugin development section. This forum is for help with the PocketMine API, not for helping people who think programming is a matter of copying and pasting.
it is.. But it can also be used for software like PocketMine. PMMP and all it's plugins are coded in PHP.