Class: CraftBook::NBT::Tag Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/craftbook/nbt/tag.rb

Overview

This class is abstract.

Abstract base class for all tag types.

Direct Known Subclasses

EnumerableTag, ValueTag

Constant Summary collapse

TYPE_END =

Not a concrete tag, implies the end of a Compound tag during serialization.

0x00
TYPE_BYTE =

A signed 8-bit integer in the range of -128 to 127 inclusive.

0x01
TYPE_SHORT =

A signed 16-bit integer in the range of -32768 to 32767 inclusive.

0x02
TYPE_INT =

A signed 32-bit integer in the range of -2147483648 to 2147483647 inclusive.

0x03
TYPE_LONG =

A signed 64-bit integer in the range of -9223372036854775808 and 9223372036854775807 inclusive.

0x04
TYPE_FLOAT =

An IEEE-754 single-precision floating point number (NaN possible).

0x05
TYPE_DOUBLE =

An IEEE-754 double-precision floating point number (NaN possible).

0x06
TYPE_BYTE_ARRAY =

A contiguous collection of signed 8-bit integers in the range of -128 to 127 inclusive.

0x07
TYPE_STRING =

A UTF-8 encoded string.

0x08
TYPE_LIST =

A collection of unnamed tags of the same type.

0x09
TYPE_COMPOUND =

A collection of named tags, order not guaranteed.

0x0A
TYPE_INT_ARRAY =

A contiguous collection of signed 32-bit integers in the range of -2147483648 to 2147483647 inclusive.

0x0B
TYPE_LONG_ARRAY =

A contiguous collection of signed 64-bit integers in the range of -9223372036854775808 and 9223372036854775807 inclusive.

0x0C

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name) ⇒ Tag

Creates a new instance of the CraftBook::NBT::Tag class.

Parameters:

  • type (Integer)

    One of the TAG_* constants indicating the primitive tag type.

  • name (String, NilClass)

    The name of the tag, or nil when unnamed.



75
76
77
78
# File 'lib/craftbook/nbt/tag.rb', line 75

def initialize(type, name)
  @type = type || raise(TypeError, 'type cannot be nil')
  @name = name
end

Instance Attribute Details

#nameString?

Returns the name of the tag, or nil if unnamed.

Returns:

  • (String?)

    the name of the tag, or nil if unnamed.



69
70
71
# File 'lib/craftbook/nbt/tag.rb', line 69

def name
  @name
end

#typeInteger (readonly)

Returns one of the TYPE_* constants to describe the primitive NBT type.

Returns:

  • (Integer)

    one of the TYPE_* constants to describe the primitive NBT type.



65
66
67
# File 'lib/craftbook/nbt/tag.rb', line 65

def type
  @type
end

Class Method Details

.parse(json) ⇒ Tag

Parses a CraftBook::NBT::Tag object from a JSON string.

Parameters:

  • json (String)

    A string in JSON format.

Returns:

Raises:



115
116
117
118
119
# File 'lib/craftbook/nbt/tag.rb', line 115

def self.parse(json)
  hash = JSON.parse(json, symbolize_names: true )
  raise(ParseError, 'invalid format, expected object') unless hash.is_a?(Hash)
  from_hash(hash)
end

Instance Method Details

#pretty(indent = ' ') ⇒ String

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

Parameters:

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

    The string inserted for each level of indent.

Returns:

  • (String)

    The NBT string as a formatted string.

See Also:



140
141
142
143
144
# File 'lib/craftbook/nbt/tag.rb', line 140

def pretty(indent = '    ')
  io = StringIO.new
  pretty_print(io, 0, indent)
  io.string
end

#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.

See Also:



155
156
157
# File 'lib/craftbook/nbt/tag.rb', line 155

def pretty_print(io = STDOUT, level = 0, indent = '    ')
  io.puts(indent * level + self.to_s)
end

#stringifyString Also known as: snbt

This method is abstract.

Returns the NBT tag as an SNBT string.

Returns:

  • (String)

    the NBT tag as an SNBT string.

Raises:

  • (NotImplementedError)

    Method must be overridden in derived classes.



125
126
127
# File 'lib/craftbook/nbt/tag.rb', line 125

def stringify
  raise(NotImplementedError, "#{__method__} must be implemented in derived classes")
end

#to_hHash{Symbol => Object} Also known as: to_hash

This method is abstract.

Returns the hash-representation of this object.

Returns:

  • (Hash{Symbol => Object})

    the hash-representation of this object.



91
92
93
# File 'lib/craftbook/nbt/tag.rb', line 91

def to_h
  { name: @name, type: @type }
end

#to_json(pretty = false, **opts) ⇒ String

Retrieves the NBT tag in JavaScript Object Notation (JSON) format.

Parameters:

  • pretty (Boolean) (defaults to: false)

    Flag indicating if output should be formatted in a more human-readable structure.

  • opts ({Symbol=>String})

    Options for how the output is formatted when using pretty flag.

Options Hash (**opts):

  • indent: (String) — default: ' '

    The string used for indenting.

  • space: (String) — default: ' '

    The string used for spaces.

  • array_nl: (String) — default: "\n"

    The string used for newlines between array elements.

  • object_nl: (String) — default: "\n"

    The string used for newlines between objects.

Returns:

  • (String)

    the JSON representation of this object.



106
107
108
# File 'lib/craftbook/nbt/tag.rb', line 106

def to_json(pretty = false, **opts)
  pretty ? JSON.pretty_generate(to_h.compact, **opts) : to_h.compact.to_json
end