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

How to get data from config?

Comments in 'Plugin Development' started by iiDeathStrokeii, Jun 4, 2015.

  1. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    I have a config.yml saved with multi-dimensional arrays:

    PHP:
    ublic function onLoad(){
        @
    mkdir($this->getDataFolder());
        
    $SpleefSettings = (new Config($this->getDataFolder()."Spleef Settings.yml"Config::YAML, array(
        
    "Spleef Lobby One" => array(
                                   
                 
    "Max Players" => 0,
                 
    'Arena Name' => null,
                     
    "Player One Spawn" => array(
                                
    "x" => null,
                                
    "y" => null,
                                
    "z" => null,
                               
                     ),
                    
                     
    "Player Two Spawn" => array(
                                
    "x" => null,
                                
    "y" => null,
                                
    "z" => null,
                               
                     ),
                    
                     
    "Player Three Spawn" => array(
                                
    "x" => null,
                                
    "y" => null,
                                
    "z" => null,
                               
                     ),
                    
                    
         ),             
       
        )
               )
                     );                                                                                               

        }
    I want to get "Max Players" from "Spleef Lobby One"

    PHP:
    if ($this->Spleef["Players"] < 3)
    Instead of 3 I will like to get max players.

    I can do:
    PHP:
    $this->SpleefSettings->get("Spleef Lobby One")
    But what do I do to call "Max Players" ?
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    First, you should use for example Max_Players instead of Max Players. To get Max_Players then use
    PHP:
    $this->SpleefSettings->getNested("Spleef_Lobby_One.Max_Players");
  3. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    Okay Thanks! Il test this!
  4. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    If it doesn't work, post the Error here!:)
  5. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    PHP:
    [WARNINGRuntimeException"yaml_parse(): scanning error encountered during parsing: mapping values are not allowed in this context (line 3, column 29)" (E_WARNINGin "/src/pocketmine/utils/Config" at line 139
    Artide likes this.
  6. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Remove the '(' at $SpleefSettings = (new Config(...
  7. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    same error...
  8. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Try to remove the first array:
    PHP:
    $SpleefSettings = (new Config($this->getDataFolder()."Spleef Settings.yml"Config::YAML"Spleef Lobby One" => array(...
    That means you also have to remove one ')' at the end, don't forget that
  9. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    lol i did
    PHP:
    ublic function onLoad(){
        @
    mkdir($this->getDataFolder());
        
    $SpleefSettings = new Config($this->getDataFolder()."Spleef Settings.yml"Config::YAML, array(
        
    "Spleef_Lobby_One" => array(
                                   
                 
    "Max_Players" => 0,
                 
    'Arena Name' => null,
                     
    "Player One Spawn" => array(
                                
    "x" => null,
                                
    "y" => null,
                                
    "z" => null,
                               
                     ),
                    
                     
    "Player Two Spawn" => array(
                                
    "x" => null,
                                
    "y" => null,
                                
    "z" => null,
                               
                     ),
                    
                     
    "Player Three Spawn" => array(
                                
    "x" => null,
                                
    "y" => null,
                                
    "z" => null,
                               
                     ),
                    
                    
         ),             
       
        )
               );
                                                                                                                    

        }
  10. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    No I mean:
    PHP:
    ublic function onLoad(){ // Did you rlly forgot a 'p' here?!
        
    @mkdir($this->getDataFolder());
        
    $SpleefSettings = new Config($this->getDataFolder()."Spleef Settings.yml"Config::YAML,
        
    "Spleef_Lobby_One" => array(
                                  
                 
    "Max_Players" => 0,
                 
    'Arena Name' => null,
                     
    "Player One Spawn" => array(
                                
    "x" => null,
                                
    "y" => null,
                                
    "z" => null,
                              
                     ),
                   
                     
    "Player Two Spawn" => array(
                                
    "x" => null,
                                
    "y" => null,
                                
    "z" => null,
                              
                     ),
                   
                     
    "Player Three Spawn" => array(
                                
    "x" => null,
                                
    "y" => null,
                                
    "z" => null,
                              
                     ),
                   
                   
      
        )
               );
                                                                                                                   

        }
  11. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    no, lol, missed when i selected for copy and past :D
  12. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    that wont work
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It is better to use linear arrays (arrays with number keys) instead of associative arrays both in configs and in the plugin. (i.e. use lists instead of maps in YAML)
    Example:
    Code:
    ---
    arenas:
      - arenaName: "This is an arena name"
        spawns: # the number of spawns here will be the max players in the arena
          - x: 1
            y: 3
            z: 5
          - x: 11
            y: 13
            z: 15
          - x: 21
            y: 23
            z: 25
      - arenaName: "This is another arena name"
        spawns: # the number of spawns here will be the max players in the arena
          - x: 31
            y: 33
            z: 35
          - x: 41
            y: 43
            z: 45
          - x: 51
            y: 53
            z: 55
    
    This is equivalent to:
    PHP:
    $configGetAll = [
      [
        
    "arenaName" => "This is an arena name",
        
    "spawns" => [
           [
    "x" => 1"y" => 3"z" => 5],
           [
    "x" => 11"y" => 13"z" => 15],
           [
    "x" => 21"y" => 23"z" => 25]
        ]
      ],
      [
        
    "arenaName" => "This is another arena name",
        
    "spawns" => [
           [
    "x" => 31"y" => 33"z" => 35],
           [
    "x" => 41"y" => 43"z" => 45],
           [
    "x" => 51"y" => 53"z" => 55]
        ]
      ],
    Last edited: Jun 6, 2015
  14. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    thanks,

    Code:
    ---
    SpleefLobbyOne:
    - arenaName: This is an arena name
      spawns:
      - x: 1
        "y": 3
        z: 5
      - x: 11
        "y": 13
        z: 15
      - x: 21
        "y": 23
        z: 25
    ...
    why is that Y looks like " "Y" " while Z and X look like " Z " " X ", the Y is in quotes?
  15. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    PHP:
    public function onLoad(){
        @
    mkdir($this->getDataFolder());
        
    $SpleefSettings = new Config ($this->getDataFolder()."Spleef Settings.yml"Config::YAML,array(
        
    "SpleefLobbyOne" => [
      [
        
    "arenaName" => "This is an arena name",
        
    "maxPlayers" => "Insert Max Players",
        
    "spawns" => [
           [
    "x" => 1"y" => 3"z" => 5],
           [
    "x" => 11"y" => 13"z" => 15],
           [
    "x" => 21"y" => 23"z" => 25]
        ]
      ],
              ],
       
               )
                 );                    
                          
                                                                                                                  

        }
    Im using above code.

    when I use this line of code:
    PHP:
    if ($this->Spleef["Players"] < $this->SpleefSettings->getNested("SpleefLobbyOne.maxPlayers")){    
    I get error:

    PHP:
    11:48:45 [CRITICAL"Could not pass event 'pocketmine\event\player\PlayerInteractEvent' to 'Spleef v1.0.0': Undefined property: Spleef\Spleef::$SpleefSettings on Spleef\Spleef
    11:48:45 [NOTICE] UndefinedPropertyException: "
    Undefined propertySpleef\Spleef::$SpleefSettings" (E_NOTICE) in "/Spleef/src/Spleef/Spleef" at line 98
    Line 98 is the if statement above :(
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I think that doesn't really matter.
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Don't hardcode SpleefLobbyOne. Make it dynamic, i.e. your plugin accepts as many arenas as possible.

    Do you even know what you are writing? Do you know what $this->Spleef means?
  18. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    yes i do
  19. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Then why do you read from $this->SpleefSettings without initializing it? $this->propertyName is different from $propertyName in PHP.
    BalAnce likes this.
  20. Andrey Nazarchuk
    Offline

    Andrey Nazarchuk Active Member

    Joined:
    Dec 30, 2013
    Posts:
    332
    Minecraft User:
    digita1eclipse
    I have a similar problem so I did not make a new thread:

    How do I properly get something from config so I can do things like this:
    PHP:
    $deaths $this->stats->get($player)[0];
    Here is the definition for $stats:
    PHP:
    $stats = new Config($this->getDataFolder() . "stats.yml"Config::YAML);
    Here is the error:
    Code:
    Error: Call to a member function get() on null
    File: /SkyWars/src/SkyWars/Main
    Line: 313
    Type: E_ERROR
    
    THIS CRASH WAS CAUSED BY A PLUGIN
    BAD PLUGIN: SkyWars v0.1
    
    Code:
    [304]                     $sender->sendMessage($swm->get("no-perm"));
    [305]                     return true;
    [306]                 }
    [307]                 return true;
    [308]               
    [309]             case "stats":
    [310]                 if($sender->hasPermission("matrixpe.command.stats") or $sender->hasPermission("matrixpe.command") or $sender->hasPermission("matrixpe")){
    [311]                     if(!(isset($args[1]))){
    [312]                         $player = $sender->getName();
    [313]                         $deaths = $this->stats->get($player)[0];
    [314]                         $kills = $this->stats->get($player)[1];
    [315]                         $points = $this->stats->get($player)[2];
    [316]                         $sender->sendMessage("§aYou have: §c" . $deaths . "§a deaths, §c" . $kills . "§a kills, and §c" . $points . "§a points.");
    [317]                         return true;
    [318]                     }else{
    [319]                         $player = $args[1];
    [320]                         $deaths = $this->stats->get($player)[0];
    [321]                         $kills = $this->stats->get($player)[1];
    [322]                         $points = $this->stats->get($player)[2];
    [323]                         $sender->sendMessage("§e" . $player. "§ahas§c " . $deaths . "§a deaths, §c" . $kills . "§a kills, and §c" . $points . "§a points.");
    

    Can you please tell me how to properly get something from a config?
    Last edited: Jun 16, 2015

Share This Page

Advertisement