Class: CraftBook::NBT::TagBuilder
- Inherits:
-
Object
- Object
- CraftBook::NBT::TagBuilder
- Defined in:
- lib/craftbook/nbt/tag_builder.rb
Overview
Provides an intuitive and simplified way of building a complete NBT document from scratch, using only basic values without the need of creating intermediate Tag objects.
Instance Attribute Summary collapse
-
#root ⇒ CompoundTag
readonly
The implicit top-level CompoundTag that the TagBuilder is creating.
Class Method Summary collapse
-
.create(name) {|builder| ... } ⇒ CompoundTag
Creates a new TagBuilder instance within a block, returning the completed CompoundTag when the block closes.
-
.from(compound_tag) ⇒ TagBuilder
Creates a new TagBuilder instance from an existing CompoundTag.
Instance Method Summary collapse
-
#add(tag) {|builder| ... } ⇒ self
(also: #<<, #push)
Adds an existing Tag instance as a child to the current node.
-
#byte(name, value) ⇒ self
Creates a ByteTag from the specified value, and adds it to the current node.
-
#byte_array(name, *values) ⇒ self
Creates a ByteArrayTag from the specified values, and adds it to the current node.
-
#compound(name, *children) ⇒ self
Creates a CompoundTag from the specified value, and adds it to the current node.
-
#double(name, value) ⇒ self
Creates a DoubleTag from the specified value, and adds it to the current node.
-
#float(name, value) ⇒ self
Creates a FloatTag from the specified value, and adds it to the current node.
-
#initialize(name) ⇒ TagBuilder
constructor
Creates a new instance of the TagBuilder class.
-
#int(name, value) ⇒ self
Creates a IntTag from the specified value, and adds it to the current node.
-
#int_array(name, *values) ⇒ self
Creates a IntArrayTag from the specified values, and adds it to the current node.
-
#list(name, child_type, *children) ⇒ self
Creates a ListTag from the specified value, and adds it to the current node.
-
#long(name, value) ⇒ self
Creates a LongTag from the specified value, and adds it to the current node.
-
#long_array(name, *values) ⇒ self
Creates a LongArrayTag from the specified values, and adds it to the current node.
-
#short(name, value) ⇒ self
Creates a ShortTag from the specified value, and adds it to the current node.
-
#string(name, value) ⇒ self
Creates a StringTag from the specified value, and adds it to the current node.
Constructor Details
#initialize(name) ⇒ TagBuilder
Creates a new instance of the CraftBook::NBT::TagBuilder class.
17 18 19 20 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 17 def initialize(name) @root = CompoundTag.new(name) @stack = [] end |
Instance Attribute Details
#root ⇒ CompoundTag (readonly)
Returns the implicit top-level CompoundTag that the CraftBook::NBT::TagBuilder is creating.
12 13 14 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 12 def root @root end |
Class Method Details
.create(name) {|builder| ... } ⇒ CompoundTag
Creates a new CraftBook::NBT::TagBuilder instance within a block, returning the completed CompoundTag when the block closes.
29 30 31 32 33 34 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 29 def self.create(name) raise(LocalJumpError, 'block required') unless block_given? builder = new(name) yield builder builder.result end |
.from(compound_tag) ⇒ TagBuilder
Creates a new CraftBook::NBT::TagBuilder instance from an existing CompoundTag.
41 42 43 44 45 46 47 48 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 41 def self.from(compound_tag) raise(TypeError, "#{compound_tag} is not a #{CompoundTag}") unless compound_tag.is_a?(CompoundTag) builder = allocate builder.instance_variable_set(:@root, compound_tag) builder.instance_variable_set(:@stack, Array.new) builder end |
Instance Method Details
#add(tag) {|builder| ... } ⇒ self Also known as: <<, push
Adds an existing CraftBook::NBT::Tag instance as a child to the current node.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 57 def add(tag) raise(TypeError, "tag cannot be nil") unless tag.is_a?(Tag) root = @stack.empty? ? @root : @stack.last if root.is_a?(CompoundTag) && tag.name.nil? warn("direct children of Compound tags must be named") elsif root.is_a?(ListTag) && tag.name tag.name = nil end root.push(tag) self end |
#byte(name, value) ⇒ self
Creates a ByteTag from the specified value, and adds it to the current node.
79 80 81 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 79 def byte(name, value) add(ByteTag.new(name, Integer(value))) end |
#byte_array(name, *values) ⇒ self
Creates a ByteArrayTag from the specified values, and adds it to the current node.
142 143 144 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 142 def byte_array(name, *values) add(ByteArrayTag.new(name, *values)) end |
#compound(name = nil, children = nil) { ... } ⇒ self #compound(name = nil, children = nil) ⇒ self
Creates a CompoundTag from the specified value, and adds it to the current node.
207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 207 def compound(name, *children) compound = CompoundTag.new(name, *children) if block_given? @stack.push(compound) yield self @stack.pop end add(compound) end |
#double(name, value) ⇒ self
Creates a DoubleTag from the specified value, and adds it to the current node.
124 125 126 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 124 def double(name, value) add(DoubleTag.new(name, Float(value))) end |
#float(name, value) ⇒ self
Creates a FloatTag from the specified value, and adds it to the current node.
115 116 117 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 115 def float(name, value) add(FloatTag.new(name, Float(value))) end |
#int(name, value) ⇒ self
Creates a IntTag from the specified value, and adds it to the current node.
97 98 99 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 97 def int(name, value) add(IntTag.new(name, Integer(value))) end |
#int_array(name, *values) ⇒ self
Creates a IntArrayTag from the specified values, and adds it to the current node.
151 152 153 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 151 def int_array(name, *values) add(IntArrayTag.new(name, *values)) end |
#list(child_type, name = nil, children = nil) { ... } ⇒ self #list(child_type, name = nil, children = nil) ⇒ self
Creates a ListTag from the specified value, and adds it to the current node.
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 180 def list(name, child_type, *children) list = ListTag.new(name, child_type, *children) if block_given? @stack.push(list) yield @stack.pop end add(list) end |
#long(name, value) ⇒ self
Creates a LongTag from the specified value, and adds it to the current node.
106 107 108 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 106 def long(name, value) add(LongTag.new(name, Integer(value))) end |
#long_array(name, *values) ⇒ self
Creates a LongArrayTag from the specified values, and adds it to the current node.
160 161 162 |
# File 'lib/craftbook/nbt/tag_builder.rb', line 160 def long_array(name, *values) add(LongArrayTag.new(name, *values)) end |