craftbelt 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # Craftbelt
2
+
3
+ Useful utility functions for Minecraft. Used in [Minefold](https://minefold.com) Funpacks.
4
+
data/craftbelt.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'craftbelt/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'craftbelt'
8
+ gem.version = Craftbelt::VERSION
9
+ gem.authors = ["Dave Newman"]
10
+ gem.email = ["dave@minefold.com"]
11
+ gem.description = %q{Useful Minecraft utilities}
12
+ gem.summary = %q{Test Minefold funpacks}
13
+ gem.homepage = "https://minefold.com"
14
+
15
+ gem.test_files = gem.files.grep(%r{^spec/})
16
+ gem.require_paths = ["lib"]
17
+
18
+ gem.add_dependency 'nbtfile'
19
+ gem.add_development_dependency 'rspec'
20
+
21
+ gem.files = %w(
22
+ Gemfile
23
+ README.md
24
+ craftbelt.gemspec
25
+ ) + Dir['{lib,spec}/**/*']
26
+ end
@@ -0,0 +1,101 @@
1
+ require 'find'
2
+ require 'nbtfile'
3
+
4
+ class MinecraftInstance
5
+ attr_reader :root
6
+
7
+ ROOT_FILES = %w(
8
+ server.properties
9
+ ops.txt
10
+ white-list.txt
11
+ )
12
+
13
+ def initialize(initial_root)
14
+ @initial_root = initial_root
15
+ end
16
+
17
+ def root
18
+ @root ||= find_root!
19
+ end
20
+
21
+ def valid?
22
+ !root.nil?
23
+ end
24
+
25
+ # converts single player worlds to server worlds
26
+ def prepare!
27
+ if level_in_root?
28
+ Dir.chdir(root) do
29
+ ROOT_FILES.each do |root_file|
30
+ `rm -f #{root_file}`
31
+ end
32
+ `mkdir -p level; mv * level 2>/dev/null; touch #{ROOT_FILES.first}`
33
+ end
34
+ end
35
+ end
36
+
37
+ def level_in_root?
38
+ relative_level_paths == ['.']
39
+ end
40
+
41
+ def to_h(include_paths = [])
42
+ {
43
+ root: File.expand_path(root),
44
+ paths: relative_level_paths + include_paths.select{|p| File.exist?(p) },
45
+ settings: read_settings
46
+ }
47
+ end
48
+
49
+ def level_dats
50
+ @level_dats ||= begin
51
+ paths = []
52
+ Find.find(@initial_root) do |path|
53
+ if path =~ /\/(level|uid)\.dat$/
54
+ paths << path
55
+ end
56
+ end
57
+ paths
58
+ end
59
+ end
60
+
61
+ def read_settings
62
+ settings = {}
63
+ begin
64
+ nbt = NBTFile.read(File.open(level_dats('.').first))
65
+ settings = {
66
+ seed: nbt[1]['Data']['RandomSeed'].value.to_s
67
+ }
68
+ rescue
69
+ # nbt read failed
70
+ end
71
+ settings
72
+ end
73
+
74
+ def level_paths
75
+ @level_paths ||= level_dats.map{|file| File.dirname(file).gsub(/^\.\//, '') }
76
+ end
77
+
78
+ def relative_level_paths
79
+ level_paths.map{|p| relative_path(root, p) }.uniq
80
+ end
81
+
82
+ def relative_path(root, path)
83
+ relative = (path.split('/') - root.split('/')).join('/')
84
+ relative == '' ? '.' : relative
85
+ end
86
+
87
+ def find_root!
88
+ Find.find(@initial_root) do |path|
89
+ ROOT_FILES.each do |root_file|
90
+ if path.end_with? root_file
91
+ return File.dirname(path)
92
+ end
93
+ end
94
+ end
95
+
96
+ # no root files, might be single player world
97
+ if level_paths.any?
98
+ File.expand_path(level_paths.min)
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,7 @@
1
+ module Craftbelt
2
+ VERSION = '0.0.1'
3
+
4
+ def self.version
5
+ VERSION
6
+ end
7
+ end
data/lib/craftbelt.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'craftbelt/schema'
2
+ require 'craftbelt/version'
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'craftbelt/minecraft_instance'
3
+
4
+ describe MinecraftInstance do
5
+ def self.paths(paths)
6
+ before do
7
+ stub = Find.stub(:find)
8
+ paths.each do |path|
9
+ stub.and_yield(path)
10
+ end
11
+ end
12
+ end
13
+
14
+ subject { MinecraftInstance.new('.') }
15
+
16
+ context 'single player world' do
17
+ paths %w(
18
+ tmp/server/level.dat
19
+ tmp/server/region/r.0.0.mca
20
+ )
21
+ its(:root) { should end_with 'tmp/server' }
22
+ its(:level_paths) { should == ['tmp/server'] }
23
+ it "should serialize" do
24
+ subject.to_h.should == {
25
+ root: File.expand_path('tmp/server'),
26
+ paths: ['.'],
27
+ settings: {}
28
+ }
29
+ end
30
+ end
31
+
32
+ context 'server world' do
33
+ paths %w(
34
+ tmp/server/server.properties
35
+ tmp/server/level/level.dat
36
+ )
37
+ its(:root) { should end_with 'tmp/server' }
38
+ its(:level_paths) { should == ['tmp/server/level'] }
39
+ it "should serialize" do
40
+ subject.to_h.should == {
41
+ root: File.expand_path('tmp/server'),
42
+ paths: ['level'],
43
+ settings: {}
44
+ }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,12 @@
1
+ # $:.unshift File.expand_path('../../lib', __FILE__)
2
+ #
3
+ # p $:
4
+
5
+ require 'rspec'
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ config.order = 'random'
12
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: craftbelt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dave Newman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nbtfile
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Useful Minecraft utilities
47
+ email:
48
+ - dave@minefold.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - Gemfile
54
+ - README.md
55
+ - craftbelt.gemspec
56
+ - lib/craftbelt/minecraft_instance.rb
57
+ - lib/craftbelt/version.rb
58
+ - lib/craftbelt.rb
59
+ - spec/craftbelt/minecraft_instance_spec.rb
60
+ - spec/spec_helper.rb
61
+ homepage: https://minefold.com
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.23
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Test Minefold funpacks
85
+ test_files: []
86
+ has_rdoc: