Module: NBTUtils::Tag

Included in:
Byte, ByteArray, Compound, Double, End, Float, Int, IntArray, List, Long, Short, String
Defined in:
lib/nbt_utils/tag.rb,
lib/nbt_utils/tag/end.rb,
lib/nbt_utils/tag/int.rb,
lib/nbt_utils/tag/byte.rb,
lib/nbt_utils/tag/list.rb,
lib/nbt_utils/tag/long.rb,
lib/nbt_utils/tag/float.rb,
lib/nbt_utils/tag/short.rb,
lib/nbt_utils/tag/double.rb,
lib/nbt_utils/tag/string.rb,
lib/nbt_utils/tag/compound.rb,
lib/nbt_utils/tag/int_array.rb,
lib/nbt_utils/tag/byte_array.rb

Defined Under Namespace

Modules: ClassMethods Classes: Byte, ByteArray, Compound, Double, End, Float, Int, IntArray, List, Long, Short, String

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/nbt_utils/tag.rb', line 5

def name
  @name
end

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/nbt_utils/tag.rb', line 5

def payload
  @payload
end

Class Method Details

.add_tag_type(index, tag_type) ⇒ Object



72
73
74
75
# File 'lib/nbt_utils/tag.rb', line 72

def self.add_tag_type(index, tag_type)
  @tag_types ||= []
  @tag_types[index] = tag_type
end

.included(base) ⇒ Object



7
8
9
# File 'lib/nbt_utils/tag.rb', line 7

def self.included(base)
  base.extend ClassMethods
end

.tag_type_to_class(tag_type) ⇒ Object



68
69
70
# File 'lib/nbt_utils/tag.rb', line 68

def self.tag_type_to_class(tag_type)
  @tag_types[tag_type.to_i]
end

Instance Method Details

#binary_type_idObject



20
21
22
# File 'lib/nbt_utils/tag.rb', line 20

def binary_type_id
  type_id.chr
end

#initialize(io, named: true) ⇒ Object



11
12
13
14
# File 'lib/nbt_utils/tag.rb', line 11

def initialize(io, named: true)
  read_name(io) if named
  @payload = self.class.payload_class.new.read(io)
end

#name_to_nbt_stringObject



38
39
40
41
42
# File 'lib/nbt_utils/tag.rb', line 38

def name_to_nbt_string
  nm = NBTUtils::TagName.new
  nm.data = name
  nm.to_binary_s
end

#read_name(io) ⇒ Object



34
35
36
# File 'lib/nbt_utils/tag.rb', line 34

def read_name(io)
  @name = NBTUtils::TagName.new.read(io).data
end

#set_value(new_value, _index = nil) ⇒ Object



48
49
50
# File 'lib/nbt_utils/tag.rb', line 48

def set_value(new_value, _index = nil)
  payload.value = new_value
end

#tag_type_to_class(tag_type) ⇒ Object



44
45
46
# File 'lib/nbt_utils/tag.rb', line 44

def tag_type_to_class(tag_type)
  NBTUtils::Tag.tag_type_to_class(tag_type)
end

#to_nbt_string(named: true) ⇒ Object



29
30
31
32
# File 'lib/nbt_utils/tag.rb', line 29

def to_nbt_string(named: true)
  result = named ? binary_type_id + name_to_nbt_string : ''
  result + payload.to_binary_s
end

#to_s(indent = 0) ⇒ Object



24
25
26
27
# File 'lib/nbt_utils/tag.rb', line 24

def to_s(indent = 0)
  klass = self.class.to_s.split('::').last
  (' ' * indent) + "TAG_#{klass}#{name ? "(\"#{name}\")" : ''}: #{payload.value}"
end

#type_idObject



16
17
18
# File 'lib/nbt_utils/tag.rb', line 16

def type_id
  self.class.type_id
end