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

Solved Overriding /kick

Discussion in 'Development' started by WinterBuild7074, Aug 4, 2017.

  1. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    How can I override the /kick command? I tried this, but it isn't working.

    PHP:
    public function onCommandPreprocess(PlayerCommandPreprocessEvent $event) {
            
    $msg $event->getMessage();
            switch(
    $msg) {
                case 
    "/kick":
                    
    $event->setCancelled(true);
                    
    $sender $event->getPlayer();
                    if(
    $sender->hasPermission("pocketmine.command.kick")) {
                        if(isset(
    $args[0])) {
                            if(isset(
    $args[1])) {
                                
    $player $this->getServer()->getPlayer($args[0]);
                                if(
    $player) {
                                    
    $reason array_splice($args199999);
                                    
    $reason_send implode(" "$reason);
                                    
    $player->kick(
                                        
    "§cKicked by Admin/Moderator." "\n" "§e" $reason_send,
                                        
    false
                                    
    );
                                } else {
                                    
    $sender->sendMessage("§cThat player is not online!");
                                }
                            } else {
                                
    $sender->sendMessage("§cUsage: /kick <player> <reason>");
                            }
                        } else {
                            
    $sender->sendMessage("§cUsage: /kick <player> <reason>");
                        }
                    } else {
                        
    $sender->sendMessage("§cYou do not have permission to use this command");
                    }
                    break;
            }
        }
     
  2. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    Try to explode() $msg and use it as $args[0]:

    PHP:
    $args explode(" "strtolower($event->getMessage()));
    if(
    $args[0] == "/kick") {
     
    //your code
    }
     
    gistrec likes this.
  3. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    I would personally unregister the command and register a custom kick command. This way seems hacky.
     
    Muqsit, gistrec, Kyd and 3 others like this.
  4. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
  5. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    Create a command class like normal and before registering it to the command map, run this:

    PHP:
    $commandMap $this->getServer()->getCommandMap();
    $command $commandMap->getCommand($name);
    $command->setLabel("kick_disabled");
    $command->unregister($commandMap);
     
    Teamblocket, gistrec and Marabou like this.
  6. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Do I really need to make a command class? I always register commands with:
    Code:
    onCommand(blablabla...)
     
  7. Rysieku

    Rysieku Spider Jockey

    Messages:
    34
    GitHub:
    rysieku
    Yes you have, otherwise command is not overriden.
     
    Slayer1K likes this.
  8. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Help?
    PHP:
    <?php

    namespace WinterBuild7074\KickWriter;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\CommandMap;
    use 
    pocketmine\utils\Config;

    class 
    Main extends PluginBase implements Listener {
       
        private 
    $kickCommand;
       
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->saveDefaultConfig();
           
            
    $name "kick";
            
    $commandMap $this->getServer()->getCommandMap();
            
    $command $commandMap->getCommand($name);
            
    $command->setLabel("kick_disabled");
            
    $command->unregister($commandMap);
           
            
    $this->kickCommand = new kickCommand($this);
            
    $commandMap->register("kick"$this->kickCommand);
        }
    }
    PHP:
    <?php
    namespace WinterBuild7074\KickWriter;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\command\PluginIdentifiableCommand;
    use 
    pocketmine\Player;

    class 
    kickCommand extends Command implements PluginIdentifiableCommand {
        private 
    $main;
       
        public function 
    __construct(Main $main){
            
    $this->main $main;
        }
       
        public function 
    execute(CommandSender $senderstring $label, array $args){
            
    $sender->sendMessage("§6Hello!");
        }
       
        public function 
    getPlugin(): Plugin{
            return 
    $this->main;
        }
    }
    Code:
    [Server thread/CRITICAL]: TypeError: "Return value of pocketmine\command\Command::getName() must be of the type string, null returned" (EXCEPTION) in "src/pocketmine/command/Command" at line 138
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    google
     
    jasonwynn10 likes this.
  10. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    The first paramater of CommandMap->register is actually the fallback prefix.

    For example, if the default /ban command was overriden by a plugin, i could run /pocketmine:ban to use it because pocketmine is the fallback prefix.

    You also need to call the parent constructor in the kick command class, like so
    PHP:
    parent::__construct("kick");
    // or with aliases
    parent::__construct("kick", [ "insert""alias""here"]);
     
  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.