1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Class Tools and items

Discussion in 'Development' started by zKoz210, Oct 15, 2017.

  1. zKoz210

    zKoz210 Spider

    Messages:
    11
    GitHub:
    zkoz210
    @dktapps I have one question, why isShovel() returns not a Boolean value, and a Tier?

    upload_2017-10-15_14-27-45.png
     
    jasonwynn10 and SOFe like this.
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It returns and integer, not a "Tier". But PHP-wise, it does return a boolean value. By default, the return value of isShovel() is Tool::TIER_NONE which corresponds to 0. if(0) is the equivalent of if(false). The boolean value of 0 is false. The boolean value of any natural number is true. So all natural numbers would output true.
    You can test it by running this script.
    PHP:
    echo "Boolean value of 0 is:";
    var_dump((bool) 0);//false

    echo "Boolean value of 1 is:"
    var_dump((bool) 1);//true

    $n mt_rand(1PHP_INT_MAX);
    echo 
    "Boolean value of natural number $n is:"
    var_dump((bool) $n);//true

    Why doesn't it directly return a boolean value?
    Simplicity. That one function can be used to check whether the tool isShovel and also to determine what kind of shovel it is.
     
    jasonwynn10 likes this.
  3. zKoz210

    zKoz210 Spider

    Messages:
    11
    GitHub:
    zkoz210
    You're a genius, of course, but iron could be a hoe too. Essence is lost this function.
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    [​IMG]
     
    HimbeersaftLP, jasonwynn10 and Muqsit like this.
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    To avoid that, the value of is{itemtype} functions are hardcoded than being stored as a class property. If it were a class property then isShovel(), isHoe(), isShears()... etc would have returned the same value.
    IronShovel->isShovel() returns true while IronShovel->isHoe() returns false. So I guess the essence is not lost... ?

    Take it this way:
    Item::isShovel() returns false if the item is not a shovel, but returns the tier type if it's a shovel.
    PHP:
    if($shovelTier Item::isShovel()){
        
    var_dump($shovelTier);
    }
     
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    By coding conventions [citation needed], is*** functions always return boolean values. It is abnormal or hacky if you return something non-boolean and expect people to use it as a boolean value.
     
    zKoz210 likes this.
  7. zKoz210

    zKoz210 Spider

    Messages:
    11
    GitHub:
    zkoz210
    I don't think this is correct.
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    In your case, it is more sensible to use pass-by-reference parameters, e.g.:

    PHP:
    public function isShovel(&$shovelTier null){
      
    $shovelTier TIER_IRON;
      return 
    true;
    }

    if(
    $item->isShovel($shovelTier)){
        
    $player->sendMessage("Your shovel tier is $shovelTier");
    }
     
    Muqsit likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.