Skip to content

Commit

Permalink
Fixed RollbackTask not using array as Threaded object. Removed Abstra…
Browse files Browse the repository at this point in the history
…ctSerializable class
  • Loading branch information
matcracker committed Jun 18, 2021
1 parent 963f1e5 commit 743aaae
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 74 deletions.

This file was deleted.

32 changes: 11 additions & 21 deletions src/matcracker/BedcoreProtect/serializable/SerializableBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@

namespace matcracker\BedcoreProtect\serializable;

use InvalidArgumentException;
use matcracker\BedcoreProtect\utils\BlockUtils;
use matcracker\BedcoreProtect\utils\WorldUtils;
use pocketmine\block\Block;
use pocketmine\block\BlockFactory;
use pocketmine\level\Position;
use pocketmine\math\Vector3;
use function get_class;

final class SerializableBlock extends AbstractSerializable
final class SerializableBlock
{
private string $name;
private int $id;
Expand All @@ -53,29 +51,21 @@ public function __construct(string $name, int $id, int $meta, int $x, int $y, in
$this->serializedNbt = $serializedNbt;
}

/**
* @param Block $object
* @return SerializableBlock
*/
public static function serialize($object): AbstractSerializable
public static function fromBlock(Block $block): self
{
if (!$object instanceof Block) {
throw new InvalidArgumentException("Expected Block instance, got " . get_class($object));
}

return new self(
$object->getName(),
$object->getId(),
$object->getDamage(),
(int)$object->getX(),
(int)$object->getY(),
(int)$object->getZ(),
$object->getLevelNonNull()->getFolderName(),
BlockUtils::serializeTileTag($object)
$block->getName(),
$block->getId(),
$block->getDamage(),
(int)$block->getX(),
(int)$block->getY(),
(int)$block->getZ(),
$block->getLevelNonNull()->getFolderName(),
BlockUtils::serializeTileTag($block)
);
}

public function unserialize(): Block
public function toBlock(): Block
{
return BlockFactory::get($this->id, $this->meta, $this->asPosition());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ protected function addSerialBlocksLogByEntity(Entity $entity, array $oldBlocks,

/** @var SerializableBlock[] $oldBlocks */
$oldBlocks = array_values(array_map(static function (Block $block): SerializableBlock {
return SerializableBlock::serialize($block);
return SerializableBlock::fromBlock($block);
}, $oldBlocks));

/** @var SerializableBlock[] $newBlocks */
$newBlocks = array_values(array_map(static function (Block $block): SerializableBlock {
return SerializableBlock::serialize($block);
return SerializableBlock::fromBlock($block);
}, $newBlocks));

$uuidEntity = EntityUtils::getUniqueId($entity);
Expand Down Expand Up @@ -346,7 +346,7 @@ protected function onRollback(bool $rollback, Level $world, CommandParser $comma
$world->addTile($tile);
}
} else {
$tile = BlockUtils::asTile($block->unserialize());
$tile = BlockUtils::asTile($block->toBlock());
if ($tile !== null) {
$world->removeTile($tile);
}
Expand Down
31 changes: 19 additions & 12 deletions src/matcracker/BedcoreProtect/tasks/async/RollbackTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,26 @@
use matcracker\BedcoreProtect\Main;
use matcracker\BedcoreProtect\serializable\SerializableBlock;
use matcracker\BedcoreProtect\storage\QueryManager;
use matcracker\BedcoreProtect\utils\Utils;
use matcracker\BedcoreProtect\utils\WorldUtils;
use pocketmine\level\format\Chunk;
use pocketmine\level\Level;
use pocketmine\math\AxisAlignedBB;
use pocketmine\scheduler\AsyncTask;
use pocketmine\Server;
use Threaded;
use function count;
use function serialize;
use function unserialize;

class RollbackTask extends AsyncTask
{
protected bool $rollback;
protected string $worldName;
protected string $senderName;
/** @var SerializableBlock[] */
protected array $blocks;
/** @var string[] */
private array $serializedChunks;
private array $blocks;
private string $serializedBlocks;
private Threaded $serializedChunks;

/**
* RollbackTask constructor.
Expand All @@ -58,8 +60,13 @@ public function __construct(bool $rollback, string $worldName, string $senderNam
$this->worldName = $worldName;
$this->senderName = $senderName;
$this->blocks = $blocks;
$this->serializedBlocks = serialize($blocks);

$this->serializedChunks = new Threaded();
foreach (WorldUtils::getChunks(WorldUtils::getNonNullWorldByName($worldName), $blocks) as $hash => $chunk) {
$this->serializedChunks[] = serialize([$hash, $chunk->fastSerialize()]);
}

$this->serializedChunks = Utils::serializeChunks(WorldUtils::getChunks(WorldUtils::getNonNullWorldByName($worldName), $blocks));
$this->storeLocal($onComplete);
}

Expand All @@ -68,11 +75,12 @@ public function onRun(): void
/** @var Chunk[] $chunks */
$chunks = [];

foreach ($this->serializedChunks as $hash => $chunkData) {
$chunks[$hash] = Chunk::fastDeserialize($chunkData);
foreach ($this->serializedChunks as $serializedChunk) {
[$hash, $serialChunk] = unserialize($serializedChunk);
$chunks[$hash] = Chunk::fastDeserialize($serialChunk);
}

foreach ($this->blocks as $block) {
foreach (unserialize($this->serializedBlocks) as $block) {
$index = Level::chunkHash($block->getX() >> 4, $block->getZ() >> 4);
if (isset($chunks[$index])) {
$chunks[$index]->setBlock($block->getX() & 0x0f, $block->getY(), $block->getZ() & 0x0f, $block->getId(), $block->getMeta());
Expand Down Expand Up @@ -113,11 +121,10 @@ public function onCompletion(Server $server): void
}

/**
* @return string[]
* @return SerializableBlock[]
*/
protected function getSerializedChunks(): array
public function getBlocks(): array
{
return $this->serializedChunks;
return $this->blocks;
}

}

0 comments on commit 743aaae

Please sign in to comment.