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

[solved] YAML Data saving Failure?

Discussion in 'Development' started by jasonwynn10, Dec 16, 2016.

  1. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    I have a factions plugin that I am in the process of making for my server. Every var_dump I run gives the output I expect, but when it is sent to the actual file, only the default appears. Here is the Code, Please Help!
    PHP:
    <?php
    namespace FactionsPro\utils;

    use 
    FactionsPro\Faction;
    use 
    FactionsPro\FactionMain;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\utils\Config;

    class 
    YAMLDataProvider {

        private 
    $plugin;
        private 
    $file;
        private 
    $default = [
            
    "Name" => "Default",
            
    "Desc" => "Description not set",
            
    "MOTD" => "",
            
    "Home" => "None",
            
    "Leader" => "None",
            
    "Members" => [
                
    "None" => Faction::LEADER
            
    ]
        ];


        public function 
    __construct(FactionMain $plugin) {
            
    $this->plugin $plugin;
            
    $this->file = new Config($this->plugin->getDataFolder()."Factions.yml",Config::YAML,[
                
    "Factions" => [
                    
    "Default" => $this->default
                
    ]
            ]);
        }

        public function 
    save(Faction $f) : bool {
            if(
    $this->plugin->devModeEnabled()) {
                
    $this->plugin->getLogger()->info("Saving ".$f->getName()."... Stage 4");
            }
            
    $factions $this->file->getAll()["Factions"];
            if(
    $this->plugin->devModeEnabled()) {
                
    $this->plugin->getLogger()->info("Faction Dump before save");
                
    var_dump($factions);
            }
            if(
    $f->getHome() instanceof Position) {
                
    $home = [$f->getHome()->getX(),$f->getHome()->getX(),$f->getHome()->getX(),$f->getHome()->getLevel()->getName()];
            }else{
                
    $home null;
            }
            
    $factions[$f->getName()] = [
                
    "Name" => $f->getName(),
                
    "Desc" => $f->getDescription(),
                
    "MOTD" => $f->getMOTD(),
                
    "Home" => $home,
                
    "Leader" => $f->getLeader(),
                
    "Members" => $f->getMembers()
            ];
            
    $this->file->set("Factions",$factions);
            if(
    $this->plugin->devModeEnabled()) {
                
    $this->plugin->getLogger()->info("Faction Dump after save");
                
    var_dump($factions);
            }
            if(
    $this->file->getAll()["Factions"] == $factions) {
                return 
    true;
            }
            return 
    false;
        }

        public function 
    getFactionData(string $f) : array {
            return 
    $this->file->getNested("Factions.{$f}",$this->default);
        }

        public function 
    getFactionsData() : array {
            return 
    $this->file->get("Factions",[
                
    "Default" => $this->default
            
    ]);
        }

        public function 
    deleteFaction(Faction $f) {
            
    $factions $this->file->getAll()["Factions"];
            if(
    $this->plugin->devModeEnabled()) {
                
    $this->plugin->getLogger()->info("Faction Dump before delete:");
                
    var_dump($factions);
            }
            unset(
    $factions["{$f->getName()}"]);
            
    $this->file->set("Factions"$factions);
            if(
    $this->plugin->devModeEnabled()) {
                
    $this->plugin->getLogger()->info("Faction Dump after delete:");
                
    var_dump($factions);
            }
        }

    }
     
  2. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Can you define "gives the output I expect" and "only the default appears"?
     
    applqpak likes this.
  3. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    Lol I don't see a save function anywhere.
    $file->save()
     
    applqpak likes this.
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Shouldn't it be $this->file->save()?
     
    applqpak and LilCrispy2o9 like this.
  5. LilCrispy2o9

    LilCrispy2o9 Spider Jockey

    Messages:
    43
    GitHub:
    lilcrispy2o9
    Ye
    I wasnt being exact, just provided an example.
     
    applqpak and HimbeersaftLP like this.
  6. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    lol, figures that I miss the most simple thing...
     
    HimbeersaftLP and applqpak like 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.