Skip to content

Commit

Permalink
Do not serialize SerializaBlocks in the BlocksQueryGeneratorTask
Browse files Browse the repository at this point in the history
  • Loading branch information
matcracker committed Jun 1, 2020
1 parent f6c0fb2 commit 7fc8486
Showing 1 changed file with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
use function count;
use function is_array;
use function mb_substr;
use function serialize;
use function unserialize;

final class BlocksQueryGeneratorTask extends QueryGeneratorTask
{
Expand All @@ -46,39 +44,34 @@ final class BlocksQueryGeneratorTask extends QueryGeneratorTask
public function __construct(int $firstInsertedId, array $oldBlocks, $newBlocks, ?callable $onComplete)
{
parent::__construct($firstInsertedId, $onComplete);
$this->oldBlocks = serialize($oldBlocks);
$this->newBlocks = serialize($newBlocks);
$this->oldBlocks = $oldBlocks;
$this->newBlocks = $newBlocks;
}

public function onRun(): void
{
$query = /**@lang text */
"INSERT INTO blocks_log(history_id, old_id, old_meta, old_nbt, new_id, new_meta, new_nbt) VALUES";

/** @var SerializableBlock[] $oldBlocks */
$oldBlocks = unserialize($this->oldBlocks);
/** @var SerializableBlock[]|SerializableBlock $newBlocks */
$newBlocks = unserialize($this->newBlocks);
if (!is_array($this->newBlocks)) {
$newId = $this->newBlocks->getId();
$newMeta = $this->newBlocks->getMeta();
$newNBT = $this->newBlocks->getSerializedNbt();

if (!is_array($newBlocks)) {
$newId = $newBlocks->getId();
$newMeta = $newBlocks->getMeta();
$newNBT = $newBlocks->getSerializedNbt();

foreach ($oldBlocks as $oldBlock) {
foreach ($this->oldBlocks as $oldBlock) {
$oldId = $oldBlock->getId();
$oldMeta = $oldBlock->getMeta();
$oldNBT = $oldBlock->getSerializedNbt();
$query .= "('{$this->logId}', '{$oldId}', '{$oldMeta}', '{$oldNBT}', '{$newId}', '{$newMeta}', '{$newNBT}'),";
$this->logId++;
}
} else {
if (count($oldBlocks) !== count($newBlocks)) {
if (count($this->oldBlocks) !== count($this->newBlocks)) {
throw new ArrayOutOfBoundsException('The number of old blocks must be the same as new blocks, or vice-versa');
}

foreach ($oldBlocks as $key => $oldBlock) {
$newBlock = $newBlocks[$key];
foreach ($this->oldBlocks as $key => $oldBlock) {
$newBlock = $this->newBlocks[$key];
$oldId = $oldBlock->getId();
$oldMeta = $oldBlock->getMeta();
$oldNBT = $oldBlock->getSerializedNbt();
Expand Down

0 comments on commit 7fc8486

Please sign in to comment.