Class: Craftbelt::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/craftbelt/instance.rb

Constant Summary collapse

ROOT_FILES =
%w(
  server.properties
  ops.txt
  white-list.txt
)

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#rootObject (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.expand_path(File.dirname(path))
      end
    end
  end

  # no root files, might be single player world
  if level_paths.any?
    File.expand_path(level_paths.min)
  end
end

#level_datsObject



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

Returns:

  • (Boolean)


38
39
40
# File 'lib/craftbelt/instance.rb', line 38

def level_in_root?
  relative_level_paths == ['.']
end

#level_pathsObject



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_settingsObject



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_pathsObject



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

#to_h(include_paths = []) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/craftbelt/instance.rb', line 42

def to_h(include_paths = [])
  Dir.chdir(root) do
    {
      root: root,
      paths: relative_level_paths + include_paths.select{|p| File.exist?(p) },
      settings: read_settings
    }
  end
end

#valid?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/craftbelt/instance.rb', line 22

def valid?
  !root.nil?
end