Class: McBlocky::Listener

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/mcblocky/listener.rb

Constant Summary

Constants included from Logging

McBlocky::Logging::BLUE, McBlocky::Logging::BOLD, McBlocky::Logging::CYAN, McBlocky::Logging::GREEN, McBlocky::Logging::MAGENTA, McBlocky::Logging::RED, McBlocky::Logging::RESET, McBlocky::Logging::WHITE, McBlocky::Logging::YELLOW

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log_command, #log_error, #log_message, #log_server, #log_status

Constructor Details

#initialize(dir, main, &block) ⇒ Listener

Returns a new instance of Listener.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mcblocky/listener.rb', line 13

def initialize(dir, main, &block)
  @dir = dir
  @main = main
  @initial_files = [
    @main,
    File.expand_path('../dsl.rb', __FILE__),
    File.expand_path('../dsl/commands.rb', __FILE__),
    File.expand_path('../dsl/repeat_chain.rb', __FILE__),
    File.expand_path('../dsl/impulse_chain.rb', __FILE__),
    File.expand_path('../dsl/selector.rb', __FILE__),
    File.expand_path('../dsl/command_block.rb', __FILE__),
    File.expand_path('../dsl/block.rb', __FILE__),
    File.expand_path('../dsl/container.rb', __FILE__),
    File.expand_path('../context.rb', __FILE__),
    File.expand_path('../executor.rb', __FILE__),
  ]
  @files = @initial_files
  @listener = Listen.to(dir, File.dirname(__FILE__), only: /\.rb$/, &method(:handle))
  @handler = block
end

Class Method Details

.from_config(&block) ⇒ Object



8
9
10
11
# File 'lib/mcblocky/listener.rb', line 8

def self.from_config(&block)
  Config.validate
  return Listener.new(File.dirname(Config.config_path), Config.config['code']['main'], &block)
end

Instance Method Details

#handle(modified, added, removed) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mcblocky/listener.rb', line 48

def handle(modified, added, removed)
  @files.each do |f|
    f = File.expand_path(f, @dir).gsub('\\','/')
    if modified.include? f or added.include? f or removed.include? f
      begin
        log_status "Reloading..."
        McBlocky.reload!
        result = Context.run_file(@main, @dir)
      rescue Exception => e
        log_error "Error in loaded file:"
        puts e.backtrace.join("\n\t")
          .sub("\n\t", ": #{e}#{e.class ? " (#{e.class})" : ""}\n\t")
        break
      end
      @handler.call(result)
      @files = @initial_files + result.required_files.to_a if result.required_files
      log_status "Reloaded."
      break
    end
  end
end

#startObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mcblocky/listener.rb', line 34

def start
  @listener.start
  begin
    log_status "Loading"
    result = Context.run_file(@main, @dir)
  rescue Exception
    log_error "Error in loaded file:"
    puts $!
    return
  end
  @handler.call(result)
  @files = @initial_files + result.required_files.to_a if result.required_files
end