Class: Craftbelt::Instance
- Inherits:
-
Object
- Object
- Craftbelt::Instance
- Defined in:
- lib/craftbelt/instance.rb
Constant Summary collapse
- ROOT_FILES =
%w( server.properties ops.txt white-list.txt )
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #find_root! ⇒ Object
-
#initialize(initial_root) ⇒ Instance
constructor
A new instance of Instance.
- #level_dats ⇒ Object
- #level_in_root? ⇒ Boolean
- #level_paths ⇒ Object
-
#prepare! ⇒ Object
converts single player worlds to server worlds.
- #read_settings ⇒ Object
- #relative_level_paths ⇒ Object
- #relative_path(root, path) ⇒ Object
- #to_h(include_paths = []) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(initial_root) ⇒ Instance
Returns a new instance of Instance.
14 15 16 |
# File 'lib/craftbelt/instance.rb', line 14 def initialize(initial_root) @initial_root = initial_root end |
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/craftbelt/instance.rb', line 6 def root @root end |
Instance Method Details
#find_root! ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/craftbelt/instance.rb', line 90 def find_root! Find.find(@initial_root) do |path| ROOT_FILES.each do |root_file| if path.end_with? root_file return File.(File.dirname(path)) end end end # no root files, might be single player world if level_paths.any? File.(level_paths.min) end end |
#level_dats ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/craftbelt/instance.rb', line 52 def level_dats @level_dats ||= begin paths = [] Find.find(@initial_root) do |path| if path =~ /\/(level|uid)\.dat$/ paths << path end end paths end end |
#level_in_root? ⇒ Boolean
38 39 40 |
# File 'lib/craftbelt/instance.rb', line 38 def level_in_root? relative_level_paths == ['.'] end |
#level_paths ⇒ Object
77 78 79 |
# File 'lib/craftbelt/instance.rb', line 77 def level_paths @level_paths ||= level_dats.map{|file| File.dirname(file).gsub(/^\.\//, '') } end |
#prepare! ⇒ Object
converts single player worlds to server worlds
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/craftbelt/instance.rb', line 27 def prepare! if level_in_root? Dir.chdir(root) do ROOT_FILES.each do |root_file| `rm -f #{root_file}` end `mkdir -p level; mv * level 2>/dev/null; touch #{ROOT_FILES.first}` end end end |
#read_settings ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/craftbelt/instance.rb', line 64 def read_settings settings = {} begin nbt = NBTFile.read(File.open(level_dats.first)) settings = { seed: nbt[1]['Data']['RandomSeed'].value.to_s } rescue # nbt read failed end settings end |
#relative_level_paths ⇒ Object
81 82 83 |
# File 'lib/craftbelt/instance.rb', line 81 def relative_level_paths level_paths.map{|p| relative_path(root, p) }.uniq end |
#relative_path(root, path) ⇒ Object
85 86 87 88 |
# File 'lib/craftbelt/instance.rb', line 85 def relative_path(root, path) relative = (path.split('/') - root.split('/')).join('/') relative == '' ? '.' : relative end |