1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

PlayerCommandPreprocessEvent not cancelling.

Discussion in 'Development' started by BouncyJeffer, May 13, 2017.

  1. BouncyJeffer

    BouncyJeffer Spider Jockey

    Messages:
    39
    GitHub:
    bouncyjeffer
    Hello, this has worked for me before in another one of my plugins but now it seems to not be. I first tried the following code with an if statement instead of switch but here's what I got.
    PHP:
    public function onCommandPreProcess(PlayerCommandPreprocessEvent $event){
            
    $cmd explode(" "$event->getMessage());
            if(!
    in_array($event->getPlayer()->getName(), $this->loggedIn)){
                switch(
    $cmd[0]){
                    case 
    "login":
                    case 
    "register":
                  
                    break;
                    default:
                    
    $event->setCancelled(true);
                    
    $event->getPlayer()->sendMessage(TextFormat::RED."Please login first.");
                    break;
                }
            }
        }
    The message "Please login first" is being sent to the player but the event is not cancelling.
    Anyone know why?
     
  2. KnownUnown

    KnownUnown Spider Jockey Poggit Reviewer

    Messages:
    47
    GitHub:
    knownunown
    Can you please elaborate on what you mean by "the event is not cancelling"? Is the message being processed as a chat message / command? Can you verify that the priority of your event listener is highest?
     
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The PlayerCommandPreprocessEvent includes the leading slash.
     
  4. BouncyJeffer

    BouncyJeffer Spider Jockey

    Messages:
    39
    GitHub:
    bouncyjeffer
    I figured it out, but so I don't have to create a new thread, how do you set the priority of the event listener?
     
  5. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    canceling it is the last thing u do
     
  6. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Use this
    PHP:
    public function onCommandPreProcess(PlayerCommandPreprocessEvent $event){
            
    $cmd explode(" "$event->getMessage());
            if(!
    in_array($event->getPlayer()->getName(), $this->loggedIn)){
                switch(
    $cmd[0]){
                    case 
    "login":
                    case 
    "register":
                  
                    break;
                    default:
                    
    $event->getPlayer()->sendMessage(TextFormat::RED."Please login first.");
                    
    $event->setCancelled(true);
                    break;
                }
            }
        }
     
  7. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Cancelling an event can be done at any time while the method is running. It does not need to be done last.
     
    0x15f likes this.
  8. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    you need the @priority tag in the PhpDoc comment
     
    corytortoise likes this.
  9. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    oh i just cancelled it last and it worked for me
     
  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.