PHP + YAML (not strictly Bukkit but related)

Discussion in 'Plugin Development' started by Crimsonfox, May 15, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Crimsonfox

    Okay, I'm pretty new to this so it may not even be possible and I'm only doing this this way because I can't get a mySQL version to work but it's been fun working with YAML so I kept going. I'm trying to add mail (essentials) in using PHP from my website but running into problems with the formatting of the YAML dump.

    I'm loading this through PHP:

    Code:
    mail:
    - 'Crimsonfox: test'
    - 'Crimsonfox: Test2'
    - 'Crimsonfox: t'
    socialspy: false
    nickname: Crimsonfox
    muted: false
    inventory:
        size: 0
    godmode: false
    teleportenabled: true
    afk: true
    jailed: false
    lastlocation:
        yaw: 176.81152
        pitch: 36.900253
        z: 2744.016947782735
        y: 67.0
        world: C3P0
        x: 835.7719458699837
    
    What I'm trying to do is adding a new mail entry in, I do my PHP stuff and it dumps this:

    Code:
    mail: null
    0: 'TestyMcgee: And his message'
    1: 'Crimsonfox: test'
    2: 'Crimsonfox: Test2'
    3: 'Crimsonfox: t'
    socialspy: false
    nickname: Crimsonfox
    muted: false
    inventory:
      size: 0
    godmode: false
    teleportenabled: true
    afk: true
    jailed: false
    lastlocation:
      yaw: 176.81152
      pitch: 36.900253
      z: 2744.0169477827
      y: 67
      world: C3P0
      x: 835.77194586998
    
    No dashes anymore but instead numbers with colons. And some of the spacing lower down has changed. I doubt I'm experienced enough to figure it out, thought I'd did quite well to insert that mail in but then it breaks when it reads the YML file because it's in a slightly different format.

    Here's the PHP if it helps:

    Code:
    <?php
    
    require_once 'lib\sfYaml.php';
    echo '<pre>';
    
    $array = sfYaml::load('crimsonfox.yml');
    $test = array_key_exists ('mail', $array);
    echo $test;
    $test2 = array_search ('mail', $array);
    echo $test2.'Blergh<br />';
    array_keys($array, '0',true);
    $CountMail = array();
    $i = 0;
    $done = false;
    $offset = 0;
    foreach ($array as $key_name => $key_value) {
        if (is_numeric($key_name)) {
            echo "BLERGH";
            
            if ($done == false) {
                $offset = array_search(strval($key_name), array_keys($array));
                $done = true;
                echo 'The first mail key offset is:<font color="red">' . $offset . '</font>';
            }
            $i++;
        }
        print "  Key = " . $key_name . " Value = " . $key_value . "<BR>";
    
    }
    echo $i;
    echo '<br />';
    print_r($array);
    echo '<br /><br />';
    
    echo $array['1'].'<br />    ';
    $message = "TestyMcgee: And his message";
    
    
    array_splice ( $array , $offset, 0, $message);
    
    
    $yaml = sfYaml::dump($array, 2);
    file_put_contents('CrimsonTest.yml', $yaml);
    
    print_r($array);
    
    ?>
    Any help would be appreciated.
     
  2. Offline

    jlogsdon

    No spaces before the dashes so the YAML parser thinks they are "top-level". It should look like this:

    Code:
    mail:
        - 'Crimsonfox: test'
        - 'Crimsonfox: Test2'
        - 'Crimsonfox: t'
    socialspy: false
    nickname: Crimsonfox
    muted: false
    inventory:
        size: 0
    godmode: false
    teleportenabled: true
    afk: true
    jailed: false
    lastlocation:
        yaw: 176.81152
        pitch: 36.900253
        z: 2744.016947782735
        y: 67.0
        world: C3P0
        x: 835.7719458699837
    
    Also, here's an easier way to inject a message:

    PHP:
    <?php

    require_once 'lib/sfYaml.php';

    $player sfYaml::load('crimsonfox.yml');

    // Setup the mail container if not already present
    if (!isset($player['mail'])) {
        
    $player['mail'] = array();
    }

    $message 'TestyMcgee: This is a test!';
    // "Shift" the message to the top of the mail container
    array_unshift($player['mail'], $message);

    $yaml sfYaml::dump($player2);

    ?>
     
  3. Offline

    Crimsonfox

    Cheers for the PHP awesome but sadly I have no control how essentials create's its player config files which is annoying. >_< So I guess I'll just do a search of the file rather than use the parser.
     
Thread Status:
Not open for further replies.

Share This Page