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

Load folder plugin without DevTools

Discussion in 'Development' started by imYannic, Dec 9, 2016.

  1. imYannic

    imYannic Baby Zombie

    Messages:
    113
    How can I load folder plugins without DevTools? When I use PluginManager::loadPlugin($path) it always returns NULL, although it returns the plugin resource when I use DevTools loadPlugin() function, but the problem is that the plugin doesn't get registeres and I don't know how, it never returns the plugin name when I do /plugins. So how can I load folder plugins or how can I register the plugin?
     
  2. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    Did you enable it also?
     
  3. imYannic

    imYannic Baby Zombie

    Messages:
    113
    I tried everything, $plugin->setEnabled(true); $this->setEnabled($plugin); (with the DevTools source code)
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    What about showing your actual code?
     
    applqpak likes this.
  5. imYannic

    imYannic Baby Zombie

    Messages:
    113
    PHP:
    <?php

    namespace Loader;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\plugin\PluginLoadOrder;
    use 
    pocketmine\event\plugin\PluginDisableEvent;
    use 
    pocketmine\event\plugin\PluginEnableEvent;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\plugin\PluginDescription;
    use 
    pocketmine\plugin\PluginLoader;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\MainLogger;
    use 
    pocketmine\utils\TextFormat;


    class 
    Main extends PluginBase{

    public function 
    onEnable(){

    $this->getServer()->getLogger()->info("Enabling Loader");


    $plugin $this->loadPlugin($this->getServer()->getDataPath()."TestPlugin");
    $this->enablePlugin($plugin);

    var_dump($plugin);


    }


    public function 
    enablePlugin(Plugin $plugin){
            if(
    $plugin instanceof PluginBase and !$plugin->isEnabled()){
                
    $this->getServer()->getLogger()->info("Enabling " $plugin->getDescription()->getFullName());
                
    $plugin->setEnabled(true);
                
    $this->getServer()->getInstance()->getPluginManager()->callEvent(new PluginEnableEvent($plugin));
            }
        }




    public function 
    loadPlugin($file){
            if(
    is_dir($file) and file_exists($file "/plugin.yml") and file_exists($file "/src/")){
                if((
    $description $this->getPluginDescription($file)) instanceof PluginDescription){
                    
    $this->getServer()->getLogger()->info(TextFormat::LIGHT_PURPLE."Loading source plugin " $description->getFullName()." by Loader plugin");
                    
    $dataFolder dirname($file) . DIRECTORY_SEPARATOR $description->getName();
                    if(
    file_exists($dataFolder) and !is_dir($dataFolder)){
                        
    trigger_error("Projected dataFolder '" $dataFolder "' for " $description->getName() . " exists and is not a directory"E_USER_WARNING);

                        return 
    null;
                    }


                    
    $className $description->getMain();
                    
    $this->getServer()->getLoader()->addPath($file "/src");

                    if(
    class_exists($classNametrue)){
                        
    $plugin = new $className();
                        
    $this->initPlugin($plugin$description$dataFolder$file);

                        return 
    $plugin;
                    }else{
                        
    trigger_error("Couldn't load source plugin " $description->getName() . ": main class not found"E_USER_WARNING);

                        return 
    null;
                    }
                }
            }

            return 
    null;
        }




    public function 
    getPluginDescription($file){
            if(
    is_dir($file) and file_exists($file "/plugin.yml")){
                
    $yaml = @file_get_contents($file "/plugin.yml");
                if(
    $yaml != ""){
                    return new 
    PluginDescription($yaml);
                }
            }
            return 
    null;
        }



    private function 
    initPlugin(PluginBase $pluginPluginDescription $description$dataFolder$file){
            
    $plugin->init($this->getPluginLoader(), $this->getServer(), $description$dataFolder$file);
            
    $plugin->onLoad();

        }



    }

     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    You are only loading the plugin internally. You are not adding the plugin to the PluginManager.
    And if you use PluginManager->loadPlugin(), it doesn't know to use this plugin of yours at all.
    You need to register your implementation of PluginLoader just like FolderPluginLoader in DevTools.
     
  7. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Ah thank you!
     
  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.