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

Random public function on command ?

Comments in 'Plugin Development' started by Jelly9912, Oct 10, 2015.

  1. Jelly9912
    Offline

    Jelly9912 Active Member

    Joined:
    Aug 8, 2015
    Posts:
    118
    Minecraft User:
    Jelly9912
    Code:
                    }elseif($args[0] == "make"){
                            if($sender->getLevel()->getName() == $this->getConfig()->get("spawn")){
                                $sender->sendTip("You are on the spawn!");
                                return true;
                      }else{
                                    $this->makeHouse($sender->getName());
                                    return true;
                }
                    }
    
    So i can use random public function on command ? This is makeHouse function but i have more functions like this and i want random e.g makeParkour, makeViphouse ... Thanks in advance :)
  2. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    What? :D You want to be random function?
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    $r rand(13);
    if(
    $r === 1){
    $this->makeHouse(...);
    return 
    true;
    }
    if(
    $r === 2){
    $this->makeParkour(...);
    return 
    true;
    }
    if(
    $r === 3){
    $this->makeVipHouse();
    return 
    true;
    }
    Jelly9912 likes this.
  4. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    431
    Plugins:
    4
    Minecraft User:
    Lambo
    It would be good practice to use a switch is this type of situation.
    jojoe77777 likes this.
  5. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    It would make the code more longer.
  6. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    431
    Plugins:
    4
    Minecraft User:
    Lambo
    So what? It looks better and is better practice. If you want short code then removed all returns in your code.
  7. Artide
    Offline

    Artide Active Member

    Joined:
    Apr 20, 2015
    Posts:
    214
    Minecraft User:
    Artide5
    PHP:
    public function switchRandom($number){
    switch(
    $number){
    case 
    0:
    return 
    $this->makeHouse();
    break;
    case 
    1:
    return 
    $this->function();
    break;
    }
    }

    //use like this:
    $this->switchRandom(mt_rand(0,1));
    //or
    self::switchRandom(mt_rand(0,1));
  8. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    The function isn't static, so you can't call it using self::switchRandom().
    Jelly9912 likes this.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    To @Gamecrafter only: self::switchRandom() is bad practice as it implies you expect it to be a static function, but actually not that "you can't", just that you "should not". PHP would understand it as $this->switchRandom() just like how it understands parent::fx() as a call to a parent function.

    To other people: ignore me, you should use $this->fx() instead of self::fx() for non-static functions.
  10. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    I am naughty, so I ignored the first three words of your post. Why is calling static function like that bad practice? Just curious :D
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Using the static function style to call a function (for $this only; won't work on other classes) would make readers think that you are trying to call a static function while you aren't.

Share This Page

Advertisement