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

Remove an array from config

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

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Here: https://github.com/RTGDaCoder/Test
     
  2. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Anyone willing to help? I'm stuck, I've no idea on what to do. I've tried many methods.
     
  3. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    probably just rework your scheme especially in PHP, dont just directly edit YML files
     
  4. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    After adding 3 worlds (with onRM commented out) the config.yml would look like this:

    Code:
    ---
    worlds:
    - world:
        blocks: "1"
    - world2:
        blocks: "1"
    - world3:
        blocks: "1"
    ...
    Which looks like this when you var_dump:

    Code:
    array(3) {
      [0]=>
      array(1) {
        ["world"]=>
        array(1) {
          ["blocks"]=>
          string(1) "1"
        }
      }
      [1]=>
      array(1) {
        ["world2"]=>
        array(1) {
          ["blocks"]=>
          string(1) "1"
        }
      }
      [2]=>
      array(1) {
        ["world3"]=>
        array(1) {
          ["blocks"]=>
          string(1) "1"
        }
      }
    }
    After running onRM on "world2" the config looks like this:

    Code:
    ---
    worlds:
      1:
        world2:
          blocks: "1"
      2:
        world3:
          blocks: "1"
    ...
    Why? Since array_search is searching the array for a value and returning the corresponding key, if $name in onRM is "world2" then it will always return false (because the value of each element is an array not a string), and then remove the element at index 0 :)

    I don't know what is planned here, but it seems unnecessary to use nested arrays. You could use a separate worlds.yml for this data and simplify things by removing the nesting. If you must, then I think you'll need to do something like this:

    Code:
        public function onRM($name) {
     
            $array = $this->config->get("worlds");
            foreach($array as $key => $value) {
                if (isset($value[$name])) unset($array[$key]);
            }
            $this->config->set("worlds", $array);
            $this->config->save();
     
        }
     
    Last edited: May 12, 2017
    SOFe and Sandertv like this.
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi, that worked but how would i get the "Blocks"?
     
  6. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Take a guess!
     
  7. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Took and it's working now :)
     
    Awzaw likes this.
  8. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    @InspectorGadget Use Config::getAll() and set that to a global var 'array' (accessed by $this->array) to check if something exists use isset($this->array[$myVar]); to remove something use unset($this->array[$myVar]); then when PluginBase::/onDisable() is called unlink the previous config file @unlink($pathToConfig); then do (new Config($pathToConfig, Config::YAML, $this->array)->save(); now you can remove objects or keys with ease and you dont have to check a file everytime.
     
  9. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Would that also work for
    Code:
    ---
    worlds:
    - City
      blocks: "1"
    ...
    
     
  10. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Yes! You could do unset($this->array["City"]["blocks"];
     
  11. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Here is base code:
    PHP:
    public function onEnable()
    {
        
    $this->array = (new Config($this->getDataFolder() . "/config.yml"Config::YAML))->getAll();
    }

    public function 
    onDisable()
    {
        @
    unlink($this->getDataFolder() . "/config.yml");
        (new 
    Config($this->getDataFolder() . "/config.yml"Config::YAML$this->array))->save();
    }

    public function 
    removeWorld($name)
    {
        unset(
    $this->array[$name]);
    }

    public function 
    removeBlock($world$blockId)
    {
        unset(
    $this->array[$world][$blockId]);
    }
     
  12. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    In other words... do as I already suggested :-/
     
    SOFe likes this.
  13. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Yours seem to be easy Awzaw. That said, i'll also give a try to what ImagicalGamer suggested for the sake of learning... :)
    Thanks @ImagicalGamer and @Awzaw
     
    jasonwynn10 likes 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.