Class: RCON::Minecraft

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 25575) ⇒ Minecraft

Given a host and a port (dotted-quad or hostname OK), creates a Query::Minecraft object. Note that this will still require an authentication packet (see the auth() method) before commands can be sent.



297
298
299
300
301
302
303
304
# File 'lib/rcon/rcon.rb', line 297

def initialize(host = 'localhost', port = 25575)
   @host = host
   @port = port
   @socket = nil
   @packet = nil
   @authed = false
   @return_packets = false
end

Instance Attribute Details

#authedObject (readonly)

Authentication Status



286
287
288
# File 'lib/rcon/rcon.rb', line 286

def authed
  @authed
end

#hostObject (readonly)

Host of connection



282
283
284
# File 'lib/rcon/rcon.rb', line 282

def host
  @host
end

#packetObject (readonly)

Packet::Source object that was sent as a result of the last query



278
279
280
# File 'lib/rcon/rcon.rb', line 278

def packet
  @packet
end

#portObject (readonly)

Port of connection



284
285
286
# File 'lib/rcon/rcon.rb', line 284

def port
  @port
end

#return_packetsObject

return full packet, or just data?



288
289
290
# File 'lib/rcon/rcon.rb', line 288

def return_packets
  @return_packets
end

#socketObject (readonly)

TCPSocket object



280
281
282
# File 'lib/rcon/rcon.rb', line 280

def socket
  @socket
end

Instance Method Details

#auth(password) ⇒ Object Also known as: authenticate



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/rcon/rcon.rb', line 352

def auth(password)
  establish_connection

  @packet = Packet::Source.new
  @packet.auth(password)

  @socket.print @packet.to_s
  rpacket = nil
  rpacket = build_response_packet

  if rpacket.command_type != Packet::Source::RESPONSE_AUTH
    raise NetworkException.new("error authenticating: #{rpacket.command_type}")
  end

  @authed = true
  if @return_packets
    return rpacket
  else
    return true
  end
end

#command(command) ⇒ Object

Sends a RCon command to the server. May be used multiple times after an authentication is successful.



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/rcon/rcon.rb', line 323

def command(command)

  if ! @authed
    raise NetworkException.new("You must authenticate the connection successfully before sending commands.")
  end

  @packet = Packet::Source.new
  @packet.command(command)

  @socket.print @packet.to_s
  rpacket = build_response_packet

  if rpacket.command_type != Packet::Source::RESPONSE_NORM
    raise NetworkException.new("error sending command: #{rpacket.command_type}")
  end

  if @return_packets
    return rpacket
  else
    return rpacket.string1
  end
end

#cvar(cvar_name) ⇒ Object

See Query#cvar.



310
311
312
313
314
315
316
# File 'lib/rcon/rcon.rb', line 310

def cvar(cvar_name)
  return_packets = @return_packets
  @return_packets = false
  response = super
  @return_packets = return_packets
  return response
end

#disconnectObject

Disconnects from the Minecraft server.



380
381
382
383
384
385
386
# File 'lib/rcon/rcon.rb', line 380

def disconnect
  if @socket
    @socket.close
    @socket = nil
    @authed = false
  end
end