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

Plugin not working

Comments in 'Plugin Development' started by PEMapModder, Feb 9, 2014.

  1. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Look at this code:
    PHP:
    <?php

    /*
    __PocketMine Plugin__
    author=PEMapModder
    name=GitBackup
    description=Needs Git be installed in the computer
    apiversion=10,11,12
    version=ALPHA
    class=BackupGitPlugin
    */
    //Alpha_1.3.10 or above for LevelAPI::getAll()
    class BackupGitPlugin implements Plugin{
        public 
    $datcfg$dat;
        public function 
    __construct(ServerAPI $api$s=0){}
        public function 
    __destruct(){
            
    $this->datcfg->setAll($this->dat);
        }
        public function 
    init(){
            
    $a=ServerAPI::request()->api;
            
    $p=$a->plugin;
            
    $c=$a->console;
            
    $path=$p->configPath($this)."memory";
            if(
    file_exists($path)){
                
    $this->datcfg=new Config($pathCONFIG_YAML);
                
    $this->dat=$this->datcfg->getAll();
            }
            else{
                
    $this->dat=array(
                    
    "inited"=>false,
                    
    "remotes"=>array()
                );
                
    $this->datcfg=new Config($pathCONFIG_YAML$this->dat);
            }
            
    $c->register("git""<subcmd> [args] Git backup (console only)", array($this"cmd"));
            if(!
    $this->dat["inited"]===false)
                
    exec("git init");
            @
    mkdir(FILE_PATH."git_worlds/");
        }
        public function 
    cmd($c$a$i){
            if(
    $i!=="console")
                return 
    "Please run this command on-console.";
            switch(
    strtolower($a[0])){
                case 
    "remote":
                    if(
    $a===array("remote")){
                        
    $ret="Remote list:\n";
                        foreach(
    $this->dat["remotes"] as $name=>$link){
                            
    $ret.="    $name$link";
                        }
                        return 
    $ret;
                    }
                    if(
    count($a)!==3)
                        return 
    "Usage: /git remote <name> <HTTP/SSH>";
                    
    $this->dat["remotes"][$a[1]]=$a[2];
                    break;
                case 
    "addworlds":
                    
    console("[NOTICE] Creating worlds backup...");
                    foreach(
    ServerAPI::request()->api->level->getAll() as $level){
                        
    $out=FILE_PATH."git_worlds/".$level->getName()."/";
                        
    $in=FILE_PATH."worlds/".$level->getName()."/";
                        
    $inDir=dir($in);
                        @
    unlink($out);
                        @
    mkdir($out);
                        while(
    false!==($filename=$inDir->read())){
                            if(
    is_file($in.$filename)){
                                
    file_put_contents($out.$filenamefile_get_contents($in.$filename));
                            }
                        }
                        
    $inDir->close();
                        
    $out.="chunks/";
                        
    $in.="chunks/";
                        
    $inDir=dir($in);
                        while(
    false!==($filename=$inDir->read())){
                            if(
    is_file($in.$filename)){
                                
    file_put_contents($out.$filenamefile_get_contents($in.$filename));
                            }
                        }
                        
    console(FORMAT_GREEN."[DEBUG] Copied level ".$level->getName());
                    }
                    
    console("[NOTICE] Finished creating world backup");
                    
    $msg="";
                    
    exec("git add git_worlds/*"$msg);
                    
    console($msg);
                    
    exec("git commit -m \"".(isset($a[1]) and $a[1]!=""?$a[1]:"world adding")."\""$msg);
                    
    console($msg);
                    break;
                case 
    "addplugins":
                    
    $this->duplicateFolder("plugins/");
                case 
    "push":
                    if(
    count($a!=2))
                        return 
    "Usage: /git push <remote name>";
                    if(!isset(
    $this->dat["remotes"][$a[1]]))
                        return 
    "Remote ".$a[1]." not found";
                    
    $path=$p->configPath($this)."temp";
                    
    file_put_contents($path"");
                    
    $subpath=trim($pathFILE_PATH);
                    
    exec("git push -u \"".$this->dat["remotes"][$a[1]]."\" master > /$subpath &");
                    return 
    "Pushing worlds. Result at $path.";
                default:
                    return 
    "Command /git ".$a[0]."doesn't exist.";
            }
        }
        public function 
    duplicateFolder($inSubpath){
            
    $inSubpath=trim($inSubpathFILE_PATH);
            if(
    substr($inSubpath, -1)!=="/")$inSubpath.="/";
            
    $out=FILE_PATH."git_".$inSubpath;
            if(!
    is_dir($inSubpath))
                return 
    false;
            
    $inDir=dir(FILE_PATH.$inSubpath);
            while(
    false!==($filename=$inDir->read())){
                if(
    is_file($in.$filename))
                    
    file_put_contents($out.$filenamefile_get_contents($in.$filename));
                elseif(
    strpos($filename".")===false){
                    
    $this->duplicateFolder($in.$filename);
                }
            }
            
    $inDir->close();
            return 
    true;
        }
    }
    There are no crashes, but the code does not seem to be working...
  2. kgdwhsk
    Offline

    kgdwhsk New Member Plugin Developer

    Joined:
    Aug 23, 2013
    Posts:
    13
    Plugins:
    1
    Code:
    <?php
    
    /*
    __PocketMine Plugin__
    author=PEMapModder
    name=GitBackup
    description=Needs Git be installed in the computer
    apiversion=10,11,12
    version=ALPHA
    class=BackupGitPlugin
    */
    //Alpha_1.3.10 or above for LevelAPI::getAll()
    class BackupGitPlugin implements Plugin{
        public $datcfg, $dat;
        public function __construct(ServerAPI $api, $s=0){}
        public function __destruct(){
            $this->datcfg->setAll($this->dat);
        }
        public function init(){
            $a=ServerAPI::request()->api;
            $p=$a->plugin;
            $c=$a->console;
            $path=$p->configPath($this)."memory";
            if(file_exists($path)){
                $this->datcfg=new Config($path, CONFIG_YAML);
                $this->dat=$this->datcfg->getAll();
            }
            else{
                $this->dat=array(
                    "inited"=>false,
                    "remotes"=>array()
                );
                $this->datcfg=new Config($path, CONFIG_YAML, $this->dat);
            }
            $c->register("git", "<subcmd> [args] Git backup (console only)", array($this, "cmd"));
            if(!$this->dat["inited"]===false)
                exec("git init");
            @mkdir(FILE_PATH."git_worlds/");
        }
        public function cmd($c, $a, $i){
            if($i!=="console")
                return "Please run this command on-console.";
            if(isset($a[0])){
                switch(strtolower($a[0])){
                    case "remote":
                        if($a===array("remote")){
                            $ret="Remote list:\n";
                            foreach($this->dat["remotes"] as $name=>$link){
                                $ret.="    $name: $link";
                            }
                            return $ret;
                        }
                        if(count($a)!==3)
                            return "Usage: /git remote <name> <HTTP/SSH>";
                        $this->dat["remotes"][$a[1]]=$a[2];
                        break;
                    case "addworlds":
                        console("[NOTICE] Creating worlds backup...");
                        foreach(ServerAPI::request()->api->level->getAll() as $level){
                            $out=FILE_PATH."git_worlds/".$level->getName()."/";
                            $in=FILE_PATH."worlds/".$level->getName()."/";
                            $inDir=dir($in);
                            @unlink($out);
                            @mkdir($out);
                            while(false!==($filename=$inDir->read())){
                                if(is_file($in.$filename)){
                                    file_put_contents($out.$filename, file_get_contents($in.$filename));
                                }
                            }
                            $inDir->close();
                            $out.="chunks/";
                            $in.="chunks/";
                            $inDir=dir($in);
                            while(false!==($filename=$inDir->read())){
                                if(is_file($in.$filename)){
                                    file_put_contents($out.$filename, file_get_contents($in.$filename));
                                }
                            }
                            console(FORMAT_GREEN."[DEBUG] Copied level ".$level->getName());
                        }
                        console("[NOTICE] Finished creating world backup");
                        $msg="";
                        exec("git add git_worlds/*", $msg);
                        console($msg);
                        exec("git commit -m \"".(isset($a[1]) and $a[1]!=""?$a[1]:"world adding")."\"", $msg);
                        console($msg);
                        break;
                    case "addplugins":
                        $this->duplicateFolder("plugins/");
                    case "push":
                        if(count($a!=2))
                            return "Usage: /git push <remote name>";
                        if(!isset($this->dat["remotes"][$a[1]]))
                            return "Remote ".$a[1]." not found";
                        $path=$p->configPath($this)."temp";
                        file_put_contents($path, "");
                        $subpath=trim($path, FILE_PATH);
                        exec("git push -u \"".$this->dat["remotes"][$a[1]]."\" master > /$subpath &");
                        return "Pushing worlds. Result at $path.";
                    default:
                        return "Command /git ".$a[0]."doesn't exist.";
                }
            }else{
            return "You can ues remote,addworlds,addplugins,push.";
            }
        }
        public function duplicateFolder($inSubpath){
            $inSubpath=trim($inSubpath, FILE_PATH);
            if(substr($inSubpath, -1)!=="/")$inSubpath.="/";
            $out=FILE_PATH."git_".$inSubpath;
            if(!is_dir($inSubpath))
                return false;
            $inDir=dir(FILE_PATH.$inSubpath);
            while(false!==($filename=$inDir->read())){
                if(is_file($in.$filename))
                    file_put_contents($out.$filename, file_get_contents($in.$filename));
                elseif(strpos($filename, ".")===false){
                    $this->duplicateFolder($in.$filename);
                }
            }
            $inDir->close();
            return true;
        }
    }
    I think I can help you.:)
  3. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
  4. kgdwhsk
    Offline

    kgdwhsk New Member Plugin Developer

    Joined:
    Aug 23, 2013
    Posts:
    13
    Plugins:
    1
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
  6. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Nope only GitHub :(

Share This Page

Advertisement