Class: Minecraft::Server
- Inherits:
-
Object
- Object
- Minecraft::Server
- 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
-
#sin ⇒ Object
Returns the value of attribute sin.
Instance Method Summary collapse
-
#initialize(command, opts) ⇒ Server
constructor
New Server instance.
-
#minecraft_exit ⇒ Object
An exit hook, checks if mobs need to be untoggled and saves the server.
Constructor Details
#initialize(command, opts) ⇒ Server
New Server instance.
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
#sin ⇒ Object
Returns the value of attribute sin.
8 9 10 |
# File 'lib/minecraft/server.rb', line 8 def sin @sin end |
Instance Method Details
#minecraft_exit ⇒ Object
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. puts "\n[+] Saving..." @extensions.save @threads.each(&:kill) @sin.puts("save-all") @sin.puts("stop") end |