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

How to excute command without permission?

Discussion in 'Development' started by chansol, Jun 20, 2017.

  1. chansol

    chansol Spider Jockey

    Messages:
    32
    GitHub:
    ParkChanSol
    Player "a" doesn't have "pocketmine.command.give"
    I want to "a" can use /give command.
    So, I make plugin.
    PHP:
    public function onCommandProcessEvent(PlayerCommandPreprocessEvent $ev) {
            
    $player $ev->getPlayer();
            
    $msg $ev->getMessage();
            
    $words explode(" "$msg);
            
    $cmd strtolower(substr(array_shift($words), 1));
            if (
    $cmd === "give") {
                
    $this->getServer()->dispatchCommand($player$msg);
            }
        }
    Is this right?
     
    Last edited by a moderator: Jun 20, 2017
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    What originally happens:
    1. Player sends the command
    2. PocketMine fires PlayerCommandPreprocessEvent
    3. If event is not cancelled, PocketMine executes the command.

    What happens with your plugin added:
    1. Player sends the command
    2. PocketMine fires PlayerCommandPreprocessEvent
    2.a) Your plugin finds that the command is /give
    2.b) Your plugin calls dispatchComman() with /give
    3. The event is not cancelled, so PocketMine executes the command again.

    If you simply want to add a permission to the player, you may use permission attachments. Here is a basic example for managing permissions: https://github.com/SOF3/simple-plug...lePermissions/SimplePermissions.php#L173-L203 https://github.com/SOF3/simple-plug.../SimplePermissions/SimplePermissions.php#L135 https://github.com/SOF3/simple-plug.../SimplePermissions/SimplePermissions.php#L160

    Also note that PlayerCommandPreprocessEvent is fired for all chat messages + commands sent to the player, so simply checking whether the command matches "?give *" doesn't guarantee that it is a command -- a chat message "ogive is a free-hand graph showing the curve of a cumulative distribution function" will also be executed as the command.
    In addition, $msg contains the leading slash, while dispatchCommand() parameter 2 does not require the leading slash (just like console commands).
     
    HimbeersaftLP and falk like this.
  3. chansol

    chansol Spider Jockey

    Messages:
    32
    GitHub:
    ParkChanSol
    Thanks
     
  4. chansol

    chansol Spider Jockey

    Messages:
    32
    GitHub:
    ParkChanSol
    I make plugin.

    PHP:
    public function onCommandProcessEvent(PlayerCommandPreprocessEvent $ev) {
            
    $player $ev->getPlayer();
            
    $msg $ev->getMessage();
       
            
    $words explode(" "$msg);
       
            
    $cmd strtolower(substr(array_shift($words), 1));
            if (
    $this->isCmd($cmd)) {
                if (
    $this->isPolice(strtolower($player->getName()))) {
       
                    
    $ev->setCancelled(true);
       
                    
    $message substr($msg1);
       
                    
    $per $this->getServer()->getCommandMap()->getCommand($cmd)->getPermission();
                    
    $ev->getPlayer()->addAttachment($this)->setPermission($pertrue);
                    
    $name $ev->getPlayer()->getName();
                    
    $this->getServer()->dispatchCommand($player$message);
                    
    $this->getServer()->getPlayerExact($name)->addAttachment($this)->setPermission($perfalse);
                }
            }
        }
      }
    When I tested, command's usage doesn't appear.
    What is wrong?
    PHP:
    public function isCmd($command) {
            return 
    $this->cmd->exists($command);
        }
     
    Last edited by a moderator: Jun 24, 2017
  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.