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

Enable a disabled event by a command

Discussion in 'Development' started by MalakasPlayzMCPE, Feb 3, 2018.

  1. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    By default a player has disabled the BlockBreakEvent:
    PHP:
        public function onBreak(BlockBreakEvent $event) {
            
    $name $event->getPlayer()->getName();
            if(!
    in_array($name$this->buildmode)) {
                
                
    $event->setCancelled();
                
            }
        }
    I tried to make the event to enable and disable with a command but my code doesn't work:

    PHP:
        public function onCommand(CommandSender $senderCommand $commandstring $label, array $args) : bool{
            
    $name $sender->getName();
            
    $this->buildmode[] = $name;

            
            if (
    $cmd->getName() == "build" && $sender->hasPermission("permission.build")) {
                if (!
    in_array($name$this->buildmode)) {
                    
                    
    $sender->sendMessage($this->prefix TextFormat::GREEN "You can build now.");
                    
                } else {
                    
                    unset(
    $this->buildmode[array_search($name$this->buildmode)]);
                    
                    
    $sender->sendMessage($this->prefix TextFormat::RED "You can't build anymore.");
                    
                   }
              }
         }
    }
     
  2. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    67
    GitHub:
    xZeroMCPE
    Be more specific on 'what does not work.

    But anyhow, you have

    Code:
    this->buildmode[] = $name;
    inside the onCommand (very first beginning), Which means that the player name would always be in the array, so checking it has no point.
     
    Last edited: Feb 4, 2018
    xXNiceAssassinlo YT and SOFe like this.
  3. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    I made it better. Forgot to update the $cmd to $command (from the ALPHA7 and higher you need to change that). Now how to make the event to get enabled?

    PHP:
        public function onCommand(CommandSender $senderCommand $commandstring $label, array $args) : bool{
            
    $name $sender->getName();
            
    $this->buildmode[] = $name;

           
            if (
    $command->getName() == "build" && $sender->hasPermission("permission.build")) {
                if (!
    in_array($name$this->buildmode)) {
                
    $sender->setGamemode(1);
                
    $sender->sendMessage($this->prefix TextFormat::GREEN "You can build now.");
               
                } else {
               
                    unset(
    $this->buildmode[array_search($name$this->buildmode)]);
               
                    
    $sender->sendMessage($this->prefix TextFormat::RED "You can't build anymore.");
                   
              }
           }
        }
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You don't need to :facepalm:
     
  5. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Bump!
     
  6. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    PHP:
    $event->setCancelled(false);
    That's wrong. Since PocketMine API 3.0.0-ALPHA7 you need to add a bool typehint to your onCommand() method. You can set the parameter variable names to anything you want.

    This is also valid:
    PHP:
    public function onCommand(CommandSender $commandSenderCommand $commandRanstring $commandLabel, array $commandArguments) : bool{
    Keep an eye out for updates to the CommandExecutor interface and you will not get in problems.
     
    Last edited: Feb 18, 2018
    xXNiceAssassinlo YT likes this.
  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.