PlayerCommandPreprocessEvent help

Discussion in 'Development' started by Zade, Sep 10, 2019 at 4:25 AM.

  1. Zade

    Zade Spider

    Messages:
    10
    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?
    PHP:
    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'];
           
            if(
    strpos($event->getMessage(), '/spectate') !== false$commandCheck true;
            if (!isset(
    $this->plugin->spectators[$player->getName()])) return;
            if (
    $str[0] != '/') {
                return;
            }

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

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.