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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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
}

0 commit comments

Comments
 (0)