Class: CraftBook::NBT::ContainerTag Abstract

Inherits:
EnumerableTag show all
Defined in:
lib/craftbook/nbt/container_tag.rb

Overview

This class is abstract.

Abstract base class for tag types that can contain other Tag objects as children.

Direct Known Subclasses

CompoundTag, ListTag

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

Attributes inherited from Tag

#name, #type

Instance Method Summary collapse

Methods inherited from EnumerableTag

#[], #[]=, #each, #push, #size

Methods inherited from Tag

parse, #pretty, #stringify, #to_json

Constructor Details

#initialize(type, name, *values) ⇒ ContainerTag

Creates a new instance of the EnumerableTag class.

Parameters:

  • name (String, NilClass)

    The name of the tag, or nil when unnamed.

  • values (Array<Tag>)

    Zero or more values to add during initialization.



15
16
17
# File 'lib/craftbook/nbt/container_tag.rb', line 15

def initialize(type, name, *values)
  super(type, name, *values)
end

Instance Method Details

#pretty_print(io = STDOUT, level = 0, indent = ' ') ⇒ void

This method returns an undefined value.

Outputs the NBT tag as a formatted and tree-structured string.

Parameters:

  • io (IO, #puts) (defaults to: STDOUT)

    An IO-like object that responds to #puts.

  • level (Integer) (defaults to: 0)

    The indentation level.

  • indent (String) (defaults to: ' ')

    The string inserted for each level of indent.



34
35
36
37
38
39
40
# File 'lib/craftbook/nbt/container_tag.rb', line 34

def pretty_print(io = STDOUT, level = 0, indent = '    ')
  space = indent * level
  io.puts(space + to_s)
  io.puts(space + '{')
  each { |child| child.pretty_print(io, level + 1, indent) }
  io.puts(space + '}')
end

#to_hHash{Symbol => Object}

Returns the hash-representation of this object.

Returns:

  • (Hash{Symbol => Object})

    the hash-representation of this object.



21
22
23
24
# File 'lib/craftbook/nbt/container_tag.rb', line 21

def to_h
  a = @values.map { |child| child.to_h }
  { name: @name, type: @type, values: a }
end