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

Optimize minigames and is that code right?

Comments in 'Plugin Development' started by MyNameIsTriXz, Feb 13, 2016.

  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Hey, I want to optimize a minigame plugin as good as I can, I want to store everything in one Main.php file, the hardest thing is adding good multigame support, which is overviewable.

    Thats how the code should look like

    PHP:

    public $games = ["Game1" => ["Players" => [playerobject1,...], "Arena" => "MiniGameArena""Phase" => "Startcountdown"]];

    But I am not really sure if it is the best way to make that, and is that code right? And last: Is that code to foreach these games right?

    PHP:

    foreach($this->games as $game => $value){

    $players $value["Players"];
    // and so on

    }



  2. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    If it works it is right if not it isnt
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    If you want to optimize things or to at least look easier to read, you should use objects rather than associative arrays for arrays with fixed keys. And use numeric keys like [0] [1] rather than string keys like ["Game1"] ["Game2"]. This is faster and gives you the ability to expand your number of games.
    I'm just going to tell you the opposite. You are saying this from a user's view, but from a programmer's view, something that works does not mean a lot compared to something that is designed well, because:
    1. It works now ≠ it will work after updates
    2. It works now ≠ it works efficiently
    3. It works now ≠ you can easily add stuff if you want to expand it in the future
    4. It works ≠ others can understand it and improve it
    It is like inventing medicine vs issuing medicine to the patient. Which one do you think is more important?
    This is the age of programming, where user interface is a small thing. Read more: http://t.co/PwPAB8L1q4
  4. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Instead arrays, use classes.
  5. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Example code please? I dont really understand how that works.
  6. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    In your main class, you'll need to setup they first.
    PHP:
    private $game;

    public function 
    OnEnable()
    {
    # ...
    $this->game = new Game($this1);
    # Now you're able to do stuff like $this->game->getStatement();
    # or $this->game->setStatement("Waiting for players");
    }

    And your Game class...
    PHP:
    class Game{

    private 
    $owner$name;

    private 
    statement "InGame";

    private 
    $players = [];

    public function 
    __construc(YourPlugin $ownerint $name /*Game number*/)
    {
    $this->owner $owner;
    $this->name $name;
    }

    public function 
    getName()
    {
    return 
    $this->name;
    }

    public function 
    setName(int $name)
    {
    $this->name $name;
    }

    public function 
    addPlayer(Player $player)
    {
    array_push($this->players$player);
    }

    public function 
    getStatement()
    {
    return 
    $this->statement;
    }

    public function 
    setStatement(string $statement)
    {
    $this->statement $statement;
    }
    This is PHP OOP, you'll have to learn it if you want understand this at all.
    Last edited: Feb 14, 2016
    MyNameIsTriXz likes this.
  7. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    As I looked to the code I understood it aswell, thanks! :)
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Last edited: Feb 14, 2016
    PocketKiller and MyNameIsTriXz like this.
  9. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Ok, but my last question, if I want to check and set for each game a player when he touches a join sign, how it should look like?
  10. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    *OOP
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Now... Where is Lombok for PHP? :D
    AndrewBit likes this.
  12. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    Who are you talking to?
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Nobody in specific. Just dropping a comment as it comes to OOP.

Share This Page

Advertisement