How can I execute a command in onEnable function? For example I want to execute the command "/ban" in onEnable
Do not execute commands as a command sender. That's bad practice. You can use $this->getServer()->getBansByName()
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 }}
Executing commands by console is slower because string has to parsed again. You should instead run the API methods directly, which is faster.