extended CompoundTag API to allow force-writes and returning defaults on bad tag types

This commit is contained in:
Dylan K. Taylor 2017-10-29 18:01:33 +00:00
parent 8aca373194
commit bb6b100443

View File

@ -151,22 +151,24 @@ class CompoundTag extends NamedTag implements \ArrayAccess{
/** /**
* Returns the value of the child tag with the specified name, or $default if the tag doesn't exist. If the child * Returns the value of the child tag with the specified name, or $default if the tag doesn't exist. If the child
* tag is not of type $expectedType, an exception will be thrown. * tag is not of type $expectedType, an exception will be thrown, unless a default is given and $badTagDefault is
* true.
* *
* @param string $name * @param string $name
* @param string $expectedClass * @param string $expectedClass
* @param mixed $default * @param mixed $default
* @param bool $badTagDefault Return the specified default if the tag is not of the expected type.
* *
* @return mixed * @return mixed
*/ */
public function getTagValue(string $name, string $expectedClass, $default = null){ public function getTagValue(string $name, string $expectedClass, $default = null, bool $badTagDefault = false){
$tag = $this->getTag($name, $expectedClass); $tag = $this->getTag($name, $badTagDefault ? NamedTag::class : $expectedClass);
if($tag !== null){ if($tag instanceof $expectedClass){
return $tag->getValue(); return $tag->getValue();
} }
if($default === null){ if($default === null){
throw new \RuntimeException("Tag with name \"$name\" not found and no default value given"); throw new \RuntimeException("Tag with name \"$name\" " . ($tag !== null ? "not of expected type" : "not found") . " and no valid default value given");
} }
return $default; return $default;
@ -179,91 +181,100 @@ class CompoundTag extends NamedTag implements \ArrayAccess{
/** /**
* @param string $name * @param string $name
* @param int|null $default * @param int|null $default
* @param bool $badTagDefault
* *
* @return int * @return int
*/ */
public function getByte(string $name, ?int $default = null) : int{ public function getByte(string $name, ?int $default = null, bool $badTagDefault = false) : int{
return $this->getTagValue($name, ByteTag::class, $default); return $this->getTagValue($name, ByteTag::class, $default, $badTagDefault);
} }
/** /**
* @param string $name * @param string $name
* @param int|null $default * @param int|null $default
* @param bool $badTagDefault
* *
* @return int * @return int
*/ */
public function getShort(string $name, ?int $default = null) : int{ public function getShort(string $name, ?int $default = null, bool $badTagDefault = false) : int{
return $this->getTagValue($name, ShortTag::class, $default); return $this->getTagValue($name, ShortTag::class, $default, $badTagDefault);
} }
/** /**
* @param string $name * @param string $name
* @param int|null $default * @param int|null $default
* @param bool $badTagDefault
* *
* @return int * @return int
*/ */
public function getInt(string $name, ?int $default = null) : int{ public function getInt(string $name, ?int $default = null, bool $badTagDefault = false) : int{
return $this->getTagValue($name, IntTag::class, $default); return $this->getTagValue($name, IntTag::class, $default, $badTagDefault);
} }
/** /**
* @param string $name * @param string $name
* @param int|null $default * @param int|null $default
* @param bool $badTagDefault
* *
* @return int * @return int
*/ */
public function getLong(string $name, ?int $default = null) : int{ public function getLong(string $name, ?int $default = null, bool $badTagDefault = false) : int{
return $this->getTagValue($name, LongTag::class, $default); return $this->getTagValue($name, LongTag::class, $default, $badTagDefault);
} }
/** /**
* @param string $name * @param string $name
* @param float|null $default * @param float|null $default
* @param bool $badTagDefault
* *
* @return float * @return float
*/ */
public function getFloat(string $name, ?float $default = null) : float{ public function getFloat(string $name, ?float $default = null, bool $badTagDefault = false) : float{
return $this->getTagValue($name, FloatTag::class, $default); return $this->getTagValue($name, FloatTag::class, $default, $badTagDefault);
} }
/** /**
* @param string $name * @param string $name
* @param float|null $default * @param float|null $default
* @param bool $badTagDefault
* *
* @return float * @return float
*/ */
public function getDouble(string $name, ?float $default = null) : float{ public function getDouble(string $name, ?float $default = null, bool $badTagDefault = false) : float{
return $this->getTagValue($name, DoubleTag::class, $default); return $this->getTagValue($name, DoubleTag::class, $default, $badTagDefault);
} }
/** /**
* @param string $name * @param string $name
* @param null|string $default * @param string|null $default
* @param bool $badTagDefault
* *
* @return string * @return string
*/ */
public function getByteArray(string $name, ?string $default = null) : string{ public function getByteArray(string $name, ?string $default = null, bool $badTagDefault = false) : string{
return $this->getTagValue($name, ByteArrayTag::class, $default); return $this->getTagValue($name, ByteArrayTag::class, $default, $badTagDefault);
} }
/** /**
* @param string $name * @param string $name
* @param null|string $default * @param string|null $default
* @param bool $badTagDefault
* *
* @return string * @return string
*/ */
public function getString(string $name, ?string $default = null) : string{ public function getString(string $name, ?string $default = null, bool $badTagDefault = false) : string{
return $this->getTagValue($name, StringTag::class, $default); return $this->getTagValue($name, StringTag::class, $default, $badTagDefault);
} }
/** /**
* @param string $name * @param string $name
* @param int[]|null $default * @param int[]|null $default
* @param bool $badTagDefault
* *
* @return int[] * @return array
*/ */
public function getIntArray(string $name, ?array $default = null) : array{ public function getIntArray(string $name, ?array $default = null, bool $badTagDefault = false) : array{
return $this->getTagValue($name, IntArrayTag::class, $default); return $this->getTagValue($name, IntArrayTag::class, $default, $badTagDefault);
} }
/** /**
@ -273,11 +284,12 @@ class CompoundTag extends NamedTag implements \ArrayAccess{
* @param string $name Name of the tag to set * @param string $name Name of the tag to set
* @param string $tagClass Class that extends NamedTag * @param string $tagClass Class that extends NamedTag
* @param mixed $value Value to set. This should be compatible with the specified tag type. * @param mixed $value Value to set. This should be compatible with the specified tag type.
* @param bool $force Force set the value even if the existing tag is not the correct type (overwrite the old tag)
*/ */
public function setTagValue(string $name, string $tagClass, $value) : void{ public function setTagValue(string $name, string $tagClass, $value, bool $force) : void{
assert(is_a($tagClass, NamedTag::class, true)); assert(is_a($tagClass, NamedTag::class, true));
$tag = $this->getTag($name, $tagClass); $tag = $this->getTag($name, $force ? NamedTag::class : $tagClass);
if($tag !== null){ if($tag instanceof $tagClass){
$tag->setValue($value); $tag->setValue($value);
}else{ }else{
$this->setTag(new $tagClass($name, $value)); $this->setTag(new $tagClass($name, $value));
@ -291,73 +303,82 @@ class CompoundTag extends NamedTag implements \ArrayAccess{
/** /**
* @param string $name * @param string $name
* @param int $value * @param int $value
* @param bool $force
*/ */
public function setByte(string $name, int $value) : void{ public function setByte(string $name, int $value, bool $force = false) : void{
$this->setTagValue($name, ByteTag::class, $value); $this->setTagValue($name, ByteTag::class, $value, $force);
} }
/** /**
* @param string $name * @param string $name
* @param int $value * @param int $value
* @param bool $force
*/ */
public function setShort(string $name, int $value) : void{ public function setShort(string $name, int $value, bool $force = false) : void{
$this->setTagValue($name, ShortTag::class, $value); $this->setTagValue($name, ShortTag::class, $value, $force);
} }
/** /**
* @param string $name * @param string $name
* @param int $value * @param int $value
* @param bool $force
*/ */
public function setInt(string $name, int $value) : void{ public function setInt(string $name, int $value, bool $force = false) : void{
$this->setTagValue($name, IntTag::class, $value); $this->setTagValue($name, IntTag::class, $value, $force);
} }
/** /**
* @param string $name * @param string $name
* @param int $value * @param int $value
* @param bool $force
*/ */
public function setLong(string $name, int $value) : void{ public function setLong(string $name, int $value, bool $force = false) : void{
$this->setTagValue($name, LongTag::class, $value); $this->setTagValue($name, LongTag::class, $value, $force);
} }
/** /**
* @param string $name * @param string $name
* @param float $value * @param float $value
* @param bool $force
*/ */
public function setFloat(string $name, float $value) : void{ public function setFloat(string $name, float $value, bool $force = false) : void{
$this->setTagValue($name, FloatTag::class, $value); $this->setTagValue($name, FloatTag::class, $value, $force);
} }
/** /**
* @param string $name * @param string $name
* @param float $value * @param float $value
* @param bool $force
*/ */
public function setDouble(string $name, float $value) : void{ public function setDouble(string $name, float $value, bool $force = false) : void{
$this->setTagValue($name, DoubleTag::class, $value); $this->setTagValue($name, DoubleTag::class, $value, $force);
} }
/** /**
* @param string $name * @param string $name
* @param string $value * @param string $value
* @param bool $force
*/ */
public function setByteArray(string $name, string $value) : void{ public function setByteArray(string $name, string $value, bool $force = false) : void{
$this->setTagValue($name, ByteArrayTag::class, $value); $this->setTagValue($name, ByteArrayTag::class, $value, $force);
} }
/** /**
* @param string $name * @param string $name
* @param string $value * @param string $value
* @param bool $force
*/ */
public function setString(string $name, string $value) : void{ public function setString(string $name, string $value, bool $force = false) : void{
$this->setTagValue($name, StringTag::class, $value); $this->setTagValue($name, StringTag::class, $value, $force);
} }
/** /**
* @param string $name * @param string $name
* @param int[] $value * @param int[] $value
* @param bool $force
*/ */
public function setIntArray(string $name, array $value) : void{ public function setIntArray(string $name, array $value, bool $force = false) : void{
$this->setTagValue($name, IntArrayTag::class, $value); $this->setTagValue($name, IntArrayTag::class, $value, $force);
} }