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

other server online players

Discussion in 'Development' started by amirTBM, Jan 30, 2020.

  1. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    Since I'm not that advanced, could you give me a summary or explanation on how I would implement that into the ScoreHud addon :)?
     
  2. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    @amirTBM I finished the addon (almost all of the code credit goes to @jasonwynn10 )
    You can edit it to your needs:
    PHP:
    <?php

    declare(strict_types 1);

    /**
     * @name Servers
     * @version 1.0.0
     * @main    JackMD\ScoreHud\Addons\Servers
     */

    namespace JackMD\ScoreHud\Addons{
        use 
    libpmquery\PMQuery;
        use 
    libpmquery\PmQueryException;
        use 
    JackMD\ScoreHud\addon\AddonBase;
        use 
    pocketmine\Player;
        use 
    pocketmine\utils\Internet;
        use 
    pocketmine\Server;
        use 
    pocketmine\scheduler\AsyncTask;
        use 
    pocketmine\scheduler\BulkCurlTask;
        use 
    pocketmine\utils\InternetException;
        use 
    pocketmine\utils\TextFormat;
        
        class 
    Servers extends AddonBase{
            public 
    $data = [];
            private 
    $queryServer;
            private 
    $host "149.28.224.203";
            private 
    $port 30000;
            private 
    $timeout 4;       
            
            public function 
    getProcessedTags(Player $player): array{
            
    $socket = @fsockopen('udp://'."149.28.224.203"30000$errno$errstr4);

            if(
    $errno) {
                
    fclose($socket);
                throw new 
    PmQueryException($errstr$errno);
            }elseif(
    $socket === false) {
                throw new 
    PmQueryException($errstr$errno);
            }

            
    stream_Set_Timeout($socket4);
            
    stream_Set_Blocking($sockettrue);

            
    // hardcoded magic https://github.com/facebookarchive/RakNet/blob/1a169895a900c9fc4841c556e16514182b75faf8/Source/RakPeer.cpp#L135
            
    $OFFLINE_MESSAGE_DATA_ID = \pack('c*'0x000xFF0xFF0x000xFE0xFE0xFE0xFE0xFD0xFD0xFD0xFD0x120x340x560x78);
            
    $command = \pack('cQ'0x01time()); // DefaultMessageIDTypes::ID_UNCONNECTED_PING + 64bit current time
            
    $command .= $OFFLINE_MESSAGE_DATA_ID;
            
    $command .= \pack('Q'2); // 64bit guid
            
    $length = \strlen($command);

            if(
    $length !== fwrite($socket$command$length)) {
                throw new 
    PmQueryException("Failed to write on socket."E_WARNING);
            }

            
    $data fread($socket4096);

            
    fclose($socket);

            if(empty(
    $data) or $data === false) {
                throw new 
    PmQueryException("Server failed to respond"E_WARNING);
            }
            if(
    substr($data01) !== "\x1C") {
                throw new 
    PmQueryException("First byte is not ID_UNCONNECTED_PONG."E_WARNING);
            }
            if(
    substr($data1716) !== $OFFLINE_MESSAGE_DATA_ID) {
                throw new 
    PmQueryException("Magic bytes do not match.");
            }

            
    // TODO: What are the 2 bytes after the magic?
            
    $data = \substr($data35);

            
    // TODO: If server-name contains a ';' it is not escaped, and will break this parsing
            
    $data = \explode(';'$data);

            return [
                
    "{GameName}" => $data[0],
                
    "{HostName}" => $data[1],
                
    "{Protocol}" => $data[2],
                
    "{Version}" => $data[3],
                
    "{Players}" => $data[4],
                
    "{MaxPlayers}" => $data[5],
                
    "{Unknown2}" => $data[6], // TODO: What is this?
                
    "{Map}" => $data[7],
                
    "{GameMode}" => $data[8],
                
    "{Unknown3}" => $data[9], // TODO: What is this?
            
    ];
            
        
        
            }
        
        }
    }
    Sorry I basically stole the code jasonwynn10, but it wouldn't let me use your class externally as a ScoreHud addon so I had to put it in....
     
    jasonwynn10 likes this.
  3. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    All Good. Remember to change out the exception classes though or it won't function on other servers ;)
     
    ethaniccc 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.