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.5"
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



811
812
813
# File 'lib/minestat.rb', line 811

def address
  @address
end

#connection_statusObject (readonly)

Connection status

Since:

  • 2.2.2



893
894
895
# File 'lib/minestat.rb', line 893

def connection_status
  @connection_status
end

#current_playersObject (readonly)

Current player count



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

def current_players
  @current_players
end

#debugObject (readonly)

Whether or not debug mode is enabled

Since:

  • 3.0.0



900
901
902
# File 'lib/minestat.rb', line 900

def debug
  @debug
end

#faviconObject (readonly)

Note:

Received using SLP 1.7 (JSON) queries

Decoded favicon

Since:

  • 2.2.2



878
879
880
# File 'lib/minestat.rb', line 878

def favicon
  @favicon
end

#favicon_b64Object (readonly)

Note:

Received using SLP 1.7 (JSON) queries

Base64-encoded favicon

Since:

  • 2.2.2



873
874
875
# File 'lib/minestat.rb', line 873

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



868
869
870
# File 'lib/minestat.rb', line 868

def json_data
  @json_data
end

#latencyObject (readonly)

Ping time to the server in milliseconds (ms)

Since:

  • 0.2.1



882
883
884
# File 'lib/minestat.rb', line 882

def latency
  @latency
end

#max_playersObject (readonly)

Maximum player limit



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

def max_players
  @max_players
end

#modeObject (readonly)

Note:

Bedrock/Pocket Edition only

Game mode

Since:

  • 2.2.0



833
834
835
# File 'lib/minestat.rb', line 833

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:



838
839
840
# File 'lib/minestat.rb', line 838

def motd
  @motd
end

#onlineObject (readonly)

Whether or not the Minecraft server is online



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

def online
  @online
end

#player_listObject (readonly)

Note:

UT3/GS4 query only

List of players

Since:

  • 3.0.0



854
855
856
# File 'lib/minestat.rb', line 854

def player_list
  @player_list
end

#plugin_listObject (readonly)

Note:

UT3/GS4 query only

List of plugins

Since:

  • 3.0.0



859
860
861
# File 'lib/minestat.rb', line 859

def plugin_list
  @plugin_list
end

#portObject (readonly)

Port (TCP or UDP) of the Minecraft server



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

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



863
864
865
# File 'lib/minestat.rb', line 863

def protocol
  @protocol
end

#request_typeObject (readonly)

Protocol used to request data from a Minecraft server



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

def request_type
  @request_type
end

#srv_addressObject (readonly)

Address of the Minecraft server from a DNS SRV record

Since:

  • 3.0.1



818
819
820
# File 'lib/minestat.rb', line 818

def srv_address
  @srv_address
end

#srv_enabledObject (readonly)

Whether or not DNS SRV resolution is enabled

Since:

  • 3.0.1



904
905
906
# File 'lib/minestat.rb', line 904

def srv_enabled
  @srv_enabled
end

#srv_portObject (readonly)

TCP port of the Minecraft server from a DNS SRV record

Since:

  • 3.0.1



822
823
824
# File 'lib/minestat.rb', line 822

def srv_port
  @srv_port
end

#srv_succeededObject (readonly)

Whether or not DNS SRV resolution was successful

Since:

  • 3.0.2



908
909
910
# File 'lib/minestat.rb', line 908

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:



843
844
845
# File 'lib/minestat.rb', line 843

def stripped_motd
  @stripped_motd
end

#timeoutObject

TCP/UDP timeout in seconds

Since:

  • 0.1.2



886
887
888
# File 'lib/minestat.rb', line 886

def timeout
  @timeout
end

#try_allObject (readonly)

Whether or not all protocols should be attempted



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

def try_all
  @try_all
end

#versionObject (readonly)

Minecraft server version



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

def version
  @version
end