Class: McBlocky::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mcblocky/config.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/mcblocky/config.rb', line 6

def config
  @config
end

.config_pathObject (readonly)

Returns the value of attribute config_path.



7
8
9
# File 'lib/mcblocky/config.rb', line 7

def config_path
  @config_path
end

Class Method Details

.load(filename) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/mcblocky/config.rb', line 8

def load(filename)
  @valid = false
  filename = File.expand_path('config.yml', filename) if File.directory? filename
  @config_path = filename
  open(filename) do |f|
    @config = YAML.safe_load(f)
  end
  validate
end

.validateObject

Raises:

  • (ArgumentError)


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
# File 'lib/mcblocky/config.rb', line 18

def validate
  return if @valid
  raise ArgumentError, "No config loaded" unless config
  raise ArgumentError, "No server section" unless config['server']
  config['code'] ||= {}

  if config['server']['ops']
    raise ArgumentError, "server.ops must be an array" unless Array === config['server']['ops']
  end

  config['server']['properties'] = {'enable-command-block' => 'true'}.merge(config['server']['properties'] || {})

  Dir.chdir File.dirname(config_path) do
    unless which 'java'
      java = config['server']['java']
      raise ArgumentError, "Java not found. Specify the full path in server.java" if !java or java.empty?
      raise ArgumentError, "Java specified in server.java is not executable" unless File.executable? java
    end

    jar = config['server']['jar']
    raise ArgumentError, "No server.jar specified" if !jar or jar.empty?
    raise ArgumentError, "Jar specified in server.jar does not exist" unless File.exist? jar

    config['code']['main'] ||= "#{File.basename File.dirname(config_path)}.rb"
    main = config['code']['main']
    raise ArgumentError, "No code.main specified" if !main or main.empty?
    raise ArgumentError, "#{main} does not exist" unless File.exist? main or File.exist? "#{main}.rb"
  end

  @valid = true
end