nbt_utils 0.0.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +5 -5
  2. data/.github/dependabot.yml +6 -0
  3. data/.github/workflows/ci.yml +24 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +37 -0
  6. data/.ruby-version +1 -0
  7. data/Gemfile +2 -0
  8. data/Gemfile.lock +49 -4
  9. data/LICENSE +7 -0
  10. data/README.md +15 -19
  11. data/Rakefile +8 -0
  12. data/lib/nbt_utils/file.rb +14 -15
  13. data/lib/nbt_utils/tag/byte.rb +4 -1
  14. data/lib/nbt_utils/tag/byte_array.rb +9 -7
  15. data/lib/nbt_utils/tag/compound.rb +22 -17
  16. data/lib/nbt_utils/tag/double.rb +4 -1
  17. data/lib/nbt_utils/tag/end.rb +6 -4
  18. data/lib/nbt_utils/tag/exceptions.rb +3 -1
  19. data/lib/nbt_utils/tag/float.rb +4 -1
  20. data/lib/nbt_utils/tag/int.rb +4 -1
  21. data/lib/nbt_utils/tag/int_array.rb +10 -8
  22. data/lib/nbt_utils/tag/list.rb +20 -15
  23. data/lib/nbt_utils/tag/long.rb +4 -1
  24. data/lib/nbt_utils/tag/short.rb +4 -1
  25. data/lib/nbt_utils/tag/string.rb +2 -0
  26. data/lib/nbt_utils/tag.rb +11 -12
  27. data/lib/nbt_utils/tag_name.rb +5 -3
  28. data/lib/nbt_utils/version.rb +3 -1
  29. data/lib/nbt_utils.rb +3 -0
  30. data/nbt_utils.gemspec +17 -11
  31. data/script.rb +21 -19
  32. data/spec/nbt_utils/file_spec.rb +60 -0
  33. data/spec/nbt_utils/tag/byte_array_spec.rb +5 -0
  34. data/spec/nbt_utils/tag/byte_spec.rb +5 -0
  35. data/spec/nbt_utils/tag/compound_spec.rb +5 -0
  36. data/spec/nbt_utils/tag/double_spec.rb +5 -0
  37. data/spec/nbt_utils/tag/end_spec.rb +5 -0
  38. data/spec/nbt_utils/tag/float_spec.rb +5 -0
  39. data/spec/nbt_utils/tag/int_array_spec.rb +5 -0
  40. data/spec/nbt_utils/tag/int_spec.rb +5 -0
  41. data/spec/nbt_utils/tag/list_spec.rb +5 -0
  42. data/spec/nbt_utils/tag/long_spec.rb +5 -0
  43. data/spec/nbt_utils/tag/short_spec.rb +5 -0
  44. data/spec/nbt_utils/tag/string_spec.rb +5 -0
  45. data/spec/nbt_utils/tag_name_spec.rb +20 -0
  46. data/spec/spec_helper.rb +102 -0
  47. data/spec/support/shared_examples/a_tag.rb +8 -0
  48. metadata +75 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c47cdd430f8c7666f68f40f505c7644efbad37aa
4
- data.tar.gz: 75efc43db2901b025bd1ff8e834e235abd9fb0a7
2
+ SHA256:
3
+ metadata.gz: 2cd4381d03d4fbcfe77b3be94f2d52d66899f8e89d1095cfbb359f3455f153d2
4
+ data.tar.gz: 1b9f9ccbaf59dc071093bc5f3d50035a54717f8fffc0cd7cc5d45e51027577e4
5
5
  SHA512:
