NotchQuery - Grab the motd, player/maxplayer from 1.8 servers

Discussion in 'Bukkit Tools' started by tehbeard, Sep 15, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    tehbeard

    Little script I made this morning that emulates the query process 1.8 clients do.
    Ip : port are the ones the CLIENT USES, not minequery's port. If in doubt, if you don't have to add a port to connect in MC, you don't need to here.

    PHP:
    <?php
    if(!function_exists("json_decode")){
    function 
    json_decode ($json)
      {
          
    $json str_replace(array("\\\\""\\\""), array("&#92;""&#34;"), $json);
          
    $parts preg_split("@(\"[^\"]*\")|([\[\]\{\},:])|\s@is"$json, -1PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE);
          foreach (
    $parts as $index => $part)
          {
              if (
    strlen($part) == 1)
              {
                  switch (
    $part)
                  {
                      case 
    "[":
                      case 
    "{":
                          
    $parts[$index] = "array(";
                          break;
                      case 
    "]":
                      case 
    "}":
                          
    $parts[$index] = ")";
                          break;
                      case 
    ":":
                        
    $parts[$index] = "=>";
                        break;
                      case 
    ",":
                        break;
                      default:
                          return 
    null;
                  }
              }
              else
              {
                  if ((
    substr($part01) != "\"") || (substr($part, -11) != "\""))
                  {
                      return 
    null;
                  }
              }
          }
          
    $json str_replace(array("&#92;""&#34;""$"), array("\\\\""\\\"""\\$"), implode(""$parts));
          return eval(
    "return $json;");
      }
      }
      if(!
    function_exists("json_encode")){
    function 
    json_encode($in) {
      
    $_escape = function ($str) {
        return 
    addcslashes($str"\v\t\n\r\f\"\\/");
      };
      
    $out "";
      if (
    is_object($in)) {
        
    $class_vars get_object_vars(($in));
        
    $arr = array();
        foreach (
    $class_vars as $key => $val) {
          
    $arr[$key] = "\"{$_escape($key)}\":\"{$val}\"";
        }
        
    $val implode(','$arr);
        
    $out .= "{{$val}}";
      }elseif (
    is_array($in)) {
        
    $obj false;
        
    $arr = array();
        foreach(
    $in AS $key => $val) {
          if(!
    is_numeric($key)) {
            
    $obj true;
          }
          
    $arr[$key] = json_encode($val);
        }
        if(
    $obj) {
          foreach(
    $arr AS $key => $val) {
            
    $arr[$key] = "\"{$_escape($key)}\":{$val}";
          }
          
    $val implode(','$arr);
          
    $out .= "{{$val}}";
        }else {
          
    $val implode(','$arr);
          
    $out .= "[{$val}]";
        }
      }elseif (
    is_bool($in)) {
        
    $out .= $in 'true' 'false';
      }elseif (
    is_null($in)) {
        
    $out .= 'null';
      }elseif (
    is_string($in)) {
        
    $out .= "\"{$_escape($in)}\"";
      }else {
        
    $out .= $in;
      }
      return 
    "{$out}";
    }    }
    /**
    * Based off the fine work of the Minequery folks
    * Author: Tehbeard
    */
    class Notchquery {
        
    /**
         * Queries a Minecraft server.
         *
         * @[URL='http://forums.bukkit.org/members/static.55705/']static[/URL]
         * @param string $address The address to the minecraft server.
         * @param int $port The port of the minecraft server.
         * @param int $timeout The time given before the connection attempt gives up.
         * @return array|bool An array on success, FALSE on failure.
         */
        
    public static function query($address$port 25565$timeout 30) {
            
    $query = array();

            
    $beginning_time microtime(true);

            
    $socket = @fsockopen($address$port$errno$errstr$timeout);

            if (!
    $socket) {
                
    // Could not establish a connection to the server.
                
    return false;
            }

            
    $end_time microtime(true);

            
    fwrite($socket"\xfe");

            
    $response "";
            while(!
    feof($socket)) {
                
    $response .= fgets($socket1024);
            }


            
    $response explode("ยง"substr($response,3));

            
    $query['motd'] = $response[0];
            
    $query['playerCount'] = $response[1];
            
    $query['maxPlayers']  = $response[2];
            return 
    $query;/
        }

        
    /**
         * Queries a Minecraft erver and returns a JSON object.
         *
         * @[URL='http://forums.bukkit.org/members/static.55705/']static[/URL]
         * @param string $address The address to the minecraft server.
         * @param int $port The port of the minecraft server.
         * @param int $timeout The time given before the connection attempt gives up.
         * @return object|bool A stdClass object on success, FALSE on failure.
         */
        
    public static function query_json($address$port 25565$timeout 30) {
        return 
    json_encode(Notchquery::query($address$port$timeout));
        }

        }
        
    ?>
    No help is given, do with it as you please, but credit for this is always nice.

    Bug fix? Feature addition? Feel free to post. No support for integrating this into your site, if you don't know how, you shouldn't be messing with this code :D


    Edit: dammit, forum search didn't show the other threads till they hit page one :'(
     
    pmx and illusion9 like this.
  2. Offline

    pmx

    works great with a few tweaks =D nice work man!
     
  3. Offline

    MaMaxGER

    It dosn't works... $address = 188.40.62.35 it that right`?
     
  4. Offline

    ryanshawty

    Should work, works for me but I removed the entire JSON section as I have JSON support.
     
  5. Offline

    tehbeard

    @MaMaxGER
    Notchquery::query("188.40.62.35"); is what you should be using
     
  6. Offline

    Birdie

    Dreamweaver gives syntax error in line 44, :O
     
  7. Offline

    madmac

    So do I For this code

    PHP:
        foreach ($class_vars as $key => $val) {
          
    $arr[$key] = "\"{$_escape($key)}\":\"{$val}\"";
        }
     
Thread Status:
Not open for further replies.

Share This Page