craftbelt 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/craftbelt/minecraft_instance.rb +74 -72
- data/lib/craftbelt/version.rb +1 -1
- data/spec/craftbelt/minecraft_instance_spec.rb +37 -35
- metadata +1 -1
@@ -1,101 +1,103 @@
|
|
1
1
|
require 'find'
|
2
2
|
require 'nbtfile'
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
module Craftbelt
|
5
|
+
class MinecraftInstance
|
6
|
+
attr_reader :root
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
ROOT_FILES = %w(
|
9
|
+
server.properties
|
10
|
+
ops.txt
|
11
|
+
white-list.txt
|
12
|
+
)
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
def initialize(initial_root)
|
15
|
+
@initial_root = initial_root
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
def root
|
19
|
+
@root ||= find_root!
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
def valid?
|
23
|
+
!root.nil?
|
24
|
+
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
# converts single player worlds to server worlds
|
27
|
+
def prepare!
|
28
|
+
if level_in_root?
|
29
|
+
Dir.chdir(root) do
|
30
|
+
ROOT_FILES.each do |root_file|
|
31
|
+
`rm -f #{root_file}`
|
32
|
+
end
|
33
|
+
`mkdir -p level; mv * level 2>/dev/null; touch #{ROOT_FILES.first}`
|
31
34
|
end
|
32
|
-
`mkdir -p level; mv * level 2>/dev/null; touch #{ROOT_FILES.first}`
|
33
35
|
end
|
34
36
|
end
|
35
|
-
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
38
|
+
def level_in_root?
|
39
|
+
relative_level_paths == ['.']
|
40
|
+
end
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
42
|
+
def to_h(include_paths = [])
|
43
|
+
{
|
44
|
+
root: File.expand_path(root),
|
45
|
+
paths: relative_level_paths + include_paths.select{|p| File.exist?(p) },
|
46
|
+
settings: read_settings
|
47
|
+
}
|
48
|
+
end
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
50
|
+
def level_dats
|
51
|
+
@level_dats ||= begin
|
52
|
+
paths = []
|
53
|
+
Find.find(@initial_root) do |path|
|
54
|
+
if path =~ /\/(level|uid)\.dat$/
|
55
|
+
paths << path
|
56
|
+
end
|
55
57
|
end
|
58
|
+
paths
|
56
59
|
end
|
57
|
-
paths
|
58
60
|
end
|
59
|
-
end
|
60
61
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
62
|
+
def read_settings
|
63
|
+
settings = {}
|
64
|
+
begin
|
65
|
+
nbt = NBTFile.read(File.open(level_dats('.').first))
|
66
|
+
settings = {
|
67
|
+
seed: nbt[1]['Data']['RandomSeed'].value.to_s
|
68
|
+
}
|
69
|
+
rescue
|
70
|
+
# nbt read failed
|
71
|
+
end
|
72
|
+
settings
|
70
73
|
end
|
71
|
-
settings
|
72
|
-
end
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
75
|
+
def level_paths
|
76
|
+
@level_paths ||= level_dats.map{|file| File.dirname(file).gsub(/^\.\//, '') }
|
77
|
+
end
|
77
78
|
|
78
|
-
|
79
|
-
|
80
|
-
|
79
|
+
def relative_level_paths
|
80
|
+
level_paths.map{|p| relative_path(root, p) }.uniq
|
81
|
+
end
|
81
82
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
83
|
+
def relative_path(root, path)
|
84
|
+
relative = (path.split('/') - root.split('/')).join('/')
|
85
|
+
relative == '' ? '.' : relative
|
86
|
+
end
|
86
87
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
88
|
+
def find_root!
|
89
|
+
Find.find(@initial_root) do |path|
|
90
|
+
ROOT_FILES.each do |root_file|
|
91
|
+
if path.end_with? root_file
|
92
|
+
return File.dirname(path)
|
93
|
+
end
|
92
94
|
end
|
93
95
|
end
|
94
|
-
end
|
95
96
|
|
96
|
-
|
97
|
-
|
98
|
-
|
97
|
+
# no root files, might be single player world
|
98
|
+
if level_paths.any?
|
99
|
+
File.expand_path(level_paths.min)
|
100
|
+
end
|
99
101
|
end
|
100
102
|
end
|
101
103
|
end
|
data/lib/craftbelt/version.rb
CHANGED
@@ -1,47 +1,49 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'craftbelt/minecraft_instance'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
module Craftbelt
|
5
|
+
describe MinecraftInstance do
|
6
|
+
def self.paths(paths)
|
7
|
+
before do
|
8
|
+
stub = Find.stub(:find)
|
9
|
+
paths.each do |path|
|
10
|
+
stub.and_yield(path)
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
12
|
-
end
|
13
14
|
|
14
|
-
|
15
|
+
subject { MinecraftInstance.new('.') }
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
17
|
+
context 'single player world' do
|
18
|
+
paths %w(
|
19
|
+
tmp/server/level.dat
|
20
|
+
tmp/server/region/r.0.0.mca
|
21
|
+
)
|
22
|
+
its(:root) { should end_with 'tmp/server' }
|
23
|
+
its(:level_paths) { should == ['tmp/server'] }
|
24
|
+
it "should serialize" do
|
25
|
+
subject.to_h.should == {
|
26
|
+
root: File.expand_path('tmp/server'),
|
27
|
+
paths: ['.'],
|
28
|
+
settings: {}
|
29
|
+
}
|
30
|
+
end
|
29
31
|
end
|
30
|
-
end
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
33
|
+
context 'server world' do
|
34
|
+
paths %w(
|
35
|
+
tmp/server/server.properties
|
36
|
+
tmp/server/level/level.dat
|
37
|
+
)
|
38
|
+
its(:root) { should end_with 'tmp/server' }
|
39
|
+
its(:level_paths) { should == ['tmp/server/level'] }
|
40
|
+
it "should serialize" do
|
41
|
+
subject.to_h.should == {
|
42
|
+
root: File.expand_path('tmp/server'),
|
43
|
+
paths: ['level'],
|
44
|
+
settings: {}
|
45
|
+
}
|
46
|
+
end
|
45
47
|
end
|
46
48
|
end
|
47
49
|
end
|