craftbelt 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,101 +1,103 @@
1
1
  require 'find'
2
2
  require 'nbtfile'
3
3
 
4
- class MinecraftInstance
5
- attr_reader :root
4
+ module Craftbelt
5
+ class MinecraftInstance
6
+ attr_reader :root
6
7
 
7
- ROOT_FILES = %w(
8
- server.properties
9
- ops.txt
10
- white-list.txt
11
- )
8
+ ROOT_FILES = %w(
9
+ server.properties
10
+ ops.txt
11
+ white-list.txt
12
+ )
12
13
 
13
- def initialize(initial_root)
14
- @initial_root = initial_root
15
- end
14
+ def initialize(initial_root)
15
+ @initial_root = initial_root
16
+ end
16
17
 
17
- def root
18
- @root ||= find_root!
19
- end
18
+ def root
19
+ @root ||= find_root!
20
+ end
20
21
 
21
- def valid?
22
- !root.nil?
23
- end
22
+ def valid?
23
+ !root.nil?
24
+ end
24
25
 
25
- # converts single player worlds to server worlds
26
- def prepare!
27
- if level_in_root?
28
- Dir.chdir(root) do
29
- ROOT_FILES.each do |root_file|
30
- `rm -f #{root_file}`
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
- def level_in_root?
38
- relative_level_paths == ['.']
39
- end
38
+ def level_in_root?
39
+ relative_level_paths == ['.']
40
+ end
40
41
 
41
- def to_h(include_paths = [])
42
- {
43
- root: File.expand_path(root),
44
- paths: relative_level_paths + include_paths.select{|p| File.exist?(p) },
45
- settings: read_settings
46
- }
47
- end
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
- def level_dats
50
- @level_dats ||= begin
51
- paths = []
52
- Find.find(@initial_root) do |path|
53
- if path =~ /\/(level|uid)\.dat$/
54
- paths << path
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
- def read_settings
62
- settings = {}
63
- begin
64
- nbt = NBTFile.read(File.open(level_dats('.').first))
65
- settings = {
66
- seed: nbt[1]['Data']['RandomSeed'].value.to_s
67
- }
68
- rescue
69
- # nbt read failed
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
- def level_paths
75
- @level_paths ||= level_dats.map{|file| File.dirname(file).gsub(/^\.\//, '') }
76
- end
75
+ def level_paths
76
+ @level_paths ||= level_dats.map{|file| File.dirname(file).gsub(/^\.\//, '') }
77
+ end
77
78
 
78
- def relative_level_paths
79
- level_paths.map{|p| relative_path(root, p) }.uniq
80
- end
79
+ def relative_level_paths
80
+ level_paths.map{|p| relative_path(root, p) }.uniq
81
+ end
81
82
 
82
- def relative_path(root, path)
83
- relative = (path.split('/') - root.split('/')).join('/')
84
- relative == '' ? '.' : relative
85
- end
83
+ def relative_path(root, path)
84
+ relative = (path.split('/') - root.split('/')).join('/')
85
+ relative == '' ? '.' : relative
86
+ end
86
87
 
87
- def find_root!
88
- Find.find(@initial_root) do |path|
89
- ROOT_FILES.each do |root_file|
90
- if path.end_with? root_file
91
- return File.dirname(path)
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
- # no root files, might be single player world
97
- if level_paths.any?
98
- File.expand_path(level_paths.min)
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
@@ -1,5 +1,5 @@
1
1
  module Craftbelt
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
 
4
4
  def self.version
5
5
  VERSION
@@ -1,47 +1,49 @@
1
1
  require 'spec_helper'
2
2
  require 'craftbelt/minecraft_instance'
3
3
 
4
- describe MinecraftInstance do
5
- def self.paths(paths)
6
- before do
7
- stub = Find.stub(:find)
8
- paths.each do |path|
9
- stub.and_yield(path)
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
- subject { MinecraftInstance.new('.') }
15
+ subject { MinecraftInstance.new('.') }
15
16
 
16
- context 'single player world' do
17
- paths %w(
18
- tmp/server/level.dat
19
- tmp/server/region/r.0.0.mca
20
- )
21
- its(:root) { should end_with 'tmp/server' }
22
- its(:level_paths) { should == ['tmp/server'] }
23
- it "should serialize" do
24
- subject.to_h.should == {
25
- root: File.expand_path('tmp/server'),
26
- paths: ['.'],
27
- settings: {}
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
- context 'server world' do
33
- paths %w(
34
- tmp/server/server.properties
35
- tmp/server/level/level.dat
36
- )
37
- its(:root) { should end_with 'tmp/server' }
38
- its(:level_paths) { should == ['tmp/server/level'] }
39
- it "should serialize" do
40
- subject.to_h.should == {
41
- root: File.expand_path('tmp/server'),
42
- paths: ['level'],
43
- settings: {}
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: craftbelt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: