Class: CraftBook::NBT::Tag Abstract
- Inherits:
-
Object
- Object
- CraftBook::NBT::Tag
- Defined in:
- lib/craftbook/nbt/tag.rb
Overview
Abstract base class for all tag types.
Direct Known Subclasses
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
to127
inclusive. 0x01
- TYPE_SHORT =
A signed 16-bit integer in the range of
-32768
to32767
inclusive. 0x02
- TYPE_INT =
A signed 32-bit integer in the range of
-2147483648
to2147483647
inclusive. 0x03
- TYPE_LONG =
A signed 64-bit integer in the range of
-9223372036854775808
and9223372036854775807
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
to127
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
to2147483647
inclusive. 0x0B
- TYPE_LONG_ARRAY =
A contiguous collection of signed 64-bit integers in the range of
-9223372036854775808
and9223372036854775807
inclusive. 0x0C
Instance Attribute Summary collapse
-
#name ⇒ String?
The name of the tag, or
nil
if unnamed. -
#type ⇒ Integer
readonly
One of the
TYPE_*
constants to describe the primitive NBT type.
Class Method Summary collapse
-
.parse(json) ⇒ Tag
Parses a Tag object from a JSON string.
Instance Method Summary collapse
-
#initialize(type, name) ⇒ Tag
constructor
Creates a new instance of the Tag class.
-
#pretty(indent = ' ') ⇒ String
Retrieves the NBT tag as a formatted and tree-structured string.
-
#pretty_print(io = STDOUT, level = 0, indent = ' ') ⇒ void
Outputs the NBT tag as a formatted and tree-structured string.
-
#stringify ⇒ String
(also: #snbt)
abstract
The NBT tag as an SNBT string.
-
#to_h ⇒ Hash{Symbol => Object}
(also: #to_hash)
abstract
The hash-representation of this object.
-
#to_json(pretty = false, **opts) ⇒ String
Retrieves the NBT tag in JavaScript Object Notation (JSON) format.
Constructor Details
#initialize(type, name) ⇒ Tag
Creates a new instance of the CraftBook::NBT::Tag class.
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
#name ⇒ String?
Returns the name of the tag, or nil
if unnamed.
69 70 71 |
# File 'lib/craftbook/nbt/tag.rb', line 69 def name @name end |
#type ⇒ Integer (readonly)
Returns 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.
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.
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.
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 |
#stringify ⇒ String Also known as: snbt
Returns the NBT tag as an SNBT string.
125 126 127 |
# File 'lib/craftbook/nbt/tag.rb', line 125 def stringify raise(NotImplementedError, "#{__method__} must be implemented in derived classes") end |
#to_h ⇒ Hash{Symbol => Object} Also known as: to_hash
Returns 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.
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 |