Class: Glowstone::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "localhost", options = {}) ⇒ Server



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/glowstone/server.rb', line 12

def initialize(host="localhost", options={})
  
  # backwards compatibility for old style invocation
  if options.is_a?(Numeric)
    options = {:port => options.to_i}
  end

  # set server name
  @name = options[:name] ? options[:name].to_s : host

  # if the host is a srv record, get its info
  begin
    resource = Resolv::DNS.new.getresource("_minecraft._tcp.#{host}", Resolv::DNS::Resource::IN::SRV)
    @host = resource.target.to_s
    @port = resource.port.to_i
  rescue
    @host = host
    @port = options[:port] ? options[:port].to_i : DEFAULT_PORT
  end

  # set request timeout
  @timeout = options[:timeout] ? options[:timeout].to_i : DEFAULT_SOCKET_TIMEOUT

  # create a reusable UDP socket
  @socket = UDPSocket.new
  @socket.connect(@host, @port)
  refresh
end

Instance Attribute Details

#gamemodeObject (readonly)

Returns the value of attribute gamemode.



10
11
12
# File 'lib/glowstone/server.rb', line 10

def gamemode
  @gamemode
end

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/glowstone/server.rb', line 8

def host
  @host
end

#map_nameObject (readonly)

Returns the value of attribute map_name.



10
11
12
# File 'lib/glowstone/server.rb', line 10

def map_name
  @map_name
end

#max_playersObject (readonly)

Returns the value of attribute max_players.



10
11
12
# File 'lib/glowstone/server.rb', line 10

def max_players
  @max_players
end

#motdObject (readonly)

Returns the value of attribute motd.



10
11
12
# File 'lib/glowstone/server.rb', line 10

def motd
  @motd
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/glowstone/server.rb', line 9

def name
  @name
end

#num_playersObject (readonly)

Returns the value of attribute num_players.



10
11
12
# File 'lib/glowstone/server.rb', line 10

def num_players
  @num_players
end

#playersObject (readonly)

Returns the value of attribute players.



10
11
12
# File 'lib/glowstone/server.rb', line 10

def players
  @players
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



10
11
12
# File 'lib/glowstone/server.rb', line 10

def plugins
  @plugins
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/glowstone/server.rb', line 8

def port
  @port
end

#timeoutObject

Returns the value of attribute timeout.



9
10
11
# File 'lib/glowstone/server.rb', line 9

def timeout
  @timeout
end

#versionObject (readonly)

Returns the value of attribute version.



10
11
12
# File 'lib/glowstone/server.rb', line 10

def version
  @version
end

Instance Method Details

#refreshObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/glowstone/server.rb', line 41

def refresh
  response = perform_request(MAGIC_BYTES + REQUEST_BYTE[:query] + CLIENT_ID + get_session_id + CLIENT_ID)
  status = StatusPacket.read response

  # there's some kind of weird bug with BinData wherein its native
  # datatypes don't play well with rails, so i'm converting them all here
  @motd = status.motd.to_s
  @gamemode = status.gametype.to_s
  @version = status.version.to_s

  @plugins = (status.plugins.empty?) ? nil : status.plugins.to_s.gsub!(/^(craft)?bukkit[^:]*:\s*/i, "").split(/;\s*/)
  @map_name = status.map_name.to_s
  @num_players = status.num_players.to_i
  @max_players = status.max_players.to_i

  player_array = status.players.to_ary
  player_array.each_with_index do |player, index|
    player_array[index] = player.to_s
  end
  @players = player_array

  self
end

#to_hashObject



65
66
67
68
69
70
71
72
73
# File 'lib/glowstone/server.rb', line 65

def to_hash
  vars_to_skip = ["@timeout", "@socket"]

  hash = Hash.new
  instance_variables.each do |var|
    hash[var.to_s.delete("@").to_sym] = instance_variable_get(var) unless vars_to_skip.include?(var.to_s)
  end
  hash
end