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

add config to array

Discussion in 'Development' started by Levi, Jun 9, 2018.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    public $kills = [];
    COnfig
    Code:
    ---
    Steve:
      kills: 22
    ...
    how do i save that config to $kills[]? on onEnable()
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    You question isn't very clear, I could asnswe with some random code that will work, but maybe not actually is what you're looking for.

    So I'll ask:
    Do you plan saving other stuff than kills, or is the config already in that format and you want to parse it?

    What should $kills look like? Should it be like $kills["name"] returns the number?
     
    Levi likes this.
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    yes $kills['name'] and the kills they got
     
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    If the current method of saving is exactly like described, and the array has to be exactly like you said, that'll work:
    (assuming $config is an instance of Config)
    PHP:
    foreach ($config->getAll() as $name => $data) {
      
    $this->kills[$name] = $data["kills"];
    }
     
  5. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    thats exactly what i tried but it doesn't work no error
     
  6. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
  7. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    United Melon Pizza?
     
  8. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    bump*
     
  9. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    Can I see how you parse your config file?
     
  10. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    setNested(playername.".kills",$kills);
     
  11. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    I mean how is your $config variable defined?
     
  12. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    new Config(data folde) stuff
     
  13. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    Ok first show me the code. Try setting your $this->kills = $config->getAll() then var_dump($this->kills) and show the output.
     
  14. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    $kills is the player's kill im on my phone right now
     
  15. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    I made a config file exactly like this:
    Code:
    ---
    Steve:
      kills: 22
    Levi:
      kills: 2
    dktapps:
      kills: 1337
    ...
    Then used this exact code:
    PHP:
         public function onEnable(){
              
    $stuff $this->getConfig()->getAll();
              
    $kills = [];
              foreach (
    $stuff as $name => $data) {
                   
    $kills[$name] = $data["kills"];
              }
              
    var_dump($kills);
        }
    and the result was this console output:
    Code:
    array(3) {
      ["Steve"]=>
      int(22)
      ["Levi"]=>
      int(2)
      ["dktapps"]=>
      int(1337)
    }
    So there is either something different than how you explained it, or something in your setup doesn't work.

    Edit: Fixed code blocks
     
  16. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    what about if steve: 22?
     
  17. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Then $this->kills = $this->getConfig()->getAll() should be enough. Sorry for the late reply, I haven't been reading my alerts for a while.
     
  18. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    You can do it with yaml_parse_file, so you won't need to construct config object.
    PHP:
    $kills = array();
    $config yaml_parse_file($this->getDataFolder() . "kills.yml");
    foreach(
    $config as $player => $key){
           
    $kills[$player] = $key;
    }
     
  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.