Skip to content

Commit

Permalink
add Tag->toSNBT
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtThiemann committed Jul 5, 2024
1 parent 17ae726 commit 255c5ad
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/Tag/ByteArrayTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . "]";
}
}
8 changes: 8 additions & 0 deletions src/Tag/ByteTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
12 changes: 12 additions & 0 deletions src/Tag/CompoundTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . "}";
}
}
8 changes: 8 additions & 0 deletions src/Tag/DoubleTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
8 changes: 8 additions & 0 deletions src/Tag/EndTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,12 @@ function equals(Tag $tag): bool
{
return $tag->getType() === $this->getType();
}

/**
* @inheritDoc
*/
public function toSNBT(): string
{
return "";
}
}
8 changes: 8 additions & 0 deletions src/Tag/FloatTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
12 changes: 12 additions & 0 deletions src/Tag/IntArrayTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . "]";
}
}
8 changes: 8 additions & 0 deletions src/Tag/IntTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
12 changes: 12 additions & 0 deletions src/Tag/ListTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . "]";
}
}
12 changes: 12 additions & 0 deletions src/Tag/LongArrayTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . "]";
}
}
8 changes: 8 additions & 0 deletions src/Tag/LongTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
8 changes: 8 additions & 0 deletions src/Tag/ShortTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
8 changes: 8 additions & 0 deletions src/Tag/StringTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 11 additions & 2 deletions src/Tag/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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
Expand Down

0 comments on commit 255c5ad

Please sign in to comment.