Class: MineStat

Inherits:
Object
  • Object
show all
Defined in:
lib/minestat.rb

Overview

Provides a Ruby interface for polling the status of Minecraft servers

Defined Under Namespace

Modules: Request, Retval

Constant Summary collapse

VERSION =

MineStat version

"3.0.3"
DEFAULT_TCP_PORT =

Default TCP port

25565
DEFAULT_BEDROCK_PORT =

Bedrock/Pocket Edition default UDP port

19132
DEFAULT_TIMEOUT =

Default TCP/UDP timeout in seconds

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, port = DEFAULT_TCP_PORT, timeout = DEFAULT_TIMEOUT, request_type = Request::NONE, debug = false, srv_enabled = true) ⇒ MineStat

Instantiates a MineStat object and polls the specified server for information

Examples:

Simply connect to an address

ms = MineStat.new("frag.land")

Connect to an address on a certain TCP or UDP port

ms = MineStat.new("frag.land", 25565)

Same as above example and additionally includes a timeout in seconds

ms = MineStat.new("frag.land", 25565, 3)

Same as above example and additionally includes an explicit protocol to use

ms = MineStat.new("frag.land", 25565, 3, MineStat::Request::QUERY)

Connect to a Bedrock server and enable debug mode

ms = MineStat.new("minecraft.frag.land", 19132, 3, MineStat::Request::BEDROCK, true)

Attempt all SLP protocols, disable debug mode, and disable DNS SRV resolution

ms = MineStat.new("minecraft.frag.land", 25565, 3, MineStat::Request::SLP, false, false)

Parameters:

  • address (String)

    Minecraft server address

  • port (Integer) (defaults to: DEFAULT_TCP_PORT)

    Minecraft server TCP or UDP port

  • timeout (Integer) (defaults to: DEFAULT_TIMEOUT)

    TCP/UDP timeout in seconds

  • request_type (Request) (defaults to: Request::NONE)

    Protocol used to poll a Minecraft server

  • debug (Boolean) (defaults to: false)

    Enable or disable error output



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/minestat.rb', line 144

def initialize(address, port = DEFAULT_TCP_PORT, timeout = DEFAULT_TIMEOUT, request_type = Request::NONE, debug = false, srv_enabled = true)
  @address = address         # address of server
  @port = port               # TCP/UDP port of server
  @srv_address               # server address from DNS SRV record
  @srv_port                  # server TCP port from DNS SRV record
  @online                    # online or offline?
  @version                   # server version
  @mode                      # game mode (Bedrock/Pocket Edition only)
  @motd                      # message of the day
  @stripped_motd             # message of the day without formatting
  @current_players           # current number of players online
  @max_players               # maximum player capacity
  @player_list               # list of players (UT3/GS4 query only)
  @plugin_list               # list of plugins (UT3/GS4 query only)
  @protocol                  # protocol level
  @json_data                 # JSON data for 1.7 queries
  @favicon_b64               # base64-encoded favicon possibly contained in JSON 1.7 responses
  @favicon                   # decoded favicon data
  @latency                   # ping time to server in milliseconds
  @timeout = timeout         # TCP/UDP timeout
  @server                    # server socket
  @request_type              # protocol version
  @connection_status         # status of connection ("Success", "Fail", "Timeout", or "Unknown")
  @try_all = false           # try all protocols?
  @debug = debug             # debug mode
  @srv_enabled = srv_enabled # enable SRV resolution?
  @srv_succeeded = false     # SRV resolution successful?

  @try_all = true if request_type == Request::NONE
  @srv_succeeded = resolve_srv() if @srv_enabled
  set_connection_status(attempt_protocols(request_type))
end

Instance Attribute Details

#addressObject (readonly)

Address (hostname or IP address) of the Minecraft server



813
814
815
# File 'lib/minestat.rb', line 813

def address
  @address
end

#connection_statusObject (readonly)

Connection status

Since:

  • 2.2.2



895
896
897
# File 'lib/minestat.rb', line 895

def connection_status
  @connection_status
end

#current_playersObject (readonly)

Current player count



848
849
850
# File 'lib/minestat.rb', line 848

def current_players
  @current_players
end

#debugObject (readonly)

Whether or not debug mode is enabled

Since:

  • 3.0.0



902
903
904
# File 'lib/minestat.rb', line 902

