minestat 2.1.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/minestat.rb +108 -24
  3. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bb7df47ea2b1dbc9593527397307a00e553ca5f67e7569d9c3d92bf26c971ef
4
- data.tar.gz: 6dd403da69ee5878a58e7b1c973b937f57f04f607d86774fb87febd46514db54
3
+ metadata.gz: b2824d765e8d40dd3a1087567b27a9a04c9df93427b344c8e47346a0a778f5be
4
+ data.tar.gz: 5974801cb768bc9a67cd789a0296b569c1f9b14442d7124135c3ddbd8d41886b
5
5
  SHA512:
6
- metadata.gz: 54c7a4cfd1fb9c07e83529f513dcb800dbd2464044389a18c08f22c8bb7435f963fdfc5acacdd174774f9b65a3dec22391f76f1e856499ca48a64f4f5e03cd9e
7
- data.tar.gz: ce1ae19de3e86ded84a5205ec38c50218181add5eaf5965e8578102299c7cd51e316a37bd58437abb2aa6f154d6b067644a0a6485fb9c823972572d3ded02a69
6
+ metadata.gz: fc7bb79f048f5d280405638d416cb74ef8e0da6873074bd6e1b70c8176c913fc5fa59ffb92aaa3673f972225851b533964402cfc76160d1d5caec4bf207c43a2
7
+ data.tar.gz: 885fb2073958d8a95f121ed7a45e230987520dd0878744650bfe6ec45082a75a1e5dc9d512e76aff6dad965b3a36e493a76e9e5e6bd085bde14d2dd562eb33e6
data/lib/minestat.rb CHANGED
@@ -20,29 +20,54 @@ require 'json'
20
20
  require 'socket'
21
21
  require 'timeout'
22
22
 
23
+ ##
24
+ # Provides a ruby interface for polling Minecraft server status.
23
25
  class MineStat
24
- VERSION = "2.1.0" # MineStat version
25
- NUM_FIELDS = 6 # number of values expected from server
26
- NUM_FIELDS_BETA = 3 # number of values expected from a 1.8b/1.3 server
27
- MAX_VARINT_SIZE = 5 # maximum number of bytes a varint can be
28
- DEFAULT_PORT = 25565 # default TCP port
29
- DEFAULT_TIMEOUT = 5 # default TCP timeout in seconds
26
+ # MineStat version
27
+ VERSION = "2.1.1"
28
+ # Number of values expected from server
29
+ NUM_FIELDS = 6
30
+ # Number of values expected from a 1.8b/1.3 server
31
+ NUM_FIELDS_BETA = 3
32
+ # Maximum number of bytes a varint can be
33
+ MAX_VARINT_SIZE = 5
34
+ # Default TCP port
35
+ DEFAULT_PORT = 25565
36
+ # Default TCP timeout in seconds
37
+ DEFAULT_TIMEOUT = 5
30
38
 
39
+ ##
40
+ # Stores constants that represent the results of a server ping
31
41
  module Retval
42
+ # The server ping completed successfully
32
43
  SUCCESS = 0
44
+ # The server ping failed due to a connection error
33
45
  CONNFAIL = -1
46
+ # The server ping failed due to a connection time out
34
47
  TIMEOUT = -2
48
+ # The server ping failed for an unknown reason
35
49
  UNKNOWN = -3
36
50
  end
37
51
 
52
+ ##
53
+ # Stores constants that represent the different kinds of server
54
+ # list pings/requests that a Minecraft server might expect when
55
+ # being polled for status information.
38
56
  module Request
57
+ # Try everything
39
58
  NONE = -1
59
+ # Server versions 1.8b to 1.3
40
60
  BETA = 0
61
+ # Server versions 1.4 to 1.5
41
62
  LEGACY = 1
63
+ # Server version 1.6
42
64
  EXTENDED = 2
65
+ # Server versions 1.7 to latest
43
66
  JSON = 3
44
67
  end
45
68
 
69
+ ##
70
+ # Instantiate an instance of MineStat and poll the specified server for information
46
71
  def initialize(address, port = DEFAULT_PORT, timeout = DEFAULT_TIMEOUT, request_type = Request::NONE)
47
72
  @address = address # address of server
48
73
  @port = port # TCP port of server
@@ -94,22 +119,25 @@ class MineStat
94
119
  end
95
120
 
96
121
  # Strips message of the day formatting characters
97
- def strip_motd(is_json = false)
98
- unless is_json
99
- @stripped_motd = @motd.gsub(/§./, "")
100
- else
122
+ def strip_motd()
123
+ unless @motd['text'] == nil
101
124
  @stripped_motd = @motd['text']
125
+ else
126
+ @stripped_motd = @motd
127
+ end
128
+ unless @motd['extra'] == nil
102
129
  json_data = @motd['extra']
103
130
  unless json_data.nil? || json_data.empty?
104
131
  json_data.each do |nested_hash|
105
132
  @stripped_motd += nested_hash['text']
106
133
  end
107
134
  end
108
- @stripped_motd = @stripped_motd.gsub(/§./, "")
109
135
  end
136
+ @stripped_motd = @stripped_motd.gsub(/§./, "")
110
137
  end
111
138
 
112
- # Connects to remote server
139
+ ##
140
+ # Establishes a connection to the Minecraft server
113
141
  def connect()
114
142
  begin
115
143
  start_time = Time.now
@@ -137,7 +165,7 @@ class MineStat
137
165
  return Retval::UNKNOWN
138
166
  end
139
167
  rescue => exception
140
- $stderr.puts exception
168
+ #$stderr.puts exception
141
169
  return Retval::UNKNOWN
142
170
  end
143
171
 
@@ -174,14 +202,17 @@ class MineStat
174
202
  return Retval::SUCCESS
