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

Unregister default commands

Discussion in 'Development' started by Kyd, Mar 25, 2017.

  1. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    It do not unregister.
     
  2. [deleted]

    [deleted] Guest

    I just tried it. It doesnt work. My testserver has just crashed.
     
  3. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Lol My server do not crash but it do not unregister command
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    This doesn't unregister it. This only blocks its execution and executes something else when it is executed.
     
    corytortoise, Muqsit and [deleted] like this.
  5. NIIP99

    NIIP99 Silverfish

    Messages:
    22
    GitHub:
    niip99
    Yep.
    I'm just not sure if Command->unregister() really removes the Command. Cuz last time I tried unregistering, the command seems like does not registered.
     
    MagicLJ likes this.
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PlayerCommandPreprocessEvent is a hacky method to deal with this. You basically can't unregister it. The closest to unregister is overwriting the command that returns "Unknown command. Type /help for a list of commands." message in red.
    And also, hide it from "/" commands list by sending this packet to every individual.
    PHP:
    $pk = new AvailableCommandsPacket();
    $data = new \stdClass();
    $pk->commands "";
    foreach(
    Server::getInstance()->getCommandMap()->getCommands() as $command){
        if (
    strtolower($command->getName()) !== "cmd2remove") {
            
    $data->{$command->getName()}->versions[0] = $command->generateCustomCommandData($player);
        }
    }
    $pk->commands json_encode($data);

    $player->dataPacket($pk);
     
    Last edited: Apr 4, 2017
  7. [deleted]

    [deleted] Guest

    Will this remove it?
     
  8. NIIP99

    NIIP99 Silverfish

    Messages:
    22
    GitHub:
    niip99
    This is a hack to hide command from showing at client side when player typed "/"
     
    MagicLJ and Muqsit like this.
  9. [deleted]

    [deleted] Guest

    Ah okay
     
  10. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Thank you @Muqsit. This code do what i want.
     
  11. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Now What I block when Player do ./command instead /?
    I tried this in PlayerCommandPreprocess, but bad
    PHP:
    }elseif($cmd[0] == "./test"){
    $e->setCancelled();
                }
     
  12. Keith

    Keith Spider Jockey

    Messages:
    42
    GitHub:
    k3ithos
    I believe this will work with ./ commands too
    PHP:
    public function checkCmd(PlayerCommandPreprocessEvent $event) {
        
    $command explode(" "strtolower($event->getMessage()));
        if(
    $command[0] === "/test") {
            
    $event->setCancelled();
        }
    }
     
    Muqsit likes this.
  13. NIIP99

    NIIP99 Silverfish

    Messages:
    22
    GitHub:
    niip99
    substr() should be used instead of explode() as you just needed to retrieve command. substr() is always better if you don't want to handle command arguments
     
    MagicLJ, Keith and Muqsit like this.
  14. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    But, in this case:
    PHP:
    public function checkCmd(PlayerCommandPreprocessEvent $event) {
        
    $command explode(" "strtolower($event->getMessage()));
        if(
    $command[0] === "/test") {
            
    $event->setCancelled();
        }
    }

    //$event->getMessage(): /test arguments
    //$command = [0 => "/test", 1 => "arguments"];
    //$command[0] = "/test"
    Substr would be better but it must be a little, not the way you wanted it.
    PHP:
    public function checkCmd(PlayerCommandPreprocessEvent $event) {
        
    $command substr($event->getMessage(), -(strlen("/test")));
        if(
    $command === "/test") {
            
    $event->setCancelled();
        }
    }
     
  15. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The subject to test for should be "/test " to avoid detecting other commands starting with "test".

    Ladies and gentlemen, let me add fuel to the fire and introduce the brand new old strstr function.
    PHP:
    if($message === "/test" or strstr($message" "true) === "/test"$event->setCancelled();
    Roses are red, violets are blue, sugar is sweet, and the world is bigger than that expected by you. There are myriad ways to do the same thing, of course:

    PHP:
    if($message === "/test" or strlen($message) >= and strpos($message"/test ") === 0$event->setCancelled();

    if(
    $message{0} === "/" and $message{1} === "t" and $message{2} === "e" and $message{3} === "s" and
        
    $message{4} === "t" and !isset($message{5}) || $message{5} === " "$event->setCancelled();

    if(
    preg_match('%^/test( |$)%'$message)) $event->setCancelled();

    if(
    true || exit(1) and !(false && exit(2)) and
        (
    $message === mt_rand() or $message !== mt_rand()) and
        
    $message !== mt_rand() and PHP_VERSION !== PHP_EOL and
        
    PHP_INT_MAX !== PHP_INT_MIN || $server->shutdown() and
        
    preg_replace('%^/test( .*)?$%'mt_rand(), $message) !== $message$event->setCancelled();
     
  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.