Class: CraftBook::NBT::ListTag

Inherits:
ContainerTag show all
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

Attributes inherited from Tag

#name, #type

Instance Method Summary collapse

Methods inherited from ContainerTag

#pretty_print

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.

Parameters:

  • name (String, NilClass)

    The name of the tag, or nil when unnamed.

  • child_type (Integer)

    One of the Tag::TYPE_* constants indicating the primitive type of the child tags.

  • values (Array<Tag>)

    Zero or more values to add during initialization.



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_typeInteger (readonly)

Returns One of the Tag::TYPE_* constants indicating the primitive type of the child tags.

Returns:

  • (Integer)

    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

#stringifyString

Returns the NBT tag as an SNBT string.

Returns:

  • (String)

    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_hHash{Symbol => Object}

Returns the hash-representation of this object.

Returns:

  • (Hash{Symbol => Object})

    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_sString

Returns the NBT tag as a formatted and human-readable string.

Returns:

  • (String)

    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