Class: NBTUtils::Tag::Compound

Inherits:
Object
  • Object
show all
Includes:
NBTUtils::Tag
Defined in:
lib/nbt_utils/tag/compound.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) ⇒ Compound



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

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

  until (last_byte = io.read(1).bytes.first) == NBTUtils::Tag::End.type_id
    klass = tag_type_to_class(last_byte)
    add_tag klass.new(io, named: true)
  end
end

Instance Attribute Details

#tag_namesObject (readonly)

Returns the value of attribute tag_names.



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

def tag_names
  @tag_names
end

Instance Method Details

#add_tag(tag) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/nbt_utils/tag/compound.rb', line 56

def add_tag(tag)
  raise MissingCompoundPayloadTagNameError if tag.name.nil?
  raise DuplicateCompoundPayloadTagNameError if tag_names.include?(tag.name)

  tag_names << tag.name
  payload << tag
end

#find_tag(name) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/nbt_utils/tag/compound.rb', line 44

def find_tag(name)
  if name.is_a?(Regexp)
    payload.detect { |tag| tag.name.to_s =~ /#{name}/ }
  else
    payload.detect { |tag| tag.name.to_s == name }
  end
end

#find_tags(name) ⇒ Object



52
53
54
# File 'lib/nbt_utils/tag/compound.rb', line 52

def find_tags(name)
  payload.select { |tag| tag.name.to_s =~ /#{name}/ }
end

#remove_tag(name) ⇒ Object



75
76
77
# File 'lib/nbt_utils/tag/compound.rb', line 75

def remove_tag(name)
  payload.delete find_tag(name)
end

#set_value(new_value, index) ⇒ Object

update one of my tags directly. sort of wonky but here to conform to the api.



71
72
73
# File 'lib/nbt_utils/tag/compound.rb', line 71

def set_value(new_value, index)
  update_tag(index, new_value)
end

#to_nbt_string(named: true) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/nbt_utils/tag/compound.rb', line 34

def to_nbt_string(named: true)
  result = named ? binary_type_id + name_to_nbt_string : ''

  result = payload.inject(result) do |r, load|
    r + load.to_nbt_string(named: true)
  end

  result + NBTUtils::Tag::End.new(nil).to_nbt_string
end

#to_s(indent = 0) ⇒ Object



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

def to_s(indent = 0)
  ret = (' ' * indent) + "TAG_Compound#{name ? "(\"#{name}\")" : ''}: #{payload.length} entries\n"
  ret << ("#{' ' * indent}{\n")
  payload.each do |load|
    ret << "#{load.to_s(indent + 2)}\n"
  end
  ret << ("#{' ' * indent}}")

  ret
end

#update_tag(name, new_value, index = nil) ⇒ Object

update one of my tags indirectly



65
66
67
68
# File 'lib/nbt_utils/tag/compound.rb', line 65

def update_tag(name, new_value, index = nil)
  tag = find_tag(name)
  tag.set_value(new_value, index)
end