Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Execute a command onEnable

Comments in 'Plugin Development' started by Adaasi, Nov 9, 2014.

  1. Adaasi
    Offline

    Adaasi Active Member

    Joined:
    Aug 14, 2014
    Posts:
    80
    How can I execute a command in onEnable function?
    For example I want to execute the command "/ban" in onEnable
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Do not execute commands as a command sender. That's bad practice. You can use $this->getServer()->getBansByName()
    ForgedPassport likes this.
  3. Adaasi
    Offline

    Adaasi Active Member

    Joined:
    Aug 14, 2014
    Posts:
    80
    The /ban command is only an example.
    I want to execute also plugin commands
  4. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Remember that the plugin might not be loaded. There are two possible approaches to fix this:
    • Add the plugin you want to use as a dependency in your plugin.yml.
      • Code:
        depend: ["PluginName"]
    • Delay the commands by 1 tick, so all the plugins have loaded when they are run.
      • PHP:
        $this->getServer()->getScheduler()->scheduleDelayedTask(new EnableTask($this), 1);
      • PHP:
        class EnableTask extends PluginTask{
          public function 
        onRun(){
            
        //Execute commands here
          
        }
        }
    EvolSoft likes this.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Executing commands by console is slower because string has to parsed again. You should instead run the API methods directly, which is faster.
    LDX and EvolSoft like this.

Share This Page

Advertisement