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

Add to config

Discussion in 'Development' started by MrDevCat, Jan 16, 2019.

  1. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    Hi!

    So I've been trying to work out how to add to the config when a players runs a command but i unfortunately don't know how.
    I looked at some old posts to try and figure out what i should be adding but i don't know what it is.
    I have looked at the posts on setConfig but it doesn't show things that fit my needs.

    I'm trying to make it add this:

    check
    - world x y z

    so it doesn't just add world, x, y and z but it also adds check so that when check is called the world, x, y and z is broadcasted.
    I can currently only add world, x, y, z.
    I already have the broadcast ready but need to get the add to config sorted.

    Can anyone help?
    $this->getConfig>set("$addition", $tpc);
     
    Last edited: Jan 16, 2019
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    If you are trying to get the config to be formatted exactly like your specifying, then you need to have the string with the data and multiple other entries in an array assigned to the key


    PHP:
    $config $plugin->getConfig();
    $config->set("check",["world x y z","entry 2"]);
    $config->save();
     
    Last edited: Jan 18, 2019
  3. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    That would force the user to have inputted the word check though wouldn't it?
    I mean when it enters check is a custom set name and when it is called everything in it is used.
     
  4. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    Sorry, brain turned to slush.
    I understand what you meant now.
    Thanks, I'll test it when I get home.
     
    Last edited: Jan 17, 2019
  5. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    [19:27:37] [Server thread/CRITICAL]: ErrorException: "Illegal offset type" (EXCEPTION) in "src/pocketmine/utils/Config" at line 449

    umm...
    Is this because of $check = $args[0];

    I can post the full error if u need it.
    I tested it on my warp plugin.
    [​IMG]
    [​IMG]
     
    Last edited: Jan 17, 2019
  6. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Shouldn't this be different? You'd be setting "check" as the value for the key, which is also an array (citation). I think you meant the opposite, like this?

    PHP:
    $config $plugin->getConfig();
    $config->set("check", ["world x y z","entry 2"]);
    $config->save();
    I'm not quite certain what you need. Do you want to add more "world x y z" entries under "check" later on, or do you just need to store one? It would help if I knew what your goal was for this code.
     
    jasonwynn10 likes this.
  7. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    Check
    - world x y z

    I want all of that added but I need it custom by players.
    So /check johni
    Will then add

    Johni
    - World x y z

    PHP:
    $config $plugin->getConfig();
    $config->set($check, [$world$x$y$z,"entry 2"]);
    $config->save();
    Would this work?
     
    Last edited: Jan 18, 2019
    jasonwynn10 likes this.
  8. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    Thank you both, you are gods.
    PHP:
    $config $this->getConfig();
    $config->set($check,[$leveled$x$y$z]);
    $config->save();
    but this won't work to recieve it.
    PHP:
    $getcheck $args[0];
    $config $this->getConfig();
    $config->get($getcheck,[$leveled$x$y$z]);
    This is the last thing i need to fix to have it fully working.
     
    Last edited: Jan 18, 2019
  9. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Config->get() Only needs the key, so it's not the same as set(). I guess that you want to retrieve the data stored with the other code snippet. I would do something like this:
    PHP:
      $config->get($args[0]); 
    However, this will return an array, so you have to handle it like one.
     
  10. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    so
    PHP:
    $config $this->getConfig();
    $config->get($args[0]);
    $a $args[0];
    Will this return what was sent as $leveled?
     
  11. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    It didn't work ^
     
    Last edited: Jan 18, 2019
  12. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    As I said, it returns an array of all the information stored, so it doesn't only return $leveled, but also $x, $y, $z. Depending on what information you want, you'll need to specify which entry from the array to choose. For $leveled, you'd do:
    PHP:
     $config->get($args[0][0]); 
    Because it's the first entry in the array. I suggest learning a bit more about arrays soon, it'll be beneficial to understanding configs like this.

    I'm sure it worked, just not how you intended it to. Any errors you get, or letting us know what did happen compared to what you expected would be very useful.
     
  13. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    Thx but that worked only to get the letters of check back (I only put 3 letters for the check name so 0, 1 and 2 got the check letters) and when I tried to get $leveled back by doing this
    PHP:
    $config->get($args[0][3]);
    It said that 3 was undefined so I tried it like this
    PHP:
    $config->get($args[1][0]);
    1 is undefined.
     
  14. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Assuming you use this to save
    PHP:
    $config $this->getConfig();
    $config->set($args[0],[$leveled$x$y$z]);
    $config->save();
    To retrieve you may use
    PHP:
    $config $this->getConfig();
    $data $config->get($args[0]);
    if(
    is_array($data)){
        
    $leveled $data[0];
        
    $x $data[1];
        
    $y $data[2];
        
    $z $data[3];
    }
     
    corytortoise likes this.
  15. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    Thanks but for some reason when that runs with this
    PHP:
    $config $this->getConfig();
                
    $data $config->get($args[0]);
                if(
    is_array($data)){
                      
    $land $data[0];
                       
    $x $data[1];
                               
    $y $data[2];
                               
    $z $data[3];
                            }
                            
    $sender->sendMessage(TextFormat::GOLD "teleporting  at $land$x$y$z!");
                            
    $sender->teleport(new Position($land$x$y$z));
    [14:05:42] [Server thread/CRITICAL]: TypeError: "Argument 4 passed to pocketmine\level\Position::__construct() must be an instance of pocketmine\level\Level or null, float given, called in /root/plugins/EmeraldWarp-master/src/ElementalMinecraftGaming/EmeraldWarp/Main.php on line 67" (EXCEPTION) in "src/pocketmine/level/Position" at line 41
     
  16. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Thats not how you construct a Position object, it should be
    PHP:
    new Position($x,$y,$z,$this->getServer()->getLevelByName($land));//
    You should place the teleporting and sendMessage code inside the curly bracket of "if" to ensure that $x $y etc are initialised
     
    corytortoise likes this.
  17. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    the p
    the plugin doesn't even load when i do that.
    PHP:
    if (isset($args[0])) {
                
    $warp $args[0];
                
    $config $this->getConfig();
                
    $data $config->get($args[0]);
                if(
    is_array($data)){
                      
    $land $data[0];
                       
    $x $data[1];
                               
    $y $data[2];
                               
    $z $data[3];
                            }
                            
    $sender->sendMessage(TextFormat::GOLD "Warping $warp at $land$x$y$z!");
                               
    $sender->teleport(new Position($x $y $z$this->getServer()->getLevelByName($land));
     
  18. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    Sorry forgot a bracket
     
  19. MrDevCat

    MrDevCat Spider Jockey

    Messages:
    28
    GitHub:
    elementalminecraftgaming
    Thank you all. My plugin works now.
     
  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.