6
- metadata.gz: 743f9440efcf013105f404653893f85e26fbb5f81705077e3df36a32276b9cdfd04447dbf633b2dbc53714af0639ff95681c908fb0537bea7cb160c618ec6bd9
7
- data.tar.gz: 4fd80a295519ec8d825f9b091d0d0c785a83f2d3029f4ac5de464ba2dacddc7ee5a6db552aa54b1a535b21435c4d4cae3254e52e311061920aa0584aaf192f15
6
+ metadata.gz: 2b9422f9cf06b75d5b85483857a96dd1a674888fd353e25305c6182c83997ca2685c401b08977eca36725f62af353a2b3fe1f0649219b77a9c9066397efd2624
7
+ data.tar.gz: 58a8fe25760a2bb28655d4f11b66ffa6ef8ae0f6e450f49d768a3fa54db81f95a1c3f238c964add962a94b49e087758b9b163045d7213bceaf05aefb29659481
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: /
5
+ schedule:
6
+ interval: daily
@@ -0,0 +1,24 @@
1
+ name: CI
2
+ on:
3
+ push:
4
+ branches: [ master ]
5
+ pull_request:
6
+ branches: [ master ]
7
+
8
+ env:
9
+ CARGO_TERM_COLOR: always
10
+
11
+ jobs:
12
+ ci:
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - name: Checkout
16
+ uses: actions/checkout@v3
17
+ - name: Setup Ruby
18
+ uses: ruby/setup-ruby@v1
19
+ with:
20
+ bundler-cache: true
21
+ - name: Run Rubocop
22
+ run: bundle exec rubocop
23
+ - name: Run Specs
24
+ run: rake spec
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,37 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: '2.7.0'
7
+ NewCops: enable
8
+
9
+ Layout/LineLength:
10
+ Max: 120
11
+
12
+ Metrics/AbcSize:
13
+ Enabled: false
14
+
15
+ Metrics/BlockLength:
16
+ Enabled: false
17
+
18
+ Metrics/ClassLength:
19
+ Enabled: false
20
+
21
+ Metrics/CyclomaticComplexity:
22
+ Enabled: false
23
+
24
+ Metrics/PerceivedComplexity:
25
+ Enabled: false
26
+
27
+ Metrics/MethodLength:
28
+ Enabled: false
29
+
30
+ RSpec/ExampleLength:
31
+ Enabled: false
32
+
33
+ RSpec/MultipleExpectations:
34
+ Enabled: false
35
+
36
+ Style/Documentation:
37
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.2
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'http://rubygems.org'
2
4
 
3
5
  gemspec
data/Gemfile.lock CHANGED
@@ -1,17 +1,62 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nbt_utils (0.0.3)
5
- bindata (~> 1.3)
4
+ nbt_utils (0.5.0)
5
+ bindata (~> 2)
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- bindata (1.3.1)
10
+ ast (2.4.2)
11
+ bindata (2.4.10)
12
+ diff-lcs (1.5.0)
13
+ parallel (1.22.1)
14
+ parser (3.1.2.0)
15
+ ast (~> 2.4.1)
16
+ rainbow (3.1.1)
17
+ regexp_parser (2.5.0)
18
+ rexml (3.2.5)
19
+ rspec (3.11.0)
20
+ rspec-core (~> 3.11.0)
21
+ rspec-expectations (~> 3.11.0)
22
+ rspec-mocks (~> 3.11.0)
23
+ rspec-core (3.11.0)
24
+ rspec-support (~> 3.11.0)
25
+ rspec-expectations (3.11.0)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.11.0)
28
+ rspec-mocks (3.11.1)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.11.0)
31
+ rspec-support (3.11.0)
32
+ rubocop (1.30.1)
33
+ parallel (~> 1.10)
34
+ parser (>= 3.1.0.0)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ regexp_parser (>= 1.8, < 3.0)
37
+ rexml (>= 3.2.5, < 4.0)
38
+ rubocop-ast (>= 1.18.0, < 2.0)
39
+ ruby-progressbar (~> 1.7)
40
+ unicode-display_width (>= 1.4.0, < 3.0)
41
+ rubocop-ast (1.18.0)
42
+ parser (>= 3.1.1.0)
43
+ rubocop-performance (1.14.2)
44
+ rubocop (>= 1.7.0, < 2.0)
45
+ rubocop-ast (>= 0.4.0)
46
+ rubocop-rspec (2.11.1)
47
+ rubocop (~> 1.19)
48
+ ruby-progressbar (1.11.0)
49
+ unicode-display_width (2.1.0)
11
50
 
12
51
  PLATFORMS
