Remove an array from config

Discussion in 'Development' started by InspectorGadget, May 7, 2017.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi, how do i remove an array from config?

    The problem is that, the name can't be found in the config.

    Thanks

    PHP:
    <?php

    /*
     * Copyright (C) 2017 RTG
     *
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */

    namespace RTG\WorldBorder;

    use 
    pocketmine\Server;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;

    class 
    Loader extends PluginBase implements Listener {
     
        public 
    $config;
        public 
    $save = array();
     
        public function 
    onEnable() {
            
    parent::onEnable();
            
    $this->config = new \pocketmine\utils\Config($this->getDataFolder() . "config.yml", \pocketmine\utils\Config::YAML, array(
                
    "worlds" => array()
            ));
        }
     
        public function 
    onAdd(\pocketmine\Player $p$name$blocks) {
         
            
    $this->config->set($name, array('blocks' => $blocks));
            
    $this->config->save();
         
        }
     
        public function 
    onRM($name) {
         
            if (
    $this->config->get($name)) {
                echo 
    'Found!';
            }

        }
     
        public function 
    onCommand(\pocketmine\command\CommandSender $sender, \pocketmine\command\Command $command$label, array $args) {
         
            switch (
    strtolower($command->getName())) {
             
                case 
    "add":
                 
                    
    $this->onAdd($sender"world""6");
                    
    $sender->sendMessage("added!");
                 
                    return 
    true;
                break;
         
                case 
    "rm":
                 
                    
    $this->onRM("world");
                    
    $sender->sendMessage("Removed!");
                 
                    return 
    true;
                break;
             
            }
         
        }
     
        public function 
    onDisable() {
            
    parent::onDisable();
        }
     
    }
     
    Last edited: May 8, 2017
  2. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
  3. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    You probably mean this:
    PHP:
    $file $this->config;
               
    $data $file->get("Your Data", []);
               
    $id array_search($YourObject$data);
               unset(
    $data[$id]);
               
    $file->set("Your Data"$data);
               
    $file->save();
               
    $file->reload();
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    show us your config then?
     
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    I've updated my code, i want to know how to set arrays to worlds and remove:

    Config shd be like this:
    Code:
    worlds:
    level:
        blocks: 7
    newlevel:
        blocks: 5
    
    
     
  6. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Maybe this:
    PHP:
     $array $this->config->get("worlds");
    $new $YourNewStuff;
    array_push($array$new);
    $this->config->set("worlds"$array);
    $thie->config->save();
    // This should work when adding something to worlds array
    // Haven't tested it but it should work
    :)
     
  7. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Let say that i define $new

    PHP:
    $new how to define like the config?
     
  8. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Define $data[$name] first then define $new, else it may cause errors
     
  9. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    code and expected result mismatch please make sure both the code and the config is produced in the same version...
    expecting
    world:
    - 6
     
  10. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    PHP:
    $data = array("New world" => array("blocks" => $id));
    $new $data
     
  11. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    I'll try that
     
  12. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    @Az928 Hey, how can i get the specific world name?

    My code for the add thing:

    PHP:
            public function onAdd($name$blocks) {

                
    $array $this->config->get("worlds");
              
                
    $new = array($name => array("blocks" => $blocks));
                
    $data $new;
                
    array_push($array$data);
                
    $this->config->set("worlds"$array);
                
    $this->config->save();
              
            }
    Config:

    Code:
    ---
    worldborder: true
    message: This is end of world
    worlds:
    - City:
        blocks: "2000"
    - City:
        blocks: "2000"
    ...
    
     
  13. MioTaku

    MioTaku Witch

    Messages:
    69
    GitHub:
    uselesswaifu
    your over thinking it just do
    Code:
    // GET THE INFO OF CERTAIN CONFIG LOCATION
    $config->get("worlds")["City"]["blocks"];
    
    // ADD A 'array' to config
    $config->setNested("worlds.City.blocks", $array);
    
     
    Last edited: May 9, 2017
  14. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    I get this error:

    Code:
    Notice: Undefined index: city in C:\Users\RTG\Desktop\TesterUnit\plugins\plugin-beta-worldborder\src\WorldBorder\WorldBorder.php on line 118
    
    Code:
    PHP:
            public function onRM($name) {
              
                
    $name strtolower($name);
                if (
    $this->config->get("worlds")[$name]) { // Line 118
                    
    echo "Found";
                }
              
            }
     
  15. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,490
    GitHub:
    jasonwynn10
    PHP:
            public function onRM(string $name) {
             
                
    //$name = strtolower($name); #config is CaSe sensitive
                
    var_dump($this->config->getAll()); // what's actually in the config?
                
    if (($worlds $this->config->get("worlds"null)) != null and array_key_exists($name$worlds)) { // Line 118
                    
    var_dump($worlds[$name]);
                    echo 
    "\n\nFound!\n";
                }
             
            }
     
    Last edited: May 10, 2017
  16. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Still the same though...
     
  17. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,490
    GitHub:
    jasonwynn10
    Is the error different?
     
  18. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Nope, nothing at all..
     
  19. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi this is my current state:

    PHP:

    <?php

    /* 
     * Copyright (C) 2017 RTG
     *
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */

    namespace RTG\WorldBorder;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\Config;

    class 
    Main extends PluginBase {
       
        public 
    $config;
       
        public function 
    onEnable() {
           
            
    $this->config = new Config($this->getDataFolder() . "config.yml"Config::YAML, array('worlds' => array()));
            
    $this->onAdd("world"'1');
            
    print_r($this->config);
            
    $this->onRM("world");
           
        }
       
        public function 
    onAdd($name$blocks) {

                
    $array $this->config->get("worlds");
               
                
    $name strtolower($name);
                
    $new = array($name => array("blocks" => $blocks));
                
    $data $new;
                
    array_push($array$data);
                
    $this->config->set("worlds"$array);
                
    $this->config->save();
                 
        }
       
        public function 
    onRM($name) {
           
            
    $array $this->config->get("worlds");
            unset(
    $array[array_search($name$array)]);
            
    $this->config->set("worlds"$array);
            
    $this->config->save();
           
        }
    }
    Last config state:

    Code:
    ---
    worlds:
      1:
        world:
          blocks: "1"
      2:
        world:
          blocks: "1"
      3:
        world:
          blocks: "1"
    ...
    
    Print_r() Error:
    Code:
    pocketmine\utils\Config Object
    (
        [config:pocketmine\utils\Config:private] => Array
            (
                [worlds] => Array
                    (
                        [1] => Array
                            (
                                [world] => Array
                                    (
                                        [blocks] => 1
                                    )
    
                            )
    
                        [2] => Array
                            (
                                [world] => Array
                                    (
                                        [blocks] => 1
                                    )
    
                            )
    
                        [3] => Array
                            (
                                [world] => Array
                                    (
                                        [blocks] => 1
                                    )
    
                            )
    
                        [4] => Array
                            (
                                [world] => Array
                                    (
                                        [blocks] => 1
                                    )
    
                            )
    
                    )
    
            )
    
        [nestedCache:pocketmine\utils\Config:private] => Array
            (
            )
    
        [file:pocketmine\utils\Config:private] => C:\Users\RTG\Desktop\TesterUnit\plugins\Test/config.yml
        [correct:pocketmine\utils\Config:private] => 1
        [type:pocketmine\utils\Config:private] => 2
        [jsonOptions:pocketmine\utils\Config:private] => 130
    )
    
     
  20. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Can you post the entire source code on github? It'll be much easier for people to help than trying to figure the code you are running from the extracts above.
     

Share This Page

  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.