Class: McBlocky::Server
- Inherits:
-
Object
show all
- Includes:
- Logging
- Defined in:
- lib/mcblocky/server.rb
Constant Summary
Constants included
from Logging
Logging::BLUE, Logging::BOLD, Logging::CYAN, Logging::GREEN, Logging::MAGENTA, Logging::RED, Logging::RESET, Logging::WHITE, Logging::YELLOW
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Logging
#log_command, #log_error, #log_message, #log_server, #log_status
Constructor Details
#initialize(jar, workdir, java = nil, ops = nil) ⇒ Server
Returns a new instance of Server.
47
48
49
50
51
52
53
54
55
|
# File 'lib/mcblocky/server.rb', line 47
def initialize(jar, workdir, java=nil, ops=nil)
@java = java || 'java'
@jar = jar
@workdir = workdir
@queue = Queue.new
@matchers = []
@message_matchers = []
@ops = ops
end
|
Class Method Details
.from_config ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/mcblocky/server.rb', line 8
def self.from_config
Config.validate
server = Config.config['server']
workdir = File.expand_path(server['workdir'], File.dirname(Config.config_path))
Dir.mkdir workdir unless Dir.exist? workdir
Dir.chdir workdir do
if server['eula']
open('eula.txt', 'w') do |f|
f.write("eula=true#{$/}")
end
end
set_server_properties(server['properties'])
end
return Server.new(server['jar'], workdir, server['java'], server['ops'])
end
|
.set_server_properties(properties, filename = 'server.properties') ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/mcblocky/server.rb', line 25
def self.set_server_properties(properties, filename='server.properties')
lines = if File.exist? filename
open(filename).readlines
else
[]
end
properties.each do |k,v|
if !lines.select{|l| l.start_with? "#{k}="}.empty?
lines.map! do |l|
if l.start_with? "#{k}="
"#{k}=#{v}#{$/}"
else
l
end
end
else
lines << "#{k}=#{v}#{$/}"
end
end
IO.write(filename, lines.join(''))
end
|
Instance Method Details
#command(cmd) ⇒ Object
82
83
84
85
|
# File 'lib/mcblocky/server.rb', line 82
def command(cmd)
log_command cmd
@stdin.write("#{cmd}#{$/}")
end
|
#loop! ⇒ Object
110
111
112
113
114
115
|
# File 'lib/mcblocky/server.rb', line 110
def loop!
wait_for_line nil
rescue ServerShutdown
log_status "Server stopped."
join
end
|
#on_line(match, &block) ⇒ Object
92
93
94
|
# File 'lib/mcblocky/server.rb', line 92
def on_line(match, &block)
@matchers << [match, block]
end
|
#on_message(match, user = nil, &block) ⇒ Object
96
97
98
|
# File 'lib/mcblocky/server.rb', line 96
def on_message(match, user=nil, &block)
@message_matchers << [match, user, block]
end
|
#say(message) ⇒ Object
87
88
89
90
|
# File 'lib/mcblocky/server.rb', line 87
def say(message)
log_message "[Server] #{message}"
@stdin.write("say #{message}#{$/}")
end
|
#start ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/mcblocky/server.rb', line 57
def start
Dir.chdir @workdir do
@stdin, @stdout, @wait_thr = Open3.popen2e "#{@java} -jar #{@jar} nogui"
end
@reader = Thread.new(@stdout) do |stream|
until stream.closed?
begin
line = stream.readline
@queue << line
log_server line
if line =~ /\<([^>]+)\> (.*)$/
log_message "<#{$1}> #{$2}"
end
rescue EOFError
break
end
end
Thread.main.raise ServerShutdown
end
wait_for_line /Done \(.*?\)!/
if @ops
@ops.each {|op| command "op #{op}"}
end
end
|
#stop ⇒ Object
100
101
102
103
104
105
106
107
108
|
# File 'lib/mcblocky/server.rb', line 100
def stop
unless @stopping
@matchers = []
command "stop"
@stdin.close
@stopping = true
end
join
end
|