This doesn't unregister it. This only blocks its execution and executes something else when it is executed.
Yep. I'm just not sure if Command->unregister() really removes the Command. Cuz last time I tried unregistering, the command seems like does not registered.
PlayerCommandPreprocessEvent is a hacky method to deal with this. You basically can't unregister it. The closest to unregister is overwriting the command that returns "Unknown command. Type /help for a list of commands." message in red. And also, hide it from "/" commands list by sending this packet to every individual. PHP: $pk = new AvailableCommandsPacket();$data = new \stdClass();$pk->commands = "";foreach(Server::getInstance()->getCommandMap()->getCommands() as $command){ if (strtolower($command->getName()) !== "cmd2remove") { $data->{$command->getName()}->versions[0] = $command->generateCustomCommandData($player); }}$pk->commands = json_encode($data);$player->dataPacket($pk);
Now What I block when Player do ./command instead /? I tried this in PlayerCommandPreprocess, but bad PHP: }elseif($cmd[0] == "./test"){$e->setCancelled(); }
I believe this will work with ./ commands too PHP: public function checkCmd(PlayerCommandPreprocessEvent $event) { $command = explode(" ", strtolower($event->getMessage())); if($command[0] === "/test") { $event->setCancelled(); }}
substr() should be used instead of explode() as you just needed to retrieve command. substr() is always better if you don't want to handle command arguments
But, in this case: PHP: public function checkCmd(PlayerCommandPreprocessEvent $event) { $command = explode(" ", strtolower($event->getMessage())); if($command[0] === "/test") { $event->setCancelled(); }}//$event->getMessage(): /test arguments//$command = [0 => "/test", 1 => "arguments"];//$command[0] = "/test" Substr would be better but it must be a little, not the way you wanted it. PHP: public function checkCmd(PlayerCommandPreprocessEvent $event) { $command = substr($event->getMessage(), -(strlen("/test"))); if($command === "/test") { $event->setCancelled(); }}
The subject to test for should be "/test " to avoid detecting other commands starting with "test". Ladies and gentlemen, let me add fuel to the fire and introduce the brand new old strstr function. PHP: if($message === "/test" or strstr($message, " ", true) === "/test") $event->setCancelled(); Roses are red, violets are blue, sugar is sweet, and the world is bigger than that expected by you. There are myriad ways to do the same thing, of course: PHP: if($message === "/test" or strlen($message) >= 6 and strpos($message, "/test ") === 0) $event->setCancelled();if($message{0} === "/" and $message{1} === "t" and $message{2} === "e" and $message{3} === "s" and $message{4} === "t" and !isset($message{5}) || $message{5} === " ") $event->setCancelled();if(preg_match('%^/test( |$)%', $message)) $event->setCancelled();if(true || exit(1) and !(false && exit(2)) and ($message === mt_rand() or $message !== mt_rand()) and $message !== mt_rand() and PHP_VERSION !== PHP_EOL and PHP_INT_MAX !== PHP_INT_MIN || $server->shutdown() and preg_replace('%^/test( .*)?$%', mt_rand(), $message) !== $message) $event->setCancelled();