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

Optimize code

Comments in 'Plugin Development' started by MyNameIsTriXz, Jan 20, 2016.

  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Hey guys, I think that I should optimize the code in my plugin, especially for multigame minigame plugins, I have chosen the way where you foreach the game arrays, in every event, so you mustnt create for each game a new function, but I am not really sure how I should do it, as example these are my thoughts:

    PHP:
    public $game1;
    public 
    $game2;
    public 
    $games;


    public function 
    onEnable(){
    $this->games = [$this->game1$this->game2];
    }    

    public function 
    onMove(PlayerMoveEvent $event){

    foreach(
    $this->games as $games){
    foreach(
    $games as $player){
    if(
    $event->getPlayer() == $player){   // player is in array and game

    }}}

    But how can I get if a player is already in a game or just waiting for the start, I am using $phase, $phase = 0 when there are not enough players in the waiting room, it switches to $phase = 1, when there are enough, $phase = 1 is used to display all players the round start timer. But how can I optimize $phase aswell or is there another way?
  2. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    First, using arrays for saving games is already wrong. You should use classes. Classes will fix everything.
    CrazedMiner likes this.
  3. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Example please? :D
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    CrazedMiner likes this.

Share This Page

Advertisement