Skip to content

Commit

Permalink
Fixed undo and redo
Browse files Browse the repository at this point in the history
  • Loading branch information
inxomnyaa committed Oct 5, 2022
1 parent 8e08010 commit 64e4fad
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions src/xenialdan/MagicWE2/task/AsyncRevertTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Generator;
use InvalidArgumentException;
use pocketmine\Server;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\TextFormat as TF;
use pocketmine\world\Position;
Expand All @@ -21,7 +22,6 @@
use function igbinary_serialize;
use function igbinary_unserialize;
use function iterator_to_array;
use function var_dump;

class AsyncRevertTask extends MWEAsyncTask{

Expand All @@ -35,20 +35,23 @@ class AsyncRevertTask extends MWEAsyncTask{

/**
* AsyncRevertTask constructor.
* @param UuidInterface $sessionUUID
*
* @param UuidInterface $sessionUUID
* @param RevertClipboard $clipboard
* @param int $type The type of clipboard pasting.
* @param int $type The type of clipboard pasting.
*/
public function __construct(UuidInterface $sessionUUID, RevertClipboard $clipboard, int $type = self::TYPE_UNDO)
{
public function __construct(UuidInterface $sessionUUID, RevertClipboard $clipboard, int $type = self::TYPE_UNDO){
$this->sessionUUID = $sessionUUID->toString();
$this->start = microtime(true);
$this->clipboard = igbinary_serialize($clipboard);
$this->type = $type;
$this->manager = AsyncWorld::fromRevertClipboard($clipboard);
$world = Server::getInstance()->getWorldManager()->getWorld($clipboard->worldId);
foreach($clipboard->chunks as $hash => $chunk){
World::getXZ($hash, $x, $z);
var_dump("Chunk $hash at $x, $z");
if($type === self::TYPE_REDO){
$this->manager->setChunk($x, $z, $world->getChunk($x, $z));
}
}
}

Expand All @@ -58,23 +61,20 @@ public function __construct(UuidInterface $sessionUUID, RevertClipboard $clipboa
* @return void
* @throws Exception
*/
public function onRun(): void{
public function onRun() : void{
$this->publishProgress([0, "Start"]);
/** @var RevertClipboard $clipboard */
$clipboard = igbinary_unserialize($this->clipboard/*, ['allowed_classes' => [RevertClipboard::class]]*/);//TODO test pm4

$manager = $this->manager;
// $oldBlocks = [];
// if ($this->type === self::TYPE_UNDO)
// $oldBlocks = iterator_to_array($this->undoChunks($manager, $clipboard));
// if ($this->type === self::TYPE_REDO)
// $oldBlocks = iterator_to_array($this->redoChunks($manager, $clipboard));
$oldBlocks = [];
if($this->type === self::TYPE_UNDO)
iterator_to_array($this->undoChunks($manager, $clipboard));
if($this->type === self::TYPE_REDO)
iterator_to_array($this->redoChunks($manager, $clipboard));
$oldBlocks = iterator_to_array($this->undoChunks($manager, $clipboard));
if($this->type === self::TYPE_REDO){
$oldBlocks = iterator_to_array($this->redoChunks($manager, $clipboard));
}
$chunks = $manager->getChunks();
$this->setResult($chunks);
$this->setResult([$chunks, $oldBlocks]);
}

/**
Expand All @@ -91,10 +91,9 @@ private function undoChunks(AsyncWorld &$manager, RevertClipboard $clipboard) :
$this->publishProgress([0, "Reverted $changed blocks out of $count"]);
//$block is "data" array
foreach($clipboard->blocksAfter as $block){
yield $block;
$block = self::singleDataToBlock($block);//turn data into real block
var_dump("HAD " . $manager->getBlockAt($block->getPosition()->x, $block->getPosition()->y, $block->getPosition()->z)->getId());
var_dump("SET " . $block);
$original = $manager->getBlockAt($block->getPosition()->x, $block->getPosition()->y, $block->getPosition()->z);
yield self::singleBlockToData($original, $block->getPosition());
$manager->setBlockAt($block->getPosition()->getFloorX(), $block->getPosition()->getFloorY(), $block->getPosition()->getFloorZ(), $block);
$changed++;
$this->publishProgress([$changed / $count, "Reverted $changed blocks out of $count"]);
Expand All @@ -104,6 +103,7 @@ private function undoChunks(AsyncWorld &$manager, RevertClipboard $clipboard) :
/**
* @param AsyncWorld $manager
* @param RevertClipboard $clipboard
*
* @return Generator
* @throws InvalidArgumentException
* @phpstan-return Generator<int, array{int, Position|null}, void, void>
Expand All @@ -114,10 +114,9 @@ private function redoChunks(AsyncWorld &$manager, RevertClipboard $clipboard) :
$this->publishProgress([0, "Redone $changed blocks out of $count"]);
//$block is "data" array
foreach($clipboard->blocksAfter as $block){
yield $block;
$block = self::singleDataToBlock($block);//turn data into real block
var_dump("HAD " . $manager->getBlockAt($block->getPosition()->x, $block->getPosition()->y, $block->getPosition()->z)->getId());
var_dump("SET " . $block);
$original = $manager->getBlockAt($block->getPosition()->x, $block->getPosition()->y, $block->getPosition()->z);
yield self::singleBlockToData($original, $block->getPosition());
$manager->setBlockAt($block->getPosition()->getFloorX(), $block->getPosition()->getFloorY(), $block->getPosition()->getFloorZ(), $block);
$changed++;
$this->publishProgress([$changed / $count, "Redone $changed blocks out of $count"]);
Expand All @@ -127,21 +126,20 @@ private function redoChunks(AsyncWorld &$manager, RevertClipboard $clipboard) :
/**
* @throws AssumptionFailedError
*/
public function onCompletion(): void
{
public function onCompletion() : void{
try{
$session = SessionHelper::getSessionByUUID(Uuid::fromString($this->sessionUUID));
if($session instanceof UserSession) $session->getBossBar()->hideFromAll();
}catch(SessionException $e){
Loader::getInstance()->getLogger()->logException($e);
$session = null;
}
$result = $this->getResult();
[$chunks, $oldBlocks] = $this->getResult();
/** @var RevertClipboard $clipboard */
$clipboard = igbinary_unserialize($this->clipboard/*, ['allowed_classes' => [RevertClipboard::class]]*/);//TODO test pm4
$clipboard->chunks = $result;
$clipboard->chunks = $chunks;
$totalCount = $changed = count($clipboard->blocksAfter);
// $clipboard->blocksAfter = $result["oldBlocks"];//already is a array of data //commented out because we don't modify the array anymore
$clipboard->blocksAfter = $oldBlocks;//already is a array of data
$world = $clipboard->getWorld();
foreach($clipboard->chunks as $hash => $chunk){
World::getXZ($hash, $x, $z);
Expand Down

0 comments on commit 64e4fad

Please sign in to comment.