Method: MCQuery.fullQuery

Defined in:
lib/query/query.rb

.fullQuery(addr = 'localhost', port = 25565) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/query/query.rb', line 49

def self.fullQuery(addr = 'localhost', port = 25565)
    @addr = addr
    @port = port
    init
    begin
        Timeout.timeout(1) do
            query = @sock.send("\xFE\xFD\x00\x01\x02\x03\x04".force_encoding(Encoding::ASCII_8BIT) + @key.to_s + "\x01\x02\x03\x04".force_encoding(Encoding::ASCII_8BIT), 0)
            data = @sock.recvfrom(1460)[0]
            buffer = data[11...-1]
            items, players = buffer.split("\x00\x00\x01player_\x00\x00".force_encoding(Encoding::ASCII_8BIT))
            if items[0...8] == 'hostname'
                items = 'motd' + items[8...-1]
            end
            vals = {}
            items = items.split("\x00")
            items.each_with_index do |key, idx|
                next unless idx % 2 == 0
                vals[key] = items[idx + 1]
            end
            
            vals["motd"] = vals["hostname"]
            vals.delete("hostname")
            vals.delete("um") if vals["um"]
            
            players = players[0..-2] if players
            if players
                vals[:players] = players.split("\x00")
            end
            puts vals
            vals["raw_plugins"] = vals["plugins"]
            parts = vals["raw_plugins"].split(":")
            server = parts[0].strip() if parts[0]
            plugins = []
            if parts.size == 2
                plugins = parts[1].split(";").map {|value| value.strip() }
            end
            vals["plugins"] = plugins
            vals["server"] = server
            vals["timestamp"] = Time.now
            return vals.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
        end
    rescue StandardError => e
        puts e
        #return "An other error occured. Check your minecraft config and tell the tech monkey this:"
        raise e
    end
end