def debug
  @debug
end

#faviconObject (readonly)

Note:

Received using SLP 1.7 (JSON) queries

Decoded favicon

Since:

  • 2.2.2



880
881
882
# File 'lib/minestat.rb', line 880

def favicon
  @favicon
end

#favicon_b64Object (readonly)

Note:

Received using SLP 1.7 (JSON) queries

Base64-encoded favicon

Since:

  • 2.2.2



875
876
877
# File 'lib/minestat.rb', line 875

def favicon_b64
  @favicon_b64
end

#json_dataObject (readonly)

Note:

Received using SLP 1.7 (JSON) queries

Complete JSON response data

Since:

  • 0.3.0



870
871
872
# File 'lib/minestat.rb', line 870

def json_data
  @json_data
end

#latencyObject (readonly)

Ping time to the server in milliseconds (ms)

Since:

  • 0.2.1



884
885
886
# File 'lib/minestat.rb', line 884

def latency
  @latency
end

#max_playersObject (readonly)

Maximum player limit



851
852
853
# File 'lib/minestat.rb', line 851

def max_players
  @max_players
end

#modeObject (readonly)

Note:

Bedrock/Pocket Edition only

Game mode

Since:

  • 2.2.0



835
836
837
# File 'lib/minestat.rb', line 835

def mode
  @mode
end

#motdObject (readonly)

Note:

If only the plain text MotD is relevant, use #stripped_motd

Full message of the day (MotD)

See Also:



840
841
842
# File 'lib/minestat.rb', line 840

def motd
  @motd
end

#onlineObject (readonly)

Whether or not the Minecraft server is online



827
828
829
# File 'lib/minestat.rb', line 827

def online
  @online
end

#player_listObject (readonly)

Note:

UT3/GS4 query only

List of players

Since:

  • 3.0.0



856
857
858
# File 'lib/minestat.rb', line 856

def player_list
  @player_list
end

#plugin_listObject (readonly)

Note:

UT3/GS4 query only

List of plugins

Since:

  • 3.0.0



861
862
863
# File 'lib/minestat.rb', line 861

def plugin_list
  @plugin_list
end

#portObject (readonly)

Port (TCP or UDP) of the Minecraft server



816
817
818
# File 'lib/minestat.rb', line 816

def port
  @port
end

#protocolObject (readonly)

Note:

This is arbitrary and varies by Minecraft version (may also be shared by multiple Minecraft versions)

Protocol level



865
866
867
# File 'lib/minestat.rb', line 865

def protocol
  @protocol
end

#request_typeObject (readonly)

Protocol used to request data from a Minecraft server



891
892
893
# File 'lib/minestat.rb', line 891

def request_type
  @request_type
end

#srv_addressObject (readonly)

Address of the Minecraft server from a DNS SRV record

Since:

  • 3.0.1



820
821
822
# File 'lib/minestat.rb', line 820

def srv_address
  @srv_address
end

#srv_enabledObject (readonly)

Whether or not DNS SRV resolution is enabled

Since:

  • 3.0.1



906
907
908
# File 'lib/minestat.rb', line 906

def srv_enabled
  @srv_enabled
end

#srv_portObject (readonly)

TCP port of the Minecraft server from a DNS SRV record

Since:

  • 3.0.1



824
825
826
# File 'lib/minestat.rb', line 824

def srv_port
  @srv_port
end

#srv_succeededObject (readonly)

Whether or not DNS SRV resolution was successful

Since:

  • 3.0.2



910
911
912
# File 'lib/minestat.rb', line 910

def srv_succeeded
  @srv_succeeded
end

#stripped_motdObject (readonly)

Note:

If the full MotD is desired, use #motd

Plain text contained within the message of the day (MotD)

See Also:



845
846
847
# File 'lib/minestat.rb', line 845

def stripped_motd
  @stripped_motd
end

#timeoutObject

TCP/UDP timeout in seconds

Since:

  • 0.1.2



888
889
890
# File 'lib/minestat.rb', line 888

def timeout
  @timeout
end

#try_allObject (readonly)

Whether or not all protocols should be attempted



898
899
900
# File 'lib/minestat.rb', line 898

def try_all
  @try_all
end

#versionObject (readonly)

Minecraft server version



830
831
832
# File 'lib/minestat.rb', line 830

def version
  @version
end