Class: Minecraft::Server

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

Overview

An instance of the server class will start a Java subprocess running the Minecraft server jarfile. The interrupt signal will be trapped to run an exit hook and threads will be created to process all pipes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, opts) ⇒ Server

New Server instance.

Examples:

opts = Slop.new do
  ...
end
server = Server.new("java -jar minecraft_server.jar", opts)

Parameters:

  • command (String)

    The command for the subprocess.

  • opts (Slop)

    Command line options from Slop.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/minecraft/server.rb', line 19

def initialize(command, opts)
  @sin, @sout, @serr, @thr = Open3.popen3(command)
  @extensions = Extensions.new(@sin, opts)
  @opts = opts

  trap("SIGINT") { minecraft_exit }

  @threads = []
  @threads << Thread.new { loop { @extensions.process(@sout.gets) } }
  @threads << Thread.new { loop { @extensions.process(@serr.gets) } }
  @threads << Thread.new { loop { @extensions.periodic; sleep 1 } }
  @threads << Thread.new { loop { @sin.puts $stdin.gets } }
  @thr.value
  exit!
end

Instance Attribute Details

#sinObject

Returns the value of attribute sin.



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

def sin
  @sin
end

Instance Method Details

#minecraft_exitObject

An exit hook, checks if mobs need to be untoggled and saves the server. Server is stopped gracefully.



37
38
39
40
41
42
43
44
45
46
# File 'lib/minecraft/server.rb', line 37

def minecraft_exit
  puts "[+] Restoring previous mob state to #{Minecraft::Tools.toggle_mobs}." if @opts[:tempmobs]
  puts "[~] The current welcome message is:"
  puts @extensions.welcome_message
  puts "\n[+] Saving..."
  @extensions.save
  @threads.each(&:kill)
  @sin.puts("save-all")
  @sin.puts("stop")
end