How can I override the /kick command? I tried this, but it isn't working. PHP: public function onCommandPreprocess(PlayerCommandPreprocessEvent $event) { $msg = $event->getMessage(); switch($msg) { case "/kick": $event->setCancelled(true); $sender = $event->getPlayer(); if($sender->hasPermission("pocketmine.command.kick")) { if(isset($args[0])) { if(isset($args[1])) { $player = $this->getServer()->getPlayer($args[0]); if($player) { $reason = array_splice($args, 1, 99999); $reason_send = implode(" ", $reason); $player->kick( "§cKicked by Admin/Moderator." . "\n" . "§e" . $reason_send, false ); } else { $sender->sendMessage("§cThat player is not online!"); } } else { $sender->sendMessage("§cUsage: /kick <player> <reason>"); } } else { $sender->sendMessage("§cUsage: /kick <player> <reason>"); } } else { $sender->sendMessage("§cYou do not have permission to use this command"); } break; } }
Try to explode() $msg and use it as $args[0]: PHP: $args = explode(" ", strtolower($event->getMessage()));if($args[0] == "/kick") { //your code}
Create a command class like normal and before registering it to the command map, run this: PHP: $commandMap = $this->getServer()->getCommandMap();$command = $commandMap->getCommand($name);$command->setLabel("kick_disabled");$command->unregister($commandMap);
Do I really need to make a command class? I always register commands with: Code: onCommand(blablabla...)
Help? PHP: <?phpnamespace WinterBuild7074\KickWriter;use pocketmine\event\Listener;use pocketmine\Player;use pocketmine\plugin\PluginBase;use pocketmine\Server;use pocketmine\command\Command;use pocketmine\command\CommandSender;use pocketmine\command\CommandMap;use pocketmine\utils\Config;class Main extends PluginBase implements Listener { private $kickCommand; public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->saveDefaultConfig(); $name = "kick"; $commandMap = $this->getServer()->getCommandMap(); $command = $commandMap->getCommand($name); $command->setLabel("kick_disabled"); $command->unregister($commandMap); $this->kickCommand = new kickCommand($this); $commandMap->register("kick", $this->kickCommand); }} PHP: <?phpnamespace WinterBuild7074\KickWriter;use pocketmine\command\Command;use pocketmine\command\CommandSender;use pocketmine\plugin\Plugin;use pocketmine\command\PluginIdentifiableCommand;use pocketmine\Player;class kickCommand extends Command implements PluginIdentifiableCommand { private $main; public function __construct(Main $main){ $this->main = $main; } public function execute(CommandSender $sender, string $label, array $args){ $sender->sendMessage("§6Hello!"); } public function getPlugin(): Plugin{ return $this->main; }} Code: [Server thread/CRITICAL]: TypeError: "Return value of pocketmine\command\Command::getName() must be of the type string, null returned" (EXCEPTION) in "src/pocketmine/command/Command" at line 138
The first paramater of CommandMap->register is actually the fallback prefix. For example, if the default /ban command was overriden by a plugin, i could run /pocketmine:ban to use it because pocketmine is the fallback prefix. You also need to call the parent constructor in the kick command class, like so PHP: parent::__construct("kick");// or with aliasesparent::__construct("kick", [ "insert", "alias", "here"]);