PlayerCommandPreprocessEvent help

Discussion in 'Development' started by Zade, Sep 10, 2019.

  1. Zade

    Zade Spider

    I'm trying to make a function that blocks players from executing certain commands while they're spectating another player, but it doesn't seem to work. Any ideas?
    public function onCmd(PlayerCommandPreprocessEvent $event) {
    $player $event->getPlayer();
    $str str_split($event->getMessage());
    $command explode(' '$event->getMessage())[0];
    $commandCheck false;
    $allowed = ['/freeze''/msg''/ban''/unban''/kick'];
    strpos($event->getMessage(), '/spectate') !== false$commandCheck true;
            if (!isset(
    $this->plugin->spectators[$player->getName()])) return;
            if (
    $str[0] != '/') {

            if (!
    $commandCheck) {
                if (!
    $player->isOp()) {
                    if (
    in_array(strtolower($command), $allowed)) return;
    $player->sendMessage(TextFormat::RED 'You cannot use this command while spectating.');
            } else return;

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.