Skip to content

Commit

Permalink
Fix inxomnyaa#175 - Paste works with offset now! & Removed debug var_…
Browse files Browse the repository at this point in the history
…dump()'s
  • Loading branch information
inxomnyaa committed Feb 9, 2020
1 parent aaff499 commit efbbfdd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: MagicWE2
main: xenialdan\MagicWE2\Loader
version: 10.0.0
version: 10.0.1
api: ["3.9","3.10","3.11"]
load: STARTUP
authors:
Expand Down
42 changes: 37 additions & 5 deletions src/xenialdan/MagicWE2/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
use pocketmine\item\Item;
use pocketmine\item\ItemBlock;
use pocketmine\item\ItemFactory;
use pocketmine\level\ChunkManager;
use pocketmine\level\Level;
use pocketmine\level\Position;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Vector3;
use pocketmine\nbt\LittleEndianNBTStream;
use pocketmine\nbt\tag\CompoundTag;
Expand Down Expand Up @@ -148,7 +151,7 @@ public static function copyAsync(Selection $selection, Session $session, int $fl
$offset = $selection->getShape()->getMinVec3()->subtract($session->getPlayer()->asVector3()->floor())->floor();
//TODO figure out wrong offset
if ($session instanceof UserSession) $session->getBossBar()->showTo([$session->getPlayer()]);
var_dump($selection->getShape()->getMinVec3(), $session->getPlayer()->asVector3(), $selection->getShape()->getMinVec3()->subtract($session->getPlayer()), $offset);
#var_dump($selection->getShape()->getMinVec3(), $session->getPlayer()->asVector3(), $selection->getShape()->getMinVec3()->subtract($session->getPlayer()), $offset);
Server::getInstance()->getAsyncPool()->submitTask(new AsyncCopyTask($session->getUUID(), $selection, $offset, $selection->getShape()->getTouchedChunks($selection->getLevel()), $flags));
} catch (Exception $e) {
$session->sendMessage($e->getMessage());
Expand Down Expand Up @@ -177,10 +180,13 @@ public static function pasteAsync(SingleClipboard $clipboard, Session $session,
#$c = $clipboard->getCenter();
#$clipboard->setCenter($target->asVector3());//TODO check
if ($session instanceof UserSession) $session->getBossBar()->showTo([$session->getPlayer()]);
$shape = $clipboard->selection->getShape();
$shape->setPasteVector($target->asVector3()->floor());//TODO fix touchedchunks: shape does not know about offset, so it can happen that chunks are missing
#$clipboard->selection->setShape($shape);
$touchedChunks = $shape->getTouchedChunks($target->getLevel());//TODO check if this is an ugly hack
$start = clone $target->asVector3()->floor()->add($clipboard->position)->floor();//start pos of paste
$end = $start->add($clipboard->selection->getShape()->getMaxVec3()->subtract($clipboard->selection->getShape()->getMinVec3()));//add size
$aabb = new AxisAlignedBB($start->getFloorX(), $start->getFloorY(), $start->getFloorZ(), $end->getFloorX(), $end->getFloorY(), $end->getFloorZ());//create paste aabb
$shape = clone $clipboard->selection->getShape();//needed
$shape->setPasteVector($target->asVector3()->floor());//needed
$clipboard->selection->setShape($shape);//needed
$touchedChunks = self::getAABBTouchedChunksTemp($target->getLevel(), $aabb);//TODO clean up or move somewhere else. Better not touch, it works.
Server::getInstance()->getAsyncPool()->submitTask(new AsyncPasteTask($session->getUUID(), $clipboard->selection, $touchedChunks, $clipboard, $flags));
} catch (Exception $e) {
$session->sendMessage($e->getMessage());
Expand All @@ -190,6 +196,32 @@ public static function pasteAsync(SingleClipboard $clipboard, Session $session,
return true;
}

/**
* @param ChunkManager $manager
* @param AxisAlignedBB $aabb
* @return string[]
*/
private static function getAABBTouchedChunksTemp(ChunkManager $manager, AxisAlignedBB $aabb): array
{
$maxX = $aabb->maxX >> 4;
$minX = $aabb->minX >> 4;
$maxZ = $aabb->maxZ >> 4;
$minZ = $aabb->minZ >> 4;
$touchedChunks = [];
for ($x = $minX; $x <= $maxX; $x++) {
for ($z = $minZ; $z <= $maxZ; $z++) {
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
print __METHOD__ . " Touched Chunk at: $x:$z" . PHP_EOL;
$touchedChunks[Level::chunkHash($x, $z)] = $chunk->fastSerialize();
}
}
print __METHOD__ . " Touched chunks count: " . count($touchedChunks) . PHP_EOL;
return $touchedChunks;
}

/**
* @param Selection $selection
* @param Session $session
Expand Down
2 changes: 1 addition & 1 deletion src/xenialdan/MagicWE2/selection/shape/Cuboid.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function getBlocks($manager, array $filterblocks = [], int $flags = API::
for ($y = intval(floor($this->getMinVec3()->y)); $y <= floor($this->getMaxVec3()->y); $y++) {
for ($z = intval(floor($this->getMinVec3()->z)); $z <= floor($this->getMaxVec3()->z); $z++) {
$block = $manager->getBlockAt($x, $y, $z)->setComponents($x, $y, $z);
var_dump("shape getblocks", $block);
#var_dump("shape getblocks", $block);
if (API::hasFlag($flags, API::FLAG_KEEP_BLOCKS) && $block->getId() !== Block::AIR) continue;
if (API::hasFlag($flags, API::FLAG_KEEP_AIR) && $block->getId() === Block::AIR) continue;

Expand Down
8 changes: 4 additions & 4 deletions src/xenialdan/MagicWE2/task/AsyncCopyTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public function onRun()
}, unserialize($this->chunks));
/** @var Selection $selection */
$selection = unserialize($this->selection);
var_dump("shape", $selection->getShape());
#var_dump("shape", $selection->getShape());
$manager = Shape::getChunkManager($chunks);
unset($chunks);
var_dump($this->offset);
#var_dump($this->offset);
$clipboard = new SingleClipboard($this->offset);
$clipboard->selection = $selection;
#$clipboard->setCenter(unserialize($this->offset));
Expand All @@ -95,10 +95,10 @@ private function copyBlocks(Selection $selection, AsyncChunkManager $manager, Si
$min = $selection->getShape()->getMinVec3();
/** @var Block $block */
foreach ($selection->getShape()->getBlocks($manager, [], $this->flags) as $block) {
var_dump("copy chunk X: " . ($block->getX() >> 4) . " Y: " . ($block->getY() >> 4));
#var_dump("copy chunk X: " . ($block->getX() >> 4) . " Y: " . ($block->getY() >> 4));
$newv3 = $block->subtract($min)->floor();
$clipboard->addEntry($newv3->getFloorX(), $newv3->getFloorY(), $newv3->getFloorZ(), new BlockEntry(RuntimeBlockMapping::toStaticRuntimeId($block->getId(), $block->getDamage())));//TODO test tiles
var_dump("copied selection block", $block);
#var_dump("copied selection block", $block);
$i++;
$progress = floor($i / $blockCount * 100);
if ($lastprogress < $progress) {//this prevents spamming packets
Expand Down
12 changes: 6 additions & 6 deletions src/xenialdan/MagicWE2/task/AsyncPasteTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(UUID $sessionUUID, Selection $selection, array $touc
{
$this->start = microtime(true);
$this->offset = $selection->getShape()->getPasteVector()->add($clipboard->position)->floor();
var_dump("paste", $selection->getShape()->getPasteVector(), "cb position", $clipboard->position, "offset", $this->offset, $clipboard);
#var_dump("paste", $selection->getShape()->getPasteVector(), "cb position", $clipboard->position, "offset", $this->offset, $clipboard);
$this->sessionUUID = $sessionUUID->toString();
$this->selection = serialize($selection);
$this->touchedChunks = serialize($touchedChunks);
Expand All @@ -70,7 +70,7 @@ public function onRun()
}, unserialize($this->touchedChunks));
foreach ($touchedChunks as $chunk) {
/** @var Chunk $chunk */
var_dump("deserialize Chunk x " . $chunk->getX() . " z " . $chunk->getZ());
#var_dump("deserialize Chunk x " . $chunk->getX() . " z " . $chunk->getZ());
}//TODO REMOVE

$manager = Shape::getChunkManager($touchedChunks);
Expand Down Expand Up @@ -108,16 +108,16 @@ private function execute(Selection $selection, AsyncChunkManager $manager, Singl
$this->publishProgress([0, "Running, changed $changed blocks out of $blockCount"]);
/** @var BlockEntry $entry */
foreach ($clipboard->iterateEntries($x, $y, $z) as $entry) {
var_dump("at cb xyz $x $y $z: $entry");
#var_dump("at cb xyz $x $y $z: $entry");
$x += $this->offset->getFloorX();
$y += $this->offset->getFloorY();
$z += $this->offset->getFloorZ();
var_dump("add offset xyz $x $y $z");
#var_dump("add offset xyz $x $y $z");
if (is_null($lastchunkx) || $x >> 4 !== $lastchunkx && $z >> 4 !== $lastchunkz) {
$lastchunkx = $x >> 4;
$lastchunkz = $z >> 4;
if (is_null($manager->getChunk($x >> 4, $z >> 4))) {
print PHP_EOL . "Not found: " . strval($x >> 4) . ":" . strval($z >> 4) . PHP_EOL;
print PHP_EOL . "Paste chunk not found in async paste manager: " . strval($x >> 4) . ":" . strval($z >> 4) . PHP_EOL;
continue;
}
}
Expand All @@ -128,7 +128,7 @@ private function execute(Selection $selection, AsyncChunkManager $manager, Singl
/** @var Block $new */
$new = $entry->toBlock()->setComponents($x, $y, $z);
$old = $manager->getBlockAt($x, $y, $z)->setComponents($x, $y, $z);
var_dump("old", $old, "new", $new);
#var_dump("old", $old, "new", $new);
yield $old;
$manager->setBlockAt($x, $y, $z, $new);
if ($manager->getBlockArrayAt($x, $y, $z) !== [$old->getId(), $old->getDamage()]) {//TODO remove? Just useless waste imo
Expand Down

0 comments on commit efbbfdd

Please sign in to comment.