13
52
  ruby
14
53
 
15
54
  DEPENDENCIES
16
- bundler (>= 1.0.0)
17
55
  nbt_utils!
56
+ rspec (~> 3)
57
+ rubocop (~> 1)
58
+ rubocop-performance (~> 1)
59
+ rubocop-rspec (~> 2)
60
+
61
+ BUNDLED WITH
62
+ 2.3.15
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2021 Michael Dungan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,32 +1,28 @@
1
- nbt_utils
2
- =========
1
+ # nbt_utils
3
2
 
4
- Some classes for handling [Minecraft](http://minecraft.net) .nbt files.
3
+ Some classes for handling [Minecraft](http://minecraft.net) `.nbt` files.
5
4
 
6
- See the spec (NBT.txt in doc directory) for more info.
5
+ See the spec (`NBT.txt` in doc directory) for more info.
7
6
 
8
- Installation
9
- ============
7
+ ## Installation
10
8
 
11
- Requires ruby 1.9 minimum. Tested with 1.9.2+
9
+ Requires Ruby 2.7 minimum.
12
10
 
13
11
  gem install nbt_utils
14
12
 
15
- No sudo. You *are* using [rvm](http://rvm.beginrescueend.com/), right?
13
+ ## Use
16
14
 
17
- Use
18
- ===
15
+ ```ruby
16
+ require 'nbt_utils'
19
17
 
20
- require 'nbt_utils'
18
+ @file = NBTUtils::File.new
19
+ @tag = @file.read('some_nbt_file.nbt')
20
+ puts @tag.to_s
21
+ @file.write('some_new_path.nbt', @tag, true) # true / false to gzip automatically
22
+ ```
21
23
 
22
- @file = NBTUtils::File.new
23
- @tag = @file.read('some_nbt_file.nbt')
24
- puts @tag.to_s
25
- @file.write('some_path.nbt', @tag, <compress? true/false>)
24
+ ### Copyright
26
25
 
27
- Copyright
28
- =========
29
-
30
- Copyright (c) 2010-2015 Michael Dungan, mpd@jesters-court.net, released under the MIT license.
26
+ Copyright Michael Dungan, mpd@jesters-court.net, released under the MIT license.
31
27
 
32
28
  The files `NBT.txt`, `test.nbt`, `test_uncompressed.nbt`, `bigtest.nbt`, and `bigtest_uncompressed.nbt` in the doc directory are mirrored from the Minecraft website and not covered under the above license.
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler'
2
4
  Bundler::GemHelper.install_tasks
5
+
6
+ begin
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:spec)
9
+ rescue LoadError # rubocop:disable Lint/SuppressedException
10
+ end
@@ -1,41 +1,40 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  class File
5
+ attr_reader :path, :content, :compressed, :tag
6
+
3
7
  def initialize(path = nil)
4
8
  @path = path
5
9
  end
6
10
 
7
- def read(path = @path)
11
+ def read(read_path = path)
8
12
  # Zlib does not provide a way to test a file for compressed, and I'm
9
13
  # not going to tool around with magic numbers, so I guess we use
10
14
  # exceptions for flow control.
11
15
  begin
12
- Zlib::GzipReader.open(path) do |f|
16
+ Zlib::GzipReader.open(read_path) do |f|
13
17
  @content = StringIO.new(f.read)
14
18
  end
15
19
  @compressed = true
16
20
  rescue Zlib::GzipFile::Error
17
- ::File.open(path) do |f|
18
- @content = StringIO.new(f.read)
19
- end
21
+ @content = StringIO.new(::File.read(read_path))
20
22
  @compressed = false
21
23
  end
22
24
 
23
-
24
- last_byte = @content.read(1).bytes.first
25
+ last_byte = content.read(1).bytes.first
25
26
  klass = NBTUtils::Tag.tag_type_to_class(last_byte)
26
27
 
27
- @tag = klass.new(@content, true)
28
+ @tag = klass.new(content, named: true)
28
29
  end
29
30
 
30
- def write(path = @path, tag = @tag, compressed = @compressed)
31
- if compressed
32
- Zlib::GzipWriter.open(path) do |gz|
33
- gz.write tag.to_nbt_string
31
+ def write(write_path = path, write_tag = tag, write_compressed = compressed)
32
+ if write_compressed
33
+ Zlib::GzipWriter.open(write_path) do |gz|
34
+ gz.write write_tag.to_nbt_string
34
35
  end
35
36
  else
36
- ::File.open(path, 'w') do |f|
37
- f.write tag.to_nbt_string
38
- end
37
+ ::File.write(write_path, write_tag.to_nbt_string)
39
38
  end
40
39
  end
41
40
  end
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
- class Byte # signed, per spec
5
+ # signed, per spec
6
+ class Byte
4
7
  include NBTUtils::Tag
5
8
 
6
9
  type_id 1
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
5
  class ByteArray
@@ -5,31 +7,31 @@ module NBTUtils
5
7
 
6
8
  type_id 7
7
9
 
8
- def initialize(io, named = true)
10
+ def initialize(io, named: true)
9
11
  read_name(io) if named
10
12
 
11
13
  len = ::BinData::Int32be.new.read(io).value
12
14
  # use single string for the payload because an array means each byte is a
13
15
  # separate object which is incredibly SLOW
14
- @payload = ::BinData::String.new(:read_length => len).read(io)
16
+ @payload = ::BinData::String.new(read_length: len).read(io)
15
17
  end
16
18
 
17
19
  def to_s(indent = 0)
18
- (' ' * indent) + "TAG_Byte_Array#{@name ? "(\"#{@name}\")" : ''}: [#{@payload.length} bytes]"
20
+ (' ' * indent) + "TAG_Byte_Array#{name ? "(\"#{name}\")" : ''}: [#{payload.length} bytes]"
19
21
  end
20
22
 
21
- def to_nbt_string(named = true)
23
+ def to_nbt_string(named: true)
22
24
  result = named ? binary_type_id + name_to_nbt_string : ''
23
25
  len = ::BinData::Int32be.new
24
- len.value = @payload.length
26
+ len.value = payload.length
25
27
  result << len.to_binary_s
26
- result + @payload.to_binary_s
28
+ result + payload.to_binary_s
27
29
  end
28
30
 
29
31
  def set_value(new_value, index)
30
32
  b = ::BinData::Uint8.new
31
33
  b.value = new_value
32
- @payload[index] = b.to_binary_s
34
+ payload[index] = b.to_binary_s
33
35
  end
34
36
  end
35
37
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
5
  class Compound
@@ -5,55 +7,58 @@ module NBTUtils
5
7
 
6
8
  type_id 10
7
9
 
8
- def initialize(io, named = true)
10
+ attr_reader :tag_names
11
+
12
+ def initialize(io, named: true)
9
13
  @payload = []
10
14
  @tag_names = []
11
15
  read_name(io) if named
12
16
 
13
17
  until (last_byte = io.read(1).bytes.first) == NBTUtils::Tag::End.type_id
14
18
  klass = tag_type_to_class(last_byte)
15
- add_tag klass.new(io, true)
19
+ add_tag klass.new(io, named: true)
16
20
  end
17
21
  end
18
22
 
19
23
  def to_s(indent = 0)
20
- ret = (' ' * indent) + "TAG_Compound#{@name ? "(\"#{@name}\")" : ''}: #{@payload.length} entries\n"
21
- ret << (' ' * indent) + "{\n"
22
- @payload.each do |load|
24
+ ret = (' ' * indent) + "TAG_Compound#{name ? "(\"#{name}\")" : ''}: #{payload.length} entries\n"
25
+ ret << ("#{' ' * indent}{\n")
26
+ payload.each do |load|
23
27
  ret << "#{load.to_s(indent + 2)}\n"
24
28
  end
25
- ret << (' ' * indent) + "}"
29
+ ret << ("#{' ' * indent}}")
26
30
 
27
31
  ret
28
32
  end
29
33
 
30
- def to_nbt_string(named = true)
34
+ def to_nbt_string(named: true)
31
35
  result = named ? binary_type_id + name_to_nbt_string : ''
32
36
 
33
- result = @payload.inject(result) do |r, load|
34
- r + load.to_nbt_string(true)
37
+ result = payload.inject(result) do |r, load|
38
+ r + load.to_nbt_string(named: true)
35
39
  end
36
40
 
37
41
  result + NBTUtils::Tag::End.new(nil).to_nbt_string
38
42
  end
39
43
 
40
44
  def find_tag(name)
41
- if name.kind_of?(Regexp)
42
- @payload.detect { |tag| tag.name.to_s =~ /#{name}/ }
45
+ if name.is_a?(Regexp)
46
+ payload.detect { |tag| tag.name.to_s =~ /#{name}/ }
43
47
  else
44
- @payload.detect { |tag| tag.name.to_s == name }
48
+ payload.detect { |tag| tag.name.to_s == name }
45
49
  end
46
50
  end
47
51
 
48
52
  def find_tags(name)
49
- @payload.select { |tag| tag.name.to_s =~ /#{name}/ }
53
+ payload.select { |tag| tag.name.to_s =~ /#{name}/ }
50
54
  end
51
55
 
52
56
  def add_tag(tag)
53
57
  raise MissingCompoundPayloadTagNameError if tag.name.nil?
54
- raise DuplicateCompoundPayloadTagNameError if @tag_names.include?(tag.name)
55
- @tag_names << tag.name
56
- @payload << tag
58
+ raise DuplicateCompoundPayloadTagNameError if tag_names.include?(tag.name)
59
+
60
+ tag_names << tag.name
61
+ payload << tag
57
62
  end
58
63
 
59
64
  # update one of my tags indirectly
@@ -68,7 +73,7 @@ module NBTUtils
68
73
  end
69
74
 
70
75
  def remove_tag(name)
71
- @payload.delete find_tag(name)
76
+ payload.delete find_tag(name)
72
77
  end
73
78
  end
74
79
  end
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
- class Double # signed, per spec
5
+ # signed, per spec
6
+ class Double
4
7
  include NBTUtils::Tag
5
8
 
6
9
  type_id 6
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
5
  class End
@@ -5,17 +7,17 @@ module NBTUtils
5
7
 
6
8
  type_id 0
7
9
 
8
- def initialize(input, named = false)
10
+ def initialize(...)
9
11
  @name = ''
10
12
  end
11
13
 
12
- def to_s(indent = 0)
14
+ def to_s(...)
13
15
  ''
14
16
  end
15
17
 
16
- def to_nbt_string(named = false)
18
+ def to_nbt_string(...)
17
19
  binary_type_id
18
20
  end
19
21
  end
20
22
  end
21
- end
23
+ end
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class DuplicateCompoundPayloadTagNameError < StandardError; end
2
- class MissingCompoundPayloadTagNameError < StandardError; end
4
+ class MissingCompoundPayloadTagNameError < StandardError; end
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
- class Float # signed, per spec
5
+ # signed, per spec
6
+ class Float
4
7
  include NBTUtils::Tag
5
8
 
6
9
  type_id 5
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
- class Int # signed, per spec
5
+ # signed, per spec
6
+ class Int
4
7
  include NBTUtils::Tag
5
8
 
6
9
  type_id 3
@@ -1,34 +1,36 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
5
  class IntArray
4
6
  include NBTUtils::Tag
5
7
 
6
- type_id 11
8
+ type_id 11
7
9
 
8
- def initialize(io, named = true)
10
+ def initialize(io, named: true)
9
11
  read_name(io) if named
10
12
 
11
13
  len = ::BinData::Int32be.new.read(io).value
12
14
 
13
- @payload = ::BinData::Array.new(:type => :int32be, :initial_length => len).read(io)
15
+ @payload = ::BinData::Array.new(type: :int32be, initial_length: len).read(io)
14
16
  end
15
17
 
16
18
  def to_s(indent = 0)
17
- (' ' * indent) + "TAG_Int_Array#{@name ? "(\"#{@name}\")" : ''}: [#{@payload.length} bytes]"
19
+ (' ' * indent) + "TAG_Int_Array#{name ? "(\"#{name}\")" : ''}: [#{payload.length} bytes]"
18
20
  end
19
21
 
20
- def to_nbt_string(named = true)
22
+ def to_nbt_string(named: true)
21
23
  result = named ? binary_type_id + name_to_nbt_string : ''
22
24
  len = ::BinData::Int32be.new
23
- len.value = @payload.length
25
+ len.value = payload.length
24
26
  result << len.to_binary_s
25
- result + @payload.to_binary_s
27
+ result + payload.to_binary_s
26
28
  end
27
29
 
28
30
  def set_value(new_value, index)
29
31
  b = ::BinData::Int32be.new
30
32
  b.value = new_value
31
- @payload[index] = b.to_binary_s
33
+ payload[index] = b.to_binary_s
32
34
  end
33
35
  end
34
36
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
5
  class List
@@ -5,7 +7,9 @@ module NBTUtils
5
7
 
6
8
  type_id 9
7
9
 
8
- def initialize(io, named = true)
10
+ attr_reader :tag_type
11
+
12
+ def initialize(io, named: true)
9
13
  @payload = []
10
14
  read_name(io) if named
11
15
 
@@ -13,41 +17,42 @@ module NBTUtils
13
17
  @tag_type = NBTUtils::Tag.tag_type_to_class(tag_id)
14
18
  len = ::BinData::Int32be.new.read(io).value
15
19
  len.times do
16
- @payload << @tag_type.new(io, false)
20
+ payload << tag_type.new(io, named: false)
17
21
  end
18
22
  end
19
23
 
20
24
  def to_s(indent = 0)
21
- ret = (' ' * indent) + "TAG_List#{@name ? "(\"#{@name}\")" : ''}: #{@payload.length} entries of type TAG_#{@tag_type.to_s.split('::').last}\n"
22
- ret << (' ' * indent) + "{\n"
23
- @payload.each do |load|
25
+ ret = "#{' ' * indent}TAG_List#{name ? "(\"#{name}\")" : ''}: #{payload.length} entries of "\
26
+ "type TAG_#{tag_type.to_s.split('::').last}\n"
27
+ ret << ("#{' ' * indent}{\n")
28
+ payload.each do |load|
24
29
  ret << "#{load.to_s(indent + 2)}\n"
25
30
  end
26
- ret << (' ' * indent) + "}"
31
+ ret << ("#{' ' * indent}}")
27
32
  ret
28
33
  end
29
34
 
30
- def to_nbt_string(named = true)
35
+ def to_nbt_string(named: true)
31
36
  result = named ? binary_type_id + name_to_nbt_string : ''
32
37
  type = ::BinData::Int8be.new
33
- type.value = @tag_type.type_id
38
+ type.value = tag_type.type_id
34
39
  result << type.to_binary_s
35
40
  len = ::BinData::Int32be.new
36
- len.value = @payload.length
41
+ len.value = payload.length
37
42
  result << len.to_binary_s
38
- @payload.inject(result) do |r, load|
39
- r + load.to_nbt_string(false)
43
+ payload.inject(result) do |r, load|
44
+ r + load.to_nbt_string(named: false)
40
45
  end
41
46
  end
42
47
 
43
48
  def set_value(new_value, index)
44
- unless new_value.kind_of?(NBTUtils::Tag)
45
- t = @tag_type.new
49
+ unless new_value.is_a?(NBTUtils::Tag)
50
+ t = tag_type.new
46
51
  t.value = new_value
47
52
  new_value = t
48
53
  end
49
-
50
- @payload[index] = new_value
54
+
55
+ payload[index] = new_value
51
56
  end
52
57
  end
53
58
  end
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
- class Long # signed, per spec
5
+ # signed, per spec
6
+ class Long
4
7
  include NBTUtils::Tag
5
8
 
6
9
  type_id 4
@@ -1,6 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
- class Short # signed, per spec
5
+ # signed, per spec
6
+ class Short
4
7
  include NBTUtils::Tag
5
8
 
6
9
  type_id 2
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module NBTUtils
2
4
  module Tag
3
5
  class String