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

Solved Bad practises

Comments in 'Plugin Development' started by Tim // robske Büba, Feb 2, 2016.

  1. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    I've heard I've done multiple bad practises and I want how to "fix" them.
    First the famous 323... But what should i write instead?:
    PHP:
            if($event->getBlock()->getID() == 323 || $event->getBlock()->getID() == 63 || $event->getBlock()->getID() == 68){
                
    $sign $event->getPlayer()->getLevel()->getTile($event->getBlock());
                if(!(
    $sign instanceof Sign)){ //Or is this enough?
                    
    return;
                }
    Second my code to get all args and put them in an string (/command I am cool would result into $FinalMsg = "I am cool")
    PHP:
    $FinalMsg $args[0];$IndexArgS 0;$ReachedEndArgs false;
    while(
    $ReachedEndArgs == false)
    {
       
    $IndexArgS++;
    if(isset(
    $args[$IndexArgS]))
    {
       
    $FinalMsg $FinalMsg." ".$args[$IndexArgS];
    }
    else
    {
       
    $ReachedEndArgs true;
    }
    }
    Last edited: Feb 2, 2016
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    $sign $event->getPlayer()->getLevel()->getTile($event->getBlock());
    if(!
    $sign instanceof Sign) return;
    PHP:
    array_shift($args);
    $msg implode(" "$args);
    PEMapModder likes this.
  3. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Thanks for the first.
    Can you please explain the secound (array_shift and implode) functions?
  4. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    (And was my code [for the msg] bad?)
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Of course. implode($glue, $array) returns a String which is the pieces of $array, glued together with $glue.
    Example:
    PHP:
    $a = array(
       
    "Hello",
       
    "PocketMine",
       
    "User");
    echo 
    implode(" "$a);
    // Echos: "Hello PocketMine User"
    echo implode(", "$a);
    // Echos: "Hello, PocketMine, User"
    echo implode("-"$a);
    // Echos: "Hello-PocketMine-User"
    array_shift, as it says, shifts the array.
    Example:
    PHP:
    // Command: /hey <player> Hello you!
    // Currently: $args[0] = <player>   $args[1] = "Hello" etc.
    // As we want $args to start from "Hello", without having <player> inside, we "shift" the array.
    // array_shift($array) basically removes the first element of the array
    // Example(Using implode() to echo it):

    echo implode(" "$args);
    // Echos: "<player> Hello you!"
    array_shift($args);
    echo 
    implode(" "$args);
    // Echos: "Hello you!"  -> The first element is gone(!), $args[0] now is "Hello"
  6. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Thanks
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Your second code snippet, if you don't want to use (or if you want to write it out yourself) implode(), it should be like this:

    PHP:
    $finalMsg $args[0];
    // I am not using $indexArgs. I use $i instead in the code below, as personal practice and for convenience.
    // $reachedEndArgs is written to but never read from. What is it there for?
    Then method 1:
    PHP:
    for($i 1$i count($args); $i++){
    // say, if $args has 10 args, the 10th arg is at $args[9],
    // so $i is always smaller than 10, so break the loop there.
      
    $finalMsg .= " " $args[$i];
      
    // this is the same as $finalMsg = $finalMsg . " " . $args[i];
    }
    Method 2:
    PHP:
    for($i 1$i < isset($args[$i]); $i++){
    // if $args[$i] exists actually does the same thing
      
    $finalMsg .= " " $args[$i];
    }
    Method 3, the whole code snippet (changes the contents of $args):
    PHP:
    $finalMsg array_shift($args);
    // array_shift removes the first element from the array and returns it
    // so we can use it as if it is an array iterator/reader
    while(($arg array_shift($args)) !== false){ // "read" the "next" arg from $args and save it into $arg
      
    $finalMsg .= " " $arg;
    }
    Method 4, the simplest way. We are all wasting time.
    PHP:
    $finalMsg "";
    foreach(
    $args as $arg){
      
    $finalMsg .= $arg " ";
    }
    $finalMsg substr($finalMsg0, -1);
    // remove the trailing space from $finalMsg
    The worst practice you have in your thread is actually your naming of variables. We usually $nameVariablesLikeThisInCamelCase or $with_underscores_like_this, NotInPascalCaseLikeThis.
    Tim // robske Büba and Vaivez66 like this.

Share This Page

Advertisement