175
203
  end
176
204
 
177
- # 1.8b/1.3
205
+ ##
178
206
  # 1.8 beta through 1.3 servers communicate as follows for a ping request:
179
207
  # 1. Client sends \xFE (server list ping)
180
208
  # 2. Server responds with:
181
209
  # 2a. \xFF (kick packet)
182
210
  # 2b. data length
183
211
  # 2c. 3 fields delimited by \u00A7 (section symbol)
184
- # The 3 fields, in order, are: message of the day, current players, and max players
212
+ # The 3 fields, in order, are:
213
+ # * message of the day
214
+ # * current players
215
+ # * max players
185
216
  def beta_request()
186
217
  retval = nil
187
218
  begin
@@ -202,7 +233,7 @@ class MineStat
202
233
  return retval
203
234
  end
204
235
 
205
- # 1.4/1.5
236
+ ##
206
237
  # 1.4 and 1.5 servers communicate as follows for a ping request:
207
238
  # 1. Client sends:
208
239
  # 1a. \xFE (server list ping)
@@ -211,8 +242,14 @@ class MineStat
211
242
  # 2a. \xFF (kick packet)
212
243
  # 2b. data length
213
244
  # 2c. 6 fields delimited by \x00 (null)
214
- # The 6 fields, in order, are: the section symbol and 1, protocol version,
215
- # server version, message of the day, current players, and max players
245
+ # The 6 fields, in order, are:
246
+ # * the section symbol and 1
247
+ # * protocol version
248
+ # * server version
249
+ # * message of the day
250
+ # * current players
251
+ # * max players
252
+ #
216
253
  # The protocol version corresponds with the server version and can be the
217
254
  # same for different server versions.
218
255
  def legacy_request()
@@ -235,7 +272,7 @@ class MineStat
235
272
  return retval
236
273
  end
237
274
 
238
- # 1.6
275
+ ##
239
276
  # 1.6 servers communicate as follows for a ping request:
240
277
  # 1. Client sends:
241
278
  # 1a. \xFE (server list ping)
@@ -252,8 +289,14 @@ class MineStat
252
289
  # 2a. \xFF (kick packet)
253
290
  # 2b. data length
254
291
  # 2c. 6 fields delimited by \x00 (null)
255
- # The 6 fields, in order, are: the section symbol and 1, protocol version,
256
- # server version, message of the day, current players, and max players
292
+ # The 6 fields, in order, are:
293
+ # * the section symbol and 1
294
+ # * protocol version
295
+ # * server version
296
+ # * message of the day
297
+ # * current players
298
+ # * max players
299
+ #
257
300
  # The protocol version corresponds with the server version and can be the
258
301
  # same for different server versions.
259
302
  def extended_legacy_request()
@@ -283,7 +326,7 @@ class MineStat
283
326
  return retval
284
327
  end
285
328
 
286
- # 1.7
329
+ ##
287
330
  # 1.7 to current servers communicate as follows for a ping request:
288
331
  # 1. Client sends:
289
332
  # 1a. \x00 (handshake packet containing the fields specified below)
@@ -329,7 +372,7 @@ class MineStat
329
372
  @protocol = json_data['version']['protocol'].to_i
330
373
  @version = json_data['version']['name']
331
374
  @motd = json_data['description']
332
- strip_motd(true)
375
+ strip_motd
333
376
  @current_players = json_data['players']['online'].to_i
334
377
  @max_players = json_data['players']['max'].to_i
335
378
  if !@version.empty? && !@motd.empty? && !@current_players.nil? && !@max_players.nil?
@@ -381,5 +424,46 @@ class MineStat
381
424
  return vint
382
425
  end
383
426
 
384
- attr_reader :address, :port, :online, :version, :motd, :stripped_motd, :current_players, :max_players, :protocol, :json_data, :latency, :request_type
427
+ # Returns the Minecraft server IP
428
+ attr_reader :address
429
+
430
+ # Returns the Minecraft server TCP port
431
+ attr_reader :port
432
+
433
+ # Returns a boolean describing whether the server is online or offline
434
+ attr_reader :online
435
+
436
+ # Returns the Minecraft version that the server is running
437
+ attr_reader :version
438
+
439
+ # Returns the full version of the MOTD
440
+ #
441
+ # If you just want the MOTD text, use stripped_motd
442
+ attr_reader :motd
443
+
444
+ # Returns just the plain text contained within the MOTD
445
+ attr_reader :stripped_motd
446
+
447
+ # Returns the current player count
448
+ attr_reader :current_players
449
+
450
+ # Returns the maximum player count
451
+ attr_reader :max_players
452
+
453
+ # Returns the SLP (Server List Ping) protocol level
454
+ #
455
+ # This is arbitrary and varies by Minecraft version.
456
+ # However, multiple Minecraft versions can share the same
457
+ # protocol level
458
+ attr_reader :protocol
459
+
460
+ # Returns the complete JSON response data for queries to Minecraft
461
+ # servers with a version greater than or equal to 1.7
462
+ attr_reader :json_data
463
+
464
+ # Returns the ping time to the server in ms
465
+ attr_reader :latency
466
+
467
+ # Returns the SLP (Server List Ping) protocol version
468
+ attr_reader :request_type
385
469
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minestat
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lloyd Dilley
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-09-08 00:00:00.000000000 Z
12
+ date: 2021-09-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: MineStat polls Minecraft server data such as version, motd, current players,
15
15
  and max players.
@@ -39,8 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  requirements: []
42
- rubyforge_project:
43
- rubygems_version: 2.7.6.2
42
+ rubygems_version: 3.2.22
44
43
  signing_key:
45
44
  specification_version: 4
46
45
  summary: Minecraft server status checker