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.



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

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



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

def authed
  @authed
end

#hostObject (readonly)

Host of connection



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

def host
  @host
end

#packetObject (readonly)

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



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

def packet
  @packet
end

#portObject (readonly)

Port of connection



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

def port
  @port
end

#return_packetsObject

return full packet, or just data?



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

def return_packets
  @return_packets
end

#socketObject (readonly)

TCPSocket object



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

def socket
  @socket
end

Instance Method Details

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



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

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.



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

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.



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

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.



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

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