-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed possible inconsistency issues with entities and tile blocks tha…
…t get some null values due to asynchronous methods.
- Loading branch information
1 parent
b643bf9
commit c4a0b50
Showing
13 changed files
with
326 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/matcracker/BedcoreProtect/serializable/AbstractSerializable.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/* | ||
* ___ __ ___ __ __ | ||
* / _ )___ ___/ /______ _______ / _ \_______ / /____ ____/ /_ | ||
* / _ / -_) _ / __/ _ \/ __/ -_) ___/ __/ _ \/ __/ -_) __/ __/ | ||
* /____/\__/\_,_/\__/\___/_/ \__/_/ /_/ \___/\__/\__/\__/\__/ | ||
* | ||
* Copyright (C) 2019 | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* @author matcracker | ||
* @link https://www.github.com/matcracker/BedcoreProtect | ||
* | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace matcracker\BedcoreProtect\serializable; | ||
|
||
abstract class AbstractSerializable | ||
{ | ||
/** | ||
* Returns an instance of AbstractSerializable from $object. | ||
* @param $object | ||
* @return static | ||
*/ | ||
abstract public static function fromPrimitive($object): self; | ||
|
||
/** | ||
* Returns an instance of primitive object from AbstractSerializable. | ||
* @return mixed | ||
*/ | ||
abstract public function toPrimitive(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/matcracker/BedcoreProtect/serializable/SerializableEntity.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
/* | ||
* ___ __ ___ __ __ | ||
* / _ )___ ___/ /______ _______ / _ \_______ / /____ ____/ /_ | ||
* / _ / -_) _ / __/ _ \/ __/ -_) ___/ __/ _ \/ __/ -_) __/ __/ | ||
* /____/\__/\_,_/\__/\___/_/ \__/_/ /_/ \___/\__/\__/\__/\__/ | ||
* | ||
* Copyright (C) 2019 | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* @author matcracker | ||
* @link https://www.github.com/matcracker/BedcoreProtect | ||
* | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace matcracker\BedcoreProtect\serializable; | ||
|
||
use http\Exception\InvalidArgumentException; | ||
use matcracker\BedcoreProtect\utils\Utils; | ||
use pocketmine\entity\Entity; | ||
use pocketmine\entity\Living; | ||
use pocketmine\Player; | ||
use RuntimeException; | ||
use function get_class; | ||
|
||
final class SerializableEntity extends SerializablePosition | ||
{ | ||
/** @var string */ | ||
private $uuid; | ||
/** @var int */ | ||
private $id; | ||
/** @var string */ | ||
private $name; | ||
/** @var string */ | ||
private $classPath; | ||
/** @var string */ | ||
private $address; | ||
/** @var string */ | ||
private $serializedNbt; | ||
|
||
public function __construct(string $uuid, int $id, string $name, string $classPath, string $address, ?float $x, ?float $y, ?float $z, ?string $worldName, string $serializedNbt) | ||
{ | ||
parent::__construct($x, $y, $z, $worldName); | ||
$this->uuid = $uuid; | ||
$this->id = $id; | ||
$this->name = $name; | ||
$this->classPath = $classPath; | ||
$this->address = $address; | ||
$this->serializedNbt = $serializedNbt; | ||
} | ||
|
||
/** | ||
* @param Entity $entity | ||
* @return SerializableEntity | ||
*/ | ||
public static function fromPrimitive($entity): AbstractSerializable | ||
{ | ||
$classPath = get_class($entity); | ||
if (!$entity instanceof Entity) { | ||
throw new InvalidArgumentException("Expected Entity instance, got " . $classPath); | ||
} | ||
|
||
$entity->saveNBT(); | ||
|
||
if ($entity instanceof Living) { | ||
$entity->namedtag->setFloat("Health", $entity->getMaxHealth()); | ||
} | ||
|
||
return new self( | ||
Utils::getEntityUniqueId($entity), | ||
$entity->getId(), | ||
Utils::getEntityName($entity), | ||
$classPath, | ||
($entity instanceof Player) ? $entity->getAddress() : "127.0.0.1", | ||
(float)$entity->getX(), | ||
(float)$entity->getY(), | ||
(float)$entity->getZ(), | ||
parent::fromPrimitive($entity->asPosition())->worldName, | ||
Utils::serializeNBT($entity->namedtag) | ||
); | ||
} | ||
|
||
/** | ||
* @return Entity | ||
*/ | ||
public function toPrimitive() | ||
{ | ||
if (($level = parent::toPrimitive()->getLevel()) === null) { | ||
throw new RuntimeException("Could not create an entity with \"null\" world."); | ||
} | ||
|
||
/** @var Entity $classPath */ | ||
$classPath = $this->classPath; | ||
return Entity::createEntity($classPath::NETWORK_ID, $level, Utils::deserializeNBT($this->serializedNbt)); | ||
} | ||
|
||
public function getUuid(): string | ||
{ | ||
return $this->uuid; | ||
} | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getAddress(): string | ||
{ | ||
return $this->address; | ||
} | ||
|
||
public function getClassPath(): string | ||
{ | ||
return $this->classPath; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getSerializedNbt(): string | ||
{ | ||
return $this->serializedNbt; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return "SerializableEntity({$this->uuid}:{$this->name})({$this->classPath})[{$this->worldName}]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.