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

Player $data

Comments in 'Plugin Development' started by SuperChipsLP, Nov 10, 2013.

  1. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    Hey there!

    I have tried the following:
    PHP:
    $this->api->chat->broadcast($data." moved!");
    When i debug that scene, my server crashes.

    What's the problem?
    I want to display the players name..
  2. 99leonchang
    Offline

    99leonchang Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    165
    Plugins:
    2
    Minecraft User:
    99leonchang
    if it's an event, try using:
    $username = $data['player']->username;
    and then just
    $this->api->chat->broadcast($username." moved!");
  3. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    Thank you! Do you know how i save the contents to a config file?
    For example:

    kevin:
    x=X
    y=Y
    z=X
    niki:
    x=X
    y=Y
    z=X

    I know how to get the data. :)
  4. 99leonchang
    Offline

    99leonchang Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    165
    Plugins:
    2
    Minecraft User:
    99leonchang
    First create a config file in the init:
    PHP:
    $this->config = new Config($this->api->plugin->configPath($this)."config.yml"CONFIG_YAML, array());
    Then write to the config wherever:
    PHP:
    $this->config->set($username, array(
    "x" => $variable,
    "y" => $variable2,
    "z" => $variable3
    ));
    and then save:
    PHP:
    $this->config->save();
  5. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    Thank you very much! :)
  6. 99leonchang
    Offline

    99leonchang Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    165
    Plugins:
    2
    Minecraft User:
    99leonchang
    No problem
    This plugin may spam your players though if you make it broadcast something each time someone moves
  7. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    I know. It was just to demonstrate you what i mean. I always try things like this to get to my final end-product which is completely different :p
  8. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    I tried it, but it gives me this error:

    Code:
    # PocketMine-MP Error Dump Sun Nov 10 12:29:51 GMT 2013
    Error: array (
      'type' => 1,
      'message' => 'Cannot use object of type Entity as array',
      'file' => 'C:\\Users\\Niklas_2\\Documents\\Programmierung\\PocketMine\\TestServer\\Hide and Seek\\PocketMine-MP\\src\\API\\PluginAPI.php(87) : eval()\'d code',
      'line' => 115,
    )
    The code that i have:


    PHP:
        case 'player.move':

        
    $username $data['player']->username;

        
    $x $data['player']->x;
        
    $y $data['player']->y;
        
    $z $data['player']->z;

        
    $this->config->set($username, array(
        
    "x" => $x,
        
    "y" => $y,
        
    "z" => $z,
        
    "hider" => true,
        
    "isBlock" => true
        
    ));

        
    $this->config->save();
  9. 99leonchang
    Offline

    99leonchang Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    165
    Plugins:
    2
    Minecraft User:
    99leonchang
    I'm not very sure what's wrong there, try using
    $username = $data->username;
  10. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    Ok. I'll try
  11. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    Tried it. It no longer crashes with this code:

    PHP:
    $username $data->username;

        
    $x $data->x;
        
    $y $data->y;
        
    $z $data->z;

        
    $this->config->set($username, array(
        
    "x" => $x,
        
    "y" => $y,
        
    "z" => $z,
        
    "hider" => true,
        
    "isBlock" => true
        
    ));

        
    $this->config->save();
    But the config output is this:

    Code:
    ---
    :
      x: '189.47663879395'
      y: 64
      z: '120.70510864258'
      hider: true
      isBlock: true
    It doesn't show the player name...

    Also, how do i retrieve for example 'notch' data?
  12. 99leonchang
    Offline

    99leonchang Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    165
    Plugins:
    2
    Minecraft User:
    99leonchang
    Hmm, that's odd
    Also, what do you mean by 'notch' data?
  13. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    The config should look like this for example:

    Code:
    ---
    notch:
      x: '189.47663879395'
      y: 64
      z: '120.70510864258'
      hider: true
      isBlock: true
    niki:
      x: '189.47663879395'
      y: 64
      z: '120.70510864258'
      hider: true
      isBlock: true
    How do i get notch's x coordinate out of the config?
  14. 99leonchang
    Offline

    99leonchang Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    165
    Plugins:
    2
    Minecraft User:
    99leonchang
    Code:
    if ($this->config->exists($data['username'])) {
                        return $this->config->get($data['username'])['x'];
                    }
                    return false;
  15. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    I will try that. But why doesn't it save the players username to the config?
  16. zgeorge2222
    Offline

    zgeorge2222 New Member

    Joined:
    Mar 3, 2014
    Posts:
    9
    Minecraft User:
    zgeorge22
    I was wondering something similar to this, but I didn't really get what happened up their. I am trying to swap 2 players position in my plugin (probably using config files), so how exactly would I save each of their positions to config files and then get Player1's position from the file and make it player2's position and vice versa? Thanks for any help.
  17. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    You don't need a config for that.
    Here's a script that does it:

    PHP:
    //Getting all online players
    $all $this->api->player->getAll();
    //Shuffle array with player objects
    shuffle($all);
    $times 0;
    $x;
    $y;
    $z;

    foreach(
    $all as $player){
    $times++;
    if(
    $times == 1){
    //Saving position of player 1
    $x $player->x;
    $y $player->y;
    $z $player->z;
    }
    if(
    $imes == 2){
    //Teleporting first player in $all to second player
    ConsoleAPI::run("tp ".$all[0]->username." "$player->username);
    //Teleporting second player to set coordinates.
    ConsoleAPI::run("tp ".$player->username." ".$x." ".$y." ".$z);
    }
    }
    Hope it works. It's not tested.
    Last edited: Mar 7, 2014
    Darunia18 likes this.
  18. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    With it, you swap to players positions as you wanted. :)
  19. zgeorge2222
    Offline

    zgeorge2222 New Member

    Joined:
    Mar 3, 2014
    Posts:
    9
    Minecraft User:
    zgeorge22
    Thanks for your help. Could you maybe explain how that script works. I want to learn how it works and why you used shuffle, where the second player teleport is, etc. Also, how can I make it loop after a random amount of time (run it once, wait a random time 30-120 seconds, and then run it again, over and over until 1 player dies). Thanks again!
    Last edited: Mar 6, 2014
  20. SuperChipsLP
    Offline

    SuperChipsLP Active Member Plugin Developer

    Joined:
    Sep 15, 2013
    Posts:
    204
    Plugins:
    5
    Minecraft User:
    Redmol
    I'll add comments to the code
    later, and eventually, i'll publish a plugin that does that. :)

Share This Page

Advertisement