Merge branch 'stable'

# Conflicts:
#	resources/vanilla
#	src/pocketmine/entity/Human.php
This commit is contained in:
Dylan K. Taylor
2019-05-17 17:43:30 +01:00
3 changed files with 61 additions and 33 deletions

View File

@ -43,7 +43,6 @@ use pocketmine\item\FoodSource;
use pocketmine\item\Item;
use pocketmine\item\Totem;
use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\ByteArrayTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ListTag;
@ -67,6 +66,7 @@ use function array_merge;
use function array_rand;
use function array_values;
use function ceil;
use function in_array;
use function max;
use function min;
use function random_int;
@ -104,18 +104,36 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
public function __construct(World $world, CompoundTag $nbt){
if($this->skin === null){
$skinTag = $nbt->getCompoundTag("Skin");
if($skinTag === null or !self::isValidSkin($skinTag->hasTag("Data", ByteArrayTag::class) ?
$skinTag->getByteArray("Data") :
$skinTag->getString("Data", "")
)){
if($skinTag === null){
throw new \InvalidStateException((new \ReflectionClass($this))->getShortName() . " must have a valid skin set");
}
$this->skin = self::deserializeSkinNBT($skinTag); //this throws if the skin is invalid
}
parent::__construct($world, $nbt);
}
/**
* @param CompoundTag $skinTag
*
* @return Skin
* @throws \InvalidArgumentException
*/
protected static function deserializeSkinNBT(CompoundTag $skinTag) : Skin{
$skin = new Skin(
$skinTag->getString("Name"),
$skinTag->hasTag("Data", StringTag::class) ? $skinTag->getString("Data") : $skinTag->getByteArray("Data"), //old data (this used to be saved as a StringTag in older versions of PM)
$skinTag->getByteArray("CapeData", ""),
$skinTag->getString("GeometryName", ""),
$skinTag->getByteArray("GeometryData", "")
);
$skin->validate();
return $skin;
}
/**
* @deprecated
*
* Checks the length of a supplied skin bitmap and returns whether the length is valid.
*
* @param string $skin
@ -123,7 +141,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
* @return bool
*/
public static function isValidSkin(string $skin) : bool{
return strlen($skin) === 64 * 64 * 4 or strlen($skin) === 64 * 32 * 4 or strlen($skin) === 128 * 128 * 4;
return in_array(strlen($skin), Skin::ACCEPTED_SKIN_SIZES, true);
}
/**
@ -155,10 +173,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
* @param Skin $skin
*/
public function setSkin(Skin $skin) : void{
if(!$skin->isValid()){
throw new \InvalidStateException("Specified skin is not valid, must be 8KiB or 16KiB");
}
$skin->validate();
$this->skin = $skin;
$this->skin->debloatGeometryData();
}
@ -590,17 +605,6 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
$this->setNameTag($nbt->getString("NameTag"));
}
$skin = $nbt->getCompoundTag("Skin");
if($skin !== null){
$this->setSkin(new Skin(
$skin->getString("Name"),
$skin->hasTag("Data", StringTag::class) ? $skin->getString("Data") : $skin->getByteArray("Data"), //old data (this used to be saved as a StringTag in older versions of PM)
$skin->getByteArray("CapeData", ""),
$skin->getString("GeometryName", ""),
$skin->getByteArray("GeometryData", "")
));
}
$this->uuid = UUID::fromData((string) $this->getId(), $this->skin->getSkinData(), $this->getNameTag());
}
@ -849,9 +853,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
}
protected function sendSpawnPacket(Player $player) : void{
if(!$this->skin->isValid()){
throw new \InvalidStateException((new \ReflectionClass($this))->getShortName() . " must have a valid skin set");
}
$this->skin->validate();
if(!($this instanceof Player)){
/* we don't use Server->updatePlayerListData() because that uses batches, which could cause race conditions in async compression mode */