mclib 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 41a05527f155a74abab8c9a3f67b1900db21d370
4
+ data.tar.gz: 1eecf5d099846fcdc63248eecc5f17d1bf356c1a
5
+ SHA512:
6
+ metadata.gz: e33ee8d0c1bc6f0bc18122d9c75d60668dd8bcf2f1c9ac2db8a3613af8e822db4507a1db410fed4d18e6c7e81f44ec9cb96493348a02691d51c9593f5878402d
7
+ data.tar.gz: cdca88f30a4dc81db854c5c3b8e178c2a5cb6ac7f8fb5fb66ad2731d6e0190b2a83c5563fb93e6ab966cc145a4baec648fbcef7c727178ce91f1b58617cf9621
@@ -0,0 +1,24 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .DS_Store
24
+ vendor/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mclib.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Caleb Albritton
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # Mclib
2
+
3
+ This library provides a Ruby API to vanilla minecraft by watching the log file and parsing the lines out into native objects and registering callbacks on server events such as login, chat, warnings and more.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mclib'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mclib
18
+
19
+ ## Usage
20
+
21
+ require 'mclib'
22
+
23
+ # set your minecraft directory, we'll use this to find the log and json files
24
+ MCLib::set_mc_dir '/Users/Caleb/Documents/minecraft/'
25
+ watcher = MCLib::Watcher.new
26
+
27
+ watcher.on :all do |event|
28
+ puts 'This will always get called'
29
+ end
30
+
31
+ watcher.on :login do |user|
32
+ puts "#{user.username} logged in from #{user.ip} at coordinates #{user.coordinates}"
33
+ end
34
+
35
+ watcher.on :chat do |username, text|
36
+ puts "User #{username} said #{text}"
37
+ end
38
+
39
+ watcher.on :warning do |warning|
40
+ puts "Warning: #{warning}"
41
+ end
42
+
43
+ watcher.start
44
+
45
+ You can also fetch the whitelist, ops, banned ips, and players as a hash using the MCLib module
46
+
47
+ MCLib::get_ops
48
+ MCLib::get_whitelist
49
+ MCLib::get_banned_ips
50
+ MCLib::get_banned_players
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it ( https://github.com/[my-github-username]/mclib/fork )
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create a new Pull Request
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec') do |task|
5
+ task.rspec_opts = ['--color', '--format', 'nested']
6
+ end
7
+
8
+ task :default => :spec
9
+ task :test => :spec
@@ -0,0 +1,35 @@
1
+ require 'json'
2
+ require 'mclib/log_parser'
3
+ require 'mclib/watcher'
4
+ require 'mclib/version'
5
+
6
+ module MCLib
7
+ @@mc_dir = ''
8
+
9
+ def self.set_mc_dir(dir)
10
+ dir.chomp! '/' # in case of trailing slash
11
+ @@mc_dir = dir
12
+ end
13
+
14
+ def self.get_mc_dir
15
+ @@mc_dir
16
+ end
17
+
18
+ def self.get_ops
19
+ json = JSON.parse(File.open(@@mc_dir + 'ops.json').read)
20
+ json.collect { |user| user['name'] }
21
+ end
22
+
23
+ def self.get_whitelist
24
+ json = JSON.parse(File.open(@@mc_dir + 'whitelist.json').read)
25
+ json.collect { |user| user['name'] }
26
+ end
27
+
28
+ def self.get_banned_ips
29
+ JSON.parse(File.open(@@mc_dir + 'banned-ips.json').read)
30
+ end
31
+
32
+ def self.get_banned_players
33
+ JSON.parse(File.open(@@mc_dir + 'banned-players.json').read)
34
+ end
35
+ end
@@ -0,0 +1,91 @@
1
+ require 'mclib'
2
+
3
+ class MCLib::EventFactory
4
+
5
+ Unknown = Struct.new :message, :meta_data
6
+ Warning = Struct.new :text
7
+ Login = Struct.new :username, :ip, :coordinates
8
+ Chat = Struct.new :username, :text
9
+
10
+ def build(message, meta_data, type)
11
+ if type == :warn
12
+ return Warning.new message
13
+ end
14
+
15
+ @message, @meta_data = message, meta_data
16
+
17
+ case detect_event
18
+ when :chat
19
+ parse_chat
20
+ when :login
21
+ parse_login
22
+ when :unknown
23
+ Unknown.new message, meta_data
24
+ else
25
+ Unknown.new message, meta_data
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def detect_event
32
+ if @message.chars[0] == '<'
33
+ :chat
34
+ elsif @message.include? 'logged in'
35
+ :login
36
+ else
37
+ :unknown
38
+ end
39
+ end
40
+
41
+ def parse_chat
42
+ name = ''
43
+ text = ''
44
+ stopped_at = 0
45
+
46
+ @message.chars.each_with_index do |char, index|
47
+ if char == '<'
48
+ next
49
+ elsif char == '>'
50
+ text = @message[index + 2..-1] # + 2 to skip trailing angle bracket and space
51
+ break
52
+ else
53
+ name << char
54
+ end
55
+ end
56
+
57
+ Chat.new name, text
58
+ end
59
+
60
+ def parse_login
61
+ name = ''
62
+ ip = ''
63
+
64
+ stopped_at = 0
65
+
66
+ @message.chars.each_with_index do |char, index|
67
+ if char != '['
68
+ name << char
69
+ else
70
+ stopped_at = index + 2
71
+ break
72
+ end
73
+ end
74
+
75
+ @message.chars[stopped_at..-1].each_with_index do |char, index|
76
+ if char != ':'
77
+ ip << char
78
+ else
79
+ break
80
+ end
81
+ end
82
+
83
+ coordinates = @message.match(/\(.*\)/)[0]
84
+ coordinates.slice! 0
85
+ coordinates.chomp! ')'
86
+ coordinates = coordinates.split(', ').map { |point| point.to_i }
87
+ coordinates = { :x => coordinates[0], :y => coordinates[1], :z => coordinates[2] }
88
+
89
+ Login.new name, ip, coordinates
90
+ end
91
+ end
@@ -0,0 +1,52 @@
1
+ require 'mclib'
2
+ require 'mclib/event_factory'
3
+ require 'logger'
4
+
5
+ class MCLib::LogParser
6
+
7
+ def initialize
8
+ @factory = MCLib::EventFactory.new
9
+ end
10
+
11
+ def parse(line)
12
+ meta_data, message = split_data line
13
+ type = type? meta_data
14
+ @factory.build message, meta_data, type
15
+ end
16
+
17
+ private
18
+
19
+ def type?(meta_data)
20
+ if meta_data.include? 'INFO'
21
+ return :info
22
+ elsif meta_data.include? 'WARN'
23
+ return :warn
24
+ else
25
+ return :unknown
26
+ # fail ArgumentError, "Unable to determine message type #{meta_data}"
27
+ end
28
+ end
29
+
30
+ def split_data(line)
31
+ buffer = []
32
+ occurences = 0
33
+ stopped_at = 0
34
+
35
+ line.chars.each_with_index do |char, i|
36
+ if char == ':' && occurences == 2
37
+ # this is the third time we've seen the colon so this is the message delimeter
38
+ stopped_at = i
39
+ break
40
+ elsif char == ':'
41
+ buffer << char
42
+ occurences += 1
43
+ else
44
+ buffer << char
45
+ end
46
+ end
47
+
48
+ stopped_at += 2 # removes trailing colon and space
49
+
50
+ return buffer.join, line[stopped_at..-1]
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module MCLib
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,56 @@
1
+ require 'mclib'
2
+ require 'mclib/log_parser'
3
+ require 'eventmapper'
4
+ require 'filewatch/tail'
5
+
6
+ class MCLib::Watcher
7
+
8
+ include EventMapper
9
+
10
+ attr_accessor :watch_thread
11
+
12
+ def initialize(mc_dir = nil)
13
+ if mc_dir.nil?
14
+ mc_dir = MCLib::get_mc_dir
15
+ end
16
+
17
+ @log_file = mc_dir + '/logs/latest.log'
18
+ @log_watcher = FileWatch::Tail.new
19
+ @parser = MCLib::LogParser.new
20
+ end
21
+
22
+ def start
23
+ @watch_thread = Thread.new do
24
+ @log_watcher.tail @log_file
25
+ @log_watcher.subscribe do |path, line|
26
+ if !line.empty? && !line.nil?
27
+ event = @parser.parse line
28
+
29
+ # fire the event with the correct event handler api parameters
30
+ delegate_parameters event
31
+
32
+ # always fire the :all handlers
33
+ fire :all, event
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def stop
40
+ @watch_thread.kill
41
+ end
42
+
43
+ private
44
+
45
+ def delegate_parameters(event)
46
+ type = event.class.name.split('::').last.downcase.to_sym
47
+ case type
48
+ when :chat
49
+ fire :chat, event.username, event.text
50
+ when :login
51
+ fire :login, event
52
+ when :warning
53
+ fire :warning, event.text
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mclib/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mclib"
8
+ spec.version = MCLib::VERSION
9
+ spec.authors = ["Caleb Albritton"]
10
+ spec.email = ["ithinkincode@gmail.com"]
11
+ spec.summary = %q{Provides a pure Ruby API to vanilla minecraft.}
12
+ spec.description = %q{This library provides a Ruby API to vanilla minecraft by watching the log file and parsing the lines out into native objects and registering callbacks on server events such as login, chat, warnings and more.}
13
+ spec.homepage = "https://github.com/C0deMaver1ck/MCLib"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "filewatch"
22
+ spec.add_runtime_dependency "eventmapper"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "pry"
28
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ # I can't figure out how to test threaded stuff without it leaving a zombie process behind
4
+ describe MCLib do
5
+ it 'should emit events from server log' do
6
+ MCLib::set_mc_dir '/Users/Caleb/Documents/minecraft/'
7
+ watcher = MCLib::Watcher.new
8
+
9
+ watcher.on :all do |event|
10
+ puts "This will always get called"
11
+ end
12
+
13
+ watcher.on :login do |user|
14
+ puts "#{user.username} logged in from #{user.ip} at coordinates #{user.coordinates}"
15
+ end
16
+
17
+ watcher.on :chat do |username, text|
18
+ puts "User #{username} said #{text}"
19
+ end
20
+
21
+ watcher.on :warning do |warning|
22
+ puts "Warning: #{warning}"
23
+ end
24
+
25
+ watcher.start
26
+
27
+ sleep 10
28
+
29
+ watcher.stop
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ require 'pry'
2
+ require 'mclib'
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # Require this file using `require "spec_helper"` to ensure that it is only
7
+ # loaded once.
8
+ #
9
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ # config.order = 'random'
20
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mclib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Caleb Albritton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: filewatch
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: eventmapper
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: This library provides a Ruby API to vanilla minecraft by watching the
98
+ log file and parsing the lines out into native objects and registering callbacks
99
+ on server events such as login, chat, warnings and more.
100
+ email:
101
+ - ithinkincode@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - .gitignore
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/mclib.rb
112
+ - lib/mclib/event_factory.rb
113
+ - lib/mclib/log_parser.rb
114
+ - lib/mclib/version.rb
115
+ - lib/mclib/watcher.rb
116
+ - mclib.gemspec
117
+ - spec/mclib_spec.rb
118
+ - spec/spec_helper.rb
119
+ homepage: https://github.com/C0deMaver1ck/MCLib
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.0.14
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Provides a pure Ruby API to vanilla minecraft.
143
+ test_files:
144
+ - spec/mclib_spec.rb
145
+ - spec/spec_helper.rb