Class: CraftBook::NBT::ListTag
- Inherits:
-
ContainerTag
- Object
- Tag
- EnumerableTag
- ContainerTag
- CraftBook::NBT::ListTag
- Defined in:
- lib/craftbook/nbt/list_tag.rb
Overview
Represents a collection of unnamed tags of the same type.
Constant Summary
Constants inherited from Tag
Tag::TYPE_BYTE, Tag::TYPE_BYTE_ARRAY, Tag::TYPE_COMPOUND, Tag::TYPE_DOUBLE, Tag::TYPE_END, Tag::TYPE_FLOAT, Tag::TYPE_INT, Tag::TYPE_INT_ARRAY, Tag::TYPE_LIST, Tag::TYPE_LONG, Tag::TYPE_LONG_ARRAY, Tag::TYPE_SHORT, Tag::TYPE_STRING
Instance Attribute Summary collapse
-
#child_type ⇒ Integer
readonly
One of the
Tag::TYPE_*
constants indicating the primitive type of the child tags.
Attributes inherited from Tag
Instance Method Summary collapse
-
#initialize(name, child_type, *values) ⇒ ListTag
constructor
Creates a new instance of the ListTag class.
-
#stringify ⇒ String
The NBT tag as an SNBT string.
-
#to_h ⇒ Hash{Symbol => Object}
The hash-representation of this object.
-
#to_s ⇒ String
The NBT tag as a formatted and human-readable string.
Methods inherited from ContainerTag
Methods inherited from EnumerableTag
#[], #[]=, #each, #push, #size
Methods inherited from Tag
parse, #pretty, #pretty_print, #to_json
Constructor Details
#initialize(name, child_type, *values) ⇒ ListTag
Creates a new instance of the CraftBook::NBT::ListTag class.
20 21 22 23 |
# File 'lib/craftbook/nbt/list_tag.rb', line 20 def initialize(name, child_type, *values) super(TYPE_LIST, name, *values) @child_type = Integer(child_type) end |
Instance Attribute Details
#child_type ⇒ Integer (readonly)
Returns One of the Tag::TYPE_*
constants indicating the primitive type of the child tags.
12 13 14 |
# File 'lib/craftbook/nbt/list_tag.rb', line 12 def child_type @child_type end |
Instance Method Details
#stringify ⇒ String
Returns the NBT tag as an SNBT string.
33 34 35 |
# File 'lib/craftbook/nbt/list_tag.rb', line 33 def stringify "#{snbt_prefix}[#{map(&:stringify).join(',')}]" end |
#to_h ⇒ Hash{Symbol => Object}
Returns the hash-representation of this object.
39 40 41 42 43 44 45 46 47 |
# File 'lib/craftbook/nbt/list_tag.rb', line 39 def to_h children = @values.map do |obj| child = obj.to_h child.delete(:name) child.delete(:type) child end { name: @name, type: @type, child_type: @child_type, values: children } end |
#to_s ⇒ String
Returns the NBT tag as a formatted and human-readable string.
27 28 29 |
# File 'lib/craftbook/nbt/list_tag.rb', line 27 def to_s "TAG_List(#{@name ? "\"#{@name}\"" : 'None'}): #{size} #{size == 1 ? 'child' : 'children'}" end |