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

Solved Boolean returning as false.

Discussion in 'Development' started by RumDaDuMCPE, Jul 8, 2018.

  1. RumDaDuMCPE

    RumDaDuMCPE Witch

    Messages:
    67
    GitHub:
    RumDaDuMCPE
    I've integrated a Heads Up Display into my Core plugin. The HUDs are of two types: Normal (for players) and Staff (for the staff). The staff's names have been hard-coded into the core.
    Here's the code:
    Function in the API:
    PHP:
    public function isStaff(string $player) : bool {
        
    $staffs = [
            
    "RumDaDuMCPE",
            
    "RoyalMCPE",
            
    "FireRage5520",
            
    "ManXmaster27"
        
    ];
        foreach (
    $staffs as $staff) {
            
    strtolower($staff);
        }
        if (
    in_array(strtolower($player), $staffs)) {
            return 
    true;
        }
        return 
    false;
    }
    The task that calls the function after checking if the player is staff or not:
    PHP:
        public function onRun($tick) {
            if (
    $this->core::getInstance()->getAPI()->isStaff($this->player->getName())) {
               
    $this->player->sendPopup(TF::colorize($this->core::getInstance()->getAPI()->sendHUD($this->player"Staff")));
            }
            
    $this->player->sendPopup(TF::colorize($this->core::getInstance()->getAPI()->sendHUD($this->player"Normal")));
        }
    }
    File containing the sendHUD Function:
    PHP:
    public function sendHUD(Player $playerstring $HUDtype) : string {
        
    $playerCount count(Server::getInstance()->getOnlinePlayers());
        
    $playerMax Server::getInstance()->getMaxPlayers();
        
    $color "&a";
        
    $tps Server::getInstance()->getTicksPerSecond();

        if (
    $tps 20) {
            
    $color "&c";
        }

        
    $staffHUD "&l&a".$player->getName()." &r&l|&r &e".$playerCount."/".$playerMax." &r&l|&r &bTPS: ".$color.$tps."\n".
        
    "If TPS is below 20 for a long time, let Rum know.";

        
    $normalHUD "&l&b".$player->getDisplayName()." &r&e| &l&a_ingameMoney_"."\n"
            
    ."_playerVoteStatus_";//$this->getVoteStatus($player);

        
    switch ($HUDtype) {
            case 
    "Staff":
                return 
    $staffHUD;
                break;

            case 
    "Normal":
                return 
    $normalHUD;
                break;

            default:
                return 
    "&cERROR FETCHING HEADS UP DISPLAY!";
        }
    }
    The issue occurs when I use a username that has been hardcoded as staff and yet I receive the normal HUD meant for the players.

    Kindly help me find a solution to this issue, Thank you.

    UPDATE: I have found the solution. I forgot to call an Else function after the If function in the Task.
    Updated code:

    PHP:
        public function onRun($tick) {
            if (
    $this->core::getInstance()->getAPI()->isStaff($this->player->getName())) {
               
    $this->player->sendPopup(TF::colorize($this->core::getInstance()->getAPI()->sendHUD($this->player"Staff")));
            } else {
               
    $this->player->sendPopup(TF::colorize($this->core::getInstance()->getAPI()->sendHUD($this->player"Normal")));
            }
        }
    }
     
    Last edited: Jul 8, 2018
  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.