Class: McBlocky::Cli

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

Constant Summary

Constants included from Logging

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

Instance Method Summary collapse

Methods included from Logging

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

Instance Method Details

#listObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mcblocky/cli.rb', line 18

def list
  begin
    Config.load(options[:config])
  rescue ArgumentError => e
    log_error "Error in #{File.basename Config.config_path}:"
    log_error e.message
    exit 1
  end
  if options[:watch]
    $old_context = nil
    listener = Listener.from_config do |context|
      Executor.to_commands(context, options[:diff] ? $old_context : nil).each{|c| puts c}
      $old_context = context
    end
    listener.start
    while true; end
  else
    context = Context.run_file(Config.config['code']['main'], File.dirname(Config.config_path))
    Executor.to_commands(context).each{|c| puts c}
  end
rescue Interrupt
end

#startObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mcblocky/cli.rb', line 42

def start
  begin
    Config.load(options[:config])
  rescue ArgumentError => e
    log_error "Error in #{File.basename Config.config_path}:"
    log_error e.message
    exit 1
  end
  $server = Server.from_config
  log_status "Starting server..."
  $server.start
  log_status "Server is ready! Connect to 127.0.0.1:25565"
  reader = Thread.new do
    until $stdin.closed?
      line = $stdin.gets.chomp
      $server.command line unless line.empty?
    end
  end
  $server.say("McBlocky is ready")
  $server.on_message '!stop' do
    log_status "Stopping server..."
    $server.stop
  end
  $server.on_message /^!/ do |message, user|
    next unless $context
    command, _, args = message.partition(/\s+/)
    $context.helpers.each do |aliases, block|
      aliases = [aliases] if String === aliases
      aliases.each do |a|
        if command == "!#{a}"
          block.call(args, user, a)
          break
        end
      end
    end
  end
  listener = Listener.from_config do |context|
    old_context = $context
    $context = context
    $context.server = $server # needed by helpers
    Executor.to_commands(context, old_context).each{|c| $server.command c}
  end
  listener.start
  $server.loop!
rescue SystemExit
  if $server
    log_status "Stopping server..."
    $server.stop
  end
  reader.kill if reader
rescue Interrupt
  if $server
    log_status "Stopping server..."
    $server.stop
  end
  reader.kill if reader
rescue Exception
  log_error "Caught error, stopping server..."
  begin
    $server.stop if $server
    reader.kill if reader
  rescue
  end
  log_error "Error trace:"
  raise
end

#versionObject



110
111
112
# File 'lib/mcblocky/cli.rb', line 110

def version
  puts "mcblocky #{McBlocky::VERSION}"
end