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

$config->set() dont work in function

Discussion in 'Development' started by Kyd, Apr 16, 2017.

  1. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    PHP:
    public function setUpMessages(Player $p$test){
        
    $cfg = new Config($this->getDataFolder() . "/playerdata/" $p->getName() . ".json"Config::JSON);
        switch(
    $lang){
            case 
    "test";
            
    $cfg->set("rgergeefe""jnefjefj");
            
    $cfg->save();
            
    $this->getLogger()->info("Ok");
        }

    public function 
    onJoin(PJE $e){
      
    $cfg = new Config($this->getDataFolder() . "/playerdata/" $p->getName() . ".json"Config::JSON);
    $p $e->getPlayer();
    $this->setUpMessages($p"test");
    }
    }
    Console write Ok,but dont set to config
     
    Last edited: Apr 16, 2017
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    config->save
     
  3. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    I have
    PHP:
    case "test";
            
    $cfg->set("rgergeefe""jnefjefj");
            
    $cfg->save(); //<----
            
    $this->getLogger()->info("Ok");
     
  4. Jonas

    Jonas Baby Zombie

    Messages:
    192
    I think you have forgot “$cfg->get();“
    PHP:
    case "test";
            
    $cfg->get("rgergeefe");
            
    $cfg->set("rgergeefe""jnefjefj");
            
    $cfg->save(); //<----
            
    $this->getLogger()->info("Ok");
     
    Last edited: Apr 16, 2017
  5. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Not sure if that's the cause, but you're using switch() on an undefined variable?

    That's nonsense. You use $configuration->get() to get the value of something in the config. Not to... initialize it or something.
     
  6. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    :facepalm:
     
  7. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Well said
     
  8. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Ok switch now using on defined variable, but not add
    PHP:
    public function setUpMessages(Player $p$test){
        
    $cfg = new Config($this->getDataFolder() . "/playerdata/" $p->getName() . ".json"Config::JSON);
        switch(
    $test){
            case 
    "test";
            
    $cfg->set("rgergeefe""jnefjefj");
            
    $cfg->save();
            
    $this->getLogger()->info("Ok");
        }
     
  9. Jonas

    Jonas Baby Zombie

    Messages:
    192
    When i use configs , i make always $cfg->get(); and it comes no errors.
     
  10. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    $cfg->get() is for getting something from config,..
     
  11. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Because it returns null, or an empty string perhaps. It doesn't do anything, it returns a value from the configuration. If it doesn't find it, it returns something empty.
     
  12. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    I tried without switch and always not work too.
    PHP:
    public function setUpMessages(Player $p$test){
        
    $cfg = new Config($this->getDataFolder() . "/playerdata/" $p->getName() . ".json"Config::JSON);
            
    $cfg->set("rgergeefe""jnefjefj");
            
    $cfg->save();
            
    $this->getLogger()->info("Ok");
        }
     
  13. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Could I just ask if you perhaps forgot to make the folders this config file should be in? Also, the first / before playerdata should be removed.
     
  14. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Lol, I tried it in PlayerChatEvent and it is working :v
     
  15. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    No I do not forgoten I have onEnable
    @mkdir($this->getDataFolder() . "playerdata");
     
  16. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    I must use PlayerLoginEvent?
    EDIT: I used PlayerLoginEvent and it working.. If there is not some way to use it in playerjoinevent i use this.
     
  17. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    in onJoin() $p is undefined while setting the config file
     
  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.