-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmpserver.rb
130 lines (106 loc) · 2.54 KB
/
smpserver.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require 'pp'
class SMPServer
attr_reader :root
attr_accessor :name
attr_accessor :desc
def initialize(root, name = nil, desc = nil)
@root = root
@name = name
@name = read_name if @name.nil?
@desc = desc
@desc = read_desc if @desc.nil?
end
def read_name
read_mcweb_line(0, "Unknown")
end
def read_desc
read_mcweb_line(1, "Minecraft SMP Server")
end
def read_mcweb_line(idx, default = nil)
mcw_path = File.join(@root, ".mcweb")
if File.exist?(mcw_path)
contents = IO.read(mcw_path).split("\n")
if idx < contents.length
return contents[idx]
end
end
return default
end
def running?
File.exist?(File.join(@root, "server.log.lck"))
end
def port
prop_path = File.join(@root, "server.properties")
if File.exist?(prop_path)
File.open(prop_path) do |f|
while line = f.gets
if line.start_with?("server-port=")
return line[12..-1].to_i
end
end
end
else
return 25565
end
end
def self.mcfiles
[".mcweb", "server.properties", "craftbukkit-0.0.1-SNAPSHOT.jar", "Minecraft_Server.exe", "minecraft_server.jar"]
end
def self.find_servers(root)
if File.exist?(root)
SMPServer.mcfiles.each do |fname|
if File.exist?(File.join(root, fname))
puts "Found server at #{root}"
return [ SMPServer.new(root) ]
end
end
accum = []
Dir[File.join(root, '*/')].each do |subdir|
accum = accum + SMPServer.find_servers(subdir)
end
puts "Recursed; found servers #{accum}"
return accum
else
puts "#{root} doesn't exist"
return []
end
end
def relative_root(real_root)
if @root.start_with?(real_root)
rel = @root[real_root.length..-1]
if rel.start_with?("/")
return rel[1..-1]
else
return rel
end
else
return @root
end
end
def minecraft_files
files = []
SMPServer.mcfiles.each do |fname|
path = File.join(root, fname)
if File.exist?(path)
if File.symlink?(path)
fname = File.readlink(path)
end
files << fname
end
end
return files
end
def plugins
plugins = []
plugin_path = File.join(root, "plugins")
if File.exist?(plugin_path) && File.directory?(plugin_path)
Dir["#{plugin_path}/*.jar"].each do |p|
if File.symlink?(p)
p = File.readlink(p)
end
plugins << File.basename(p)
end
end
return plugins
end
end