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

How to do this?

Comments in 'Plugin Development' started by Marcelo234, Jun 3, 2016.

  1. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    Hey Guys.....All of you know, how to save (for a short time) numbers...Like this...
    PHP:
    public $test 10;
    And you can change the number to a other number...
    So....You also know, how to save Playernames for a short time in the Code.....like...
    PHP:
    public $players = [];
    ...So now my Question...
    How I can combinate this....
    So how I can save (for a short time) in the Code the Playername with the Coins from the Player....
  2. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Thats called "Multidimensional arrays"

    PHP:
    public $players["Steve" => 23"Alex" => 382];
    To get the coins of a player do:
    PHP:
    $this->players["Steve"]; 
    It returns 23.
  3. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    So.ok....I can change the Playercoins....And how I can change the Playernames?
  4. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    PHP:
    $this->players["NewPlayerName"] = $this->players["OldPlayerName"];
    unset(
    $this->player["OldPlayerName"]);
  5. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    SOFe likes this.
  6. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Straightforward answer to your question.
    PHP:
    public $players = [];

    public function 
    setCoins(Player $playerint $amount) {
      
    $this->players[$player->getName()] = $amount;
    }

    public function 
    getCoins(Player $player) {
      return 
    $this->players[$player->getName()];
    }
    Now you can do things like:
    PHP:
    $this->setCoins($player$this->getCoins($player) + 1); // Adds 1 to the players coin amount.
  7. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    Is there a way to Check which player the most coins have?
  8. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    max($this->players);
  9. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    Okay?And how to get the Second best player?
  10. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    PHP:
    arsort($this->players);
    $secondBestName array_keys($this->players)[1];
    $secondBestAmount array_values($this->players)[1];
    $bestFive array_slice($this->players05true);
    $number 1;
    foreach(
    $bestFive as $name => $amount){
      echo 
    "#$number$name (\$$amount)"PHP_EOL;
      
    $number++;
    }
    Test run: http://bit.ly/1Uowltk
    Last edited: Jun 8, 2016
    Gamecrafter likes this.
  11. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I think that a for loop should be faster and more efficient in this case.
    http://php.net/manual/en/control-structures.for.php
    And also, what if he's not organizing his players array as you? I don't think your method will be the best idea, it totally depends on how his code is.

    @PEMapModder won this time.
  12. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    The nostalgia got to you, didn't it? I don't see @PEMapModder anywhere here, only @SOFe. But how much would the difference be when it comes to speed? How much more efficient? How many more lines?
    Last edited: Jun 9, 2016
    SOFe likes this.
  13. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Off-topic alarm.

    You may become new @PEMapModder, or you already are? :rolleyes:
    Anyway good work creating these great posts from which even I can learn new things ;)

    Back on-topic

    This is nohow related with PocketMine plugin development except that plugins are in written in php which obviously you don't know well. Please learn that first, it's essential in creating plugins, seriously just take this day and go through CodeAcademy PHP courses or DerekBanas video and you'll be good to go ;)
  14. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    I don't get what you mean.
    More effectively, do it like this:

    PHP:
    private $top10 = array();

    function 
    onDataUpdate($playerName$newAmount){
      
    $this->top10[$playerName] = $newAmount;
      
    arsort($this->top10);
      if(
    count($this->top10) > 10){
        
    $this->top10 array_slice($this->top10010true);
      }
    }
    In this way, you don't need to recalculate a lot of data every query, but sort a really small amount of data every update.
  15. Marcelo234
    Offline

    Marcelo234 New Member

    Joined:
    Oct 18, 2014
    Posts:
    204
    And than return $this->top10
  16. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    No. Execute this every time any data are changed. Check $this->top10 when you want to query the top data.
  17. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Your given help/tips/info are really helpful and you're active as @PEMapModder was once. I'm getting little suspicious. Maybe @PEMapModder decided to return in new form (profile). I'd wish that would be true.

    P.S. If something sounds insane, well that's because of my grammar (English isn't my native language)
    Ritch likes this.

Share This Page

Advertisement