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

Unregister command

Discussion in 'Development' started by Kenn Fatt, Nov 26, 2016.

  1. Kenn Fatt

    Kenn Fatt Slime

    Messages:
    82
    GitHub:
    kennfatt
    Hello ppl!

    i have a configuration files to allow my plugin register selected commands.

    for example fly command, i make it to false (disable), see my config below :
    Code:
    fly-mode: false
    and this is my plugin code :
    PHP:
    public function registerCommands() {
    if (
    $this->getSetting("fly-mode") !== false$this->getCommand("fly")->setExecutor(new FlyCommand($this));
    }
    and after that, 'fly' command has been disabled, but player still can use 'fly' command with output "usage: /fly"

    my question is, how to prevent player from output message : usage /fly ?

    thanks
     
  2. Kenn Fatt

    Kenn Fatt Slime

    Messages:
    82
    GitHub:
    kennfatt
    i have an alternative way, i removed usage: /fly on plugin.yml config.
    is it the best way to solve my problem?
     
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Just add return true; at the end of your code of the command.
    Removing the usage is like the worst way, the command might not even show up in for example /help
     
  4. Kenn Fatt

    Kenn Fatt Slime

    Messages:
    82
    GitHub:
    kennfatt
    i've already return true; on every end of my command. but usage still appear
     
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    depends on usage
    if you want to fully disable it means no one can use it you should not register it
    if you wish to limit it without fall back to returning usage try sandertv method also add the specifyed check to prevent restricted user from using it
     
  6. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    well i dont really know then i dont have mention issue but my command listener is not as same as how you do it
    maybe return void aka return; will do it? no clue just assumptions
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Can you show all your code? You don't show other parts of your main plugin, or how you declared the /fly command in the first place, or whether getSettings() works.

    Assuming getSettings() works, and you declared the /fly command plugin.yml:
    Your /fly command is declared in plugin.yml, which would create an instance of PluginCommand that sets your plugin main as the executor. If you don't set the executor of the command (if your if-statement is false), it will remain using your plugin main as the executor.
    You mentioned that you put return true at the end of every command (actually you didn't define any commands! You only defined command executors!), but you most likely missed out the main class. It is a command executor too, and in this case the main class is used as the command executor if you did not register the commands.

    Therefore, the solution is as simple as adding this function in the main class:
    PHP:
    public function onCommand(CommandSender $senderCommand $c$l, array $a){
      
    $sender->sendMessage("This command is disabled!");
      return 
    true;
    }
     
    Sandertv likes this.
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    False! It will show up in /help.

    But this is not the right way of fixing problems. You cut the town gas supply from the beginning when there is a leakage, but this doesn't completely fix the problem of gas leakage!
     
    Sandertv likes this.
  9. Kenn Fatt

    Kenn Fatt Slime

    Messages:
    82
    GitHub:
    kennfatt
    My Main plugin and that code related to command i want to disabled :
    PHP:
      public function onEnable(){
        
    $this->getLogger()->info($this->"§l§8Var§4Craft §eCore§f enabled!");
        
    $this->getServer()->getPluginManager()->registerEvents(new EventListener($this),$this);
        
    $this->loadConfigs();
        
    $this->saveConfig();
        
    $this->registerCommands();
     }

      public function 
    registerCommands() {
        if (
    $this->getSetting("fly-mode") !== false$this->getCommand("fly")->setExecutor(new FlyCommand($this));
      }

      public function 
    loadConfigs() {
        
    $this->config = new cfg($this->getDataFolder() . "config.yml"cfg::YAML, [
          
    "fly-mode" => true
          
    ]);
      }
    here is FlyCommand class :
    PHP:
      public function __construct(a $own) {
        
    $this->own $own;
      }

      public function 
    onCommand(cs $pc $c$l, array $args) {
        if (
    $c->getName("fly")) {
         
    //code
         
    return true;
        }
       }
     
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The critical problem is that you didn't show where you declared the command.

    The best way to do so is to not declare the commands in plugin.yml, but truly register command classes instead of command executor classes.
     
    Jack Noordhuis 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.