diff --git a/src/Tag/ByteArrayTag.php b/src/Tag/ByteArrayTag.php index 649a5fe..62c8140 100644 --- a/src/Tag/ByteArrayTag.php +++ b/src/Tag/ByteArrayTag.php @@ -75,4 +75,16 @@ protected function getValueString(): string } return $this->count() . " byte" . ($this->count() === 1 ? "" : "s") . " [" . implode(" ", $values) . "]"; } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + $values = []; + foreach ($this->valueArray as $val) { + $values[] = $val . "b"; + } + return "[B;" . implode(", ", $values) . "]"; + } } diff --git a/src/Tag/ByteTag.php b/src/Tag/ByteTag.php index 4b36375..9b2d380 100644 --- a/src/Tag/ByteTag.php +++ b/src/Tag/ByteTag.php @@ -34,4 +34,12 @@ protected static function readContentRaw(Reader $reader, TagOptions $options): s { return $reader->getDeserializer()->readByte()->getRawData(); } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + return $this->value . "b"; + } } diff --git a/src/Tag/CompoundTag.php b/src/Tag/CompoundTag.php index d53ed87..b1a5471 100644 --- a/src/Tag/CompoundTag.php +++ b/src/Tag/CompoundTag.php @@ -413,4 +413,16 @@ function equals(Tag $tag): bool } return true; } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + $data = []; + foreach ($this->valueArray as $value) { + $data[] = json_encode($value->getName()) . ": " . $value->toSNBT(); + } + return "{" . implode(", ", $data) . "}"; + } } diff --git a/src/Tag/DoubleTag.php b/src/Tag/DoubleTag.php index 703279d..30c3e7e 100644 --- a/src/Tag/DoubleTag.php +++ b/src/Tag/DoubleTag.php @@ -51,4 +51,12 @@ public function setValue(float $value): static $this->resetRawValue(); return parent::setValue($value); } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + return $this->value . "d"; + } } diff --git a/src/Tag/EndTag.php b/src/Tag/EndTag.php index 1e4356c..322fd78 100644 --- a/src/Tag/EndTag.php +++ b/src/Tag/EndTag.php @@ -64,4 +64,12 @@ function equals(Tag $tag): bool { return $tag->getType() === $this->getType(); } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + return ""; + } } diff --git a/src/Tag/FloatTag.php b/src/Tag/FloatTag.php index aab7954..800f76a 100644 --- a/src/Tag/FloatTag.php +++ b/src/Tag/FloatTag.php @@ -51,4 +51,12 @@ public function setValue(float $value): static $this->resetRawValue(); return parent::setValue($value); } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + return $this->value . "f"; + } } diff --git a/src/Tag/IntArrayTag.php b/src/Tag/IntArrayTag.php index 465db18..6e6eef7 100644 --- a/src/Tag/IntArrayTag.php +++ b/src/Tag/IntArrayTag.php @@ -59,4 +59,16 @@ protected function checkArrayKey($offset): bool { return is_int($offset); } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + $values = []; + foreach ($this->valueArray as $val) { + $values[] = $val; + } + return "[I;" . implode(", ", $values) . "]"; + } } diff --git a/src/Tag/IntTag.php b/src/Tag/IntTag.php index d40c797..ef07c46 100644 --- a/src/Tag/IntTag.php +++ b/src/Tag/IntTag.php @@ -34,4 +34,12 @@ protected static function readContentRaw(Reader $reader, TagOptions $options): s { return $reader->getDeserializer()->readInt()->getRawData(); } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + return strval($this->value); + } } diff --git a/src/Tag/ListTag.php b/src/Tag/ListTag.php index e1b4004..3c22d33 100644 --- a/src/Tag/ListTag.php +++ b/src/Tag/ListTag.php @@ -255,4 +255,16 @@ protected function getValueString(): string } return parent::getValueString(); } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + $values = []; + foreach ($this->valueArray as $value) { + $values[] = $value->toSNBT(); + } + return "[" . implode(", ", $values) . "]"; + } } diff --git a/src/Tag/LongArrayTag.php b/src/Tag/LongArrayTag.php index 9c88d15..fa2b6fc 100644 --- a/src/Tag/LongArrayTag.php +++ b/src/Tag/LongArrayTag.php @@ -88,4 +88,16 @@ protected function checkArrayKey($offset): bool { return is_int($offset); } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + $values = []; + foreach ($this->valueArray as $val) { + $values[] = $val . "L"; + } + return "[L;" . implode(", ", $values) . "]"; + } } diff --git a/src/Tag/LongTag.php b/src/Tag/LongTag.php index f5d65cd..032fc27 100644 --- a/src/Tag/LongTag.php +++ b/src/Tag/LongTag.php @@ -53,4 +53,12 @@ public function setValue(int $value): static $this->resetRawValue(); return parent::setValue($value); } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + return $this->value . "L"; + } } diff --git a/src/Tag/ShortTag.php b/src/Tag/ShortTag.php index 6e6f23a..6253fca 100644 --- a/src/Tag/ShortTag.php +++ b/src/Tag/ShortTag.php @@ -34,4 +34,12 @@ protected static function readContentRaw(Reader $reader, TagOptions $options): s { return $reader->getDeserializer()->readShort()->getRawData(); } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + return $this->value . "s"; + } } diff --git a/src/Tag/StringTag.php b/src/Tag/StringTag.php index eaccb97..fbe5ccd 100644 --- a/src/Tag/StringTag.php +++ b/src/Tag/StringTag.php @@ -94,4 +94,12 @@ public function equals(Tag $tag): bool return $tag instanceof StringTag && $this->getType() === $tag->getType() && $tag->getValue() === $this->getValue(); } + + /** + * @inheritDoc + */ + public function toSNBT(): string + { + return json_encode($this->value); + } } diff --git a/src/Tag/Tag.php b/src/Tag/Tag.php index dca6bf7..db58dc7 100644 --- a/src/Tag/Tag.php +++ b/src/Tag/Tag.php @@ -233,11 +233,12 @@ public function write(Writer $writer): static /** * @param string $str + * @param int $width * @return string */ - protected function indent(string $str): string + protected function indent(string $str, int $width = 2): string { - return " " . str_replace("\n", "\n ", $str); + return str_repeat(" ", $width) . str_replace("\n", "\n ", $str); } /** @@ -256,6 +257,14 @@ public function __toString(): string return $this->getTagTypeString() . "('" . ($this->getName() ?: "None") . "'): " . $this->getValueString(); } + /** + * Convert tag to SNBT + * See https://minecraft.wiki/w/NBT_format#Conversion_to_SNBT + * + * @return string + */ + abstract public function toSNBT(): string; + /** * @param Tag $tag * @return bool