Skip to content

Commit 255c5ad

Browse files
committed
add Tag->toSNBT
1 parent 17ae726 commit 255c5ad

14 files changed

+135
-2
lines changed

src/Tag/ByteArrayTag.php

+12
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,16 @@ protected function getValueString(): string
7575
}
7676
return $this->count() . " byte" . ($this->count() === 1 ? "" : "s") . " [" . implode(" ", $values) . "]";
7777
}
78+
79+
/**
80+
* @inheritDoc
81+
*/
82+
public function toSNBT(): string
83+
{
84+
$values = [];
85+
foreach ($this->valueArray as $val) {
86+
$values[] = $val . "b";
87+
}
88+
return "[B;" . implode(", ", $values) . "]";
89+
}
7890
}

src/Tag/ByteTag.php

+8
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,12 @@ protected static function readContentRaw(Reader $reader, TagOptions $options): s
3434
{
3535
return $reader->getDeserializer()->readByte()->getRawData();
3636
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function toSNBT(): string
42+
{
43+
return $this->value . "b";
44+
}
3745
}

src/Tag/CompoundTag.php

+12
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,16 @@ function equals(Tag $tag): bool
413413
}
414414
return true;
415415
}
416+
417+
/**
418+
* @inheritDoc
419+
*/
420+
public function toSNBT(): string
421+
{
422+
$data = [];
423+
foreach ($this->valueArray as $value) {
424+
$data[] = json_encode($value->getName()) . ": " . $value->toSNBT();
425+
}
426+
return "{" . implode(", ", $data) . "}";
427+
}
416428
}

src/Tag/DoubleTag.php

+8
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,12 @@ public function setValue(float $value): static
5151
$this->resetRawValue();
5252
return parent::setValue($value);
5353
}
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
public function toSNBT(): string
59+
{
60+
return $this->value . "d";
61+
}
5462
}

src/Tag/EndTag.php

+8
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,12 @@ function equals(Tag $tag): bool
6464
{
6565
return $tag->getType() === $this->getType();
6666
}
67+
68+
/**
69+
* @inheritDoc
70+
*/
71+
public function toSNBT(): string
72+
{
73+
return "";
74+
}
6775
}

src/Tag/FloatTag.php

+8
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,12 @@ public function setValue(float $value): static
5151
$this->resetRawValue();
5252
return parent::setValue($value);
5353
}
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
public function toSNBT(): string
59+
{
60+
return $this->value . "f";
61+
}
5462
}

src/Tag/IntArrayTag.php

+12
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,16 @@ protected function checkArrayKey($offset): bool
5959
{
6060
return is_int($offset);
6161
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function toSNBT(): string
67+
{
68+
$values = [];
69+
foreach ($this->valueArray as $val) {
70+
$values[] = $val;
71+
}
72+
return "[I;" . implode(", ", $values) . "]";
73+
}
6274
}

src/Tag/IntTag.php

+8
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,12 @@ protected static function readContentRaw(Reader $reader, TagOptions $options): s
3434
{
3535
return $reader->getDeserializer()->readInt()->getRawData();
3636
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function toSNBT(): string
42+
{
43+
return strval($this->value);
44+
}
3745
}

src/Tag/ListTag.php

+12
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,16 @@ protected function getValueString(): string
255255
}
256256
return parent::getValueString();
257257
}
258+
259+
/**
260+
* @inheritDoc
261+
*/
262+
public function toSNBT(): string
263+
{
264+
$values = [];
265+
foreach ($this->valueArray as $value) {
266+
$values[] = $value->toSNBT();
267+
}
268+
return "[" . implode(", ", $values) . "]";
269+
}
258270
}

src/Tag/LongArrayTag.php

+12
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,16 @@ protected function checkArrayKey($offset): bool
8888
{
8989
return is_int($offset);
9090
}
91+
92+
/**
93+
* @inheritDoc
94+
*/
95+
public function toSNBT(): string
96+
{
97+
$values = [];
98+
foreach ($this->valueArray as $val) {
99+
$values[] = $val . "L";
100+
}
101+
return "[L;" . implode(", ", $values) . "]";
102+
}
91103
}

src/Tag/LongTag.php

+8
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ public function setValue(int $value): static
5353
$this->resetRawValue();
5454
return parent::setValue($value);
5555
}
56+
57+
/**
58+
* @inheritDoc
59+
*/
60+
public function toSNBT(): string
61+
{
62+
return $this->value . "L";
63+
}
5664
}

src/Tag/ShortTag.php

+8
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,12 @@ protected static function readContentRaw(Reader $reader, TagOptions $options): s
3434
{
3535
return $reader->getDeserializer()->readShort()->getRawData();
3636
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function toSNBT(): string
42+
{
43+
return $this->value . "s";
44+
}
3745
}

src/Tag/StringTag.php

+8
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,12 @@ public function equals(Tag $tag): bool
9494
return $tag instanceof StringTag && $this->getType() === $tag->getType() &&
9595
$tag->getValue() === $this->getValue();
9696
}
97+
98+
/**
99+
* @inheritDoc
100+
*/
101+
public function toSNBT(): string
102+
{
103+
return json_encode($this->value);
104+
}
97105
}

src/Tag/Tag.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,12 @@ public function write(Writer $writer): static
233233

234234
/**
235235
* @param string $str
236+
* @param int $width
236237
* @return string
237238
*/
238-
protected function indent(string $str): string
239+
protected function indent(string $str, int $width = 2): string
239240
{
240-
return " " . str_replace("\n", "\n ", $str);
241+
return str_repeat(" ", $width) . str_replace("\n", "\n ", $str);
241242
}
242243

243244
/**
@@ -256,6 +257,14 @@ public function __toString(): string
256257
return $this->getTagTypeString() . "('" . ($this->getName() ?: "None") . "'): " . $this->getValueString();
257258
}
258259

260+
/**
261+
* Convert tag to SNBT
262+
* See https://minecraft.wiki/w/NBT_format#Conversion_to_SNBT
263+
*
264+
* @return string
265+
*/
266+
abstract public function toSNBT(): string;
267+
259268
/**
260269
* @param Tag $tag
261270
* @return bool

0 commit comments

Comments
 (0)