-
Notifications
You must be signed in to change notification settings - Fork 2
/
manager.rb
executable file
·83 lines (71 loc) · 1.86 KB
/
manager.rb
1
2
3
4
5
6
7
8
9
10
11
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
require 'active_support/core_ext/string'
require 'open3'
require 'yaml'
require './system_cmds.rb'
BASE_DIR = Dir.pwd
PLUGIN_DIR = File.join(BASE_DIR, 'plugins')
if File.exists? 'minecraft.yml'
config_file = 'minecraft.yml'
elsif File.exists? 'config/minecraft.yml'
config_file = 'config/minecraft.yml'
else
puts "Unable to find minecraft.yml config file please copy the 'config/minecraft.yml.example' file to 'config/minecraft.yml' and update it with your settings"
exit
end
$CONFIG = YAML.load_file(config_file)
Xmx = $CONFIG['Xmx'] || '1024M'
Xms = $CONFIG['Xms'] || '1024M'
cmd = "java -Xmx#{Xmx} -Xms#{Xms} -jar #{$CONFIG['server_jar']} nogui"
def get_cmd(line)
if m = line.match(/<(\w+)>\s#(\w+)\s*(.*)/)
marray = m.to_a
matched = marray.shift
cmd = { :user => marray.shift, :cmd => marray.shift.to_sym }
cmd[:opts] = marray.shift.split(' ').compact
return cmd
else
false
end
end
puts "Starting minecraft server"
Dir.chdir($CONFIG['server_path']) do
Open3.popen3(cmd) do |stdin,stdout,stderr|
ready = false
while !ready do
if select([stderr],nil,nil, 5)
output = stderr.gets
if output.match(/\[INFO\] Done/)
puts "Server ready"
ready = true
else
puts output
end
else
puts "Nothing to do"
end
end
begin
sys = SystemCmds.new(stdin, stdout, stderr)
puts "Waiting for users"
loop do
if outputs = select([stdout,stderr], nil, nil, 5)
outputs.first.each do |io|
line = io.gets
puts line
sys.process(line)
end
end
end
ensure
puts "Stopping server"
begin
sys.process('stop', 'console')
rescue => e
end
end
end
end