Skip to content

Commit

Permalink
added Tag->equals to compare NBT tags
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtThiemann committed Feb 28, 2022
1 parent 692eb12 commit 5cfc1a4
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 6 deletions.
21 changes: 20 additions & 1 deletion src/Tag/ArrayValueTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,23 @@ public function jsonSerialize()
{
return $this->valueArray;
}
}

/**
* @inheritDoc
*/
public function equals(Tag $tag): bool
{
if($tag === $this) {
return true;
}
if(!$tag instanceof ArrayValueTag || $this->getType() !== $tag->getType() || count($tag) !== count($this)) {
return false;
}
foreach ($this as $i => $val) {
if($val !== $tag[$i]) {
return false;
}
}
return true;
}
}
23 changes: 23 additions & 0 deletions src/Tag/CompoundTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,27 @@ public function getString(string $name): ?StringTag
$tag = $this->get($name);
return $tag instanceof StringTag ? $tag : null;
}

/**
* @inheritDoc
*/
function equals(Tag $tag): bool
{
if($tag === $this) {
return true;
}
if(!$tag instanceof CompoundTag || $this->getType() !== $tag->getType() || count($tag) !== count($this)) {
return false;
}
/**
* @var string $key
* @var Tag $val
*/
foreach ($this as $key => $val) {
if(!isset($tag[$key]) || !$val->equals($tag[$key])) {
return false;
}
}
return true;
}
}
10 changes: 9 additions & 1 deletion src/Tag/EndTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@ public function jsonSerialize()
{
return null;
}
}

/**
* @inheritDoc
*/
function equals(Tag $tag): bool
{
return $tag->getType() === $this->getType();
}
}
11 changes: 10 additions & 1 deletion src/Tag/FloatValueTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@ public function jsonSerialize()
{
return $this->value;
}
}

/**
* @inheritDoc
*/
public function equals(Tag $tag): bool
{
return $tag instanceof FloatValueTag && $this->getType() === $tag->getType() &&
$tag->getValue() === $this->getValue();
}
}
11 changes: 10 additions & 1 deletion src/Tag/IntValueTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@ public function jsonSerialize()
{
return $this->value;
}
}

/**
* @inheritDoc
*/
public function equals(Tag $tag): bool
{
return $tag instanceof IntValueTag && $this->getType() === $tag->getType() &&
$tag->getValue() === $this->getValue();
}
}
26 changes: 25 additions & 1 deletion src/Tag/ListTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,28 @@ protected function getTagTypeString(): string
{
return parent::getTagTypeString() . "<" . TagType::NAMES[$this->contentTagType] . ">";
}
}

/**
* @inheritDoc
*/
public function equals(Tag $tag): bool
{
if($tag === $this) {
return true;
}
if(!$tag instanceof ListTag || $this->getType() !== $tag->getType() ||
$this->getContentTag() !== $tag->getContentTag() || count($tag) !== count($this)) {
return false;
}
/**
* @var int $i
* @var Tag $val
*/
foreach ($this as $i => $val) {
if(!$val->equals($tag[$i])) {
return false;
}
}
return true;
}
}
11 changes: 10 additions & 1 deletion src/Tag/StringTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,13 @@ public function jsonSerialize()
{
return $this->value;
}
}

/**
* @inheritDoc
*/
public function equals(Tag $tag): bool
{
return $tag instanceof StringTag && $this->getType() === $tag->getType() &&
$tag->getValue() === $this->getValue();
}
}
6 changes: 6 additions & 0 deletions src/Tag/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ public function __toString(): string
return $this->getTagTypeString() . "('" . ($this->getName() ?: "None") . "'): " . $this->getValueString();
}

/**
* @param Tag $tag
* @return bool
*/
abstract function equals(Tag $tag): bool;

/**
* @return string
*/
Expand Down

0 comments on commit 5cfc1a4

Please sign in to comment.