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

How to "$this->config->set".

Comments in 'Plugin Development' started by Angelo Vidrio, Jan 10, 2015.

  1. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    ...:confused:
  2. Mariachi
    Offline

    Mariachi New Member

    Joined:
    Oct 11, 2014
    Posts:
    23
    Minecraft User:
    Mariachi
  3. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    NOTE: Always remember to save your config whenever you make a change, or when the server shutdown xD
    For array inside a file, use:
    PHP:
    $this->getConfig()->getNested();
    Where you separate each key of the different arrays with a dot (.), Example:
    PHP:
    # Let's imagine that the array that you will work on is like:
    $Food = [
         
    "GoodFood" => [
              
    "Apple",
              
    "Orange"
         
    ], "JunkFood" => [
              
    "Hamburger",
              
    "Pizza"
         
    ]
    ];

    // Now you want to get all the "GoodFood" content, but what to use?

    # If you use the function 'get("Food")' you will get everything inside our '$Food' array, so the logic will tell you to do something like:
    $this->getConfig()->get("Food")["GoodFood"];

    #But if you use the function 'getNested("Food.GoodFood")' you will only get the content from the 'GoodFood' part:
    $this->getConfig()->getNested("Food.GoodFood");
    /**
     * Both options are OK, but I really, REALLY recommend the usage of 'getNested()', so to divide each key, use a DOT
     * Now to modify them, use the 'setNeted()' property, it works the same as 'getNested()', the only thing that changes is the value to set xD
     */
    iJoshuaHD likes this.
  4. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    We all learned from errors ;)
  5. Angelo Vidrio
    Offline

    Angelo Vidrio Active Member Plugin Developer

    Joined:
    Jan 16, 2014
    Posts:
    123
    Plugins:
    5
    Minecraft User:
    LilCrispy45
    So would I be able to get "Apple" by doing
    PHP:
    $this->getConfig()->getNested("Food.GoodFood.Apple");
  6. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    No, only use DOTS to separate the keys of a multi-dimensional array, what you say will return an error (probably xD)
  7. Angelo Vidrio
    Offline

    Angelo Vidrio Active Member Plugin Developer

    Joined:
    Jan 16, 2014
    Posts:
    123
    Plugins:
    5
    Minecraft User:
    LilCrispy45
    Ok then... so how would I get "Apple"?
  8. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    In this case, the most near result will be all the content of the GoodFood array, so you can make something like this to detect if Apple exist:
    PHP:
    $goodfood $this->getConfig()->getNested("Food.GoodFood");
    var_dump(isset($goodfood["Apple"]));

    # The 'var_dump()' function is to test things, so in this case, it will return 'true' or 'false' depending if "Apple" exist in the array
  9. Angelo Vidrio
    Offline

    Angelo Vidrio Active Member Plugin Developer

    Joined:
    Jan 16, 2014
    Posts:
    123
    Plugins:
    5
    Minecraft User:
    LilCrispy45
    What if I want to add another GoodFood, what would I do then?
  10. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,130
    Plugins:
    4
    Minecraft User:
    iksaku
    Use the 'setNested()' but instead of giving a string, give an array with all the values: the old ones and the new ones
  11. Angelo Vidrio
    Offline

    Angelo Vidrio Active Member Plugin Developer

    Joined:
    Jan 16, 2014
    Posts:
    123
    Plugins:
    5
    Minecraft User:
    LilCrispy45
    Ok so say when a players runs the command /goodfood add <foodname>(This is just an example), it would do
    PHP:
    $getOldFood $this->getConfig()->getNested("Food.GoodFood");
    $getNewFood $args[1];
    $this->getConfig()->setNested("Food.GoodFood")[
                      
    "$getOldFood",
                      
    "$getNewFood"
    ];
                       
      

Share This Page

Advertisement