Class: NBTUtils::Tag::List

Inherits:
Object
  • Object
show all
Includes:
NBTUtils::Tag
Defined in:
lib/nbt_utils/tag/list.rb

Instance Attribute Summary collapse

Attributes included from NBTUtils::Tag

#name, #payload

Instance Method Summary collapse

Methods included from NBTUtils::Tag

add_tag_type, #binary_type_id, included, #name_to_nbt_string, #read_name, #tag_type_to_class, tag_type_to_class, #type_id

Constructor Details

#initialize(io, named: true) ⇒ List

Returns a new instance of List.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nbt_utils/tag/list.rb', line 12

def initialize(io, named: true)
  @payload = []
  read_name(io) if named

  tag_id = io.read(1).bytes.first.to_i
  @tag_type = NBTUtils::Tag.tag_type_to_class(tag_id)
  len = ::BinData::Int32be.new.read(io).value
  len.times do
    payload << tag_type.new(io, named: false)
  end
end

Instance Attribute Details

#tag_typeObject (readonly)

Returns the value of attribute tag_type.



10
11
12
# File 'lib/nbt_utils/tag/list.rb', line 10

def tag_type
  @tag_type
end

Instance Method Details

#set_value(new_value, index) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/nbt_utils/tag/list.rb', line 48

def set_value(new_value, index)
  unless new_value.is_a?(NBTUtils::Tag)
    t = tag_type.new
    t.value = new_value
    new_value = t
  end

  payload[index] = new_value
end

#to_nbt_string(named: true) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nbt_utils/tag/list.rb', line 35

def to_nbt_string(named: true)
  result = named ? binary_type_id + name_to_nbt_string : ''
  type = ::BinData::Int8be.new
  type.value = tag_type.type_id
  result << type.to_binary_s
  len = ::BinData::Int32be.new
  len.value = payload.length
  result << len.to_binary_s
  payload.inject(result) do |r, load|
    r + load.to_nbt_string(named: false)
  end
end

#to_s(indent = 0) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/nbt_utils/tag/list.rb', line 24

def to_s(indent = 0)
  ret = "#{' ' * indent}TAG_List#{name ? "(\"#{name}\")" : ''}: #{payload.length} entries of "\
        "type TAG_#{tag_type.to_s.split('::').last}\n"
  ret << ("#{' ' * indent}{\n")
  payload.each do |load|
    ret << "#{load.to_s(indent + 2)}\n"
  end
  ret << ("#{' ' * indent}}")
  ret
end