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

How to optimize code?

Comments in 'Plugin Development' started by alexwolf_dev, Oct 23, 2016.

  1. alexwolf_dev
    Offline

    alexwolf_dev New Member

    Joined:
    Apr 6, 2016
    Posts:
    10
    Minecraft User:
    skillhearts
    I have a problem. Server use 2 GB Ram, but it stopped by lacks memory. How to optimize this code?

    PHP:
    public function setPlayer($p){
            
    $pos = array("1","2","3","4","5","6","7","8");
            
    $rand array_rand($pos);
            
    $r $pos[$rand];
            if(
    $this->pos[$r] == "0"){
                
    $name $p->getName();
                
    $this->pos[$r] = "$name";
               
                if(
    $r == 1){
                    
    $p->teleport(new Vector3(13532103));
                }
                if(
    $r == 2){
                    
    $p->teleport(new Vector3(13533139));
                }
                if(
    $r == 3){
                    
    $p->teleport(new Vector3(14433161));
                }
                if(
    $r == 4){
                    
    $p->teleport(new Vector3(11133159));
                }
                if(
    $r == 5){
                    
    $p->teleport(new Vector3(8933120));
                }
                if(
    $r == 6){
                    
    $p->teleport(new Vector3(1143285));
                }
                if(
    $r == 7){
                    
    $p->teleport(new Vector3(1573075));
                }
                if(
    $r == 8){
                    
    $p->teleport(new Vector3(17235120));
                }
            } else {
                
    $this->setPlayer($p);
            }
        }
  2. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    I told you how before:

    http://forums.pocketmine.net/threads/help-me.18531/#post-173037
    HimbeersaftLP likes this.
  3. alexwolf_dev
    Offline

    alexwolf_dev New Member

    Joined:
    Apr 6, 2016
    Posts:
    10
    Minecraft User:
    skillhearts
  4. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    If player quits, you can simply do ++$this->pn, think!! -_-
    applqpak likes this.
  5. alexwolf_dev
    Offline

    alexwolf_dev New Member

    Joined:
    Apr 6, 2016
    Posts:
    10
    Minecraft User:
    skillhearts
    This is a bad code)
    I use this:
    PHP:
    unset($pos[array_search($r$pos)]);
  6. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Why is it a bad code? I use that method in my HungerGames plugin and it works perfectly.
    HimbeersaftLP likes this.
  7. Thunder33345
    Offline

    Thunder33345 Notable Member

    Joined:
    Apr 3, 2014
    Posts:
    755
    Minecraft User:
    Thunder33345
    i think using "$this->setPlayer($p);" means you are technically fork bombing yourself
    HimbeersaftLP likes this.
  8. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Ohh yeah, I didn't even notice. xD But true ↑, that code is like a never-ending loop, that's why the server is stopping.
    applqpak and HimbeersaftLP like this.
  9. alexwolf_dev
    Offline

    alexwolf_dev New Member

    Joined:
    Apr 6, 2016
    Posts:
    10
    Minecraft User:
    skillhearts
    Okey, thanks. Now right?
    PHP:

        
    public function setPlayer($p){
            
    $rand array_rand($this->pos_array);
            
    $r $this->pos_array[$rand];
            unset(
    $this->pos_array[array_search($r$this->pos_array)]);
            if(
    $this->pos[$r] == "0"){
                
    $name $p->getName();
                
    $this->pos[$r] = "$name";
                
    //unset($this->pos_array[array_search("$r", $this->pos_array)]);

    //$this->pos_array = array_flip($this->pos_array);
    unset($this->pos_array[$r]);

                switch (
    $r) {
                    case 
    1:
                    
    $p->teleport(new Vector3(13532103));
                    break;
                   
                    case 
    2:
                    
    $p->teleport(new Vector3(13533139));
                    break;
                   
                    case 
    3:
                    
    $p->teleport(new Vector3(14433161));
                    break;
                   
                    case 
    4:
                    
    $p->teleport(new Vector3(11133159));
                    break;
                   
                    case 
    5:
                    
    $p->teleport(new Vector3(8933120));
                    break;
                   
                    case 
    6:
                    
    $p->teleport(new Vector3(1143285));
                    break;
                   
                    case 
    7:
                    
    $p->teleport(new Vector3(1573075));
                    break;
                   
                    case 
    8:
                    
    $p->teleport(new Vector3(17235120));
                    break;
                }
            }
        }
  10. alexwolf_dev
    Offline

    alexwolf_dev New Member

    Joined:
    Apr 6, 2016
    Posts:
    10
    Minecraft User:
    skillhearts
    Player vasa1 join on pos1, player vasa2 on pos2, player vasa3 on pos3. After vasa1 left to the game. Player John join on pos3, but there was vasa3!
    PHP:
    <?php
    $pos 
    8;
    $pos $pos-3//3 players
    $pos $pos+1//+1 player (vasa1)

  11. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    What?
    HimbeersaftLP and HotFireyDeath like this.
  12. alexwolf_dev
    Offline

    alexwolf_dev New Member

    Joined:
    Apr 6, 2016
    Posts:
    10
    Minecraft User:
    skillhearts
    Sorry for bad English, my main language is Russian) Your code set players on $var++; or $var--; If some players left the game position will empty!
  13. Thunder33345
    Offline

    Thunder33345 Notable Member

    Joined:
    Apr 3, 2014
    Posts:
    755
    Minecraft User:
    Thunder33345
    Code:
    var $posts = [
    "1" => [
    "x" => "xpos",
    "y" => "ypos",
    "z" => "zpos",
    "player" = null
    ]
    "2" => [
    "x" => "xpos",
    "y" => "ypos",
    "z" => "zpos",
    "player" = null
    ]
    //...
    ]
    function tpPos($player){
    foreach ($this->$posts as key=>post){
    if !$post['player'] instanceof Player OR $player instanceof Player AND isoffline{
    $this->posts[$key]['player'] = $player
    //do stuff like tp since it means there no player on that spot yet
    break;
    }
    }
    
    }
    
    sorry with the horrible indents i was just me coding on the fly
    also do not just copy and paste the code
    but you get the idea and the reasons to not use magic numbers
    also remember to do something if the array is full like breaking out and tell the player about it

    calling the function itself wont make it slove itself
    HimbeersaftLP likes this.
  14. HimbeersaftLP
    Offline

    HimbeersaftLP Active Member

    Joined:
    Sep 24, 2016
    Posts:
    50
    Minecraft User:
    HimbeersaftLP
    I'm sure if everything i right in this if:
    PHP:
    if !$post['player'] instanceof Player OR $player instanceof Player AND isoffline{
    (Not sure, could be right too)

    P.S.: You can use [PHP] instead of [CODE] to get synthax highlighting. :p
    xBeastMode likes this.
  15. Thunder33345
    Offline

    Thunder33345 Notable Member

    Joined:
    Apr 3, 2014
    Posts:
    755
    Minecraft User:
    Thunder33345
    well not directly i often just give them the idea in a form of code to do what they wish not the code it self
    basically on that line check if player is NOT instanceof player OR player is instance of Player (kind of redundant code)AND is offline
    HimbeersaftLP likes this.

Share This Page

Advertisement